:root {
  --bg: #0d0d0f;
  --glass: rgba(18, 18, 22, 0.72);
  --glass-strong: rgba(22, 22, 28, 0.94);
  --line: rgba(255, 255, 255, 0.14);
  --text: #f4f2ee;
  --muted: rgba(244, 242, 238, 0.62);
  --accent: #ffb020;
  --accent-ink: #1d1400;
  --low: #6fd0ff;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--bg); color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button, input, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }

#stage { position: fixed; inset: 0; touch-action: none; }
#video, #scene, #overlay { position: absolute; inset: 0; width: 100%; height: 100%; }
#video { object-fit: cover; background: #000; }
#overlay { pointer-events: auto; }

#crosshair {
  position: absolute; left: 50%; top: 50%; width: 28px; height: 28px;
  transform: translate(-50%, -50%); pointer-events: none; opacity: 0.85;
  background:
    linear-gradient(#fff, #fff) center / 2px 10px no-repeat,
    linear-gradient(#fff, #fff) center / 10px 2px no-repeat;
  border: 1.5px solid rgba(255, 255, 255, 0.7); border-radius: 50%;
  filter: drop-shadow(0 0 2px rgba(0, 0, 0, 0.8));
}
#edgeMarker {
  position: absolute; left: 12px; top: 50%; transform: translateY(-50%);
  font-size: 22px; color: var(--accent); pointer-events: none;
  text-shadow: 0 0 4px #000;
}

/* Top bar */
#topbar {
  position: fixed; top: 0; left: 0; right: 0;
  padding: calc(var(--safe-top) + 10px) 16px 0;
  display: flex; gap: 10px; align-items: flex-start; justify-content: space-between;
  pointer-events: none;
}
#topbar > * { pointer-events: auto; }
.pill {
  display: flex; flex-direction: column; align-items: flex-start; min-width: 0;
  background: var(--glass); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 8px 14px; backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  text-align: left; cursor: pointer;
}
#periodName { font-weight: 650; }
.muted { color: var(--muted); font-size: 13px; }
.icon-btn {
  width: 44px; height: 44px; flex: none; display: grid; place-items: center;
  background: var(--glass); border: 1px solid var(--line); border-radius: 50%;
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); cursor: pointer;
}

