:root {
  --bg: #0d1117;
  --panel: #161b22;
  --line: #2a313c;
  --text: #e6edf3;
  --dim: #8b949e;
  --accent: #ff8a3d;
  --fresh: #4fb3ff;
  --burn: #ffb02e;
  --ok: #3fb950;
  --warn: #f85149;
  --gap: 6px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden; }
body {
  font: 13px/1.2 -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", "Segoe UI", sans-serif;
  touch-action: manipulation;
  user-select: none; -webkit-user-select: none;
}
button { font: inherit; color: inherit; -webkit-appearance: none; appearance: none; display: inline-flex; align-items: center; justify-content: center; line-height: 1; margin: 0; }

#app {
  height: 100vh; height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto auto;
  padding: env(safe-area-inset-top) max(8px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(8px, env(safe-area-inset-left));
  gap: var(--gap);
  max-width: 560px; margin: 0 auto;
}

header { display: flex; align-items: center; gap: 8px; padding-top: 6px; min-width: 0; }
.brand { font-weight: 300; letter-spacing: .12em; font-size: 12px; color: var(--dim); }
.brand b, h1 b { font-weight: 800; color: var(--accent); }
.tach { flex: 1; min-width: 0; display: grid; grid-template-columns: auto 1fr; align-items: baseline; column-gap: 4px; }
#rpm { font: 700 22px/1 ui-monospace, "SF Mono", Menlo, monospace; text-align: right; min-width: 3.4em; font-variant-numeric: tabular-nums; }
.tach small { color: var(--dim); font-size: 10px; }
.tachbar { grid-column: 1 / 3; position: relative; height: 4px; background: var(--line); border-radius: 2px; margin-top: 3px; overflow: hidden; }
.tachbar i { position: absolute; inset: 0 auto 0 0; width: 0; background: linear-gradient(90deg, var(--fresh), var(--accent)); }
.tachbar em { position: absolute; inset: 0 0 0 auto; width: calc(100% * 1500 / 15500); background: rgba(248, 81, 73, .45); }  /* 幅は JS が limiter から決める */
/* シフトライト */
.shiftlight { display: flex; gap: 3px; }
.shiftlight i { width: 7px; height: 7px; border-radius: 50%; background: var(--line); }
header.shift .shiftlight i, header.redzone .shiftlight i { background: var(--accent); box-shadow: 0 0 6px var(--accent); }
header.redzone .shiftlight i { background: var(--warn); box-shadow: 0 0 8px var(--warn); animation: blink .12s steps(2) infinite; }
header.shift #rpm { color: var(--accent); }
header.redzone #rpm { color: var(--warn); }
@keyframes blink { 50% { opacity: .15; } }
/* 狭い画面: ロゴを隠し、操作列を詰める */
@media (max-width: 400px) {
  .brand { display: none; }
  .row label { width: 54px; font-size: 10.5px; }
  .speed { gap: 4px; }
  .seg button { font-size: 10px; }
  .act { padding: 0 7px; font-size: 10px; }
}
.icon { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; width: 40px; height: 36px; flex: none; padding: 0; }
.icon.txt { width: auto; padding: 0 12px; font-size: 10px; font-weight: 700; letter-spacing: .08em; color: var(--accent); }
.icon.on { border-color: var(--accent); }

