/* Home Access Gateway dashboard. Mobile-first; no browser-specific hacks
   beyond safe-area insets and 16px inputs (prevents zoom on iOS browsers). */

:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #2563eb;
  --primary-text: #ffffff;
  --ok: #16a34a;
  --warn: #d97706;
  --bad: #dc2626;
  --idle: #94a3b8;
  --ok-bg: #dcfce7;
  --bad-bg: #fee2e2;
  --warn-bg: #fef3c7;
  --shadow: 0 1px 2px rgb(15 23 42 / 6%), 0 4px 16px rgb(15 23 42 / 5%);
  --radius: 14px;
  --tabbar-h: 64px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120;
    --surface: #131c2e;
    --surface-2: #1a2438;
    --text: #e2e8f0;
    --muted: #94a3b8;
    --border: #243049;
    --primary: #3b82f6;
    --ok: #22c55e;
    --warn: #f59e0b;
    --bad: #f87171;
    --idle: #64748b;
    --ok-bg: #0f2e1c;
    --bad-bg: #3a1518;
    --warn-bg: #3a2a0d;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  min-height: 100dvh;
}
[hidden] { display: none !important; }
h1 { font-size: 1.4rem; margin: .25rem 0; }
h2 { font-size: 1rem; margin: 0; font-weight: 650; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }
.warn-text { color: var(--warn); margin: .4rem 0; }
.error { color: var(--bad); min-height: 1.2em; margin: .5rem 0 0; }
.mono { font-family: ui-monospace, "SF Mono", Consolas, monospace; font-size: .92em; }

.boot { display: grid; place-items: center; min-height: 100dvh; color: var(--muted); }

/* Buttons */
.btn {
  appearance: none;
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  min-height: 44px; padding: 0 1rem;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface-2); color: var(--text);
  font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
  touch-action: manipulation; user-select: none;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .55; cursor: default; transform: none; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.danger { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn.danger-outline { background: transparent; border-color: var(--bad); color: var(--bad); }
.btn.light { background: #fff; color: #991b1b; border-color: #fff; }
.btn.small { min-height: 36px; padding: 0 .75rem; font-size: .9rem; }
.btn.block { display: flex; width: 100%; margin-top: .6rem; }
.btn.big { min-height: 58px; font-size: 1.15rem; border-radius: 14px; }
.link { appearance: none; background: none; border: 0; color: var(--primary); font: inherit; font-weight: 600; padding: .4rem; cursor: pointer; min-height: 36px; }
.link.danger { color: var(--bad); }
.file-btn { position: relative; }

/* Forms */
label { display: block; margin: .7rem 0 0; font-size: .9rem; color: var(--muted); }
input, select, textarea {
  display: block; width: 100%; margin-top: .3rem;
  min-height: 44px; padding: .55rem .75rem;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); color: var(--text);
  font: inherit; font-size: 16px;
}
textarea { min-height: 90px; resize: vertical; }
input:focus-visible, select:focus-visible, textarea:focus-visible, .btn:focus-visible, .chip:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.switch { display: flex; align-items: center; gap: .6rem; color: var(--text); font-size: 1rem; min-height: 44px; }
.switch input { width: 22px; height: 22px; min-height: 0; margin: 0; accent-color: var(--primary); flex: none; }

/* Login */
.login { display: grid; place-items: center; min-height: 100dvh; padding: max(1rem, env(safe-area-inset-top)) 1rem max(1rem, env(safe-area-inset-bottom)); }
.login-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border); border-radius: 18px; padding: 1.5rem; box-shadow: var(--shadow); text-align: center; }
.login-card form { text-align: left; }
.login-logo { display: block; margin: 0 auto .5rem; }
.divider { display: flex; align-items: center; gap: .75rem; color: var(--muted); margin: 1rem 0 .25rem; font-size: .85rem; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }

