:root {
  --bg: #f5f3ee;
  --surface: #ffffff;
  --surface-2: #efece5;
  --line: #dedad0;
  --text: #17211e;
  --muted: #5d6965;
  --brand: #0d6b5f;
  --brand-ink: #ffffff;
  --brand-soft: #dcefeb;
  --ok: #17794a;
  --ok-soft: #dff3e7;
  --warn: #9a5b00;
  --warn-soft: #fdf0d5;
  --danger: #b3261e;
  --danger-soft: #fbe4e2;
  --console: #101614;
  --console-text: #d7e3df;
  --radius: 16px;
  --tab-h: 64px;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 6px 20px rgb(0 0 0 / 5%);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101614;
    --surface: #19221f;
    --surface-2: #222d29;
    --line: #2f3c37;
    --text: #e9f0ed;
    --muted: #9fb0aa;
    --brand: #4fc3b1;
    --brand-ink: #06231f;
    --brand-soft: #17332e;
    --ok: #6fd39b;
    --ok-soft: #173326;
    --warn: #f0bf62;
    --warn-soft: #3a2e14;
    --danger: #ff8a80;
    --danger-soft: #3d1c1a;
    --console: #0a0f0d;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-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;
  overscroll-behavior-y: contain;
}
h1, h2, h3, p { margin: 0; }
h1 { font-size: 1.6rem; line-height: 1.2; letter-spacing: -0.01em; }
h2 { font-size: 1.2rem; line-height: 1.25; }
button, input { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; }
.muted { color: var(--muted); }
.eyebrow { display: block; font-size: 0.72rem; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); }

/* App bar */
.bar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.logo { display: grid; place-items: center; width: 36px; height: 36px; border-radius: 10px; background: var(--brand); color: var(--brand-ink); font-weight: 800; }
.bar-text { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.2; }
.bar-text small { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 0.8rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
body:not([data-conn="off"]) .dot { background: var(--ok); box-shadow: 0 0 0 3px var(--ok-soft); }

/* Progress strip */
.progress {
  position: sticky; top: calc(57px + env(safe-area-inset-top)); z-index: 19;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px; background: var(--brand-soft); color: var(--text);
  font-weight: 600; font-size: 0.92rem; border-bottom: 1px solid var(--line);
}
.progress #progressLabel { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.spinner { width: 18px; height: 18px; border-radius: 50%; border: 2.5px solid color-mix(in srgb, var(--brand) 25%, transparent); border-top-color: var(--brand); animation: spin 0.8s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Layout */
main { max-width: 640px; margin: 0 auto; padding: 16px max(16px, env(safe-area-inset-right)) calc(var(--tab-h) + 28px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left)); }
.screen, .panel { display: none; }
body[data-conn="off"] #screenConnect,
body[data-conn="locked"] #screenUnlock { display: flex; }
body[data-conn="ready"][data-tab="check"] [data-panel="check"],
body[data-conn="ready"][data-tab="sim"] [data-panel="sim"],
body[data-conn="ready"][data-tab="actions"] [data-panel="actions"],
body[data-conn="ready"][data-tab="log"] [data-panel="log"] { display: block; }
body:not([data-conn="ready"]) .tabs { display: none; }
body:not([data-conn="ready"]) main { padding-bottom: 32px; }

