:root {
  --bg: #F2F2F7; --card: #FFFFFF; --ink: #000000; --muted: #6C6C70; --separator: #E0E0E5;
  --blue: #0071E3; --tint: #E3EEFC; --green: #248A3D; --red: #D70015; --fill: #E3E3E8;
  --overlay: rgba(0, 0, 0, .35);
  --font: -apple-system, 'SF Pro Text', Inter, system-ui, sans-serif;
}
:root[data-theme="dark"] {
  --bg: #000000; --card: #1C1C1E; --ink: #FFFFFF; --muted: #98989F; --separator: #38383A;
  --blue: #0A84FF; --tint: #12304F; --green: #30D158; --red: #FF453A; --fill: #2C2C2E;
  --overlay: rgba(0, 0, 0, .6);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --bg: #000000; --card: #1C1C1E; --ink: #FFFFFF; --muted: #98989F; --separator: #38383A;
    --blue: #0A84FF; --tint: #12304F; --green: #30D158; --red: #FF453A; --fill: #2C2C2E;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font: 17px/1.3 var(--font); }
body { min-height: 100vh; padding: 0 16px calc(84px + env(safe-area-inset-bottom)); }
#app { max-width: 640px; margin: 0 auto; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; text-align: left; }
button:focus-visible, input:focus-visible { outline: 2px solid var(--blue); outline-offset: 2px; }
button:disabled { opacity: .45; cursor: default; }
.muted { color: var(--muted); }

/* header */
.nav { display: flex; align-items: center; justify-content: space-between; min-height: 44px; padding-top: env(safe-area-inset-top); }
.nav .spacer { flex: 1; }
.icon-btn { width: 44px; height: 44px; display: grid; place-items: center; color: var(--blue); border-radius: 22px; }
.icon-btn[aria-disabled="true"] { color: var(--muted); opacity: .6; }
.icon-btn svg { width: 24px; height: 24px; }
.text-btn { color: var(--blue); min-height: 44px; padding: 0 4px; display: inline-flex; align-items: center; }
h1 { font-size: 34px; line-height: 1.12; font-weight: 700; margin: 4px 0 2px; }
.subtitle { color: var(--muted); font-size: 15px; margin: 0 0 16px; }

/* groups */
.group-title { color: var(--muted); font-size: 13px; text-transform: uppercase; margin: 22px 16px 6px; letter-spacing: .02em; }
.group-footer { color: var(--muted); font-size: 13px; margin: 6px 16px 0; }
.group { background: var(--card); border-radius: 12px; overflow: hidden; }
.row { display: flex; align-items: center; gap: 12px; min-height: 46px; padding: 8px 16px; position: relative; width: 100%; }
.row-wrap { display: flex; align-items: center; position: relative; }
.row-wrap > .row { flex: 1; min-width: 0; }
.row-wrap + .row-wrap::before { content: ""; position: absolute; top: 0; left: 16px; right: 0; height: 1px; background: var(--separator); }
.row + .row::before { content: ""; position: absolute; top: 0; left: 16px; right: 0; height: 1px; background: var(--separator); }
.row.tall { padding: 10px 16px; min-height: 60px; }
.row-main { flex: 1; min-width: 0; }
.row-title { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-sub { display: block; color: var(--muted); font-size: 13px; margin-top: 1px; }
.row-value { color: var(--muted); }
.row.link { color: var(--blue); }
button.row:active { background: var(--fill); }
.tile { width: 36px; height: 36px; border-radius: 9px; background: var(--tint); color: var(--blue); display: grid; place-items: center; flex: none; }
.tile svg { width: 22px; height: 22px; }
.tile.off { background: var(--fill); color: var(--muted); }
.dot { width: 10px; height: 10px; border-radius: 5px; background: var(--muted); flex: none; opacity: .5; }
.dot.on { background: var(--green); opacity: 1; }
.trash { color: var(--red); }
.mini-btn { margin-top: 6px; min-height: 32px; padding: 0 12px; border-radius: 8px; background: var(--tint); color: var(--blue); font-size: 15px; display: inline-flex; align-items: center; }
.empty { padding: 28px 16px; text-align: center; color: var(--muted); }
.check { color: var(--blue); width: 22px; height: 22px; }

/* buttons */
.btn { display: flex; align-items: center; justify-content: center; gap: 8px; width: 100%; height: 50px; border-radius: 12px; font-weight: 600; text-align: center; }
.btn.filled { background: var(--blue); color: #fff; }
.btn.tinted { background: var(--tint); color: var(--blue); }
.btn + .btn { margin-top: 10px; }
.spinner { width: 18px; height: 18px; border-radius: 50%; border: 2px solid currentColor; border-right-color: transparent; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* sheets */
.overlay { position: fixed; inset: 0; background: var(--overlay); display: flex; align-items: flex-end; justify-content: center; z-index: 20; animation: fade .2s ease; }
.sheet { width: 100%; max-width: 640px; max-height: 92vh; overflow-y: auto; background: var(--bg); border-radius: 16px 16px 0 0; padding: 0 16px calc(16px + env(safe-area-inset-bottom)); animation: rise .25s ease; }
.sheet.action { background: transparent; padding-bottom: calc(8px + env(safe-area-inset-bottom)); }
.sheet .nav { padding-top: 6px; }
.sheet-title { font-weight: 600; text-align: center; flex: 1; }
.action .group { margin-bottom: 8px; }
.action-title { text-align: center; color: var(--muted); font-size: 13px; padding: 14px 16px 10px; }
.action .row { justify-content: center; text-align: center; min-height: 56px; }
.action .row.blue { color: var(--blue); }
.action .row.red { color: var(--red); }
.action .row.bold { font-weight: 600; }
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translateY(40px); opacity: 0; } }

.field { width: 100%; background: none; border: 0; color: var(--ink); font: inherit; text-align: right; padding: 0; }
.field::placeholder { color: var(--muted); }
.seg { display: flex; background: var(--fill); border-radius: 9px; padding: 2px; }
.seg span { flex: 1; text-align: center; padding: 5px 8px; border-radius: 7px; font-size: 15px; }
.seg span.on { background: var(--card); font-weight: 600; }

/* done */
.done { text-align: center; padding: 8px 0 4px; }
.done-check { width: 56px; height: 56px; color: var(--green); }
.done h2 { font-size: 20px; margin: 8px 0 4px; overflow-wrap: anywhere; }
.qr-card { background: #fff; border-radius: 16px; padding: 16px; margin: 16px auto; width: min(100%, 320px); aspect-ratio: 1; display: grid; place-items: center; }
.qr-card svg { width: 100%; height: 100%; display: block; }
.qr-fail { color: #000; font-size: 15px; text-align: center; }

/* profile */
.profile { text-align: center; padding: 8px 0 8px; }
.avatar { width: 84px; height: 84px; margin: 8px auto 10px; border-radius: 42px; background: var(--blue); color: #fff; display: grid; place-items: center; font-size: 32px; font-weight: 600; }
.profile h1 { font-size: 24px; margin: 0; }
.status-ok { color: var(--green); }
.status-bad { color: var(--red); }

/* server */
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.stat { background: var(--card); border-radius: 12px; padding: 14px 16px; }
.stat .label { color: var(--muted); font-size: 13px; }
.stat .value { font-size: 28px; font-weight: 700; margin-top: 4px; }
.stat .sub { color: var(--muted); font-size: 13px; }
.bars { padding: 12px 16px 14px; display: grid; gap: 12px; }
.bar-label { display: flex; justify-content: space-between; font-size: 15px; margin-bottom: 4px; }
.bar { height: 10px; border-radius: 5px; background: var(--fill); overflow: hidden; }
.bar > i { display: block; height: 100%; width: var(--w, 0%); background: var(--blue); border-radius: 5px; }
.bar.alt > i { background: var(--green); }
.user-actions { flex: none; }
.pill { min-height: 34px; padding: 0 14px; border-radius: 8px; font-size: 15px; display: inline-flex; align-items: center; }
.pill.red { color: var(--red); background: var(--fill); }
.pill.tinted { color: var(--blue); background: var(--tint); }
.banner { background: var(--tint); color: var(--blue); border-radius: 12px; padding: 12px 16px; margin-bottom: 12px; font-size: 15px; }

/* states */
.center { min-height: 70vh; display: grid; place-content: center; text-align: center; gap: 10px; padding: 0 12px; }
.center h1 { font-size: 24px; }
.center p { color: var(--muted); margin: 0; }
.center .btn { max-width: 280px; margin: 8px auto 0; }
.skeleton { height: 52px; border-radius: 12px; background: var(--card); margin-bottom: 10px; opacity: .7; animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .35; } }

/* tab bar + toast */
.tabbar { position: fixed; left: 0; right: 0; bottom: 0; display: flex; justify-content: center; background: color-mix(in srgb, var(--card) 92%, transparent); border-top: 1px solid var(--separator); padding-bottom: env(safe-area-inset-bottom); z-index: 10; }
.tabbar > div { display: flex; width: 100%; max-width: 640px; }
.tab { flex: 1; min-height: 56px; display: grid; justify-items: center; align-content: center; gap: 2px; font-size: 11px; color: var(--muted); text-align: center; }
.tab svg { width: 26px; height: 26px; }
.tab[aria-current="page"] { color: var(--blue); }
#toast { position: fixed; left: 50%; bottom: calc(96px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px); max-width: calc(100% - 32px); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 15px; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 40; text-align: center; }
#toast.show { opacity: 1; transform: translate(-50%, 0); }
.busy-veil { position: fixed; inset: 0; z-index: 30; background: var(--overlay); display: grid; place-items: center; }
.busy-veil .spinner { width: 34px; height: 34px; border-width: 3px; color: #fff; }

@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }

/* utilities */
.mt16 { margin-top: 16px; } .mt18 { margin-top: 18px; } .mt20 { margin-top: 20px; }
.w60 { width: 60px; }
.bold { font-weight: 600; }
.flat { margin: 2px 0 0; }
.seg { width: 100%; }
.ghost { position: fixed; opacity: 0; pointer-events: none; }
.done-check { display: block; width: 56px; height: 56px; margin: 0 auto; }
.done-check svg, .check svg { display: block; width: 100%; height: 100%; }
.check { display: block; width: 22px; height: 22px; flex: none; }
.done { padding-top: 0; }
.qr-card { margin: 12px auto; width: min(100%, 280px); }
.sheet:focus { outline: none; }
.tiles { margin-top: 12px; }