/* Layout */
.main { padding: 0 0 calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 1rem); }
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + .5rem) max(1rem, env(safe-area-inset-right)) .5rem max(1rem, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 700; font-size: 1.1rem; }
.live { display: inline-flex; align-items: center; gap: .4rem; background: var(--surface); border: 1px solid var(--border); border-radius: 999px; padding: .25rem .7rem; font: inherit; font-size: .8rem; color: var(--muted); min-height: 32px; }
.live .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--idle); }
.live.on .dot { background: var(--ok); }
.live.off .dot { background: var(--bad); }

.view { padding: .25rem max(1rem, env(safe-area-inset-right)) 0 max(1rem, env(safe-area-inset-left)); max-width: 720px; margin: 0 auto; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; margin: .75rem 0; box-shadow: var(--shadow); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: .5rem; margin: .25rem 0 .5rem; }
form.card .card-head:not(:first-child) { margin-top: 1.2rem; }
details.card summary { cursor: pointer; list-style: none; display: flex; align-items: center; min-height: 32px; }
details.card summary::-webkit-details-marker { display: none; }
details.card summary::after { content: "›"; margin-left: auto; transform: rotate(90deg); color: var(--muted); font-size: 1.3rem; }
details.card[open] summary::after { transform: rotate(-90deg); }
.count { background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; padding: 0 .55rem; font-size: .8rem; color: var(--muted); }
.empty { color: var(--muted); font-size: .92rem; padding: .25rem 0; }