/* Hero screens */
.hero { flex-direction: column; gap: 16px; padding-top: 12px; }
.hero .lead { color: var(--muted); }
.hero-art { position: relative; width: 132px; height: 132px; margin: 12px auto 4px; display: grid; place-items: center; }
.hero-art span { position: absolute; inset: 0; border-radius: 50%; border: 2px solid var(--brand); opacity: 0; animation: ping 2.8s ease-out infinite; }
.hero-art span:nth-child(2) { animation-delay: 0.9s; }
.hero-art span:nth-child(3) { animation-delay: 1.8s; }
.hero-art b { display: grid; place-items: center; width: 64px; height: 64px; border-radius: 20px; background: var(--brand); color: var(--brand-ink); font-size: 1.6rem; }
@keyframes ping { 0% { transform: scale(0.45); opacity: 0.7; } 100% { transform: scale(1); opacity: 0; } }
.steps { list-style: none; margin: 8px 0 0; padding: 0; display: grid; gap: 10px; color: var(--muted); }
.steps li { display: flex; align-items: center; gap: 12px; }
.steps b { display: grid; place-items: center; width: 28px; height: 28px; border-radius: 50%; background: var(--surface-2); color: var(--text); font-size: 0.85rem; flex: none; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 0 18px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); font-weight: 650;
}
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
@media (prefers-color-scheme: dark) { .btn.danger { color: #2a0b09; } }
.btn.ghost { background: transparent; }
.btn.big { min-height: 56px; font-size: 1.05rem; width: 100%; }
.btn.block { width: 100%; }
.btn.small { min-height: 40px; padding: 0 14px; font-size: 0.9rem; }
.btn:active:not(:disabled), .list button:active:not(:disabled) { transform: scale(0.985); filter: brightness(0.96); }
button:disabled { opacity: 0.5; cursor: not-allowed; }
:focus-visible { outline: 3px solid color-mix(in srgb, var(--brand) 60%, transparent); outline-offset: 2px; }

/* Banners */
.banner { display: grid; gap: 2px; padding: 12px 14px; border-radius: 12px; font-size: 0.92rem; }
.banner.ok { background: var(--ok-soft); color: var(--text); border-left: 4px solid var(--ok); }
.banner.warn { background: var(--warn-soft); color: var(--text); border-left: 4px solid var(--warn); }

/* PIN form */
.pin-form { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; }
.pin-form label { font-weight: 650; font-size: 0.9rem; }
.pin-row { display: flex; gap: 8px; align-items: center; }
.pin-row input {
  flex: 1; width: 100%; min-width: 0; min-height: 56px; padding: 0 16px; border-radius: 12px;
  border: 1.5px solid var(--line); background: var(--surface);
  font-size: 1.35rem; letter-spacing: 0.2em; font-variant-numeric: tabular-nums;
}
.pin-row input:focus { border-color: var(--brand); outline: none; }
.field-error { color: var(--danger); font-weight: 600; font-size: 0.92rem; }

/* Cards & tiles */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); display: grid; gap: 12px; }
.device-card { grid-template-columns: minmax(0, 1fr); }
.device-card .btn { width: 100%; }
.device-card h2 { font-size: 1.15rem; overflow-wrap: anywhere; font-variant-numeric: tabular-nums; }
.device-card p { font-size: 0.86rem; }
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 12px; }
.tile { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; min-height: 92px; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.tile small { color: var(--muted); font-size: 0.78rem; display: flex; align-items: center; gap: 6px; }
.tile small::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--line); }
.tile.ok small::before { background: var(--ok); }
.tile.warn small::before { background: var(--warn); }
.tile strong { font-size: 1.15rem; line-height: 1.25; overflow-wrap: anywhere; }
.tile span { color: var(--muted); font-size: 0.8rem; overflow-wrap: anywhere; }
.tile.empty strong { color: var(--muted); }
.section-title, .panel-title { margin: 20px 4px 10px; }
.panel-title { margin-top: 4px; }
.section-title { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }

/* Action lists */
.list { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); margin-top: 12px; }
.list button { display: flex; align-items: center; gap: 12px; width: 100%; min-height: 64px; padding: 10px 16px; background: none; border: 0; border-top: 1px solid var(--line); text-align: left; }
.list button:first-child { border-top: 0; }
.list span { flex: 1; min-width: 0; display: grid; }
.list b { font-weight: 650; }
.list small { color: var(--muted); font-size: 0.84rem; }
.list i { font-style: normal; font-size: 1.4rem; color: var(--muted); }
.list .danger b { color: var(--danger); }