.viz { position: relative; min-height: 0; overflow: hidden; background: radial-gradient(ellipse at 50% 40%, #1a212b, var(--bg) 70%); border-radius: 10px; }
canvas { display: block; position: absolute; inset: 0; width: 100%; height: 100%; }
.cwrap { position: relative; min-height: 0; }

.graph { height: 136px; display: grid; grid-template-rows: auto minmax(0, 1fr); gap: 2px; background: var(--panel); border-radius: 10px; padding: 4px 6px; }
.tabs { display: flex; gap: 4px; align-items: center; min-width: 0; }
.tabs button { background: none; border: 1px solid var(--line); border-radius: 6px; padding: 0 10px; height: 30px; font-size: 12px; color: var(--dim); flex: none; }
.tabs button.on { color: var(--text); border-color: var(--accent); }
.tabs button.run { margin-left: 2px; color: var(--accent); border-color: var(--accent); }
.tabs button.run:disabled { opacity: .6; }
#graphinfo { margin-left: auto; font-size: 10px; color: var(--dim); font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; flex: 1; text-align: right; }

.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 3px; }
.stats div { background: var(--panel); border-radius: 6px; padding: 3px 6px; display: grid; grid-template-columns: 1fr auto; align-items: baseline; }
.stats label { grid-column: 1 / 3; font-size: 9.5px; color: var(--dim); }
.stats b { font: 600 14px/1.1 ui-monospace, "SF Mono", Menlo, monospace; font-variant-numeric: tabular-nums; }
.stats small { font-size: 9px; color: var(--dim); }

.controls { display: grid; gap: 2px; padding-bottom: 6px; }
.row { display: flex; align-items: center; gap: 8px; height: 32px; }
.row label { width: 64px; font-size: 11px; color: var(--dim); flex: none; }
.row output { width: 40px; text-align: right; font: 600 12px ui-monospace, Menlo, monospace; flex: none; font-variant-numeric: tabular-nums; }
input[type=range] { flex: 1; min-width: 0; -webkit-appearance: none; appearance: none; background: none; height: 32px; margin: 0; }
input[type=range]::-webkit-slider-runnable-track { height: 6px; background: var(--line); border-radius: 3px; }
input[type=range]::-moz-range-track { height: 6px; background: var(--line); border-radius: 3px; }
input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 24px; height: 24px; margin-top: -9px; border-radius: 50%; background: var(--text); border: 3px solid var(--accent); }
input[type=range]::-moz-range-thumb { width: 18px; height: 18px; border-radius: 50%; background: var(--text); border: 3px solid var(--accent); }
#c-throttle::-webkit-slider-runnable-track { background: linear-gradient(90deg, var(--line), #6b3a1d); }
.chip { background: var(--panel); border: 1px solid var(--line); border-radius: 6px; font-size: 10px; padding: 0 8px; height: 28px; color: var(--dim); flex: none; }
.chip.on { color: var(--bg); background: var(--ok); border-color: var(--ok); font-weight: 700; }
input:disabled { opacity: .35; }
input[type=range] { touch-action: pan-y; }

.speed { gap: 6px; }
.seg { flex: 1; min-width: 0; display: flex; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; overflow: hidden; height: 32px; }
.seg button { flex: 1; min-width: 0; background: none; border: 0; font-size: 11px; color: var(--dim); padding: 0; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--accent); color: var(--bg); font-weight: 700; }
.act { flex: none; height: 32px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel); font-weight: 700; font-size: 11px; padding: 0 10px; }
#start:active, #start.cranking { background: var(--ok); color: var(--bg); }
.ign.on { border-color: var(--ok); color: var(--ok); }
.ign:not(.on) { border-color: var(--warn); color: var(--warn); }

#overlay { position: fixed; inset: 0; background: rgba(13, 17, 23, .82); display: grid; place-items: center; z-index: 10; backdrop-filter: blur(3px); }
#overlay.hidden { display: none; }
.card { text-align: center; padding: 24px; }
.card h1 { font-weight: 300; letter-spacing: .15em; margin: 0 0 8px; }
.card p { color: var(--dim); }
#go { margin: 14px 0; background: var(--accent); color: var(--bg); border: 0; border-radius: 12px; padding: 14px 22px; font-size: 16px; font-weight: 700; }
.note { font-size: 11px; }