.banner { display: flex; align-items: center; justify-content: space-between; gap: .75rem; margin: .5rem 1rem 0; padding: .75rem 1rem; border-radius: 12px; }
.banner.danger { background: var(--bad); color: #fff; }

/* Device card */
.device { text-align: center; padding: 1.25rem 1rem; }
.device .ip { font-size: .9rem; color: var(--muted); }
.device .state { font-size: 1.35rem; font-weight: 750; margin: .35rem 0 .1rem; }
.device.ok { border-color: color-mix(in srgb, var(--ok) 45%, var(--border)); }
.device.ok .state { color: var(--ok); }
.device.locked .state { color: var(--bad); }
.countdown { font-variant-numeric: tabular-nums; font-size: 2.6rem; font-weight: 750; letter-spacing: -.02em; line-height: 1.1; margin: .3rem 0; }
.countdown.soon { color: var(--warn); }
.progress { height: 6px; background: var(--surface-2); border-radius: 999px; overflow: hidden; margin: .5rem 0 .25rem; }
.progress > span { display: block; height: 100%; background: var(--ok); border-radius: inherit; transition: width 1s linear; }
.progress.soon > span { background: var(--warn); }
.chips { display: flex; flex-wrap: wrap; gap: .4rem; justify-content: center; margin: .75rem 0 .25rem; }
#computer-filter { justify-content: flex-start; }
.chip { appearance: none; min-height: 36px; padding: 0 .85rem; border-radius: 999px; border: 1px solid var(--border); background: var(--surface-2); color: var(--text); font: inherit; font-size: .9rem; font-weight: 600; cursor: pointer; touch-action: manipulation; }
.chip.on { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.device-actions { display: grid; grid-template-columns: 1fr 1fr; gap: .5rem; margin-top: .75rem; }
.device-actions .btn { margin: 0; }

/* Lists */
.item { display: flex; align-items: center; gap: .75rem; padding: .7rem 0; border-top: 1px solid var(--border); }
.item:first-child { border-top: 0; }
.item .grow { flex: 1; min-width: 0; }
.item .title { font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .sub { color: var(--muted); font-size: .85rem; overflow-wrap: anywhere; }
.item .actions { display: flex; gap: .35rem; flex-wrap: wrap; justify-content: flex-end; }
.status { width: 12px; height: 12px; border-radius: 50%; flex: none; background: var(--idle); box-shadow: 0 0 0 3px color-mix(in srgb, var(--idle) 25%, transparent); }
.status.online { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 25%, transparent); }
.status.refused { background: var(--warn); box-shadow: 0 0 0 3px color-mix(in srgb, var(--warn) 25%, transparent); }
.status.unreachable { background: var(--bad); box-shadow: 0 0 0 3px color-mix(in srgb, var(--bad) 25%, transparent); }
.status.live { background: var(--ok); animation: pulse 2s infinite; }
.status.grace { background: var(--warn); }
@keyframes pulse { 50% { box-shadow: 0 0 0 6px color-mix(in srgb, var(--ok) 0%, transparent); } }
@media (prefers-reduced-motion: reduce) { .status.live { animation: none; } .progress > span { transition: none; } }
.badge { display: inline-block; font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .03em; padding: .05rem .45rem; border-radius: 6px; background: var(--surface-2); border: 1px solid var(--border); color: var(--muted); vertical-align: 1px; }
.badge.bad { background: var(--bad-bg); color: var(--bad); border-color: transparent; }
.badge.warn { background: var(--warn-bg); color: var(--warn); border-color: transparent; }
.badge.ok { background: var(--ok-bg); color: var(--ok); border-color: transparent; }
#quick .item { flex-wrap: wrap; }
#quick .item .actions { width: 100%; justify-content: flex-start; padding-left: calc(12px + .75rem); }
@media (min-width: 560px) { #quick .item { flex-wrap: nowrap; } #quick .item .actions { width: auto; padding-left: 0; } }
.computer { padding: .85rem 0; align-items: flex-start; }
.computer .status { margin-top: .35rem; }
.computer.unreachable .title { color: var(--bad); }
.computer.disabled { opacity: .6; }
.computer .actions { margin-top: .5rem; justify-content: flex-start; }

.toolbar { display: flex; gap: .5rem; align-items: center; margin-top: .5rem; }
.toolbar input, .toolbar select { margin: 0; }
.toolbar .btn { flex: none; }
.row-actions { display: flex; gap: .5rem; margin: .25rem 0; }

dl.kv { display: grid; grid-template-columns: auto 1fr; gap: .25rem .75rem; margin: 0; font-size: .88rem; }
dl.kv dt { color: var(--muted); }
dl.kv dd { margin: 0; overflow-wrap: anywhere; }
pre.log { white-space: pre-wrap; overflow-wrap: anywhere; font-size: .75rem; max-height: 240px; overflow: auto; background: var(--surface-2); padding: .5rem; border-radius: 8px; }

/* Tab bar */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
}
.tabbar button { appearance: none; background: none; border: 0; color: var(--muted); font: inherit; font-size: .72rem; font-weight: 600; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: .15rem; cursor: pointer; touch-action: manipulation; }
.tabbar button.on { color: var(--primary); }
.tabbar svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* Modal + toast */
.modal { border: 0; padding: 0; border-radius: 18px; width: min(92vw, 440px); max-height: 88dvh; background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgb(0 0 0 / 35%); }
.modal::backdrop { background: rgb(2 6 23 / 55%); }
.modal-body { padding: 1.25rem; overflow: auto; max-height: 88dvh; }
.modal-body h2 { font-size: 1.15rem; margin-bottom: .25rem; }
.modal-actions { display: flex; gap: .5rem; justify-content: flex-end; margin-top: 1rem; flex-wrap: wrap; }
.modal-actions .btn { flex: 1 1 auto; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 .6rem; }
.toast { position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 12px); transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none; z-index: 50; background: #0f172a; color: #fff; padding: .7rem 1rem; border-radius: 12px; max-width: min(92vw, 420px); font-size: .92rem; transition: opacity .2s, transform .2s; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.bad { background: #991b1b; }

@media (min-width: 900px) {
  .view[data-view="home"] { max-width: 960px; display: grid; grid-template-columns: 1fr 1fr; gap: 0 1rem; align-items: start; }
  .view[data-view="home"] .device { grid-row: span 2; }
  .lockdown-btn { grid-column: 1 / -1; }
}
