:root {
  --bg: #101418;
  --bg-2: #161c21;
  --panel: #1c2329;
  --panel-2: #232c33;
  --line: #2f3a43;
  --text: #e8edf1;
  --muted: #95a3ae;
  --accent: #ffb020;
  --accent-2: #3fb8ff;
  --ok: #3ddc84;
  --hl: #ffd23f;
  --radius: 10px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text); }
body {
  font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 13px;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}
button, select { font-family: inherit; }

#app {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ---------- top bar (single row) ---------- */
.topbar {
  display: flex; align-items: center; gap: 6px;
  padding: 5px 8px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--line);
  min-height: 40px;
}
.brand { display: flex; align-items: center; gap: 5px; font-weight: 800; font-size: 13px; white-space: nowrap; }
.brand-mark { color: var(--accent); }
.pick {
  appearance: none; -webkit-appearance: none;
  background: var(--panel-2) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='7'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%2395a3ae' stroke-width='1.8' fill='none'/%3E%3C/svg%3E") no-repeat right 8px center;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 24px 6px 9px;
  font-size: 13px; font-weight: 600;
  min-width: 0;
}
.pick.model { color: var(--accent); font-weight: 800; flex: none; }
.pick.task { flex: 1; }
.tools { display: flex; gap: 5px; flex: none; }
.tbtn {
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text);
  border-radius: 8px; padding: 0 9px; height: 30px; min-width: 34px;
  font-size: 13px; font-weight: 700; cursor: pointer;
}
.tbtn.off { opacity: .5; }
.tbtn.info { font: italic 800 15px Georgia, serif; border-radius: 50%; width: 30px; padding: 0; }
.tbtn.install { background: var(--accent); color: #1b1300; border-color: var(--accent); }

/* ---------- stage: meter | (guide + bench) ---------- */
#stage {
  position: relative;
  overflow: hidden;
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1.18fr) minmax(0, 1fr);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.meter-wrap { position: relative; z-index: 6; min-height: 0; min-width: 0; display: flex; }
.meter-pane {
  position: relative; flex: 1; min-height: 0; min-width: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 4px 2px 4px 6px;
}
.meter-pane svg { width: 100%; height: 100%; display: block; overflow: visible; }
.side { position: relative; z-index: 1; min-height: 0; min-width: 0; display: flex; flex-direction: column; padding: 6px 8px 6px 2px; gap: 6px; }