/* Bottom HUD */
#hud {
  position: fixed; left: 0; right: 0; bottom: 0;
  padding: 0 16px calc(var(--safe-bottom) + 14px);
  display: flex; flex-direction: column; gap: 8px; pointer-events: none;
}
#hud > * { pointer-events: auto; }
.readrow { display: flex; gap: 8px; align-items: center; justify-content: center; min-width: 0; }
.readrow .btn.small { flex: none; min-height: 32px; padding: 4px 10px; border-radius: 999px; }
.btn.attention { background: #ff6b5a; color: #fff; border-color: transparent; animation: pulse 1.2s ease-in-out infinite alternate; }
@keyframes pulse { to { opacity: 0.6; } }
.readout {
  min-width: 0; max-width: 100%;
  background: var(--glass); border: 1px solid var(--line); border-radius: 999px;
  padding: 5px 12px; font-size: 13px; font-variant-numeric: tabular-nums;
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.actions { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.actions .btn { padding: 10px 4px; font-size: 14px; }
.card {
  background: var(--glass-strong); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 10px 12px; font-size: 14px;
}
.card p { margin: 0 0 6px; }
.card .hint { margin: 0 0 8px; }
.btn.small { min-height: 36px; padding: 6px 10px; font-size: 13px; }
.sw.sun { height: 10px; background: rgba(255, 184, 48, 0.6); }
.sw.shade { height: 10px; border: 1px solid rgba(255,255,255,0.4);
  background: repeating-linear-gradient(45deg, #0c0c14 0 3px, #9696aa 3px 5px); }

.btn {
  min-height: 44px; padding: 10px 12px; border-radius: 12px; cursor: pointer;
  background: var(--glass); border: 1px solid var(--line); font-weight: 600;
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}
.btn:active { transform: scale(0.97); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.ghost { background: rgba(255, 255, 255, 0.06); }
.btn.on { background: var(--low); color: #00222f; border-color: transparent; }
.btn.big { width: 100%; min-height: 52px; font-size: 16px; }

/* Calibration banner */
.banner {
  position: fixed; left: 16px; right: 16px; top: calc(var(--safe-top) + 74px);
  background: var(--glass-strong); border: 1px solid var(--accent); border-radius: var(--radius);
  padding: 12px 14px;
}
.banner p { margin: 0 0 10px; }
.banner.warn { border-color: #ff6b5a; }
.row { display: flex; gap: 8px; }
.row > .btn { flex: 1; }
.row.spaced { margin-top: 18px; }

/* Start screen */
.screen {
  position: fixed; inset: 0; display: grid; place-items: center; padding: 24px 16px;
  background: radial-gradient(120% 80% at 50% 0%, #2b2215 0%, var(--bg) 60%);
}
.start-card { width: 100%; max-width: 380px; text-align: center; display: grid; gap: 12px; }
.start-card h1 { margin: 0; font-size: 30px; letter-spacing: -0.02em; }
.start-card p { margin: 0 0 8px; color: var(--muted); }
.start-card .logo { margin: 0 auto; }
.fine { font-size: 12px; }

/* Sheets */
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; max-height: 82vh; overflow-y: auto;
  background: var(--glass-strong); border-top: 1px solid var(--line);
  border-radius: 20px 20px 0 0; padding: 14px 16px calc(var(--safe-bottom) + 20px);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  animation: up 0.18s ease-out;
}
@keyframes up { from { transform: translateY(30px); opacity: 0; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.sheet h2 { margin: 0; font-size: 18px; }
.sheet h3 { margin: 18px 0 6px; font-size: 15px; display: flex; justify-content: space-between; }
.sheet h3 output { color: var(--accent); font-variant-numeric: tabular-nums; }
.close { width: 36px; height: 36px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 6px 0 12px; }
.chip {
  padding: 8px 12px; border-radius: 999px; border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.06); cursor: pointer;
}
.chip.on { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 650; }
.custom { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 8px; }
.custom label { display: grid; gap: 4px; color: var(--muted); font-size: 13px; }
.date-in { display: flex; gap: 6px; }
.date-in input { width: 56px; }
input[type=number], select {
  background: rgba(255, 255, 255, 0.08); border: 1px solid var(--line); border-radius: 10px;
  padding: 8px; min-width: 0; color-scheme: dark;
}
select { flex: 1; }
.opt {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 0; border-top: 1px solid var(--line);
}
.opt select { flex: 0 1 auto; max-width: 60%; }
input[type=checkbox] { width: 22px; height: 22px; accent-color: var(--accent); }
input[type=range] { width: 100%; accent-color: var(--accent); }
.opt input[type=range] { width: 50%; }
.hint { color: var(--muted); font-size: 13px; margin: 6px 0 10px; }
.loc { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 8px; }
.loc label { display: grid; gap: 4px; color: var(--muted); font-size: 13px; }
.legend { color: var(--muted); font-size: 13px; display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px; margin: 12px 0 0; }
.sw { display: inline-block; width: 18px; height: 4px; border-radius: 2px; margin-right: 4px; vertical-align: middle; }
.sw.high { background: var(--accent); }
.sw.low { background: var(--low); }
.sw.band { height: 10px; background: rgba(255, 184, 48, 0.4); }

/* Debug */
#debug {
  position: fixed; left: 8px; right: 8px; top: calc(var(--safe-top) + 70px); max-height: 52vh;
  overflow: auto; background: rgba(0, 0, 0, 0.78); border-radius: 10px; padding: 8px 10px;
  font: 11px/1.35 ui-monospace, Menlo, Consolas, monospace; pointer-events: auto;
}
#debug pre { margin: 0; white-space: pre-wrap; word-break: break-all; }
#debugLog { color: #ffcf7a; margin-top: 6px !important; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 130px); transform: translateX(-50%);
  max-width: calc(100% - 32px); background: var(--glass-strong); border: 1px solid var(--line);
  border-radius: 12px; padding: 10px 14px; text-align: center; font-size: 14px;
}
