:root {
  --ink: #1c2340;
  --panel: rgba(20, 26, 52, 0.78);
  --card: #fffdf5;
  --gold: #ffc928;
  --gold-d: #c98e00;
  --green: #4bd04b;
  --green-d: #23902b;
  --red: #ff5a52;
  --red-d: #b92b25;
  --blue: #3fa4ff;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --font: ui-rounded, "SF Pro Rounded", "Nunito", "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; height: 100%; width: 100%;
  overflow: hidden; overscroll-behavior: none;
  background: #1c2340; color: #fff;
  font-family: var(--font);
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
  touch-action: none;
}

body { position: fixed; inset: 0; }

canvas#game { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }

.hidden { display: none !important; }

button { font-family: inherit; color: inherit; border: 0; cursor: pointer; touch-action: manipulation; }

/* ------------------------------------------------------------------ HUD */
#hud { position: absolute; inset: 0; pointer-events: none; }
#hud button { pointer-events: auto; }

.hud-top {
  position: absolute; left: 0; right: 0; top: 0;
  padding: calc(8px + var(--sat)) calc(10px + var(--sar)) 0 calc(10px + var(--sal));
  display: flex; align-items: flex-start; gap: 8px; justify-content: space-between;
}
.hud-box {
  background: rgba(20, 26, 52, 0.62); border: 2px solid rgba(255, 255, 255, 0.22);
  border-radius: 14px; padding: 5px 11px;
  font-weight: 800; font-variant-numeric: tabular-nums;
}
.hud-dist { min-width: 96px; }
#dist { font-size: clamp(18px, 3.4vmin, 28px); line-height: 1.1; }
#best { font-size: 11px; opacity: 0.8; font-weight: 700; }

