:root {
  --ink: #f4f2ee;
  --ink-dim: #c9c5be;
  --glass: rgba(24, 26, 29, 0.62);
  --glass-strong: rgba(24, 26, 29, 0.86);
  --line: rgba(255, 255, 255, 0.14);
  --accent: #e9c46a;
  --accent-ink: #1c1f22;
  --gate-bg: #f3efe7;
  --gate-card: #ffffff;
  --gate-ink: #23262a;
  --gate-muted: #6b6f75;
  --gate-line: #ddd6ca;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --gate-bg: #16181b; --gate-card: #202327; --gate-ink: #eceae6; --gate-muted: #9a9ea5; --gate-line: #33373c;
  }
}
:root[data-theme="dark"] {
  --gate-bg: #16181b; --gate-card: #202327; --gate-ink: #eceae6; --gate-muted: #9a9ea5; --gate-line: #33373c;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: #1c1f22; color: var(--ink);
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif; overflow: hidden;
  -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent; }
button { font: inherit; }

/* gate */
.gate { position: fixed; inset: 0; display: grid; place-items: center; background: var(--gate-bg); padding: 16px; }
.gate-card { width: min(360px, 100%); background: var(--gate-card); color: var(--gate-ink); border: 1px solid var(--gate-line);
  border-radius: 14px; padding: 28px 24px; display: grid; gap: 10px; }
.gate-card h1 { margin: 0; font-size: 24px; letter-spacing: .2px; }
.gate-card p { margin: 0 0 8px; color: var(--gate-muted); }
.gate-card label { font-size: 13px; color: var(--gate-muted); }
.gate-card input { font-size: 16px; padding: 10px 12px; border-radius: 8px; border: 1px solid var(--gate-line);
  background: transparent; color: var(--gate-ink); -webkit-user-select: text; user-select: text; }
.gate-card button { padding: 10px; border-radius: 8px; border: 0; background: #2f6f5e; color: #fff; font-weight: 600; cursor: pointer; }
.gate-err { color: #c0392b !important; margin: 0 !important; }

/* loading */
.loading { position: fixed; inset: 0; display: grid; place-items: center; background: #1c1f22; z-index: 30; padding: 16px; }
.loading-card { width: min(420px, 100%); text-align: center; }
.loading-title { font-size: 22px; margin-bottom: 18px; color: var(--ink); }
.bar { height: 4px; background: #34383d; border-radius: 2px; overflow: hidden; }
#bar-fill { height: 100%; width: 0; background: var(--accent); transition: width .2s; }
#loading-msg { margin-top: 10px; color: var(--ink-dim); font-size: 13px; }

/* app */
#c { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
#labels { position: fixed; inset: 0; pointer-events: none; }
.hud-top { position: fixed; left: max(12px, env(safe-area-inset-left)); top: max(10px, env(safe-area-inset-top));
  pointer-events: none; }
.title { background: var(--glass); padding: 7px 12px; border-radius: 10px; backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px); display: flex; gap: 10px; align-items: baseline; }
.title b { font-weight: 600; }
#room-name { color: var(--ink-dim); }
#room-name:not(:empty)::before { content: "·"; margin-right: 10px; }

.hud-buttons { position: fixed; right: max(12px, env(safe-area-inset-right)); top: max(10px, env(safe-area-inset-top));
  display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; max-width: calc(100vw - 24px); }
.hud-buttons button, .menu button, .help button { background: var(--glass); color: var(--ink); border: 1px solid var(--line);
  padding: 7px 11px; border-radius: 9px; cursor: pointer; backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); }
.hud-buttons button.toggle.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.hud-buttons button:disabled { opacity: .45; cursor: default; }

.menu { position: fixed; right: max(12px, env(safe-area-inset-right)); top: 56px; background: var(--glass-strong);
  border: 1px solid var(--line); border-radius: 12px; padding: 6px; display: grid; gap: 2px; max-height: 70vh; overflow: auto;
  min-width: 200px; z-index: 5; }
.menu button { text-align: left; background: transparent; border: 0; padding: 8px 10px; backdrop-filter: none; }
.menu button:hover { background: rgba(255,255,255,.08); }

#minimap { position: fixed; left: max(12px, env(safe-area-inset-left)); bottom: max(12px, env(safe-area-inset-bottom));
  width: min(320px, 46vw); height: min(400px, 56vw); border-radius: 12px; background: rgba(24,26,29,.78);
  border: 1px solid var(--line); cursor: crosshair; }
#crosshair { position: fixed; left: 50%; top: 50%; width: 4px; height: 4px; margin: -2px; border-radius: 50%;
  background: rgba(255,255,255,.7); pointer-events: none; }
