:root {
  /* Identidad Topay: navy denso + acento teal (pagos/gestión), no azul sky genérico */
  --bg: #0b1220;
  --bg-elevated: #111a2b;
  --bg-panel: #152033;
  --bg-panel-2: #1c2a40;
  --bg-hover: #24344f;
  --border: #2a3b55;
  --border-strong: #3d5273;
  --text: #e8eef7;
  --text-muted: #8fa0b8;
  --accent: #1fb8a0;
  --accent-soft: rgba(31, 184, 160, 0.14);
  --accent-dark: #15907e;
  --ok: #3ecf8e;
  --warn: #e6b84d;
  --danger: #e85d6a;
  --info: #5b9fd4;
  --radius: 6px;
  --radius-sm: 4px;
  --font: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  --font-size: 12.5px;
  --line: 1.35;
  --header-h: 42px;
  --sidebar-w: 200px;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  font-family: var(--font);
  font-size: var(--font-size);
  line-height: var(--line);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ========== App chrome ========== */
.app-header {
  position: sticky;
  top: 0;
  z-index: 40;
  height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--space-4);
  background: linear-gradient(180deg, #121c2e 0%, var(--bg-elevated) 100%);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 1px 0 rgba(31, 184, 160, 0.12);
}

.app-header-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 14px;
  letter-spacing: 0.02em;
  color: var(--text);
  text-decoration: none;
}
.app-header-brand:hover { text-decoration: none; color: var(--text); }

.app-header-mark {
  width: 22px;
  height: 22px;
  border-radius: 5px;
  background: linear-gradient(145deg, var(--accent) 0%, #0e7a6a 100%);
  display: grid;
  place-items: center;
  color: #041512;
  font-size: 11px;
  font-weight: 800;
}

.app-header-meta {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  color: var(--text-muted);
  font-size: 12px;
}

.db-badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--bg-panel-2);
  color: var(--accent);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.02em;
  white-space: nowrap;
}
.db-badge:empty { display: none; }

.app-shell {
  display: flex;
  min-height: calc(100vh - var(--header-h));
}

.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--bg-elevated);
  border-right: 1px solid var(--border);
  padding: var(--space-3) var(--space-2);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.sidebar-section {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
  padding: var(--space-3) var(--space-2) var(--space-1);
}

.sidebar nav a {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 34px;
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  text-decoration: none;
  font-size: 12.5px;
  border: 1px solid transparent;
  transition: background 0.12s ease, color 0.12s ease, border-color 0.12s ease;
}

.sidebar nav a .nav-icon {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  opacity: 0.85;
}

.sidebar nav a:hover {
  background: var(--bg-hover);
  color: var(--text);
  text-decoration: none;
  border-color: var(--border);
}

.sidebar nav a.active {
  background: var(--accent-soft);
  color: var(--accent);
  border-color: rgba(31, 184, 160, 0.35);
  font-weight: 600;
}

.sidebar nav a.active .nav-icon { opacity: 1; }

.main {
  flex: 1;
  min-width: 0;
  padding: var(--space-3) var(--space-4);
  overflow-y: auto;
  background:
    radial-gradient(1200px 400px at 100% -10%, rgba(31, 184, 160, 0.06), transparent 55%),
    var(--bg);
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-3);
  margin-bottom: var(--space-3);
  min-height: 28px;
}

.topbar h2 {
  margin: 0;
  font-size: 17px;
  font-weight: 650;
  letter-spacing: -0.01em;
}

#usuario-actual {
  color: var(--text-muted);
  margin-right: var(--space-2);
  font-size: 12px;
}

/* ========== Cards / headings ========== */
.card {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--space-3);
  margin-bottom: var(--space-3);
}

h2 { margin: 0 0 var(--space-2); font-size: 17px; }
h3 { margin: 0 0 var(--space-2); font-size: 14px; font-weight: 650; }
h4 { margin: 12px 0 6px; font-size: 12px; font-weight: 650; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; }

/* ========== Tables (dense data) ========== */
.table-wrap,
.clientes-grid-wrap,
.contactos-grid-wrap {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-elevated);
}