.hud-fuel { flex: 1 1 auto; max-width: 300px; display: flex; align-items: center; gap: 8px; padding: 8px 11px; }
.fuel-ic {
  width: 14px; height: 17px; flex: none; border-radius: 3px; background: var(--red);
  border: 2px solid #fff; position: relative;
}
.fuel-ic::after { content: ""; position: absolute; left: 1px; top: -6px; width: 6px; height: 4px; background: #fff; border-radius: 1px; }
.fuelbar { flex: 1; height: 14px; border-radius: 8px; background: rgba(0, 0, 0, 0.45); overflow: hidden; }
#fuelFill {
  height: 100%; width: 100%; border-radius: 8px; transform-origin: left center;
  background: linear-gradient(#7dea6b, #35b53a);
}
.hud-fuel.low #fuelFill { background: linear-gradient(#ff8a7a, #e0342c); }
.hud-fuel.low { animation: pulse 0.6s infinite alternate; }
@keyframes pulse { to { border-color: #ff5a52; background: rgba(120, 20, 20, 0.7); } }

.hud-right { display: flex; gap: 8px; align-items: center; }
.hud-coins { display: flex; align-items: center; gap: 6px; font-size: clamp(15px, 2.8vmin, 22px); }
.coin-ic {
  display: inline-block; width: 1em; height: 1em; border-radius: 50%; flex: none;
  background: radial-gradient(circle at 35% 30%, #fff3b0, var(--gold) 55%, var(--gold-d));
  border: 2px solid #8a5d00;
}
.icon-btn {
  width: 42px; height: 42px; border-radius: 13px; flex: none;
  background: rgba(20, 26, 52, 0.62); border: 2px solid rgba(255, 255, 255, 0.22);
  font-size: 20px; font-weight: 800; display: grid; place-items: center;
}
.icon-btn:active { transform: scale(0.94); }
.icon-btn.off { opacity: 0.45; text-decoration: line-through; }
.pause-ic { width: 12px; height: 15px; border-left: 4px solid #fff; border-right: 4px solid #fff; }

#toast {
  position: absolute; left: 50%; top: 24%; transform: translateX(-50%);
  font-size: clamp(20px, 5vmin, 40px); font-weight: 900; letter-spacing: 0.02em; white-space: nowrap;
  color: var(--gold); text-shadow: 0 3px 0 #7a4a00, 0 0 18px rgba(0, 0, 0, 0.45);
  opacity: 0;
}
#toast.show { animation: toast 1.25s ease-out; }
@keyframes toast {
  0% { opacity: 0; transform: translateX(-50%) scale(0.5); }
  15% { opacity: 1; transform: translateX(-50%) scale(1.15); }
  28% { transform: translateX(-50%) scale(1); }
  75% { opacity: 1; }
  100% { opacity: 0; transform: translateX(-50%) translateY(-28px); }
}

.pedal {
  position: absolute; bottom: calc(14px + var(--sab));
  width: clamp(92px, 21vmin, 150px); height: clamp(92px, 21vmin, 150px);
  border-radius: 26px; border: 3px solid rgba(255, 255, 255, 0.55);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-weight: 900; font-size: clamp(15px, 3vmin, 22px); letter-spacing: 0.06em;
  box-shadow: 0 6px 0 rgba(0, 0, 0, 0.35);
  transition: transform 0.05s, box-shadow 0.05s;
  touch-action: none;
}
.pedal small { font-size: 11px; opacity: 0.75; font-weight: 700; letter-spacing: 0; }
.pedal-brake { left: calc(14px + var(--sal)); background: rgba(255, 90, 82, 0.72); }
.pedal-gas { right: calc(14px + var(--sar)); background: rgba(75, 208, 75, 0.72); }
.pedal.down { transform: translateY(5px) scale(0.97); box-shadow: 0 1px 0 rgba(0, 0, 0, 0.35); filter: brightness(1.25); }
@media (pointer: coarse) { .pedal small { display: none; } }

/* ---------------------------------------------------------------- screens */
.screen { position: absolute; inset: 0; display: flex; flex-direction: column; }

#menu {
  padding: calc(8px + var(--sat)) calc(12px + var(--sar)) calc(10px + var(--sab)) calc(12px + var(--sal));
  background: linear-gradient(rgba(16, 20, 44, 0.55), rgba(16, 20, 44, 0.25) 40%, rgba(16, 20, 44, 0.6));
  gap: 8px;
}
.menu-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.logo {
  margin: 0; font-size: clamp(22px, 5.2vmin, 42px); font-weight: 900; letter-spacing: 0.03em; line-height: 1;
  color: #fff; text-shadow: 0 3px 0 #10142c;
  font-style: italic;
}
.logo b { color: var(--gold); margin-left: 0.18em; text-shadow: 0 3px 0 #7a4a00; }
.head-right { display: flex; align-items: center; gap: 8px; }
.test-btn {
  font: inherit; font-weight: 900; font-size: 12px; letter-spacing: 0.06em; color: rgba(255, 255, 255, 0.7); cursor: pointer;
  background: rgba(20, 26, 52, 0.5); border: 2px dashed rgba(255, 255, 255, 0.3); border-radius: 12px; padding: 7px 10px; white-space: nowrap;
}
.test-btn.on { color: #10142c; background: var(--gold); border: 2px solid #fff; }
.wallet {
  display: flex; align-items: center; gap: 7px; font-weight: 900; font-size: clamp(16px, 3vmin, 22px);
  background: rgba(20, 26, 52, 0.7); border: 2px solid rgba(255, 255, 255, 0.22); border-radius: 14px; padding: 6px 12px;
  font-variant-numeric: tabular-nums;
}

.notice {
  min-height: 20px; text-align: center; font-weight: 800; font-size: 14px; color: #ffe08a;
  text-shadow: 0 1px 0 #10142c; opacity: 0; transition: opacity 0.2s;
}
.notice.show { opacity: 1; }

.panel {
  flex: 1 1 auto; min-height: 0;
  display: flex; align-items: center; gap: 12px;
  overflow-x: auto; overflow-y: hidden; touch-action: pan-x;
  padding: 4px 2px 8px; scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}
.panel::-webkit-scrollbar { display: none; }
.panel::before, .panel::after { content: ""; flex: 1 0 0px; }

.item {
  flex: 0 0 auto; width: clamp(168px, 22vw, 230px);
  background: var(--card); color: var(--ink);
  border-radius: 18px; border: 3px solid #10142c; box-shadow: 0 6px 0 #10142c;
  padding: 8px; display: flex; flex-direction: column; gap: 5px;
  position: relative;
}
.item.selected { border-color: var(--gold); box-shadow: 0 6px 0 var(--gold-d), 0 0 0 3px rgba(255, 201, 40, 0.35); }
.item canvas { width: 100%; aspect-ratio: 16 / 9; border-radius: 11px; display: block; background: #dbeeff; }
.item.locked canvas { filter: saturate(0.35) brightness(0.8); }
.item h3 { margin: 2px 0 0; font-size: 17px; font-weight: 900; line-height: 1.1; }
.item .kind { font-size: 11px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.06em; color: #6b7392; }
.item p { margin: 0; font-size: 12px; line-height: 1.25; color: #4a5272; min-height: 2.5em; }
.item .meta { font-size: 12px; font-weight: 800; color: #6b7392; }
.lock {
  position: absolute; top: 14px; right: 14px; background: #10142c; color: #fff; font-size: 11px; font-weight: 900;
  padding: 3px 8px; border-radius: 9px; letter-spacing: 0.05em;
}

.pips { display: flex; gap: 3px; margin: 2px 0; }
.pips i { flex: 1; height: 9px; border-radius: 3px; background: #dcdfea; }
.pips i.on { background: var(--blue); }
.item.tune h3 { font-size: 16px; }
.item.tune { width: clamp(138px, 17vw, 200px); }
.tune-title { flex: 0 0 auto; color: #fff; font-weight: 900; text-align: center; max-width: 150px; text-shadow: 0 2px 0 #10142c; }
.tune-title canvas { width: 140px; height: 84px; display: block; }
.tune-title span { display: block; font-size: 15px; }

.btn {
  border-radius: 13px; padding: 9px 14px; font-weight: 900; font-size: 15px; letter-spacing: 0.03em;
  background: #e8ebf5; color: var(--ink); border: 2px solid #10142c; box-shadow: 0 4px 0 #10142c;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  transition: transform 0.05s, box-shadow 0.05s;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 #10142c; }
.btn-buy { background: var(--gold); }
.btn-buy.poor { background: #d9d3bd; color: #7a7460; }
.btn-on { background: #cfe9ff; }
.btn[disabled] { opacity: 0.55; box-shadow: 0 2px 0 #10142c; pointer-events: none; }
.btn-play {
  background: linear-gradient(#6fe46b, #34b53a); color: #0d3a12; font-size: clamp(18px, 3.6vmin, 26px);
  padding: 10px clamp(22px, 5vw, 48px); border-radius: 16px;
}

.menu-foot { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.tabs { display: flex; gap: 6px; background: rgba(20, 26, 52, 0.7); padding: 5px; border-radius: 15px; border: 2px solid rgba(255, 255, 255, 0.2); }
.tab { background: transparent; padding: 8px clamp(10px, 2.4vw, 20px); border-radius: 10px; font-weight: 900; font-size: clamp(13px, 2.4vmin, 16px); color: #cfd5ee; }
.tab.active { background: #fff; color: var(--ink); }

.rotate-tip { display: none; margin: 0; text-align: center; font-size: 12px; font-weight: 700; opacity: 0.85; }
@media (orientation: portrait) and (pointer: coarse) { .rotate-tip { display: block; } }

@media (orientation: portrait) {
  .panel { flex-wrap: wrap; justify-content: center; align-content: center; overflow-x: hidden; overflow-y: auto; touch-action: pan-y; }
  .panel::before, .panel::after { display: none; }
  .item, .item.tune { width: calc(50% - 8px); max-width: 230px; }
  .tune-title { flex-basis: 100%; max-width: none; }
  .tune-title canvas { margin: 0 auto; }
}

/* overlays */
.overlay { align-items: center; justify-content: center; background: rgba(12, 15, 34, 0.6); padding: 16px; }
.card {
  background: var(--card); color: var(--ink); border-radius: 22px; border: 3px solid #10142c; box-shadow: 0 8px 0 #10142c;
  padding: 16px 20px; width: min(340px, 92vw); max-height: 96%; overflow: auto;
  display: flex; flex-direction: column; gap: 9px; text-align: center;
  animation: pop 0.22s ease-out;
}
@keyframes pop { from { transform: scale(0.85); opacity: 0; } }
.card h2 { margin: 0; font-size: 26px; font-weight: 900; }
.record {
  align-self: center; background: var(--red); color: #fff; font-weight: 900; font-size: 13px; letter-spacing: 0.08em;
  padding: 3px 12px; border-radius: 10px; transform: rotate(-3deg);
}
.stats { margin: 2px 0 4px; display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.stats div { background: #eef0f8; border-radius: 12px; padding: 6px 8px; }
.stats dt { font-size: 11px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.05em; color: #6b7392; }
.stats dd { margin: 0; font-size: 19px; font-weight: 900; font-variant-numeric: tabular-nums; }

/* short screens (phones in landscape) */
@media (max-height: 460px) {
  .item p { display: none; }
  .item { padding: 6px; gap: 3px; width: clamp(150px, 21vw, 200px); }
  .item canvas { aspect-ratio: 2 / 1; }
  .item h3 { font-size: 15px; }
  .btn { padding: 6px 12px; font-size: 14px; }
  .btn-play { padding: 8px 30px; font-size: 20px; }
  .notice { min-height: 0; height: 0; overflow: visible; font-size: 13px; }
  #menu { gap: 5px; }
  .card { padding: 10px 16px; gap: 6px; }
  .card h2 { font-size: 21px; }
  .stats dd { font-size: 16px; }
  .tune-title canvas { width: 110px; height: 66px; }
}
@media (max-height: 340px) {
  .item canvas { aspect-ratio: 5 / 2; }
  .item .kind { display: none; }
}
@media (max-width: 520px) {
  .hud-dist { min-width: 0; }
  .hud-fuel { max-width: 130px; }
}

@media (prefers-reduced-motion: reduce) {
  .card, .hud-fuel.low { animation-duration: 0.01s; }
}
