/* Gate PWA styles. Tokens shared with the device setup page (docs/API.md). */
:root {
  --bg: #f6f7f9; --surface: #ffffff; --text: #14171f; --muted: #5b6475; --border: #e3e6eb;
  --accent: #1f6feb; --accent-text: #ffffff; --ok: #1a7f37; --warn: #9a6700; --danger: #cf222e;
  --soft: rgba(31, 111, 235, .09);
  --ok-soft: rgba(26, 127, 55, .12); --warn-soft: rgba(154, 103, 0, .12); --danger-soft: rgba(207, 34, 46, .1);
  --shadow: 0 1px 2px rgba(20, 23, 31, .06), 0 2px 8px rgba(20, 23, 31, .04);
  --radius: 14px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --tabbar-h: 64px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1117; --surface: #161b22; --text: #e6edf3; --muted: #8b949e; --border: #30363d; --accent: #4493f8;
    --accent-text: #ffffff; --ok: #3fb950; --warn: #d29922; --danger: #f85149;
    --soft: rgba(68, 147, 248, .14);
    --ok-soft: rgba(63, 185, 80, .15); --warn-soft: rgba(210, 153, 34, .15); --danger-soft: rgba(248, 81, 73, .15);
    --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 var(--font);
  min-height: 100vh; min-height: 100dvh;
  padding-left: env(safe-area-inset-left); padding-right: env(safe-area-inset-right);
  -webkit-tap-highlight-color: transparent;
}
h1, h2, h3 { line-height: 1.2; margin: 0; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
p { margin: 0; }
a { color: var(--accent); }
img, svg { display: block; }
[hidden] { display: none !important; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; border-radius: 6px; }

.skip { position: absolute; left: -999px; top: 0; z-index: 100; background: var(--surface); padding: 12px; }
.skip:focus { left: 8px; top: 8px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* Layout */
.topbar {
  position: sticky; top: 0; z-index: 10; background: var(--bg);
  padding-top: env(safe-area-inset-top); border-bottom: 1px solid var(--border);
}
.topbar-inner { max-width: 720px; margin: 0 auto; display: flex; align-items: center; gap: 10px; padding: 10px 16px; min-height: 56px; }
.brand { border-radius: 7px; }
#title { font-size: 1.2rem; outline: none; }
.page {
  max-width: 720px; margin: 0 auto; padding: 16px 16px calc(var(--tabbar-h) + 32px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 16px;
}
body.no-tabs .page { padding-bottom: calc(32px + env(safe-area-inset-bottom)); }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  background: var(--surface); border-top: 1px solid var(--border);
  padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  display: flex; justify-content: center;
}
.tabbar-inner { display: flex; width: 100%; max-width: 720px; }
.tabbar a {
  flex: 1; min-height: var(--tabbar-h); display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px; color: var(--muted); text-decoration: none; font-size: .75rem; font-weight: 600;
}
.tabbar a svg { width: 24px; height: 24px; }
.tabbar a[aria-current="page"] { color: var(--accent); }

/* Cards and lists */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 12px; min-width: 0;
}
.card.center { align-items: center; text-align: center; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.stack-lg { display: flex; flex-direction: column; gap: 20px; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.row.nowrap { flex-wrap: nowrap; }
.grow { flex: 1; min-width: 0; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.break { overflow-wrap: anywhere; }
.section-title { font-size: .8rem; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 8px 4px -4px; }
.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; }
.list > li { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-top: 1px solid var(--border); min-height: 52px; }
.list > li:first-child { border-top: 0; }
.list .who { flex: 1; min-width: 0; }
.list .who > * { overflow: hidden; text-overflow: ellipsis; }
hr { border: 0; border-top: 1px solid var(--border); margin: 0; width: 100%; }

.hero { padding: 24px 16px; gap: 16px; }
.hero img { width: 72px; height: 72px; border-radius: 18px; }
.hero h2 { font-size: 1.4rem; }

/* Buttons */
.btn {
  appearance: none; border: 1px solid transparent; border-radius: 12px; min-height: 48px; padding: 10px 18px;
  font: inherit; font-weight: 600; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  background: var(--soft); color: var(--accent); text-decoration: none; text-align: center;
  transition: transform .08s ease, opacity .15s ease, background-color .15s ease; touch-action: manipulation; user-select: none;
}
.btn:active:not(:disabled) { transform: scale(.97); }
.btn:disabled, .btn[aria-disabled="true"] { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.danger { background: var(--danger-soft); color: var(--danger); }
.btn.danger.solid { background: var(--danger); color: #fff; }
.btn.ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn.block { width: 100%; }
.btn.big { min-height: 64px; font-size: 1.15rem; border-radius: 16px; }
.btn.sm { min-height: 40px; padding: 6px 12px; font-size: .9rem; }
.btn svg { width: 20px; height: 20px; flex: none; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-row > .btn { flex: 1 1 auto; }
.link-btn { background: none; border: 0; padding: 12px 4px; min-height: 48px; font: inherit; color: var(--accent); cursor: pointer; text-decoration: underline; }

/* Forms */
form { display: flex; flex-direction: column; gap: 12px; margin: 0; }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > label { font-weight: 600; font-size: .9rem; }
.field .hint { font-size: .85rem; color: var(--muted); font-weight: 400; }
.field .err { font-size: .85rem; color: var(--danger); font-weight: 600; }
input, select {
  font: inherit; font-size: 16px; color: var(--text); background: var(--bg); border: 1px solid var(--border);
  border-radius: 12px; min-height: 48px; padding: 10px 12px; width: 100%; min-width: 0;
}
input[type="file"] { padding: 10px; }
input[aria-invalid="true"] { border-color: var(--danger); }
input:focus-visible, select:focus-visible { outline: 3px solid var(--accent); outline-offset: 0; }
.fields-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.fields-4 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
@media (min-width: 560px) { .fields-4 { grid-template-columns: repeat(4, 1fr); } }

.switch { display: inline-flex; align-items: center; gap: 10px; min-height: 48px; cursor: pointer; font-weight: 600; }
.switch input { appearance: none; width: 48px; height: 28px; min-height: 0; padding: 0; border-radius: 14px; background: var(--border); position: relative; cursor: pointer; border: 0; transition: background-color .15s; }
.switch input::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.3); transition: transform .15s; }
.switch input:checked { background: var(--accent); }
.switch input:checked::after { transform: translateX(20px); }
.switch input:disabled { opacity: .5; cursor: not-allowed; }

details > summary { cursor: pointer; min-height: 48px; display: flex; align-items: center; font-weight: 600; color: var(--accent); list-style: none; }
details > summary::-webkit-details-marker { display: none; }
details > summary::before { content: "›"; display: inline-block; width: 1.2em; font-size: 1.3em; transition: transform .15s; }
details[open] > summary::before { transform: rotate(90deg); }
details > :not(summary) { margin-top: 8px; }

/* Segmented control */
.segmented { display: flex; gap: 4px; padding: 4px; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; overflow-x: auto; scrollbar-width: none; }
.segmented::-webkit-scrollbar { display: none; }
.segmented button {
  flex: 1 0 auto; min-height: 44px; padding: 8px 12px; border: 0; border-radius: 10px; background: transparent;
  color: var(--muted); font: inherit; font-weight: 600; font-size: .9rem; cursor: pointer; white-space: nowrap;
}
.segmented button[aria-selected="true"] { background: var(--accent); color: var(--accent-text); }

/* Status */
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.on { background: var(--ok); box-shadow: 0 0 0 3px var(--ok-soft); }
.dot.off { background: var(--danger); }
.badge { display: inline-flex; align-items: center; min-height: 26px; padding: 2px 10px; border-radius: 999px; font-size: .8rem; font-weight: 700; background: var(--soft); color: var(--accent); white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.badge.muted { background: var(--bg); color: var(--muted); border: 1px solid var(--border); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }

/* Gates */
.gate-card { gap: 14px; }
.gate-head { display: flex; align-items: center; gap: 10px; }
.gate-head h3 { font-size: 1.15rem; flex: 1; min-width: 0; overflow-wrap: anywhere; }
.gate-sub { display: flex; align-items: center; gap: 8px; font-size: .875rem; color: var(--muted); }
.gate-actions { display: grid; grid-template-columns: repeat(auto-fit, minmax(90px, 1fr)); gap: 10px; }
.gate-actions .btn { min-height: 72px; font-size: 1.1rem; border-radius: 16px; }
.gate-msg { min-height: 1.4em; font-size: .9rem; font-weight: 600; }
.gate-msg.ok { color: var(--ok); }
.gate-msg.error { color: var(--danger); }
.gate-card.offline .gate-actions .btn { opacity: .75; }

/* Pair code */
.pair-code { font: 700 2.2rem/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; letter-spacing: .12em; color: var(--text); }
.pair-label { font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 700; }

/* Steps */
.steps { margin: 0; padding: 0; list-style: none; counter-reset: s; display: flex; flex-direction: column; gap: 12px; }
.steps li { counter-increment: s; display: flex; gap: 12px; align-items: flex-start; }
.steps li::before { content: counter(s); flex: none; width: 28px; height: 28px; border-radius: 50%; background: var(--accent); color: var(--accent-text); display: grid; place-items: center; font-weight: 700; font-size: .9rem; }
.steps li > div { padding-top: 3px; }
code { white-space: nowrap; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; background: var(--bg); border: 1px solid var(--border); border-radius: 6px; padding: 1px 5px; font-size: .9em; }

/* Notices */
.notice { border-radius: 12px; padding: 12px 14px; background: var(--soft); color: var(--text); font-size: .95rem; }
.notice.warn { background: var(--warn-soft); }
.notice.danger { background: var(--danger-soft); }
.empty { text-align: center; color: var(--muted); padding: 32px 16px; display: flex; flex-direction: column; gap: 12px; align-items: center; }
.spinner { width: 28px; height: 28px; border-radius: 50%; border: 3px solid var(--border); border-top-color: var(--accent); animation: spin .8s linear infinite; margin: 32px auto; }
@keyframes spin { to { transform: rotate(360deg); } }
progress { width: 100%; height: 10px; accent-color: var(--accent); }

/* Log */
.log li { align-items: flex-start; }
.log time { flex: none; width: 6.5em; color: var(--muted); font-size: .85rem; }

/* Toasts */
.toasts {
  position: fixed; left: 0; right: 0; z-index: 50; pointer-events: none;
  bottom: calc(var(--tabbar-h) + 12px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 0 16px;
}
body.no-tabs .toasts { bottom: calc(16px + env(safe-area-inset-bottom)); }
.toast {
  pointer-events: auto; max-width: 480px; width: 100%; padding: 12px 16px; border-radius: 12px;
  background: var(--text); color: var(--bg); font-weight: 600; box-shadow: 0 6px 24px rgba(0,0,0,.25);
  animation: toast-in .2s ease;
}
.toast.error { background: var(--danger); color: #fff; }
.toast.ok { background: var(--ok); color: #fff; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* Dialog */
dialog {
  border: 1px solid var(--border); border-radius: 18px; padding: 0; width: min(520px, calc(100vw - 32px));
  background: var(--surface); color: var(--text); box-shadow: 0 12px 48px rgba(0,0,0,.3);
  max-height: calc(100dvh - 32px);
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }
.dialog-body { padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.dialog-body .btn-row { justify-content: flex-end; }

@media (min-width: 720px) {
  .page { padding-top: 24px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition: none !important; scroll-behavior: auto !important; }
}