.click-hint { position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%); background: var(--glass);
  padding: 8px 14px; border-radius: 10px; color: var(--ink); pointer-events: none; white-space: nowrap; }

.help { position: fixed; inset: 0; margin: auto; width: min(560px, calc(100vw - 32px)); height: fit-content; max-height: 86vh;
  overflow: auto; background: var(--glass-strong); border: 1px solid var(--line); border-radius: 14px; padding: 20px 22px; z-index: 6; }
.help h2 { margin: 0 0 12px; font-size: 18px; font-weight: 600; }
.help table { border-collapse: collapse; width: 100%; }
.help td { padding: 5px 0; border-bottom: 1px solid var(--line); vertical-align: top; }
.help td:first-child { color: var(--accent); width: 42%; padding-right: 12px; }
.help .note { color: var(--ink-dim); font-size: 13px; margin: 14px 0; }

/* measurement labels */
.ml { position: absolute; transform: translate(-50%, -50%); pointer-events: none; white-space: nowrap;
  font-size: 12px; color: #1c1f22; background: rgba(255, 236, 170, 0.92); padding: 2px 6px; border-radius: 5px;
  font-variant-numeric: tabular-nums; box-shadow: 0 1px 3px rgba(0,0,0,.25); }
.ml.room { background: rgba(28, 31, 34, 0.86); color: #fff; padding: 6px 9px; border-radius: 8px; text-align: center; line-height: 1.35; }
.ml.room b { font-size: 13px; }
.ml.room small { color: #e9c46a; }
.ml.op { background: rgba(160, 220, 255, 0.92); }
.ml.h { background: rgba(190, 255, 190, 0.92); }

.joy { position: fixed; width: 120px; height: 120px; margin: -60px 0 0 -60px; border-radius: 50%;
  border: 2px solid rgba(255,255,255,.35); background: rgba(0,0,0,.15); pointer-events: none; }
#joy-knob { position: absolute; left: 50%; top: 50%; width: 48px; height: 48px; margin: -24px; border-radius: 50%;
  background: rgba(255,255,255,.55); }

@media (max-width: 640px) {
  .hud-buttons { top: auto; bottom: max(10px, env(safe-area-inset-bottom)); right: 8px; left: 8px; justify-content: center; }
  .hud-buttons button { padding: 8px 9px; font-size: 13px; }
  #minimap { bottom: 70px; }
  .menu { top: auto; bottom: 64px; left: 8px; right: 8px; }
  .click-hint { display: none; }
}

.panel { position: fixed; right: max(12px, env(safe-area-inset-right)); top: 56px; z-index: 5; width: min(340px, calc(100vw - 24px));
  background: var(--glass-strong); border: 1px solid var(--line); border-radius: 12px; padding: 12px; display: grid; gap: 10px; }
.panel .row { display: flex; align-items: center; gap: 10px; }
.panel label { width: 48px; color: var(--ink-dim); font-size: 13px; }
.panel input[type=range] { flex: 1; accent-color: var(--accent); }
.panel select { flex: 1; background: #2a2d31; color: var(--ink); border: 1px solid var(--line); border-radius: 7px; padding: 5px; font: inherit; }
.panel .seg { gap: 6px; }
.panel .seg button { flex: 1; background: transparent; color: var(--ink); border: 1px solid var(--line); border-radius: 8px; padding: 6px 4px; cursor: pointer; font-size: 13px; }
.panel .seg button.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.panel .small { color: var(--ink-dim); font-size: 12px; }
#t-hour-label { width: 44px; text-align: right; font-variant-numeric: tabular-nums; }
@media (max-width: 640px) { .panel { top: auto; bottom: 64px; left: 8px; right: 8px; width: auto; } }
