:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #f8f9fc;
  --text: #1c2333;
  --muted: #5f6b80;
  --border: #e3e7ef;
  --primary: #2f5bea;
  --primary-soft: #e8eefe;
  --primary-text: #ffffff;
  --error: #c62828;
  --error-bg: #fdecea;
  --ok: #1b7f4b;
  --warn: #8a5a00;
  --warn-bg: #fff4d6;
  --pill-s: 70%;
  --pill-bg-l: 93%;
  --pill-fg-l: 30%;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151c;
    --surface: #1b2029;
    --surface-2: #212734;
    --text: #e6e9ef;
    --muted: #9aa4b5;
    --border: #2c3340;
    --primary: #5b80f5;
    --primary-soft: #26314f;
    --error: #ff8a80;
    --error-bg: #3a1f1f;
    --ok: #6fd39b;
    --warn: #f3c969;
    --warn-bg: #3a3020;
    --pill-s: 35%;
    --pill-bg-l: 24%;
    --pill-fg-l: 82%;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
  line-height: 1.4;
}

h1 { font-size: 1.05rem; margin: 0; }
h2 { font-size: 1rem; margin: 0; }
h3 { font-size: 0.8rem; margin: 1rem 0 0.4rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }

.muted { color: var(--muted); }
.small { font-size: 0.8rem; }
.error-text { color: var(--error); }
.ok-text { color: var(--ok); }

/* --- Barra superior --- */

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  padding: 0.5rem 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.topbar-right { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }
.topbar-right form { margin: 0; }

.layout { max-width: 1440px; margin: 0 auto; padding: 12px 16px 24px; }

/* --- Controles --- */

button, .button {
  font: inherit;
  display: inline-block;
  border: 1px solid var(--primary);
  background: var(--primary);
  color: var(--primary-text);
  border-radius: 6px;
  padding: 0.45rem 0.85rem;
  cursor: pointer;
  text-decoration: none;
  line-height: 1.2;
}

button.small, .button.small { font-size: 0.8rem; padding: 0.3rem 0.6rem; }
button.secondary { background: transparent; color: var(--text); border-color: var(--border); }
button.secondary:hover:not(:disabled) { background: var(--surface-2); }
button:disabled { opacity: 0.45; cursor: default; }

input, select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.4rem 0.55rem;
  width: 100%;
}

textarea { resize: vertical; }

label { display: flex; flex-direction: column; gap: 0.2rem; font-size: 0.8rem; color: var(--muted); }

form.inline { display: flex; align-items: flex-end; gap: 0.5rem; }
form.inline label { flex: 1; min-width: 0; }

.alert { padding: 0.55rem 0.75rem; border-radius: 6px; margin: 0 0 12px; font-size: 0.85rem; }
.alert.error { background: var(--error-bg); color: var(--error); }
.alert.warn { background: var(--warn-bg); color: var(--warn); }

.connect-panel { max-width: 560px; margin: 48px auto; padding: 24px; text-align: center; }
.connect-panel h2 { font-size: 1.2rem; margin-bottom: 0.5rem; }
.connect-panel p { margin: 0 0 1rem; }
.connect-panel .alert { margin: 1rem 0 0; text-align: left; }

/* --- Pestañas --- */

.tabs { display: flex; gap: 0.4rem; margin-bottom: 12px; overflow-x: auto; }

.tab {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.45rem 0.8rem;
  white-space: nowrap;
}

.tab:hover { background: var(--surface-2); }

.tab[aria-selected="true"] {
  background: var(--primary-soft);
  border-color: var(--primary);
  color: var(--primary);
  font-weight: 600;
}

.tab .count {
  font-size: 0.75rem;
  font-weight: 600;
  background: var(--surface-2);
  color: var(--muted);
  border-radius: 999px;
  padding: 0.05rem 0.45rem;
}

.tab[aria-selected="true"] .count { background: var(--primary); color: var(--primary-text); }

/* --- Área de trabajo --- */

.workspace { display: grid; gap: 12px; align-items: start; }

@media (min-width: 1100px) {
  .workspace:has(.detail:not([hidden])) { grid-template-columns: minmax(0, 1fr) 380px; }
  .detail { position: sticky; top: 12px; max-height: calc(100vh - 24px); overflow-y: auto; }
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  min-width: 0;
}

.list-card { overflow: hidden; }

.list-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
}

.list-card > #list-message { margin: 0; padding: 0.5rem 0.75rem; }

.pager { display: flex; gap: 0.35rem; }

/* --- Tabla compacta --- */

.table-wrap { overflow-x: auto; }

table.grid { width: 100%; border-collapse: collapse; font-size: 0.82rem; table-layout: auto; }

table.grid th {
  position: sticky;
  top: 0;
  background: var(--surface-2);
  text-align: left;
  font-weight: 600;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
  padding: 0.4rem 0.6rem;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

table.grid td {
  padding: 0.35rem 0.6rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
  max-width: 260px;
}

table.grid td .main, table.grid td .sub {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

table.grid td .sub { font-size: 0.75rem; color: var(--muted); }
table.grid td:first-child .main { font-weight: 600; }
table.grid .nowrap { white-space: nowrap; }
table.grid .num { text-align: right; font-variant-numeric: tabular-nums; }

table.grid tbody tr { cursor: pointer; }
table.grid tbody tr:hover { background: var(--surface-2); }
table.grid tbody tr:focus-visible { outline: 2px solid var(--primary); outline-offset: -2px; }
table.grid tbody tr.selected { background: var(--primary-soft); }
table.grid tbody tr.selected td:first-child { box-shadow: inset 3px 0 0 var(--primary); }
#list-body.loading { opacity: 0.5; }

.pill {
  display: inline-block;
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  vertical-align: middle;
  font-size: 0.74rem;
  font-weight: 500;
  padding: 0.12rem 0.5rem;
  border-radius: 999px;
  background: hsl(var(--h, 220) var(--pill-s) var(--pill-bg-l));
  color: hsl(var(--h, 220) var(--pill-s) var(--pill-fg-l));
}

@media (max-width: 1279px) { .hide-md { display: none; } }
@media (max-width: 720px) { .hide-sm { display: none; } }

/* --- Panel de detalle --- */

.detail { padding: 12px 14px; }

.detail-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 0.5rem; margin-bottom: 0.75rem; }
.detail-head p { margin: 0.2rem 0 0; }

.detail form { margin-bottom: 0.6rem; }

#note-form { display: grid; gap: 0.4rem; }
#note-form button { justify-self: end; }
#detail-message { margin: 0.25rem 0; }

.notes { list-style: none; padding: 0; margin: 0; }
.notes li { padding: 0.5rem 0; border-top: 1px solid var(--border); font-size: 0.85rem; }
.notes .note-head { display: flex; justify-content: space-between; gap: 0.5rem; font-size: 0.8rem; }
.notes .note-head span { white-space: nowrap; }
.notes p { margin: 0.2rem 0 0; white-space: pre-wrap; overflow-wrap: anywhere; }

/* --- Inicio de sesión --- */

.login-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; }

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 24px;
}

.login-card h1 { margin-bottom: 0.25rem; font-size: 1.2rem; }
.login-card label { margin-bottom: 0.75rem; }
.login-card button { width: 100%; margin-top: 0.5rem; }
