:root {
  color-scheme: dark;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --bg: #0a0c14;
  --surface: #12151f;
  --surface-2: #1a1e2b;
  --line: rgba(155, 165, 188, .12);
  --text: #eef1fa;
  --muted: #8a93a8;
  --blue: #5b8cff;
  --violet: #8d5cff;
  --green: #45d59a;
  --red: #ff6b7a;
  --amber: #ffc36b;
}

* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; background: var(--bg); color: var(--text); font-size: 15px; }
button, input, select, textarea { font: inherit; }
button, summary { cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.mono { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0, 0, 0, 0); }
h2, h3 { margin: 0; }
h2 { font-size: 1.45rem; font-weight: 800; }
h3 { font-size: 1rem; font-weight: 700; }

/* Entrada */
.login-shell { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login-card { width: min(380px, 100%); padding: 36px; border: 1px solid var(--line); border-radius: 20px; background: var(--surface); }
.login-card h1 { margin: 0; font-size: 1.7rem; }
.brand-mark { width: 52px; height: 52px; display: grid; place-items: center; margin-bottom: 18px; border-radius: 15px; background: linear-gradient(135deg, var(--blue), var(--violet)); font-size: 1.6rem; font-weight: 900; }
.brand-mark.small { width: 34px; height: 34px; margin: 0; border-radius: 10px; font-size: 1rem; flex: none; }

/* Formularios */
.stack { display: grid; gap: 10px; margin-top: 22px; }
label { color: var(--muted); font-size: .82rem; font-weight: 600; }
input, select, textarea { min-height: 40px; padding: 0 12px; border: 1px solid var(--line); border-radius: 10px; outline: none; background: #0c0f18; color: var(--text); }
textarea { padding-top: 10px; resize: vertical; }
label > input, label > select, label > textarea { width: 100%; margin-top: 6px; color: var(--text); }
input:focus, select:focus, textarea:focus { border-color: var(--blue); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-grid .full { grid-column: 1 / -1; }
.form-actions { display: flex; align-items: center; gap: 10px; }
.check { display: flex; align-items: center; gap: 9px; color: var(--text); }
.check input { width: 18px; min-height: 18px; margin: 0; }
.form-error { margin: 0; color: #ff9ca7; font-size: .85rem; }
.form-error:empty { display: none; }

/* Botones */
.button { min-height: 38px; padding: 0 16px; border: 1px solid transparent; border-radius: 10px; color: white; font-weight: 700; white-space: nowrap; }
.button.primary { background: var(--blue); }
.button.primary:hover { background: #6d99ff; }
.button.secondary { border-color: var(--line); background: var(--surface-2); }
.button.secondary:hover { border-color: rgba(91, 140, 255, .5); }
.button.ghost { background: transparent; color: var(--muted); }
.button.ghost:hover { color: var(--text); }
.button.danger { border-color: rgba(255, 107, 122, .3); background: transparent; color: #ff9ca7; }
.button.danger:hover { background: rgba(255, 107, 122, .1); }
.button.compact { min-height: 32px; padding: 0 12px; border-radius: 8px; font-size: .85rem; }
.button:disabled { cursor: not-allowed; opacity: .4; }
.icon-button { width: 34px; height: 34px; border: 0; border-radius: 9px; background: transparent; color: var(--muted); font-size: 1.15rem; }
.icon-button:hover { background: var(--surface-2); color: var(--text); }

/* Estructura */
.topbar { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 10px clamp(16px, 3vw, 40px); border-bottom: 1px solid var(--line); background: rgba(10, 12, 20, .92); backdrop-filter: blur(14px); }
.brand-inline, .topbar-actions { display: flex; align-items: center; gap: 14px; min-width: 0; }
.brand-inline { flex: 1; }
.topbar-actions { flex: none; }
.section-tabs { min-width: 0; }
.topbar-actions .muted { font-size: .85rem; }
.content { width: min(1200px, calc(100% - 32px)); margin: 0 auto; padding: 28px 0 60px; }
.section-tabs { display: flex; gap: 4px; }
.nav-group { position: relative; }
.nav-group summary { list-style: none; display: flex; align-items: center; gap: 6px; min-height: 36px; padding: 0 12px; border-radius: 9px; color: var(--muted); font-weight: 700; white-space: nowrap; }
.nav-group summary::-webkit-details-marker { display: none; }
.nav-group summary::after { content: '▾'; font-size: .75rem; opacity: .7; }
.nav-group summary:hover, .nav-group[open] summary { color: var(--text); }
.nav-group.current summary { background: var(--surface-2); color: var(--text); }
.nav-items { position: absolute; left: 0; top: calc(100% + 6px); z-index: 20; min-width: 190px; padding: 6px; border: 1px solid var(--line); border-radius: 12px; background: #1a1e2b; box-shadow: 0 18px 40px rgba(0, 0, 0, .45); }
.section-tab { display: flex; justify-content: space-between; align-items: center; width: 100%; min-height: 38px; padding: 0 12px; border: 0; border-radius: 8px; background: transparent; color: var(--text); font-weight: 600; text-align: left; white-space: nowrap; }
.section-tab:hover { background: rgba(255, 255, 255, .06); }
.section-tab.active { color: #8fb1ff; }
.tab-count { margin-left: 4px; color: var(--muted); font-size: .78rem; font-weight: 600; }
.section-view { animation: enter .15s ease both; }
@keyframes enter { from { opacity: 0; } }

.page-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.heading-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.stats { display: flex; gap: 20px; color: var(--muted); font-size: .9rem; }
.stats strong { color: var(--text); font-size: 1.05rem; }
.search { width: 220px; }

.panel { margin-bottom: 16px; padding: 18px; border: 1px solid var(--line); border-radius: 14px; background: var(--surface); }
.panel.flush { padding: 0; overflow: hidden; }
.panel.narrow { max-width: 720px; }
.padded { padding: 14px 16px; }
.panel-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.panel-heading.padded { margin: 0; padding-bottom: 6px; }
details.panel summary { font-weight: 700; color: var(--muted); }
details.panel summary:hover { color: var(--text); }
details.panel[open] summary { margin-bottom: 16px; color: var(--text); }
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; border-bottom: 1px solid var(--line); }
.toolbar #device-purge-pending { margin-left: auto; }

/* Tablas */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 13px 16px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
th { color: var(--muted); font-size: .75rem; font-weight: 600; }
td { font-size: .92rem; }
tbody tr:hover { background: rgba(255, 255, 255, .02); }
.clickable tr { cursor: pointer; }
.sub { display: block; margin-top: 2px; color: var(--muted); font-size: .8rem; }
.actions-cell { width: 1%; text-align: right; }
.actions-cell > * + * { margin-left: 6px; }
.warn { color: var(--amber); }
.ok { color: var(--green); }

.badge { display: inline-flex; align-items: center; gap: 6px; font-size: .85rem; font-weight: 600; }
.badge::before { content: ''; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.badge.pending { color: var(--amber); }
.badge.active { color: var(--green); }
.badge.expired, .badge.revoked { color: #ff8f9b; }

/* Menú de acciones */
.menu { position: relative; display: inline-block; }
.menu summary { list-style: none; width: 32px; height: 32px; display: inline-grid; place-items: center; border-radius: 8px; color: var(--muted); font-size: 1.2rem; line-height: 1; }
.menu summary::-webkit-details-marker { display: none; }
.menu summary:hover, .menu[open] summary { background: var(--surface-2); color: var(--text); }
.menu-items { position: fixed; z-index: 20; min-width: 180px; padding: 6px; border: 1px solid var(--line); border-radius: 12px; background: #1a1e2b; box-shadow: 0 18px 40px rgba(0, 0, 0, .45); }
.menu-items button { display: block; width: 100%; padding: 9px 12px; border: 0; border-radius: 8px; background: transparent; color: var(--text); text-align: left; font-size: .9rem; }
.menu-items button:hover { background: rgba(255, 255, 255, .06); }
.menu-items button.danger { color: #ff9ca7; }
.menu-items hr { margin: 5px 0; border: 0; border-top: 1px solid var(--line); }

/* Estado del servicio */
.service-bar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; padding: 12px 16px; }
.service-actions { margin-left: auto; display: flex; gap: 4px; }
.status-ok { color: var(--green); font-size: .9rem; font-weight: 600; }
.incident-list { display: flex; flex-direction: column; gap: 8px; }
.incident-list:empty { display: none; }
.incident { display: flex; align-items: center; gap: 12px; color: var(--amber); font-weight: 600; font-size: .9rem; }

/* Servidores */
.server-url { max-width: 320px; overflow: hidden; text-overflow: ellipsis; }
.ip-list { display: flex; flex-wrap: wrap; gap: 5px; white-space: normal; }
.ip-chip { padding: 2px 7px; border-radius: 6px; background: var(--surface-2); color: #c9d6ff; font-size: .75rem; }
.quality { font-weight: 700; }

.empty-state { margin: 0; padding: 28px; color: var(--muted); text-align: center; }
.pagination { display: flex; align-items: center; justify-content: space-between; font-size: .85rem; border-top: 1px solid var(--line); }
.pagination > div { display: flex; gap: 4px; }

/* Cuenta */
.back-link { padding-left: 0; margin-bottom: 8px; }
.info-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 18px 24px; margin: 0; }
.info-grid dt { color: var(--muted); font-size: .8rem; margin-bottom: 4px; }
.info-grid dd { margin: 0; overflow-wrap: anywhere; }
.info-grid .wide { grid-column: 1 / -1; }
.code-big { font-size: 1.15rem; letter-spacing: .04em; }
.option-row { display: flex; flex-wrap: wrap; gap: 8px; }
.danger-zone { margin-top: 14px; padding-top: 14px; border-top: 1px solid var(--line); }
.link-device { display: flex; gap: 8px; flex-wrap: wrap; border-top: 1px solid var(--line); }
.link-device select { flex: 1; min-width: 200px; max-width: 420px; }
.account-link { padding: 0; border: 0; background: none; color: #8fb1ff; font-size: .8rem; }
.account-link:hover { text-decoration: underline; }

/* Diálogo y avisos */
.credential-dialog { width: min(420px, calc(100% - 28px)); padding: 0; border: 1px solid var(--line); border-radius: 16px; background: var(--surface); color: var(--text); }
.credential-dialog::backdrop { background: rgba(3, 5, 10, .75); }
.credential-dialog form { margin: 0; padding: 24px; }
.credential-dialog .muted { margin: 6px 0 0; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
.toast { position: fixed; right: 20px; bottom: 20px; z-index: 30; max-width: 380px; padding: 12px 16px; border-radius: 10px; background: #262c3d; box-shadow: 0 16px 40px rgba(0, 0, 0, .4); opacity: 0; transform: translateY(10px); pointer-events: none; transition: .2s ease; }
.toast.visible { opacity: 1; transform: none; }

.line-cell { grid-column: span 2; }
.line-actions { display: inline-flex; gap: 2px; margin-left: 4px; }
@media (max-width: 760px) { .line-cell { grid-column: auto; } }
.credit-pill { padding: 5px 10px; border-radius: 999px; background: var(--surface-2); color: var(--text); font-size: .85rem; font-weight: 700; white-space: nowrap; }
.credit-pill small { color: var(--muted); font-weight: 600; }
.credit-pill.low { color: var(--amber); }
.extend-label { margin: 6px 0 0; font-weight: 700; }
.extend-options { display: grid; gap: 8px; }
.playback-reports { display: grid; gap: 6px; max-height: 220px; margin: 0; padding: 0; overflow-y: auto; list-style: none; font-size: .85rem; }
.playback-reports li { padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; }
.playback-reports li span { display: block; color: var(--muted); font-size: .8rem; }
.playback-reports .empty { border: 0; padding: 0; color: var(--muted); }
.muted.small { margin: 0; font-size: .8rem; }
#playback-dialog .dialog-actions { flex-wrap: wrap; }
#playback-request { margin-right: auto; }
.extend-option { display: flex; align-items: center; gap: 12px; padding: 12px 14px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface-2); color: var(--text); cursor: pointer; }
.extend-option:has(input:checked) { border-color: var(--blue); background: rgba(91, 140, 255, .1); }
.extend-option.disabled { opacity: .45; cursor: not-allowed; }
.extend-option input { width: 18px; min-height: 18px; margin: 0; }
.extend-option strong { flex: 1; }
.extend-option .sub { margin: 0; text-align: right; }

/* ---------- Móvil ---------- */
@media (max-width: 760px) {
  /* Barra superior en dos filas: logo y acciones; debajo, los tres menús. */
  .topbar { flex-wrap: wrap; gap: 8px; padding: 8px 12px; }
  .brand-inline { display: contents; }
  .brand-mark.small { order: 0; }
  .topbar-actions { order: 1; margin-left: auto; gap: 4px; }
  .topbar-actions #current-user { display: none; }
  .credit-pill small { display: none; }
  .section-tabs { order: 2; width: 100%; gap: 4px; }
  .nav-group { flex: 1; }
  .nav-group summary { justify-content: center; padding: 0 6px; font-size: .9rem; background: var(--surface); }
  .nav-group:last-child .nav-items { left: auto; right: 0; }
  .nav-items { min-width: 170px; }

  .content { width: calc(100% - 20px); padding: 16px 0 40px; }
  h2 { font-size: 1.3rem; }
  .page-head { gap: 10px; margin-bottom: 12px; }
  .heading-actions { width: 100%; }
  .search { flex: 1; width: auto; min-width: 0; }
  .stats { width: 100%; justify-content: space-between; gap: 8px; font-size: .85rem; }
  .toolbar select { flex: 1; }
  .toolbar #device-purge-pending { margin-left: 0; width: 100%; }
  .form-grid { grid-template-columns: 1fr; }
  .form-grid .full { grid-column: auto; }
  .panel { padding: 14px; }
  .panel.narrow { max-width: none; }
  .service-bar { padding: 12px 14px; }
  .service-actions { margin-left: 0; width: 100%; }
  .service-actions .button { flex: 1; }
  .info-grid { grid-template-columns: 1fr 1fr; gap: 14px; }
  .line-cell, .info-grid > div:first-child { grid-column: 1 / -1; }
  .option-row .button { flex: 1 1 auto; }
  .link-device select { min-width: 0; max-width: none; }
  .pagination { padding: 10px 14px; }
  .toast { left: 12px; right: 12px; bottom: 12px; max-width: none; }

  /* Tablas como tarjetas: cada dato con su etiqueta y el "⋯" en la esquina. */
  .table-wrap { overflow: visible; }
  .table-wrap table, .table-wrap tbody, .table-wrap tr, .table-wrap td { display: block; width: 100%; }
  .table-wrap thead { display: none; }
  .table-wrap tr { position: relative; padding: 12px 14px; border-bottom: 1px solid var(--line); }
  .table-wrap tbody tr:last-child { border-bottom: 0; }
  .table-wrap td { padding: 2px 0; border: 0; white-space: normal; font-size: .9rem; }
  .table-wrap td:first-child { padding: 0 44px 6px 0; font-size: 1rem; }
  .table-wrap td[data-label]:not(:first-child)::before { content: attr(data-label); display: inline-block; min-width: 116px; color: var(--muted); font-size: .8rem; }
  .table-wrap td.actions-cell { position: absolute; top: 8px; right: 8px; width: auto; padding: 0; }
  .table-wrap td.actions-cell:has(.button) { position: static; padding-top: 8px; text-align: left; }
  .table-wrap td:empty, .table-wrap td.blank:not(:first-child) { display: none; }
  .ip-list { display: inline-flex; }
}
