:root {
  --ink: #21343a;
  --cream: #f5f0df;
  --glass: rgba(247, 244, 230, .88);
  --accent: #ed6b4f;
}

* { box-sizing: border-box; }
button:focus { outline: none; }

html, body, #game { width: 100%; height: 100%; margin: 0; overflow: hidden; }

body {
  background: #b9dce1;
  color: var(--ink);
  font-family: "Trebuchet MS", "Avenir Next", system-ui, sans-serif;
  user-select: none;
}

#game { position: relative; }
#game-canvas { display: block; width: 100%; height: 100%; cursor: crosshair; touch-action: none; }

#hud {
  position: absolute;
  left: 22px;
  right: 22px;
  top: 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 28px;
  pointer-events: none;
}

.hud-cluster {
  min-height: 62px;
  padding: 10px 12px 10px 18px;
  display: flex;
  align-items: center;
  gap: clamp(14px, 2vw, 34px);
  background: var(--glass);
  border: 1px solid rgba(255,255,255,.7);
  border-radius: 18px;
  box-shadow: 0 12px 35px rgba(42, 73, 80, .14);
  backdrop-filter: blur(12px);
  pointer-events: auto;
}

.brand { font-size: 13px; font-weight: 900; letter-spacing: .12em; white-space: nowrap; }
.radar-dot { display: inline-block; width: 10px; height: 10px; margin-right: 8px; border: 3px solid #4e9b8c; border-radius: 50%; }
.hud-stat { min-width: 64px; }
.hud-stat small { display: block; color: #708084; font-size: 9px; font-weight: 800; letter-spacing: .13em; }
.hud-stat strong { font-size: 15px; }
.crash-stat.is-nonzero strong { color: #c95048; }
.diversion-stat.is-nonzero strong { color: #d87518; }

#legend {
  position: absolute;
  left: 22px;
  bottom: 18px;
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  width: 178px;
  padding: 11px;
  border-radius: 12px;
  background: rgba(37, 57, 62, .76);
  color: white;
  font-size: 11px;
  backdrop-filter: blur(8px);
}

#legend button { padding: 0; border: 0; background: transparent; color: inherit; font: inherit; cursor: pointer; }
#legend button:hover kbd { background: rgba(255,255,255,.3); }
#legend button.active kbd { background: rgba(64,121,111,.88); border-color: rgba(189,229,221,.58); color: white; }
#legend .control-pair { grid-row: 1; display: flex; align-items: center; justify-content: center; }
#legend .control-group { display: grid; grid-template-rows: auto auto; justify-items: center; gap: 5px; }
#legend .control-label { grid-row: 2; color: #d4e3e3; font-size: 11px; font-weight: 800; letter-spacing: .04em; text-transform: uppercase; }
#legend .pan-control { padding-bottom: 8px; border-bottom: 1px solid rgba(255,255,255,.16); }
#legend .camera-key-groups { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
#legend .arrow-controls { display: grid; grid-template-columns: repeat(3, 24px); grid-template-rows: repeat(2, 22px); gap: 1px; }
#legend .arrow-controls [data-game-key="ArrowUp"] { grid-column: 2; grid-row: 1; }
#legend .arrow-controls [data-game-key="ArrowLeft"] { grid-column: 1; grid-row: 2; }
#legend .arrow-controls [data-game-key="ArrowDown"] { grid-column: 2; grid-row: 2; }
#legend .arrow-controls [data-game-key="ArrowRight"] { grid-column: 3; grid-row: 2; }
#legend .arrow-controls kbd { min-width: 24px; margin-right: 0; padding-inline: 3px; }
#legend .control-hint { color: #bed0d1; font-size: 11px; line-height: 1.35; }
#legend .camera-cycle { display: flex; align-items: center; justify-content: space-between; color: #d4e3e3; font-size: 10px; font-weight: 800; white-space: nowrap; }
#legend .camera-cycle kbd { margin: 0; }
#legend .normal-view { display: block; text-align: left; }
#legend .utility-controls { display: flex; gap: 8px; padding-top: 7px; border-top: 1px solid rgba(255,255,255,.16); }
#legend .utility-controls button { flex: 1; min-width: 0; text-align: left; white-space: nowrap; }

#interface-toggle {
  position: absolute;
  right: 22px;
  bottom: 18px;
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 6px 8px;
  border: 1px solid rgba(255,255,255,.24);
  border-radius: 11px;
  background: rgba(37,57,62,.76);
  color: white;
  font: inherit;
  font-size: 9px;
  font-weight: 800;
  cursor: pointer;
  backdrop-filter: blur(8px);
  transition: opacity .3s ease, background .2s ease;
}
#interface-toggle:hover { background: rgba(37,57,62,.9); }
#interface-toggle.active { background: rgba(64,121,111,.9); border-color: rgba(189,229,221,.58); }
#interface-toggle kbd { margin: 0; }
#interface-toggle.timed-out { opacity: 0; pointer-events: none; }

#speed-button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  min-height: 37px;
  padding: 7px 9px 7px 12px;
  border: 1px solid rgba(255,255,255,.18) !important;
  border-radius: 8px;
  background: rgba(255,255,255,.08) !important;
  color: white;
  font: inherit;
  font-size: 11px;
  font-weight: 800;
  cursor: pointer;
}

#speed-button:hover { background: rgba(255,255,255,.14) !important; }
#speed-button.active { background: rgba(64, 121, 111, .88) !important; border-color: rgba(189,229,221,.58) !important; }
#speed-button kbd {
  display: inline-grid;
  place-items: center;
  width: 29px;
  height: 25px;
  margin: 0;
  padding: 0 0 3px;
  border-color: rgba(255,255,255,.55);
  background: rgba(255,255,255,.18);
  color: #e5fff9;
  font-family: Arial, sans-serif;
  font-size: 18px;
  font-weight: 900;
  line-height: 1;
  text-shadow: 0 1px 1px rgba(0,0,0,.25);
}