.table-wrap table,
.clientes-grid-wrap table,
.contactos-grid-wrap table,
.card > table {
  margin: 0;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}

th, td {
  text-align: left;
  padding: 5px 8px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

th {
  color: var(--text-muted);
  font-weight: 650;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  background: var(--bg-panel-2);
  white-space: nowrap;
}

tbody tr {
  cursor: default;
  transition: background 0.1s ease;
}

tbody tr:hover td {
  background: var(--bg-hover);
}

tbody tr:last-child td { border-bottom: none; }

.cell-num,
.cell-date,
td.num,
td.date {
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1;
  white-space: nowrap;
  color: var(--text);
}

td.acciones,
.acciones {
  white-space: nowrap;
  font-size: 12px;
}

td.acciones a:hover { text-decoration: underline; }

td.acciones .link-btn {
  background: none;
  border: none;
  padding: 0;
  margin: 0;
  font: inherit;
  color: var(--danger);
  cursor: pointer;
  text-decoration: none;
}

td.acciones .link-btn:hover { text-decoration: underline; }

/* ========== Buttons ========== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 32px;
  padding: 6px 12px;
  background: var(--accent-dark);
  color: #fff;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 12.5px;
  font-family: inherit;
  font-weight: 600;
  line-height: 1.2;
  text-decoration: none;
}
.btn:hover {
  background: var(--accent);
  color: #041512;
  text-decoration: none;
}
.btn.secondary {
  background: var(--bg-panel-2);
  border-color: var(--border-strong);
  color: var(--text);
}
.btn.secondary:hover {
  background: var(--bg-hover);
  color: var(--text);
}
.btn.danger {
  background: rgba(232, 93, 106, 0.16);
  border-color: rgba(232, 93, 106, 0.45);
  color: #ffb4bb;
}
.btn.danger:hover {
  background: var(--danger);
  color: #1a0a0c;
}

/* ========== Forms ========== */
input, textarea, select {
  width: 100%;
  min-height: 32px;
  padding: 6px 8px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 12.5px;
  font-family: inherit;
  margin-bottom: 8px;
}
input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}
textarea { min-height: 72px; resize: vertical; }
label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  display: block;
  margin-bottom: 3px;
  letter-spacing: 0.02em;
}
input[type="checkbox"] {
  width: 16px;
  height: 16px;
  min-height: 16px;
  margin: 0;
  accent-color: var(--accent);
}

/* ========== Badges ========== */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 8px 2px 6px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 650;
  line-height: 1.3;
  border: 1px solid transparent;
  white-space: nowrap;
  vertical-align: middle;
}
.badge::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
  flex-shrink: 0;
  box-shadow: 0 0 0 2px rgba(255,255,255,0.06);
}
.badge.abierta {
  background: rgba(232, 93, 106, 0.14);
  color: #ff9aa4;
  border-color: rgba(232, 93, 106, 0.35);
}
.badge.comunicada {
  background: rgba(230, 184, 77, 0.14);
  color: #f0d078;
  border-color: rgba(230, 184, 77, 0.35);
}
.badge.cerrada {
  background: rgba(62, 207, 142, 0.12);
  color: #6ee7b0;
  border-color: rgba(62, 207, 142, 0.35);
}
.badge.creado,
.badge.pendiente {
  background: rgba(143, 160, 184, 0.12);
  color: #b7c4d8;
  border-color: rgba(143, 160, 184, 0.3);
}
.badge.enviado {
  background: rgba(62, 207, 142, 0.12);
  color: #6ee7b0;
  border-color: rgba(62, 207, 142, 0.35);
}
.badge.error {
  background: rgba(232, 93, 106, 0.14);
  color: #ff9aa4;
  border-color: rgba(232, 93, 106, 0.35);
}

