:root {
  --bg: #0d0f12;
  --panel: rgba(16, 19, 23, 0.72);
  --panel-solid: #14181d;
  --line: rgba(255, 255, 255, 0.08);
  --text: #e7eaee;
  --muted: #8a949f;
  --accent: #e2402f;
  --accent-soft: rgba(226, 64, 47, 0.18);
  --ok: #43d17d;
  --intake: #3d8bff;
  --compression: #8fc3ff;
  --power: #ff7a1a;
  --exhaust: #8a7d70;
  --radius: 10px;
  font-family: "Segoe UI", Inter, system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden; }
button { font: inherit; color: inherit; }

#viewport { position: fixed; inset: 0; }
#viewport canvas { display: block; outline: none; }
#labels { position: fixed; inset: 0; pointer-events: none; }

/* boot */
.boot { position: fixed; inset: 0; display: grid; place-content: center; text-align: center; background: var(--bg); z-index: 20; transition: opacity 0.6s ease; }
.boot-title { font-size: 28px; font-weight: 600; letter-spacing: 0.04em; }
.boot-step { margin-top: 10px; color: var(--muted); font-size: 14px; }
.boot.done { opacity: 0; pointer-events: none; }

.ui { position: fixed; inset: 0; pointer-events: none; transition: opacity 0.8s ease; }
.ui.hidden { opacity: 0; }
.ui.hidden * { pointer-events: none !important; }
.ui > * { pointer-events: auto; }
.ui::before, .ui::after { content: ""; position: absolute; left: 0; right: 0; pointer-events: none; }
.ui::before { top: 0; height: 120px; background: linear-gradient(rgba(8, 9, 11, 0.55), rgba(8, 9, 11, 0)); }
.ui::after { bottom: 0; height: 150px; background: linear-gradient(rgba(8, 9, 11, 0), rgba(8, 9, 11, 0.6)); }
.card { background: var(--panel); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); border: 1px solid var(--line); border-radius: var(--radius); }

