:root {
  --bg: #000;
  --fg: #fff;
  --muted: #a1a1aa;
  --card: rgba(28, 28, 30, 0.92);
  --card2: #2c2c2e;
  --line: rgba(255, 255, 255, 0.14);
  --accent: #ffd60a;
  --go: #30d158;
  --amber: #ffb020;
  --red: #ff3b30;
  --blue: #0a84ff;
  --sa-t: env(safe-area-inset-top, 0px);
  --sa-r: env(safe-area-inset-right, 0px);
  --sa-b: env(safe-area-inset-bottom, 0px);
  --sa-l: env(safe-area-inset-left, 0px);
  --pad: 12px;
  --tap: 64px;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg); overflow: hidden; }
body {
  font: 17px/1.3 -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
  overscroll-behavior: none; touch-action: manipulation;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; touch-action: manipulation; }
button:disabled { opacity: 0.35; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
input, select { font: inherit; }

/* ---------- camera + overlays ---------- */
#preview { position: fixed; inset: 0; width: 100%; height: 100%; object-fit: contain; background: #000; z-index: 0; }
#shade { position: fixed; inset: 0; z-index: 1; pointer-events: none; background: transparent; transition: background 0.3s; }
#frame { position: fixed; inset: 0; z-index: 3; pointer-events: none; box-shadow: inset 0 0 0 0 transparent; transition: box-shadow 0.3s; }
.screen { position: fixed; inset: 0; z-index: 2; padding: calc(var(--sa-t) + var(--pad)) calc(var(--sa-r) + var(--pad)) calc(var(--sa-b) + var(--pad)) calc(var(--sa-l) + var(--pad)); }

/* ---------- ready screen ---------- */
#ready { display: grid; grid-template-columns: 1fr auto; grid-template-rows: auto auto 1fr auto; grid-template-areas: "top top" "banner banner" ". start" "controls start"; gap: 10px; }
.topbar { grid-area: top; display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; min-width: 0; flex: 1; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; min-height: 36px; padding: 6px 12px;
  border-radius: 18px; background: rgba(0, 0, 0, 0.62); border: 1px solid var(--line);
  font-size: 15px; font-weight: 600; white-space: nowrap; -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px);
  max-width: 100%; overflow: hidden; text-overflow: ellipsis; display: inline-block; line-height: 22px;
}
.chip.ok { color: #b7f5c5; border-color: rgba(48, 209, 88, 0.5); }
.chip.warn { color: #111; background: var(--amber); border-color: var(--amber); }
.chip.bad { color: #fff; background: var(--red); border-color: var(--red); }
.chip.info { color: #cfe6ff; border-color: rgba(10, 132, 255, 0.6); }
.navbtns { display: flex; gap: 6px; flex: none; }
.navbtn {
  width: 60px; height: 60px; border-radius: 16px; background: rgba(0, 0, 0, 0.62); border: 1px solid var(--line);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: 11px; font-weight: 600;
  -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px);
}
.reconnect {
  grid-area: banner; justify-self: start; min-height: var(--tap); padding: 0 22px; border-radius: 16px;
  background: var(--amber); color: #111; font-weight: 800; font-size: 20px;
}

.controls { grid-area: controls; display: flex; flex-direction: column; gap: 10px; align-self: end; min-width: 0; }
.row { display: flex; gap: 10px; flex-wrap: wrap; }
.seg {
  display: flex; border-radius: 18px; background: rgba(0, 0, 0, 0.62); border: 1px solid var(--line); padding: 4px; gap: 4px;
  -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px);
}
.seg button { min-width: 60px; height: 56px; padding: 0 12px; border-radius: 14px; font-weight: 700; font-size: 20px; }
.seg button.on { background: var(--fg); color: #000; }
.lens button { min-width: 72px; border-radius: 28px; font-size: 21px; }
.lens button.on { background: var(--accent); color: #000; }
.batt button { font-size: 28px; min-width: 56px; }
.battval { display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1; min-width: 78px !important; background: rgba(255,255,255,0.1); }
.battval small { font-size: 11px; letter-spacing: 0.08em; color: var(--muted); }
.battval b { font-size: 28px; }
.len button { font-variant-numeric: tabular-nums; font-size: 19px; }
.pill {
  min-height: 56px; padding: 0 18px; border-radius: 18px; background: rgba(0, 0, 0, 0.62); border: 1px solid var(--line);
  font-weight: 700; font-size: 18px; -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px);
}
.pill.mute.off { background: var(--red); border-color: var(--red); }
.pill.wide { width: 100%; }
.pill.danger { color: #ff8a80; }

.startwrap { grid-area: start; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; align-self: center; }
.start {
  width: clamp(130px, 34vmin, 190px); aspect-ratio: 1; border-radius: 50%;
  background: var(--red); color: #fff; box-shadow: 0 0 0 6px rgba(255, 255, 255, 0.9), 0 8px 30px rgba(0, 0, 0, 0.5);
  display: flex; flex-direction: column; align-items: center; justify-content: center; font-weight: 900; letter-spacing: 0.04em;
}
.start span { font-size: clamp(26px, 8vmin, 38px); }
.start small { font-size: 17px; font-weight: 700; opacity: 0.9; font-variant-numeric: tabular-nums; }
.start:active { transform: scale(0.96); }
.start:disabled { opacity: 0.5; }
.nextmatch { font-weight: 800; font-size: 18px; padding: 4px 12px; border-radius: 12px; background: rgba(0, 0, 0, 0.6); }

@media (orientation: portrait) {
  #ready { grid-template-columns: 1fr; grid-template-rows: auto auto 1fr auto auto; grid-template-areas: "top" "banner" "." "controls" "start"; }
  .navbtns { flex-direction: column; }
  .controls { align-self: end; }
  .row { justify-content: center; }
  .startwrap { flex-direction: row; gap: 16px; padding-bottom: 4px; }
  .nextmatch { order: -1; }
}

/* ---------- match screen ---------- */
body.phase-match #shade { background: rgba(0, 0, 0, 0.74); }
body.phase-match.bright #shade { background: rgba(0, 0, 0, 0.22); }
#matchScr { display: grid; grid-template-columns: 1fr auto; grid-template-rows: auto 1fr; grid-template-areas: "head btns" "clock btns"; gap: 12px; }
.mhead { grid-area: head; font-weight: 700; font-size: 18px; display: flex; gap: 12px; align-items: center; color: #e5e5ea; }
.rec { display: inline-flex; align-items: center; gap: 6px; color: var(--red); font-weight: 900; }
.rec i { width: 12px; height: 12px; border-radius: 50%; background: var(--red); animation: blink 1s steps(2, start) infinite; }
body.paused .rec { color: var(--muted); }
body.paused .rec i { animation: none; background: var(--muted); }
.clockwrap { grid-area: clock; display: flex; flex-direction: column; align-items: center; justify-content: center; min-width: 0; }
#clock {
  font-family: -apple-system, "SF Pro Display", system-ui, sans-serif; font-weight: 800; font-variant-numeric: tabular-nums;
  font-size: min(29vw, 76vh); line-height: 0.9; letter-spacing: -0.02em; color: #fff;
  text-shadow: 0 0 24px rgba(0, 0, 0, 0.9), 0 2px 4px #000;
}
#clock.long { font-size: min(23vw, 70vh); }
#pausedLbl { visibility: hidden; font-size: min(7vw, 12vh); font-weight: 900; letter-spacing: 0.2em; color: var(--accent); }
body.warn #clock { color: var(--amber); }
body.crit #clock { color: var(--red); }
body.warn #frame { box-shadow: inset 0 0 0 10px var(--amber); }
body.crit #frame { box-shadow: inset 0 0 0 14px var(--red); }
body.paused #clock { animation: flash 1s ease-in-out infinite; color: var(--accent); }
body.paused #pausedLbl { visibility: visible; animation: flash 1s ease-in-out infinite; }
body.paused #frame { box-shadow: inset 0 0 0 10px var(--accent); animation: flash 1s ease-in-out infinite; }
@keyframes flash { 50% { opacity: 0.2; } }
@keyframes blink { to { visibility: hidden; } }

.mbtns { grid-area: btns; display: flex; flex-direction: column; gap: 14px; justify-content: center; width: clamp(150px, 21vw, 220px); }
.mbtn { flex: 1; max-height: 170px; min-height: 90px; border-radius: 24px; font-weight: 900; font-size: clamp(24px, 4vw, 34px); letter-spacing: 0.04em; }
.mbtn.pause { background: var(--accent); color: #000; }
body.paused .mbtn.pause { background: var(--go); color: #000; }
.mbtn.end { background: rgba(255, 59, 48, 0.18); color: #fff; border: 3px solid var(--red); }
.mbtn.end.confirm { background: var(--red); font-size: clamp(18px, 3vw, 24px); }
.eye { position: fixed; left: calc(var(--sa-l) + var(--pad)); bottom: calc(var(--sa-b) + var(--pad)); width: 56px; height: 56px; border-radius: 50%; background: rgba(0,0,0,0.6); border: 1px solid var(--line); display: flex; align-items: center; justify-content: center; z-index: 4; }
body.bright .eye { background: var(--fg); color: #000; }

@media (orientation: portrait) {
  #matchScr { grid-template-columns: 1fr; grid-template-rows: auto 1fr auto; grid-template-areas: "head" "clock" "btns"; }
  #clock { font-size: min(40vw, 34vh); }
  #clock.long { font-size: min(30vw, 30vh); }
  #pausedLbl { font-size: 9vw; }
  .mbtns { flex-direction: row; width: auto; height: 130px; padding-left: 70px; }
  .mbtn { font-size: 28px; }
}

/* ---------- countdown ---------- */
.countdown { position: fixed; inset: 0; z-index: 10; background: rgba(0, 0, 0, 0.6); display: flex; flex-direction: column; align-items: center; justify-content: center; }
#cdNum { font-size: min(60vw, 70vh); font-weight: 900; line-height: 1; color: var(--accent); font-variant-numeric: tabular-nums; text-shadow: 0 0 30px #000; }
#cdNum.go { color: var(--go); font-size: min(30vw, 50vh); }
.countdown .ghost { position: absolute; bottom: calc(var(--sa-b) + 20px); }

/* ---------- save sheet ---------- */
.sheetback { position: fixed; inset: 0; z-index: 20; background: rgba(0, 0, 0, 0.55); display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  width: 100%; max-width: 900px; background: #1c1c1e; border-radius: 26px 26px 0 0; border-top: 1px solid var(--line);
  padding: 16px calc(var(--sa-r) + 16px) calc(var(--sa-b) + 14px) calc(var(--sa-l) + 16px);
  display: grid; grid-template-columns: 1fr 1fr; gap: 16px; animation: up 0.18s ease-out;
}
@keyframes up { from { transform: translateY(40%); opacity: 0; } }
.sheet-l { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.sumline { font-weight: 700; color: #e5e5ea; font-size: 16px; }
.vlabel { font-size: 13px; letter-spacing: 0.1em; color: var(--muted); font-weight: 700; }
.vdisplay { font-size: 64px; font-weight: 800; line-height: 1; font-variant-numeric: tabular-nums; }
.vdisplay small { font-size: 28px; margin-left: 6px; color: var(--muted); }
.vdisplay.bad { color: var(--red); }
.vdrop { font-size: 18px; font-weight: 700; color: var(--accent); min-height: 1.3em; }
.note { width: 100%; min-height: 48px; border-radius: 12px; border: 1px solid var(--line); background: #000; color: #fff; padding: 0 14px; font-size: 17px; -webkit-user-select: text; user-select: text; }
.sheet-actions { display: flex; gap: 10px; margin-top: auto; }
.save { flex: 1; min-height: 64px; border-radius: 18px; background: var(--go); color: #000; font-weight: 900; font-size: 24px; letter-spacing: 0.04em; }
.ghost { min-height: 56px; padding: 0 18px; border-radius: 16px; background: rgba(255, 255, 255, 0.1); font-weight: 700; }
.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.keypad button { min-height: 60px; border-radius: 16px; background: var(--card2); font-size: 28px; font-weight: 700; }
.keypad button:active { background: #48484a; }
@media (orientation: portrait) {
  .sheet { grid-template-columns: 1fr; }
  .vdisplay { font-size: 72px; }
  .keypad button { min-height: 64px; }
}
@media (orientation: landscape) and (max-height: 430px) {
  .keypad button { min-height: 0; height: calc((100vh - var(--sa-b) - 100px) / 4.4); max-height: 70px; }
  .vdisplay { font-size: 56px; }
}

/* ---------- modals ---------- */
.modal { position: fixed; inset: 0; z-index: 30; background: rgba(0, 0, 0, 0.6); display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal-card { background: #1c1c1e; border-radius: 22px; padding: 18px; width: min(560px, 100%); max-height: 100%; overflow: auto; display: flex; flex-direction: column; gap: 14px; }
.modal-card h2 { margin: 0; font-size: 20px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(72px, 1fr)); gap: 8px; }
.grid button { height: 64px; border-radius: 14px; background: var(--card2); font-size: 26px; font-weight: 800; }
.grid button.on { background: var(--fg); color: #000; }
.lenedit { display: flex; align-items: center; justify-content: center; gap: 8px; }
.lenedit button { height: 60px; padding: 0 12px; border-radius: 14px; background: var(--card2); font-weight: 700; }
.lenedit output { font-size: 48px; font-weight: 800; min-width: 3.2ch; text-align: center; font-variant-numeric: tabular-nums; }
.modal-actions { display: flex; gap: 10px; }

/* ---------- pages ---------- */
.page {
  position: fixed; inset: 0; z-index: 15; background: #000; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: calc(var(--sa-t) + 8px) calc(var(--sa-r) + 16px) calc(var(--sa-b) + 24px) calc(var(--sa-l) + 16px);
  -webkit-user-select: text; user-select: text;
}
.pagehead { position: sticky; top: calc(-1 * var(--sa-t) - 8px); padding-top: 4px; background: #000; z-index: 1; display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 12px; min-height: 64px; }
.pagehead h1 { margin: 0; font-size: 22px; text-align: center; }
.back { min-height: 52px; padding: 0 14px 0 6px; font-size: 19px; color: var(--blue); font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 14px; }
.loglist { list-style: none; margin: 8px 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.logitem { background: #1c1c1e; border-radius: 16px; padding: 12px 14px; display: grid; grid-template-columns: auto 1fr auto; gap: 4px 12px; align-items: center; }
.logitem .num { font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums; }
.logitem .meta { font-size: 15px; color: #e5e5ea; }
.logitem .meta2 { grid-column: 2 / 4; font-size: 13px; color: var(--muted); }
.logitem .note { grid-column: 1 / 4; min-height: 0; padding: 0; border: 0; background: none; font-size: 14px; color: #d1d1d6; font-style: italic; }
.badge { justify-self: end; padding: 6px 10px; border-radius: 10px; font-size: 13px; font-weight: 800; white-space: nowrap; background: var(--card2); }
.badge.uploaded { background: rgba(48, 209, 88, 0.2); color: #7ee29a; }
.badge.uploading { background: rgba(10, 132, 255, 0.25); color: #9ccaff; }
.badge.queued { background: rgba(255, 214, 10, 0.18); color: var(--accent); }
.badge.error { background: rgba(255, 59, 48, 0.25); color: #ff8a80; }
a.badge { text-decoration: none; }
.packlist { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 10px; margin-top: 8px; }
.pack { background: #1c1c1e; border-radius: 16px; padding: 14px; }
.pack h2 { margin: 0 0 6px; font-size: 22px; display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.pack dl { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; margin: 0; font-size: 15px; }
.pack dt { color: var(--muted); }
.pack dd { margin: 0; font-weight: 700; text-align: right; font-variant-numeric: tabular-nums; }
.pack.flag { box-shadow: inset 0 0 0 2px var(--amber); }
.flagtag { font-size: 12px; background: var(--amber); color: #000; padding: 4px 8px; border-radius: 8px; }
.spark { width: 100%; height: 36px; margin-top: 8px; }
.settings { max-width: 640px; margin: 0 auto; display: flex; flex-direction: column; gap: 10px; }
.settings h3 { margin: 18px 0 0; font-size: 14px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); }
.field { display: flex; align-items: center; justify-content: space-between; gap: 12px; background: #1c1c1e; border-radius: 14px; padding: 8px 14px; min-height: 56px; }
.field span { flex: 1; }
.field input[type="text"], .field input[type="number"], .field select { background: #000; color: #fff; border: 1px solid var(--line); border-radius: 10px; min-height: 42px; padding: 0 10px; max-width: 60%; }
.field input[type="text"] { flex: 1; min-width: 0; font-size: 14px; }
.field input[type="number"] { width: 100px; }
.field.toggle input { width: 28px; height: 28px; accent-color: var(--go); }
.btnrow { display: flex; flex-wrap: wrap; gap: 8px; }
.btnrow .pill { flex: 1; min-width: 140px; }

.toast {
  position: fixed; z-index: 50; left: 50%; top: 40%; transform: translate(-50%, -50%);
  max-width: calc(100% - 32px); padding: 12px 18px; border-radius: 16px; background: rgba(28, 28, 30, 0.95); border: 1px solid var(--line);
  font-weight: 600; box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5); text-align: center;
}
.toast.bad { background: var(--red); }