/* ========== Thread / misc ========== */
.thread {
  border-left: 2px solid var(--border-strong);
  padding-left: var(--space-3);
  margin-left: 4px;
}
.thread .msg { margin-bottom: var(--space-3); }
.thread .msg .meta { font-size: 11px; color: var(--text-muted); margin-bottom: 4px; }
.thread .msg .body {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  white-space: pre-wrap;
  font-size: 12.5px;
}
.msg-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.checkbox-list {
  max-height: 220px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 8px;
  background: var(--bg);
}
.checkbox-list label {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
  font-size: 12.5px;
  font-weight: 400;
  margin-bottom: 4px;
  min-height: 28px;
  text-transform: none;
  letter-spacing: 0;
}
.checkbox-list input[type=checkbox] { width: 16px; height: 16px; margin: 0; }

.filtros-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 8px 10px;
  margin-bottom: 8px;
}

.contactos-grid-wrap { margin: 8px 0; }
.contactos-grid th, .contactos-grid td { vertical-align: middle; }
.contactos-grid input { margin-bottom: 0; min-width: 110px; }
.contactos-grid .btn-quitar-contacto { padding: 4px 8px; min-width: 32px; min-height: 28px; }

.clientes-grid-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}
.clientes-grid-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
}
.clientes-grid-count { color: var(--text-muted); font-size: 12px; }
.clientes-grid { width: 100%; min-width: 960px; }
.clientes-grid th.sortable {
  cursor: pointer;
  user-select: none;
}
.clientes-grid th.sortable:hover { color: var(--accent); }
.clientes-grid th.sorted { color: var(--accent); }
.clientes-grid .filtros-row th {
  padding: 4px 6px 6px;
  vertical-align: top;
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
  background: var(--bg-panel);
}
.clientes-grid .filtros-row input,
.clientes-grid .filtros-row select {
  margin-bottom: 0;
  font-size: 12px;
  padding: 4px 6px;
  min-height: 28px;
}
.clientes-grid thead tr:first-child th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--bg-panel-2);
}

.error-msg {
  color: #ff9aa4;
  font-size: 12px;
  margin: 6px 0;
  min-height: 14px;
}

/* ========== Login ========== */
.login-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 20px;
  background:
    radial-gradient(700px 320px at 20% 10%, rgba(31, 184, 160, 0.12), transparent 60%),
    radial-gradient(600px 280px at 90% 80%, rgba(91, 159, 212, 0.08), transparent 55%),
    var(--bg);
}
.login-box {
  width: 340px;
  max-width: 100%;
  padding: 22px 20px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
}
.login-box h1 {
  text-align: center;
  color: var(--text);
  margin: 0 0 4px;
  font-size: 18px;
}
.login-box .login-sub {
  text-align: center;
  color: var(--text-muted);
  font-size: 12px;
  margin: 0 0 18px;
}
.login-brand-row {
  display: flex;
  justify-content: center;
  margin-bottom: 10px;
}

/* ========== Modal exportar ========== */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(4, 12, 18, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal-dialog {
  width: min(520px, 100%);
  max-height: min(90vh, 640px);
  overflow: auto;
  background: var(--bg-panel);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 18px 18px 14px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.45);
}
.modal-dialog h3 {
  margin: 0 0 6px;
  font-size: 16px;
}
.modal-hint {
  margin: 0 0 12px;
  color: var(--text-muted);
  font-size: 13px;
}
.export-campos-actions {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}
.export-campos-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 12px;
  margin-bottom: 12px;
}
.export-campos-sep {
  grid-column: 1 / -1;
  margin-top: 6px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.export-campo {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  font-size: 13px;
  cursor: pointer;
  margin: 0;
}
.export-campo input {
  width: auto;
  margin-top: 2px;
  flex-shrink: 0;
}
.export-campo em {
  color: var(--text-muted);
  font-style: normal;
  font-size: 11px;
}
.export-contactos {
  margin: 4px 0 14px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}
.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

@media (max-width: 860px) {
  .app-shell { flex-direction: column; }
  .sidebar {
    width: 100%;
    border-right: none;
    border-bottom: 1px solid var(--border);
    flex-direction: row;
    flex-wrap: wrap;
    padding: 8px;
    gap: 4px;
  }
  .sidebar-section { display: none; }
  .sidebar nav {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    width: 100%;
  }
  .sidebar nav a { flex: 1 1 auto; }
  .main { padding: 10px 12px; }
}