/* title */
.title { position: absolute; top: 18px; left: 22px; pointer-events: auto; text-shadow: 0 1px 8px rgba(0,0,0,0.6); }
.title-main { font-size: 22px; font-weight: 650; letter-spacing: 0.02em; }
.title-main span { color: var(--accent); }
.title-sub { margin-top: 2px; font-size: 12.5px; color: var(--muted); }
.link { background: none; border: 0; color: #b9c3cd; text-decoration: underline; cursor: pointer; font-size: 12.5px; padding: 0 0 0 8px; }
.link:hover { color: #fff; }

/* info card */
.info { position: absolute; top: 80px; left: 22px; width: 330px; padding: 14px 16px 14px; }
.info.hidden { display: none; }
.info-name { font-weight: 600; font-size: 15px; padding-right: 20px; }
.info-desc { margin-top: 6px; font-size: 13px; line-height: 1.45; color: #c3cbd3; }
.info-live { margin-top: 8px; font-size: 12.5px; color: #ffd28a; font-variant-numeric: tabular-nums; }
.close { position: absolute; top: 6px; right: 8px; background: none; border: 0; color: var(--muted); font-size: 20px; cursor: pointer; line-height: 1; }
.close:hover { color: #fff; }

/* gauges */
.gauges { position: absolute; top: 16px; right: 16px; width: 252px; padding: 12px 14px 10px; }
#tach { width: 220px; height: 220px; display: block; margin: 0 auto; }
.readouts { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 12px; margin-top: 4px; }
.readouts div { display: flex; flex-direction: column; border-top: 1px solid var(--line); padding-top: 5px; }
.readouts label { font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.readouts b { font-size: 17px; font-weight: 600; font-variant-numeric: tabular-nums; }
.readouts i { font-style: normal; font-size: 11px; color: var(--muted); margin-left: 2px; }
.readouts div { position: relative; }
.readouts i { position: absolute; right: 0; bottom: 3px; }

/* cylinders */
.cyls { position: absolute; right: 16px; top: 456px; width: 252px; padding: 10px 14px 12px; }
.cyl-head { display: flex; justify-content: space-between; font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
#crank-angle { font-variant-numeric: tabular-nums; color: var(--text); }
.cyl-row { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.cyl-row em { font-style: normal; font-size: 11px; color: var(--muted); width: 44px; }
.cyl-cells { display: grid; grid-template-columns: repeat(4, 1fr); gap: 5px; flex: 1; }
.cell { height: 34px; border-radius: 6px; display: grid; place-content: center; font-size: 12px; font-weight: 600; background: #222; position: relative; overflow: hidden; transition: background 0.08s linear; }
.cell small { display: block; font-size: 9px; font-weight: 500; opacity: 0.8; text-align: center; }
.cell.intake { background: color-mix(in srgb, var(--intake) 55%, #111); }
.cell.compression { background: color-mix(in srgb, var(--compression) 40%, #111); }
.cell.power { background: color-mix(in srgb, var(--power) 60%, #111); }
.cell.exhaust { background: color-mix(in srgb, var(--exhaust) 55%, #111); }
.cell .flash { position: absolute; inset: 0; background: radial-gradient(circle, #fff6d0 0%, rgba(255,160,40,0.7) 45%, rgba(255,80,0,0) 75%); opacity: 0; }
.cyl-legend { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.lg { font-size: 10.5px; color: var(--muted); display: inline-flex; align-items: center; gap: 4px; }
.lg::before { content: ""; width: 8px; height: 8px; border-radius: 2px; background: var(--c); }
.lg.intake { --c: var(--intake); }
.lg.compression { --c: var(--compression); }
.lg.power { --c: var(--power); }
.lg.exhaust { --c: var(--exhaust); }

/* dock */
.dock { position: absolute; left: 50%; bottom: 40px; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; align-items: center; }
.dock-row { display: flex; gap: 8px; align-items: stretch; background: var(--panel); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); border: 1px solid var(--line); border-radius: var(--radius); padding: 8px; }
.start { display: flex; align-items: center; gap: 8px; min-width: 112px; justify-content: center; border: 1px solid rgba(226, 64, 47, 0.5); background: var(--accent-soft); border-radius: 8px; padding: 0 16px; font-weight: 650; cursor: pointer; letter-spacing: 0.04em; text-transform: uppercase; font-size: 13px; }
.start .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 10px var(--accent); }
.start.running { border-color: rgba(67, 209, 125, 0.55); background: rgba(67, 209, 125, 0.14); }
.start.running .dot { background: var(--ok); box-shadow: 0 0 10px var(--ok); }
.start:disabled { opacity: 0.4; cursor: not-allowed; }
.start:hover:not(:disabled) { filter: brightness(1.2); }
.slider { display: flex; flex-direction: column; justify-content: center; width: 200px; padding: 2px 8px; }
.slider label { font-size: 11px; color: var(--muted); display: flex; justify-content: space-between; text-transform: uppercase; letter-spacing: 0.05em; }
.slider label b { color: var(--text); font-variant-numeric: tabular-nums; }
input[type=range] { -webkit-appearance: none; appearance: none; width: 100%; height: 22px; background: none; cursor: pointer; margin: 2px 0 0; }
input[type=range]::-webkit-slider-runnable-track { height: 4px; border-radius: 2px; background: linear-gradient(90deg, var(--accent) var(--p, 0%), rgba(255,255,255,0.14) var(--p, 0%)); }
input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 16px; height: 16px; border-radius: 50%; background: #f2f2f2; margin-top: -6px; box-shadow: 0 1px 4px rgba(0,0,0,0.5); }
input[type=range]::-moz-range-track { height: 4px; border-radius: 2px; background: rgba(255,255,255,0.14); }
input[type=range]::-moz-range-progress { height: 4px; border-radius: 2px; background: var(--accent); }
input[type=range]::-moz-range-thumb { width: 16px; height: 16px; border-radius: 50%; background: #f2f2f2; border: 0; }
input[type=range]:disabled { opacity: 0.35; cursor: default; }
.seg { display: flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { background: transparent; border: 0; border-right: 1px solid var(--line); padding: 7px 11px; font-size: 12.5px; color: #c3cbd3; cursor: pointer; white-space: nowrap; }
.seg button:last-child { border-right: 0; }
.seg button:hover { background: rgba(255,255,255,0.06); color: #fff; }
.seg button.on { background: rgba(255,255,255,0.12); color: #fff; box-shadow: inset 0 -2px 0 var(--accent); }
.seg button:disabled { opacity: 0.4; cursor: not-allowed; }
.sound { align-items: center; padding-right: 10px; }
.seg button.attn { color: #fff; background: var(--accent-soft); box-shadow: inset 0 -2px 0 var(--accent); }
.sound input { width: 80px; margin-left: 8px; }

.help { position: absolute; left: 22px; right: 22px; bottom: 10px; display: flex; justify-content: space-between; font-size: 11.5px; color: #7c8792; text-shadow: 0 1px 4px rgba(0,0,0,0.8); pointer-events: none; }
.help b { color: #aab4be; font-weight: 600; }

.toast { position: absolute; left: 50%; top: 22px; transform: translateX(-50%); background: var(--panel-solid); border: 1px solid var(--line); padding: 9px 16px; border-radius: 8px; font-size: 13px; transition: opacity 0.3s ease; }
.toast.hidden { opacity: 0; pointer-events: none; }

/* labels */
.label3d { position: relative; transform: translate(0, 0); pointer-events: none; }
.label3d .tag { position: absolute; left: 10px; bottom: 10px; white-space: nowrap; font-size: 11.5px; font-weight: 600; letter-spacing: 0.02em; color: #fff; background: rgba(12, 14, 17, 0.82); border: 1px solid rgba(255,255,255,0.14); border-left: 2px solid var(--accent); padding: 3px 8px; border-radius: 4px; }
.label3d .pin { position: absolute; left: -4px; top: -4px; width: 8px; height: 8px; border-radius: 50%; background: #fff; box-shadow: 0 0 0 2px rgba(226,64,47,0.8); }
.label3d .lead { position: absolute; left: 0; bottom: 0; width: 10px; height: 12px; border-left: 1px solid rgba(255,255,255,0.65); border-top: 1px solid rgba(255,255,255,0.65); }

/* specs dialog */
.specs { background: var(--panel-solid); color: var(--text); border: 1px solid var(--line); border-radius: 12px; width: min(680px, calc(100vw - 32px)); padding: 22px 26px; }
.specs::backdrop { background: rgba(0,0,0,0.55); }
.specs h2 { margin: 0 0 12px; font-size: 18px; font-weight: 600; }
.specs h3 { margin: 18px 0 6px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.specs table { width: 100%; border-collapse: collapse; font-size: 13px; }
.specs td { padding: 6px 4px; border-bottom: 1px solid var(--line); }
.specs td:last-child { color: var(--muted); text-align: right; font-size: 12px; }
.specs a { color: #9cc6ff; }
.specs ul { margin: 0; padding-left: 18px; font-size: 13px; line-height: 1.6; }
.specs .note { font-size: 12px; color: var(--muted); line-height: 1.5; }
.specs form { position: absolute; right: 10px; top: 8px; }

.fatal { position: fixed; left: 16px; right: 16px; bottom: 16px; background: #3a1010; border: 1px solid #a33; color: #fdd; padding: 12px 14px; border-radius: 8px; font: 12px/1.4 Consolas, monospace; white-space: pre-wrap; z-index: 50; }
.fatal.hidden { display: none; }

.rev, .more { border: 1px solid var(--line); background: rgba(255,255,255,0.05); border-radius: 8px; padding: 0 14px; font-size: 12.5px; color: #dfe4ea; cursor: pointer; white-space: nowrap; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; touch-action: none; }
.rev:hover, .more:hover { background: rgba(255,255,255,0.1); }
.rev.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.more { display: none; }

@media (max-width: 1100px) {
  .cyls { display: none; }
  .slider { width: 150px; }
  .help span:last-child { display: none; }
}

/* Phones (portrait and landscape) */
@media (max-width: 760px), (max-height: 520px) {
  .ui::before { height: 70px; }
  .ui::after { height: 120px; }
  .title { top: calc(8px + env(safe-area-inset-top)); left: calc(12px + env(safe-area-inset-left)); max-width: calc(100vw - 150px); }
  .title-main { font-size: 16px; }
  .title-sub { font-size: 10.5px; line-height: 1.4; }
  .link { font-size: 11px; padding-left: 0; display: block; }

  .gauges { top: calc(8px + env(safe-area-inset-top)); right: calc(8px + env(safe-area-inset-right)); width: 112px; padding: 6px 8px 6px; transform: none; }
  #tach { width: 96px; height: 96px; }
  .readouts { grid-template-columns: 1fr; gap: 2px; margin-top: 2px; }
  .readouts div:nth-child(n+3) { display: none; }
  .readouts div { padding-top: 2px; }
  .readouts label { font-size: 9px; }
  .readouts b { font-size: 13px; }
  .readouts i { font-size: 9.5px; }
  .cyls { display: none; }

  .info { top: calc(64px + env(safe-area-inset-top)); left: 8px; right: 130px; width: auto; max-height: 34vh; overflow-y: auto; padding: 10px 12px; }
  .info-name { font-size: 13.5px; }
  .info-desc, .info-live { font-size: 12px; }
  .toast { top: auto; bottom: calc(76px + env(safe-area-inset-bottom)); max-width: 84vw; font-size: 12px; text-align: center; }
  .help { display: none; }
  .label3d .tag { font-size: 10px; padding: 2px 6px; }

  .dock { left: calc(8px + env(safe-area-inset-left)); right: calc(8px + env(safe-area-inset-right)); bottom: calc(8px + env(safe-area-inset-bottom)); transform: none; align-items: stretch; gap: 6px; }
  .dock-row { flex-wrap: wrap; padding: 6px; gap: 6px; }
  .start { min-width: 0; padding: 0 12px; min-height: 42px; font-size: 12px; }
  .rev, .more { min-height: 42px; padding: 0 12px; }
  .more { display: block; }
  .slider.throttle { flex: 1 1 100px; width: auto; min-width: 90px; }
  .dock .opt, .dock-row.secondary { display: none; }
  .dock.expanded .opt { display: flex; flex: 1 1 140px; width: auto; }
  .dock.expanded .dock-row.secondary { display: flex; max-height: 38vh; overflow-y: auto; }
  .seg { flex-wrap: wrap; }
  .seg button { padding: 9px 9px; font-size: 12px; }
  .sound input { width: 70px; }
}
@media (max-height: 520px) {
  .gauges { width: 104px; }
  #tach { width: 84px; height: 84px; }
  .readouts div:nth-child(n+2) { display: none; }
  .dock.expanded .dock-row.secondary { max-height: 46vh; }
}