/* Segmented */
.segmented { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 4px; padding: 4px; background: var(--surface-2); border-radius: 12px; }
.segmented button { min-height: 48px; border: 0; border-radius: 9px; background: transparent; font-weight: 650; display: grid; place-content: center; justify-items: center; line-height: 1.15; }
.segmented button small { font-size: 0.68rem; font-weight: 500; color: var(--muted); }
.segmented button[aria-checked="true"] { background: var(--surface); box-shadow: 0 1px 3px rgb(0 0 0 / 18%); color: var(--brand); }
.sim-info { font-size: 0.88rem; white-space: pre-line; }

.advanced { margin-top: 20px; }
.advanced summary { min-height: 48px; display: flex; align-items: center; padding: 0 4px; font-weight: 650; color: var(--muted); cursor: pointer; }
.advanced[open] summary { color: var(--text); }

/* Log */
.log-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.log-head .panel-title { margin: 4px; }
.log-tools { display: flex; gap: 4px; }
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 10px 0; scrollbar-width: none; }
.chips button { flex: none; min-height: 36px; padding: 0 14px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); font-size: 0.86rem; font-weight: 600; }
.chips button.on { background: var(--text); color: var(--bg); border-color: var(--text); }
.log {
  background: var(--console); color: var(--console-text); border-radius: var(--radius);
  padding: 8px 0; height: calc(100dvh - 300px - env(safe-area-inset-bottom)); min-height: 240px; overflow-y: auto;
  font: 12.5px/1.5 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
.log-empty { padding: 24px 16px; color: #8fa39c; font-family: system-ui, sans-serif; font-size: 0.9rem; }
.log-row { display: grid; grid-template-columns: auto 1fr; column-gap: 8px; padding: 2px 12px; }
.log-row time { color: #71857e; }
.log-row span { overflow-wrap: anywhere; white-space: pre-wrap; }
.log-row em { font-style: normal; font-weight: 700; margin-right: 6px; }
.log-row.app { background: rgb(255 255 255 / 5%); }
.log-row.app em { color: #7fd6c7; }
.log-row.error em, .log-row.error span { color: #ff9d94; }
.log-row.lvl-ALRT em { color: #f3a6e6; }
.log-row.lvl-USER em { color: #8fd3ff; }
.log-row.lvl-INFO em { color: #e7cf7a; }
.log-row.lvl-DEBG em { color: #9aa8a3; }
.log-row.prompt { opacity: 0.45; }
.log[data-filter="app"] .log-row:not(.app):not(.error),
.log[data-filter="dev"] .log-row.app,
.log[data-filter="dev"] .log-row.prompt,
.log[data-filter="alert"] .log-row:not(.lvl-ALRT):not(.lvl-USER):not(.error) { display: none; }

/* Tab bar */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: repeat(4, 1fr);
  padding: 6px max(8px, env(safe-area-inset-right)) calc(6px + env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--surface) 94%, transparent); backdrop-filter: blur(10px);
  border-top: 1px solid var(--line);
}
.tabs button { position: relative; min-height: 52px; display: grid; justify-items: center; align-content: center; gap: 2px; border: 0; background: none; border-radius: 12px; font-size: 0.74rem; font-weight: 650; color: var(--muted); }
.tabs svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.tabs b { position: absolute; top: 6px; left: calc(50% + 8px); width: 9px; height: 9px; border-radius: 50%; background: var(--warn); }
body[data-tab="check"] .tabs [data-tab="check"],
body[data-tab="sim"] .tabs [data-tab="sim"],
body[data-tab="actions"] .tabs [data-tab="actions"],
body[data-tab="log"] .tabs [data-tab="log"] { color: var(--brand); background: var(--brand-soft); }

/* Sheets */
.sheet {
  width: 100%; max-width: 640px; max-height: 88dvh; margin: auto auto 0; padding: 0;
  border: 0; border-radius: 20px 20px 0 0; background: var(--surface); color: var(--text);
  box-shadow: 0 -8px 40px rgb(0 0 0 / 25%);
}
.sheet[open] { display: flex; flex-direction: column; animation: rise 0.2s ease-out; }
.sheet::backdrop { background: rgb(0 0 0 / 45%); }
@keyframes rise { from { transform: translateY(24px); opacity: 0; } }
.sheet-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; padding: 18px 16px 10px; }
.sheet-head h2 { margin-bottom: 6px; }
.sheet-scroll { overflow-y: auto; padding: 0 16px 8px; display: grid; gap: 12px; }
.sheet-foot { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 8px; padding: 12px 16px calc(14px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
.confirm { padding: 20px 16px 0; }
.confirm[open] { gap: 8px; }
.confirm .sheet-foot { margin: 12px -16px 0; }
.pill { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 999px; font-size: 0.8rem; font-weight: 700; background: var(--brand-soft); color: var(--brand); }
.pill.ok { background: var(--ok-soft); color: var(--ok); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.error { background: var(--danger-soft); color: var(--danger); }
.result { border: 1px solid var(--line); border-radius: 14px; overflow: hidden; }
.result h3 { font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.07em; color: var(--muted); padding: 10px 14px 6px; display: flex; justify-content: space-between; gap: 8px; }
.result h3 span { text-transform: none; letter-spacing: 0; font-weight: 600; }
.result dl { margin: 0; }
.result dl div { display: flex; justify-content: space-between; gap: 16px; padding: 9px 14px; border-top: 1px solid var(--line); }
.result dt { color: var(--muted); flex: none; max-width: 48%; }
.result dd { margin: 0; text-align: right; font-weight: 600; overflow-wrap: anywhere; font-variant-numeric: tabular-nums; }
.result ul { list-style: none; margin: 0; padding: 0; }
.result li { padding: 8px 14px; border-top: 1px solid var(--line); font: 12.5px/1.45 ui-monospace, Menlo, Consolas, monospace; overflow-wrap: anywhere; }
.result p { padding: 10px 14px; border-top: 1px solid var(--line); }
.result.warn { border-color: var(--warn); background: var(--warn-soft); }
.live, .raw pre { margin: 0; background: var(--console); color: var(--console-text); border-radius: 12px; padding: 12px; font: 12px/1.5 ui-monospace, Menlo, Consolas, monospace; white-space: pre-wrap; overflow-wrap: anywhere; max-height: 40dvh; overflow-y: auto; }
.raw summary { min-height: 44px; display: flex; align-items: center; color: var(--muted); font-weight: 600; cursor: pointer; }

/* Toasts */
.toasts { position: fixed; left: 0; right: 0; bottom: calc(var(--tab-h) + 20px + env(safe-area-inset-bottom)); z-index: 60; display: grid; justify-items: center; gap: 8px; padding: 0 16px; pointer-events: none; }
.toast { max-width: 480px; padding: 12px 16px; border-radius: 12px; background: var(--text); color: var(--bg); font-weight: 600; font-size: 0.92rem; box-shadow: 0 8px 24px rgb(0 0 0 / 25%); }
.toast.error { background: #b3261e; color: #fff; }
#sheetBody { display: grid; gap: 12px; }
#sheetBody:empty { display: none; }

/* Wide screens: log sits beside the active panel */
@media (min-width: 980px) {
  main { max-width: 1120px; }
  body:not([data-conn="ready"]) main { max-width: 520px; padding-top: 40px; }
  body[data-conn="ready"] main { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 24px; align-items: start; padding-bottom: 32px; }
  body[data-conn="ready"] [data-panel="log"] { display: block !important; position: sticky; top: 84px; }
  .log { height: calc(100dvh - 240px); }
  body { display: flex; flex-direction: column; }
  main { order: 2; width: 100%; }
  .tabs { position: static; order: 1; width: 100%; max-width: 1120px; margin: 0 auto; grid-template-columns: repeat(3, max-content); gap: 6px; justify-content: start; padding: 10px 16px; border-top: 0; background: var(--bg); }
  .tabs button { grid-auto-flow: column; gap: 8px; padding: 0 16px; min-height: 44px; font-size: 0.92rem; }
  .tabs [data-tab="log"] { display: none; }
  .sheet { margin: auto; border-radius: 20px; }
  .toasts { bottom: 24px; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