.volume-control { display: grid; grid-template-columns: auto 1fr 31px; align-items: center; gap: 7px; color: #d4e3e3; font-size: 10px; font-weight: 800; }
.volume-control output { color: white; text-align: right; font-variant-numeric: tabular-nums; }
.volume-control input { width: 100%; height: 16px; margin: 0; padding: 0; cursor: pointer; accent-color: #75c2b3; }
.volume-control input:focus-visible { outline: 2px solid #e5fff9; outline-offset: 2px; }

#debug-controls {
  position: absolute;
  right: 22px;
  bottom: 61px;
  display: grid;
  grid-template-columns: repeat(2, minmax(126px, 1fr));
  gap: 6px;
  width: 270px;
  padding: 9px;
  border: 1px solid rgba(255,255,255,.24);
  border-radius: 14px;
  background: rgba(37, 57, 62, .76);
  box-shadow: 0 10px 28px rgba(31, 50, 55, .16);
  backdrop-filter: blur(8px);
}
#debug-controls.debug-hidden { display: none; }

#debug-controls .debug-label {
  grid-column: 1 / -1;
  padding: 0 3px 2px;
  color: #a7dad0;
  font-size: 9px;
  font-weight: 900;
  letter-spacing: .18em;
}

#debug-controls button {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  min-height: 31px;
  padding: 6px 7px 6px 9px;
  border: 1px solid rgba(255,255,255,.14);
  border-radius: 8px;
  background: rgba(255,255,255,.09);
  color: white;
  font: inherit;
  font-size: 10px;
  cursor: pointer;
}

#debug-controls button:hover { background: rgba(255,255,255,.18); }
#debug-controls button:disabled { opacity: .38; cursor: not-allowed; background: rgba(255,255,255,.06); }
#debug-controls button kbd { margin: 0; color: #bde5dd; font-size: 8px; }
#debug-controls .debug-edit { grid-column: 1 / -1; justify-content: center; background: rgba(167,218,208,.16); color: #d9fff7; font-weight: 900; letter-spacing: .06em; }