/* SETUP シート（下からせり上がる。エンジンの断面が上に見えたまま調整できる） */
#sheet { position: fixed; inset: 0; z-index: 8; pointer-events: none; }
#sheet.open { pointer-events: auto; }
#sheet .backdrop { position: absolute; inset: 0; }
#sheet .panel {
  position: absolute; left: 0; right: 0; bottom: 0; max-width: 560px; margin: 0 auto;
  height: 63vh; height: 63dvh;
  display: grid; grid-template-rows: auto minmax(0, 1fr);
  background: var(--panel); border-top: 1px solid var(--line); border-radius: 14px 14px 0 0;
  box-shadow: 0 -10px 28px rgba(0, 0, 0, .5);
  padding-bottom: env(safe-area-inset-bottom);
  transform: translateY(105%); transition: transform .22s ease;
}
#sheet.open .panel { transform: none; }
.phead { display: flex; align-items: center; gap: 8px; padding: 6px 10px 6px 14px; border-bottom: 1px solid var(--line); min-height: 44px; }
.phead b { letter-spacing: .12em; font-size: 12px; color: var(--accent); }
#sheetnote { flex: 1; font-size: 10px; color: var(--dim); line-height: 1.3; }
#sheetclose { width: 40px; height: 36px; }
.pbody { overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; padding: 0 14px 18px; touch-action: pan-y; }
.sec { padding-top: 10px; }
.sec > h3 { margin: 0 0 2px; font-size: 11px; font-weight: 700; letter-spacing: .08em; color: var(--dim); display: flex; gap: 8px; align-items: baseline; }
.sec > h3 small { font-weight: 400; letter-spacing: 0; font-size: 10px; color: var(--warn); }
.srow { padding: 4px 0 6px; border-top: 1px solid rgba(42, 49, 60, .6); }
.sec > h3 + .srow, .sec > .chips + .srow { border-top: 0; }
.shead { display: flex; align-items: baseline; gap: 8px; }
.shead label { font-size: 12px; flex: 1; }
.shead output { font: 600 12px ui-monospace, Menlo, monospace; font-variant-numeric: tabular-nums; }
.srow input[type=range] { width: 100%; flex: none; display: block; }
.srow .seg { margin: 3px 0; }
.srow .seg button { min-height: 32px; }
.hint { margin: 0; font-size: 10.5px; line-height: 1.35; color: var(--dim); }
.calc { margin: 0 0 1px; font: 600 11px ui-monospace, Menlo, monospace; color: var(--fresh); }
.calc:empty { display: none; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 4px 0 6px; }
.pchip { min-height: 36px; padding: 0 12px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); color: var(--text); font-size: 12px; font-weight: 600; }
.pchip.on { border-color: var(--accent); color: var(--accent); }
.pchip.reset { color: var(--dim); font-weight: 400; }
.sec.design.locked .srow { opacity: .8; }
.sec.design.locked input[type=range] { opacity: .35; }
.lockmsg { display: none; }
.sec.design.locked .lockmsg { display: inline; }

/* 横向き: 左にエンジン、右にグラフと操作 */
@media (orientation: landscape) and (max-height: 560px) {
  #app {
    max-width: none;
    grid-template-columns: minmax(0, 1fr) minmax(300px, 46%);
    grid-template-rows: auto minmax(0, 1fr) auto auto;
  }
  header { grid-column: 1 / 3; padding-top: 2px; }
  .viz { grid-row: 2 / 5; }
  .graph { grid-column: 2; height: auto; min-height: 80px; }
  .stats, .controls { grid-column: 2; }
  .row { height: 28px; }
  .controls { padding-bottom: 2px; }
  /* シートは右側に寄せて、左のエンジンを見せたまま調整する */
  #sheet .panel {
    left: auto; right: 0; top: 0; bottom: 0; width: min(54%, 480px); height: auto; max-width: none; margin: 0;
    border-top: 0; border-left: 1px solid var(--line); border-radius: 14px 0 0 14px;
    transform: translateX(105%);
    padding-right: env(safe-area-inset-right);
  }
  #sheet.open .panel { transform: none; }
}
