:root {
  --bg: #0b0d10;
  --panel: #14181d;
  --line: #262c34;
  --text: #e8eaed;
  --dim: #8a929c;
  --accent: #4da3ff;
  --amber: #ffb020;
  --red: #ff3b30;
  --green: #2ecc71;
  --btn: #1c2229;
  --btn-active: #2b3540;
  --gap: clamp(8px, 1.2vw, 16px);
  --btn-h: max(72px, 9vh);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
}

.app {
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  padding: var(--gap) max(var(--gap), 16px);
}
.app > * { flex: 0 0 auto; }
.app > .dash { flex: 1 1 auto; }

.status {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 20px;
  font-size: clamp(14px, 1.6vh, 18px);
  color: var(--text);
}
.status .right { margin-left: auto; }
.dim { color: var(--dim); }
.ok { color: var(--green); }
.warn { color: var(--amber); }
.bad { color: var(--red); }

.msg {
  background: #3a2a07;
  border: 1px solid var(--amber);
  color: #ffe2a8;
  padding: 10px 14px;
  border-radius: 10px;
  font-size: clamp(15px, 1.9vh, 20px);
}
.msg.error { background: #3a0d0b; border-color: var(--red); color: #ffc9c4; }

/* Brzinomjer i okretaji */
.dash {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.25fr);
  gap: calc(var(--gap) * 2);
  align-items: center;
  min-height: 0;
  overflow: hidden;
}

.speed-box { display: flex; align-items: baseline; justify-content: center; gap: 12px; min-width: 0; }
.speed {
  font-size: clamp(64px, min(24vh, 17vw), 300px);
  font-weight: 700;
  line-height: 0.9;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.03em;
}
.unit { font-size: clamp(18px, 3vh, 34px); color: var(--dim); }

.engine-box { display: flex; flex-direction: column; gap: calc(var(--gap) * 0.8); min-width: 0; }
.gear-row { display: flex; align-items: center; gap: calc(var(--gap) * 1.5); }
.gear {
  min-width: 1.6em;
  text-align: center;
  font-size: clamp(44px, min(11vh, 8vw), 130px);
  font-weight: 700;
  line-height: 1;
  padding: 0.05em 0.2em;
  border: 3px solid var(--line);
  border-radius: 14px;
}
.gear.shifting { border-color: var(--accent); }
.rpm-num {
  font-size: clamp(30px, min(7vh, 5vw), 80px);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.rpm-num small { font-size: 0.4em; color: var(--dim); font-weight: 400; }

.rpm-bar {
  position: relative;
  height: clamp(28px, 6vh, 64px);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
}
.rpm-fill {
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, #2a6fd6, var(--accent));
  transform-origin: left center;
  transform: scaleX(0);
  will-change: transform;
}
.rpm-bar.high .rpm-fill { background: linear-gradient(90deg, #2a6fd6, var(--amber)); }
.rpm-bar.limiter .rpm-fill { background: var(--red); }
.rpm-red {
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  background: repeating-linear-gradient(135deg, rgba(255, 59, 48, 0.35) 0 8px, rgba(255, 59, 48, 0.15) 8px 16px);
}
.rpm-scale {
  position: relative;
  height: 1.4em;
  font-size: clamp(13px, 1.6vh, 18px);
  color: var(--dim);
  font-variant-numeric: tabular-nums;
}
.rpm-scale span { position: absolute; transform: translateX(-50%); }

/* Kontrole */
.controls { display: flex; gap: var(--gap); align-items: stretch; flex-wrap: wrap; }
.row-main > * { flex: 1 1 0; }
.row-main .btn-rev { flex: 2 1 0; }
.row-main .seg { flex: 2 1 0; }

.btn, .seg button {
  min-height: var(--btn-h);
  border: 1px solid var(--line);
  background: var(--btn);
  color: var(--text);
  font: inherit;
  font-size: clamp(18px, 2.6vh, 28px);
  font-weight: 650;
  letter-spacing: 0.02em;
  border-radius: 14px;
  padding: 0 18px;
  touch-action: manipulation;
  cursor: pointer;
}
.btn:active, .seg button:active { background: var(--btn-active); }
.btn:disabled { opacity: 0.35; }
.btn[aria-pressed="true"] { background: var(--btn-active); border-color: var(--accent); color: #fff; }
.btn-shift { font-size: clamp(32px, 4.5vh, 48px); font-weight: 500; }
.btn-start { background: #13351f; border-color: #1f6b3a; }
.btn-start.running { background: #3d1513; border-color: #7a2621; }
.btn-rev {
  background: #3a1210;
  border-color: #7a2621;
  touch-action: none;
  -webkit-touch-callout: none;
}
.btn-rev.held { background: var(--red); color: #fff; }

.seg { display: flex; border-radius: 14px; overflow: hidden; border: 1px solid var(--line); }
.seg button { flex: 1 1 0; border: 0; border-radius: 0; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--accent); color: #04121f; }

.row-sub { align-items: center; flex-wrap: nowrap; }
.row-sub .btn-small { flex: 0 0 auto; white-space: nowrap; }
.btn-small { min-height: max(64px, 7.5vh); font-size: clamp(15px, 2vh, 22px); padding: 0 14px; }

.field { display: flex; flex-direction: column; gap: 6px; font-size: clamp(14px, 1.8vh, 20px); color: var(--dim); }
.field b { color: var(--text); font-variant-numeric: tabular-nums; }
.field select {
  min-height: max(56px, 6.5vh);
  min-width: 0;
  width: clamp(170px, 18vw, 320px);
  background: var(--btn);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 12px;
  font: inherit;
  font-size: clamp(16px, 2.2vh, 22px);
  padding: 0 12px;
}
.field-vol { flex: 1 1 120px; min-width: 100px; }
.field-wide { flex: 1 1 260px; }

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: max(48px, 5.5vh);
  background: transparent;
  touch-action: none;
}
input[type="range"]::-webkit-slider-runnable-track { height: 10px; background: var(--line); border-radius: 5px; }
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 40px;
  height: 40px;
  margin-top: -15px;
  border-radius: 50%;
  background: var(--accent);
  border: 3px solid #0b0d10;
}
input[type="range"]::-moz-range-track { height: 10px; background: var(--line); border-radius: 5px; }
input[type="range"]::-moz-range-thumb { width: 36px; height: 36px; border-radius: 50%; background: var(--accent); border: 3px solid #0b0d10; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: var(--gap);
  display: flex;
  flex-direction: column;
  gap: var(--gap);
}
.panel[hidden], .panel-body[hidden], .msg[hidden] { display: none; }
.seg-small button { min-height: max(56px, 6vh); font-size: clamp(15px, 2vh, 22px); }
.panel-body { display: flex; gap: calc(var(--gap) * 2); align-items: center; flex-wrap: wrap; }

@media (max-width: 900px) {
  .dash { grid-template-columns: 1fr; }
  .speed { font-size: clamp(56px, 16vh, 160px); }
}