.level-editor {
  position: absolute;
  z-index: 40;
  left: 18px;
  top: 18px;
  bottom: 18px;
  width: min(370px, calc(100% - 36px));
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border: 1px solid rgba(255,255,255,.72);
  border-radius: 18px;
  background: rgba(244,241,226,.94);
  box-shadow: 0 22px 65px rgba(25,48,54,.27);
  backdrop-filter: blur(14px);
  user-select: none;
}
.level-editor.hidden, .editor-properties.hidden { display: none; }
.editor-tab-panel.hidden { display: none; }
.editor-header { padding: 14px; border-bottom: 1px solid rgba(36,58,64,.12); }
.editor-header > div:first-child { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.editor-header small { color: #6d8f8a; font-size: 9px; font-weight: 900; letter-spacing: .16em; }
.editor-header strong { font-size: 17px; }
.editor-zoom { margin-left: auto; color: #4f7e76; font-size: 9px; font-weight: 900; letter-spacing: .04em; white-space: nowrap; }
.editor-zoom-controls { display: flex; gap: 4px; }
.editor-zoom-controls button { display: grid; width: 20px; height: 20px; padding: 0; border-radius: 50%; place-items: center; font-size: 14px; font-weight: 900; line-height: 1; }
.editor-actions { display: grid; grid-template-columns: 1fr 1fr 1fr 1.15fr; gap: 6px; margin-top: 11px; }
.editor-tabs { display: grid; grid-template-columns: repeat(4, 1fr); gap: 5px; margin-top: 8px; }
.editor-tabs button { min-height: 28px; padding: 5px; font-size: 10px; font-weight: 900; text-transform: uppercase; letter-spacing: .06em; }
.editor-tabs button.active { background: #406f67; border-color: #406f67; color: white; }
.level-editor button, .level-editor input, .level-editor select, .level-editor textarea, .editor-properties button, .editor-properties input, .editor-properties select { font: inherit; }
.level-editor button, .editor-properties button { border: 1px solid rgba(37,61,67,.15); border-radius: 8px; background: rgba(37,61,67,.07); color: #294047; cursor: pointer; }
.level-editor button:hover, .level-editor button.active, .editor-properties button:hover { background: rgba(73,133,122,.18); border-color: rgba(60,112,103,.35); }
.level-editor button.primary { background: #2f625a; border-color: #2f625a; color: white; font-weight: 900; }
.editor-actions button { min-height: 34px; padding: 6px; font-size: 10px; }
.editor-status { min-height: 0; margin: 8px 0 0; color: #4f7e76; font-size: 10px; line-height: 1.35; }
.editor-status:empty { display: none; }
.editor-status.error { color: #b24642; }
.editor-scroll { flex: 1; overflow-y: auto; padding: 0 14px 18px; }
.editor-section { padding: 15px 0; border-bottom: 1px solid rgba(36,58,64,.1); }
.editor-section:last-child { border-bottom: 0; }
.editor-section h2 { margin: 0 0 10px; font-size: 12px; letter-spacing: .09em; text-transform: uppercase; }
.editor-section h3 { margin: 12px 0 7px; color: #667b7f; font-size: 9px; letter-spacing: .1em; text-transform: uppercase; }
.editor-field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.editor-field-grid label { display: grid; gap: 4px; color: #607378; font-size: 9px; font-weight: 900; letter-spacing: .03em; }
.editor-field-grid label.wide { grid-column: 1 / -1; }
.editor-field-grid input, .editor-field-grid select, .editor-field-grid textarea {
  width: 100%; min-width: 0; padding: 7px 8px; border: 1px solid rgba(40,65,71,.16); border-radius: 7px; outline: none; background: rgba(255,255,255,.72); color: #243a40; user-select: text;
}
.editor-field-grid textarea { resize: vertical; line-height: 1.35; }
.editor-field-grid input:focus, .editor-field-grid select:focus, .editor-field-grid textarea:focus { border-color: #4e8c81; box-shadow: 0 0 0 2px rgba(78,140,129,.12); }
.editor-tools { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.editor-tools button { min-height: 34px; padding: 7px; font-size: 10px; }
.terrain-tools { display: grid; grid-template-columns: 1fr 1fr; gap: 5px; }
.terrain-tools button { min-height: 38px; padding: 6px 3px; font-size: 9px; }
.terrain-tools kbd { display: block; margin-top: 3px; }
.history-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 8px; }
.history-actions button { min-height: 32px; padding: 5px; font-size: 9px; }
.history-actions button:disabled { cursor: default; opacity: .42; }
.terrain-colors { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.terrain-color { display: flex; align-items: center; gap: 7px; min-height: 32px; padding: 5px 7px !important; text-align: left; font-size: 9px; font-weight: 800; }
.terrain-color::before { width: 18px; height: 18px; flex: 0 0 18px; border: 2px solid rgba(255,255,255,.88); border-radius: 50%; background: var(--swatch); box-shadow: 0 0 0 1px rgba(32,53,59,.2); content: ''; }
.terrain-color.active { border-color: #294047 !important; box-shadow: 0 0 0 1px #294047; }
.editor-help, .editor-empty { margin: 10px 0 0; color: #718286; font-size: 10px; line-height: 1.45; }
.level-editor.sound-editor-open { width: min(560px, calc(100% - 36px)); }
.sound-intro { display: grid; gap: 9px; }
.sound-intro .editor-help { margin: 0; }
.sound-delay { display: flex; align-items: center; gap: 7px; color: #607378; font-size: 10px; font-weight: 900; }
.sound-delay input { width: 62px; padding: 5px 7px; border: 1px solid rgba(40,65,71,.16); border-radius: 7px; background: rgba(255,255,255,.72); color: #243a40; }
.sound-catalog { padding: 3px 0; }
.sound-group { padding: 10px 0 2px; }
.sound-group h3 { margin: 0 0 6px; color: #667b7f; font-size: 9px; letter-spacing: .12em; text-transform: uppercase; }
.sound-row { display: grid; grid-template-columns: minmax(0,1fr) 58px 58px; gap: 6px; margin-bottom: 6px; padding: 5px; border: 1px solid transparent; border-radius: 10px; }
.sound-row.selected { border-color: rgba(64,111,103,.28); background: rgba(73,133,122,.08); }
.sound-row > button { min-height: 38px; padding: 6px; font-size: 9px; font-weight: 900; }
.sound-row .sound-info { display: grid; gap: 2px; justify-items: start; min-width: 0; padding-left: 8px; text-align: left; }
.sound-info strong, .sound-info small { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sound-info small { color: #718286; font-size: 8px; font-weight: 700; }
.sound-selected-line { margin: 0 0 8px; color: #536b70; font-size: 11px; }
.sound-editor-link { display: block; margin: 10px 0 8px; padding: 9px; border-radius: 8px; background: #2f625a; color: white; text-align: center; text-decoration: none; font-size: 10px; font-weight: 900; }
.sound-editor-link:hover { background: #274f49; }
.sound-json { box-sizing: border-box; width: 100%; resize: vertical; padding: 9px; border: 1px solid rgba(40,65,71,.16); border-radius: 8px; outline: none; background: rgba(255,255,255,.78); color: #243a40; font: 9px/1.4 ui-monospace, SFMono-Regular, Consolas, monospace !important; user-select: text; }
.sound-json:focus { border-color: #4e8c81; box-shadow: 0 0 0 2px rgba(78,140,129,.12); }
.sound-exchange-actions { display: grid; grid-template-columns: .8fr 1fr 1.25fr; gap: 6px; margin-top: 7px; }
.sound-exchange-actions button { min-height: 34px; padding: 6px; font-size: 9px; font-weight: 900; }
.editor-properties {
  position: absolute;
  z-index: 40;
  right: 18px;
  top: 18px;
  width: min(310px, calc(100% - 36px));
  max-height: calc(100% - 36px);
  overflow: hidden;
  border: 1px solid rgba(255,255,255,.72);
  border-radius: 18px;
  background: rgba(244,241,226,.94);
  box-shadow: 0 22px 65px rgba(25,48,54,.27);
  backdrop-filter: blur(14px);
  user-select: none;
}
.editor-properties-header { display: flex; align-items: baseline; justify-content: space-between; padding: 14px; border-bottom: 1px solid rgba(36,58,64,.12); }
.editor-properties-header small { color: #6d8f8a; font-size: 9px; font-weight: 900; letter-spacing: .16em; }
.editor-properties-header strong { font-size: 17px; }
.editor-properties-body { max-height: calc(100vh - 105px); overflow-y: auto; padding: 14px; }
.editor-inspector-title { display: flex; align-items: center; justify-content: space-between; margin-bottom: 9px; }
.editor-inspector-title strong { font-size: 13px; }
.editor-inspector-title button { padding: 5px 8px; color: #a44642; font-size: 9px; }
.editor-check { grid-column: 1 / -1; display: flex !important; grid-auto-flow: column; justify-content: start; align-items: center; }
.editor-check input { width: auto; }
.editor-active #hud, .editor-active #legend, .editor-active #interface-toggle, .editor-active #debug-controls, .editor-active #spawn-indicators, .editor-active .panel, .editor-active .toast { display: none; }
#game-canvas.editor-placing { cursor: copy; }
#game-canvas.editor-shifting-terrain { cursor: move; }

#spawn-indicators { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.spawn-indicator {
  position: absolute;
  width: 52px;
  height: 52px;
  transform: translate(-50%, -50%);
  border: 3px solid rgba(255,255,255,.92);
  border-radius: 50%;
  background: rgba(38,61,67,.78);
  box-shadow: 0 0 0 5px rgba(38,61,67,.2), 0 7px 18px rgba(22,42,47,.24);
  color: white;
}
.spawn-indicator::after {
  content: attr(data-seconds);
  position: absolute;
  left: 50%;
  top: calc(100% + 7px);
  transform: translateX(-50%);
  padding: 2px 5px;
  border-radius: 5px;
  background: rgba(38,61,67,.8);
  font-size: 9px;
  font-weight: 900;
}
.spawn-arrow {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 25px;
  height: 4px;
  border-radius: 3px;
  background: currentColor;
  transform-origin: center;
}
.spawn-arrow::after {
  content: '';
  position: absolute;
  right: -1px;
  top: -5px;
  width: 10px;
  height: 10px;
  border-top: 4px solid currentColor;
  border-right: 4px solid currentColor;
  transform: rotate(45deg);
}

kbd {
  display: inline-block;
  min-width: 20px;
  margin-right: 3px;
  padding: 2px 5px;
  border: 1px solid rgba(255,255,255,.34);
  border-radius: 5px;
  background: rgba(255,255,255,.14);
  text-align: center;
  font: inherit;
  font-weight: 900;
}

.panel {
  position: absolute;
  left: 50%;
  transform: translate(-50%, 0);
  background: var(--glass);
  border: 1px solid rgba(255,255,255,.8);
  box-shadow: 0 18px 55px rgba(31, 57, 63, .2);
  backdrop-filter: blur(14px);
  text-align: center;
  transition: opacity .35s ease, transform .35s ease;
}

.panel.hidden { opacity: 0; pointer-events: none; transform: translate(-50%, 16px); }
.level-card { bottom: 42px; width: min(510px, calc(100% - 34px)); padding: 18px 28px 20px; border-radius: 20px; }
.level-card p:first-child, .end-panel > p:first-child, .pause-panel > p:first-child { margin: 0; color: var(--accent); font-size: 10px; font-weight: 900; letter-spacing: .2em; }
.level-card h1 { margin: 4px 0; font-size: 30px; }
.level-card p:last-child { margin: 0; color: #5e7075; font-size: 13px; line-height: 1.45; }

.pause-panel { top: 45%; width: 300px; padding: 24px; border-radius: 20px; }
.pause-panel h2, .end-panel h2 { margin: 5px 0; font-size: 31px; }
.pause-panel span { color: #6a797c; font-size: 13px; }

.end-panel { top: 50%; width: min(420px, calc(100% - 34px)); padding: 28px; border-radius: 24px; transform: translate(-50%, -50%); }
.end-panel.hidden { transform: translate(-50%, calc(-50% + 16px)); }
.end-panel p { color: #607176; line-height: 1.5; }
.end-panel button {
  border: 0;
  border-radius: 12px;
  padding: 13px 18px;
  background: #273e44;
  color: white;
  font: inherit;
  font-weight: 800;
  cursor: pointer;
}
.end-panel button span { margin-left: 10px; color: #a7dad0; }

.toast {
  position: absolute;
  top: 96px;
  left: 50%;
  transform: translateX(-50%);
  padding: 9px 14px;
  border-radius: 10px;
  background: rgba(35, 55, 60, .88);
  color: white;
  font-size: 12px;
  font-weight: 800;
  transition: opacity .2s;
}
.toast.hidden { opacity: 0; }
.toast.camera-view { border: 1px solid rgba(167,218,208,.55); background: rgba(28,49,54,.94); }
.toast.camera-view kbd { margin: 0 3px 0 5px; border-color: rgba(189,229,221,.58); background: rgba(64,121,111,.88); }

#hud, #legend, #debug-controls, #spawn-indicators, .toast { transition: opacity .25s ease; }
.interface-hidden #hud,
.interface-hidden #legend,
.interface-hidden #debug-controls,
.interface-hidden #spawn-indicators,
.interface-hidden .toast { opacity: 0; pointer-events: none; }

@media (max-width: 720px) {
  #hud { left: 10px; right: 10px; top: 10px; gap: 8px; }
  .hud-cluster { min-height: 56px; gap: 10px; padding: 8px 10px; }
  .brand { display: none; }
  .hud-right .hud-stat:nth-of-type(4) { display: none; }
  .hud-right .hud-stat:nth-of-type(3) { display: none; }
  #legend { left: 10px; bottom: 10px; gap: 6px 9px; }
  #legend > .control-hint:first-of-type { display: none; }
  #legend .control-label { display: none; }
  #interface-toggle { right: 10px; bottom: 10px; }
  #debug-controls { right: 10px; bottom: 53px; width: 148px; grid-template-columns: 1fr; }
  #debug-controls .debug-label { grid-column: 1; }
  #debug-controls .debug-edit { grid-column: 1; }
  #debug-controls button kbd { display: none; }
  .level-editor { left: 10px; top: 10px; bottom: 10px; width: min(340px, calc(100% - 20px)); }
  .editor-properties { right: 10px; top: auto; bottom: 10px; width: min(300px, calc(100% - 20px)); max-height: 48%; }
}