/* ---------- guide ---------- */
.guide {
  flex: none;
  background: linear-gradient(180deg, #1f2a31, #1a2228);
  border: 1px solid var(--line);
  border-left: 4px solid var(--accent);
  border-radius: var(--radius);
  padding: 6px 10px 7px;
  min-height: 58px;
}
.guide.free { border-left-color: var(--accent-2); }
.guide.done { border-left-color: var(--ok); }
.guide-head { display: flex; align-items: center; gap: 7px; font-size: 10.5px; color: var(--muted); }
.guide-mode { font-weight: 800; letter-spacing: .7px; color: var(--accent); }
.guide.free .guide-mode { color: var(--accent-2); }
.guide.done .guide-mode { color: var(--ok); }
.guide-count { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 60%; }
.guide-progress { flex: 1; height: 3px; background: var(--line); border-radius: 3px; overflow: hidden; }
.guide-progress div { height: 100%; width: 0; background: var(--accent); transition: width .35s; }
.guide.done .guide-progress div { background: var(--ok); }
.guide-text { font-weight: 650; font-size: 14px; line-height: 1.3; margin-top: 3px; }
.guide-text b { color: var(--hl); }
.guide-text .k {
  display: inline-block; border: 1px solid #5d6b75; border-radius: 5px; padding: 0 4px;
  font-size: 12px; line-height: 17px; background: #11171b; white-space: nowrap; color: var(--hl);
}
.guide-sub { font-size: 12px; color: var(--muted); line-height: 1.35; margin-top: 2px; }
.guide-sub:empty { display: none; }
.guide-act:empty { display: none; }
.guide .next-btn {
  margin-top: 4px; border: 0; border-radius: 7px; padding: 5px 11px;
  background: var(--ok); color: #062312; font-size: 12.5px; font-weight: 800; cursor: pointer;
}
.guide.flash { animation: flash .6s; }
@keyframes flash { 0% { background: #2d4a35; } 100% { background: #1c252b; } }

/* ---------- bench ---------- */
.bench-pane {
  position: relative; flex: 1; min-height: 0; display: flex; flex-direction: column;
  background:
    linear-gradient(180deg, rgba(255,255,255,.03), rgba(0,0,0,.15)),
    repeating-linear-gradient(90deg, #1b2227 0 38px, #1d252a 38px 76px);
  border: 1px solid var(--line); border-radius: var(--radius);
}
.cyl-chips { display: flex; gap: 5px; padding: 5px 6px 0; overflow-x: auto; scrollbar-width: none; touch-action: pan-x; }
.cyl-chips::-webkit-scrollbar { display: none; }
.cyl-chip {
  flex: none; border: 1px solid var(--line); background: #11171b; color: var(--muted);
  border-radius: 999px; padding: 3px 9px; font-size: 11.5px; font-weight: 700; cursor: pointer; white-space: nowrap;
}
.cyl-chip.on { color: #1b1300; background: var(--accent); border-color: var(--accent); }
.cyl-chip.hl { box-shadow: 0 0 0 2px var(--hl); animation: pulse 1.1s infinite; }
.bench { flex: 1; min-height: 0; }
.bench svg { width: 100%; height: 100%; display: block; }
.bench-status { font-size: 11px; color: var(--muted); padding: 0 8px 4px; min-height: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bench-status b { color: var(--text); font-weight: 600; }

#tubeCanvas { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 5; }

.toast {
  position: absolute; right: 12px; bottom: 12px; transform: translateY(20px);
  background: #2b1d10; border: 1px solid #7a4b14; color: #ffd9a6;
  padding: 7px 12px; border-radius: 9px; font-size: 12.5px; font-weight: 600;
  opacity: 0; pointer-events: none; transition: opacity .25s, transform .25s; z-index: 8;
  max-width: min(420px, calc(100% - 24px)); text-align: center;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.ok { background: #10291a; border-color: #2d7a4a; color: #b7f5cf; }
.toast.bad { background: #3a1210; border-color: #a33a30; color: #ffc2bb; }

/* ---------- device svg ---------- */
.dev-key { cursor: pointer; }
.dev-key .cap { transition: transform .05s; transform-box: fill-box; transform-origin: center; }
.dev-key.pressed .cap { transform: scale(.92); }
.dev-key.pressed .face { fill: #050607; }
.dev-key.pressed .rim { stroke: #ffd23f; }
.dev-key.latched .rim { stroke: #ff9f1a; }
.dev-key .ring { fill: none; stroke: var(--hl); stroke-width: 4; opacity: 0; }
.dev-key.hl .ring { animation: ring 1.1s infinite; }
@keyframes ring { 0% { opacity: .95; stroke-width: 6; } 70% { opacity: .15; stroke-width: 2; } 100% { opacity: .95; stroke-width: 6; } }
@keyframes pulse { 0%,100% { box-shadow: 0 0 0 2px var(--hl); } 50% { box-shadow: 0 0 0 5px rgba(255,210,63,.35); } }

.bench .hl-target { animation: hlsvg 1.1s infinite; }
@keyframes hlsvg { 0%,100% { filter: drop-shadow(0 0 0 rgba(255,210,63,0)); } 50% { filter: drop-shadow(0 0 6px rgba(255,210,63,1)); } }
.bench .tap { cursor: pointer; }
.bench .reg-hint { animation: hintPulse 0.9s ease-in-out infinite; pointer-events: none; }
@keyframes hintPulse { 0%, 100% { opacity: 1; transform: translateY(0); } 50% { opacity: .6; transform: translateY(-3px); } }

/* ---------- rotate prompt (portrait phones) ---------- */
.rotate {
  position: fixed; inset: 0; z-index: 50; display: none;
  flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  background: var(--bg); text-align: center; padding: 30px; font-size: 15px; color: var(--muted);
}
.rotate b { color: var(--text); font-size: 18px; }
.rotate-phone { font-size: 58px; animation: turn 2.2s ease-in-out infinite; }
@keyframes turn { 0%, 20% { transform: rotate(0); } 50%, 80% { transform: rotate(-90deg); } 100% { transform: rotate(0); } }
@media (orientation: portrait) and (max-width: 899px) {
  .rotate { display: flex; }
}

/* ---------- help dialog ---------- */
.info-dlg {
  border: 1px solid var(--line); border-radius: 14px; background: var(--panel); color: var(--text);
  padding: 0; width: min(560px, calc(100% - 24px)); max-height: calc(100dvh - 24px);
  display: flex; flex-direction: column; overflow: hidden;
}
.info-dlg:not([open]) { display: none; }
.info-dlg::backdrop { background: rgba(0,0,0,.6); }
.dlg-scroll { overflow-y: auto; padding: 12px 16px 6px; flex: 1; min-height: 0; }
.info-dlg h2 { margin: 0 0 4px; font-size: 17px; }
.info-dlg h3 { margin: 12px 0 5px; font-size: 12px; text-transform: uppercase; letter-spacing: .8px; color: var(--accent); }
.cards { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.card { background: var(--panel-2); border: 1px solid var(--line); border-radius: 9px; padding: 7px 8px; display: flex; flex-direction: column; gap: 2px; }
.card b { font-size: 13.5px; }
.card span { font-size: 12px; color: var(--text); }
.card small { font-size: 11px; color: var(--muted); }
.howto { margin: 0; padding-left: 17px; font-size: 12.5px; line-height: 1.45; }
.small { font-size: 12.5px; line-height: 1.45; margin: 0; color: var(--text); }
.info-dlg kbd { border: 1px solid var(--line); border-radius: 4px; padding: 0 4px; background: #11171b; font-size: 11.5px; }
.about {
  margin-top: 14px; padding: 9px 12px; border-radius: 10px; background: #13202a; border: 1px solid #27465c;
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 6px; font-size: 13px;
}
.about a { color: var(--accent-2); font-weight: 700; text-decoration: none; }
.disclaimer { color: var(--muted); font-size: 11px; margin: 8px 0 4px; }
.dlg-foot { flex: none; border-top: 1px solid var(--line); padding: 8px 12px; background: var(--panel); margin: 0; }
.dlg-close {
  width: 100%; border: 0; border-radius: 9px; padding: 9px 16px; background: var(--accent); color: #1b1300;
  font-size: 14px; font-weight: 800; cursor: pointer;
}

.factory-btn {
  width: 100%; margin-top: 6px; border: 1px solid #7a3a33; border-radius: 9px; padding: 8px 12px;
  background: #2a1513; color: #ffc2bb; font-size: 13px; font-weight: 700; cursor: pointer;
}
.factory-btn.armed { background: #c62828; border-color: #ff6b5e; color: #fff; }

/* ---------- cylinder editor ---------- */
.cyl-dlg { width: min(520px, calc(100% - 24px)); }
.muted { color: var(--muted); }
.cyl-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 10px; margin-top: 8px; }
.cyl-field { display: flex; flex-direction: column; gap: 2px; font-size: 12px; }
.cyl-field label { color: var(--muted); font-weight: 600; }
.cyl-field .row { display: flex; align-items: center; gap: 6px; }
.cyl-field input {
  flex: 1; min-width: 0; background: #11171b; color: var(--text); border: 1px solid var(--line); border-radius: 7px;
  padding: 6px 8px; font: 600 14px Inter, system-ui, sans-serif;
}
.cyl-field input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.cyl-field input.changed { border-color: #e0574a; }
.cyl-field .unit { color: var(--muted); font-size: 12px; min-width: 30px; }
.cyl-field .eq { color: var(--muted); font-size: 11px; min-height: 13px; }
.cyl-field.bal input { color: var(--muted); }
.cyl-msg { min-height: 16px; font-size: 12px; margin-top: 6px; color: #ffc2bb; font-weight: 600; }
.cyl-foot { display: flex; gap: 6px; }
.cyl-foot .dlg-close { flex: 1.2; }
.btn-2 {
  flex: 1; border: 1px solid var(--line); border-radius: 9px; padding: 9px 10px; background: #11171b; color: var(--text);
  font-size: 13px; font-weight: 700; cursor: pointer;
}
.dlg-close:disabled { opacity: .45; cursor: default; }
.cyl-chip.mod { border-color: #e0574a; color: #ffb3aa; }
.cyl-chip.mod.on { background: #c62828; border-color: #c62828; color: #fff; }

/* ---------- size tweaks ---------- */
@media (max-width: 720px) {
  .brand-text { display: none; }
  .tbtn { padding: 0 7px; min-width: 30px; }
}
@media (max-width: 560px) {
  #fsBtn { display: none; }
}
@media (max-height: 420px) {
  .topbar { min-height: 36px; padding: 3px 6px; }
  .pick { padding: 4px 22px 4px 8px; font-size: 12px; }
  .tbtn { height: 28px; font-size: 12px; }
  .guide { padding: 5px 8px 6px; min-height: 50px; }
  .guide-text { font-size: 12.5px; }
  .guide-sub { font-size: 11px; }
  .guide-text .k { font-size: 11px; line-height: 15px; }
  .cards { grid-template-columns: repeat(3, 1fr); }
}
@media (min-width: 1100px) {
  body { font-size: 14px; }
  .guide-text { font-size: 16px; }
  .guide-sub { font-size: 13px; }
}

/* ---------- meter full-screen view ---------- */
.focus-btn {
  position: absolute; top: 6px; right: 6px; z-index: 7;
  border: 1px solid var(--line); background: rgba(28,35,41,.9); color: var(--text);
  border-radius: 9px; padding: 5px 10px; font-size: 12.5px; font-weight: 800; cursor: pointer;
}
.focus-btn:active { background: var(--panel-2); }
#app.focus .topbar { display: none; }
#app.focus #stage { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) auto; }
#app.focus .side { grid-row: 2; padding: 0 6px 4px; }
#app.focus .meter-wrap { grid-row: 1; }
#app.focus .bench-pane, #app.focus .guide-sub, #app.focus .guide-count, #app.focus .guide-progress { display: none; }
#app.focus .guide { display: flex; align-items: center; gap: 8px; min-height: 0; padding: 4px 10px; }
#app.focus .guide-head { flex: none; }
#app.focus .guide-text { flex: 1; min-width: 0; margin: 0; font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#app.focus .guide-act { flex: none; }
#app.focus .guide .next-btn { margin: 0; padding: 4px 10px; }
#app.focus .meter-pane { padding: 2px 4px 4px; }
#app.focus .focus-btn { top: auto; bottom: 4px; right: 4px; }
