/* ==========================================================================
   ECHO RUNNER — interface styles

   Flat design: solid fills, hairline rules, no gradients, no glows, no blur.
   Palette is a warm paper-and-ink set (bone / terracotta / pine / ochre)
   rather than the usual dark-neon treatment.
   ========================================================================== */

:root {
  /* surfaces */
  --paper:        #efe9dd;
  --surface:      #faf7f0;
  --surface-2:    #e5dfd1;
  --surface-3:    #d9d2c1;
  --rule:         #d3cbb9;
  --rule-strong:  #b9af99;

  /* ink */
  --ink:          #23211d;
  --ink-2:        #5e584d;
  --ink-3:        #8b8478;

  /* accents */
  --clay:         #c2543c;   /* brand / active */
  --clay-soft:    #f4ded6;
  --pine:         #2c6a5b;   /* confident command */
  --pine-soft:    #d8e7e1;
  --ochre:        #bf8c26;   /* low confidence */
  --ochre-soft:   #f5e8ca;
  --crimson:      #9c2f42;   /* error */
  --crimson-soft: #f3d9de;

  --radius:       10px;
  --radius-sm:    7px;
  --font:  "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
  --mono:  "JetBrains Mono", "SF Mono", ui-monospace, Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; min-height: 100%; }

body {
  font-family: var(--font);
  color: var(--ink);
  background: var(--paper);
  -webkit-font-smoothing: antialiased;
}

.app {
  max-width: 1460px;
  margin: 0 auto;
  padding: 28px 26px 40px;
}

/* ------------------------------------------------------------------ header */

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  flex-wrap: wrap;
  margin-bottom: 22px;
  padding-bottom: 20px;
  border-bottom: 2px solid var(--ink);
}

.brand { display: flex; align-items: center; gap: 15px; }

.brand-mark {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 3px;
  width: 48px; height: 48px;
  border-radius: var(--radius-sm);
  background: var(--clay);
}

.wave-bar {
  width: 4px;
  border-radius: 1px;
  background: var(--surface);
  animation: wave 1.1s ease-in-out infinite;
}
.wave-bar:nth-child(1) { height: 12px; animation-delay: 0s; }
.wave-bar:nth-child(2) { height: 24px; animation-delay: .15s; }
.wave-bar:nth-child(3) { height: 18px; animation-delay: .3s; }
.wave-bar:nth-child(4) { height: 9px;  animation-delay: .45s; }

@keyframes wave {
  0%, 100% { transform: scaleY(.5); }
  50%      { transform: scaleY(1.2); }
}

.brand-text h1 {
  margin: 0;
  font-size: 25px;
  letter-spacing: -0.4px;
  font-weight: 800;
  color: var(--ink);
}

.brand-text p {
  margin: 3px 0 0;
  font-size: 13.5px;
  color: var(--ink-2);
}
.brand-text b { color: var(--clay); font-weight: 700; }

.brand-badges { display: flex; gap: 7px; flex-wrap: wrap; }

.badge {
  font-size: 10.5px;
  letter-spacing: .6px;
  text-transform: uppercase;
  font-weight: 700;
  padding: 6px 11px;
  border-radius: var(--radius-sm);
  color: var(--ink-2);
  background: var(--surface-2);
}
.badge-accent { color: var(--surface); background: var(--pine); }

/* ------------------------------------------------------------------ layout */

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 20px;
  align-items: start;
}

.game-column { min-width: 0; }

/* ----------------------------------------------------------------- statbar */

.statbar {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 13px 18px;
  margin-bottom: 12px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--rule);
}

.stat { display: flex; flex-direction: column; gap: 5px; }
.stat-grow { flex: 1; min-width: 90px; }

.stat-label {
  font-size: 10px;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: var(--ink-3);
  font-weight: 700;
}

.stat-value {
  font-family: var(--mono);
  font-size: 18px;
  font-weight: 700;
  line-height: 1;
  color: var(--ink);
}
.stat-sub { font-size: 12px; color: var(--ink-3); }

.stat-lives { display: flex; gap: 5px; align-items: center; height: 18px; }

.life {
  width: 12px; height: 12px;
  border-radius: 3px;
  background: var(--clay);
  transition: background .2s ease, transform .2s ease;
}
.life-lost { background: var(--surface-3); transform: scale(.78); }

.progress-track {
  height: 8px;
  border-radius: 4px;
  background: var(--surface-2);
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  width: 0%;
  background: var(--pine);
  transition: width .12s linear;
}

/* ------------------------------------------------------------------ canvas */

.canvas-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--rule);
  background: #e4d7bd;
}

#game-canvas { display: block; width: 100%; height: 100%; }

/* ---------------------------------------------------------------- overlays */

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  text-align: center;
  background: rgba(239, 233, 221, 0.96);
  opacity: 0;
  pointer-events: none;
  transition: opacity .22s ease;
  z-index: 3;
  /* On a very small phone the start screen can be taller than the square
     canvas — scroll it rather than clipping the START button off. */
  overflow-y: auto;
  overscroll-behavior: contain;
}
.overlay.is-visible { opacity: 1; pointer-events: auto; }

.overlay-inner {
  max-width: 560px;
  animation: rise .3s ease-out;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: none; }
}

.overlay-eyebrow {
  display: inline-block;
  font-size: 10.5px;
  letter-spacing: 1.4px;
  text-transform: uppercase;
  font-weight: 700;
  color: var(--surface);
  background: var(--clay);
  padding: 5px 11px;
  border-radius: var(--radius-sm);
  margin-bottom: 15px;
}
.overlay-eyebrow-danger { background: var(--crimson); }
.overlay-eyebrow-good   { background: var(--pine); }

.overlay-title {
  margin: 0 0 12px;
  font-size: 46px;
  font-weight: 800;
  letter-spacing: -1.4px;
  color: var(--ink);
}

.overlay-sub {
  margin: 0 auto 20px;
  max-width: 450px;
  font-size: 14.5px;
  line-height: 1.6;
  color: var(--ink-2);
}

.cmd-legend {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
  margin-bottom: 20px;
}

.cmd-chip {
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 11px 20px;
  border-radius: var(--radius-sm);
  background: var(--surface);
  border: 1px solid var(--rule);
}
.cmd-key {
  font-family: var(--mono);
  font-size: 15px;
  font-weight: 700;
  color: var(--clay);
}
.cmd-desc { font-size: 11px; color: var(--ink-3); letter-spacing: .3px; }

.overlay-note {
  font-size: 12.5px;
  line-height: 1.65;
  color: var(--ink-3);
  max-width: 430px;
  margin: 0 auto 22px;
}

.overlay-fineprint {
  margin: 18px auto 0;
  max-width: 420px;
  font-size: 11.5px;
  line-height: 1.6;
  color: var(--ink-3);
}
.overlay-fineprint b { color: var(--ink-2); }

.result-grid {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin: 4px 0 24px;
  flex-wrap: wrap;
}
.result {
  min-width: 106px;
  padding: 13px 16px;
  border-radius: var(--radius-sm);
  background: var(--surface);
  border: 1px solid var(--rule);
}
.result span {
  display: block;
  font-size: 10px;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: var(--ink-3);
  font-weight: 700;
  margin-bottom: 6px;
}
.result b { font-family: var(--mono); font-size: 20px; color: var(--ink); }

.error-icon {
  width: 50px; height: 50px;
  margin: 0 auto 16px;
  border-radius: var(--radius-sm);
  display: grid;
  place-items: center;
  font-size: 28px;
  font-weight: 800;
  color: var(--surface);
  background: var(--crimson);
}

.error-hint {
  margin: 0 auto 24px;
  max-width: 460px;
  padding: 13px 16px;
  font-size: 12.5px;
  line-height: 1.65;
  text-align: left;
  color: var(--ink-2);
  border-radius: var(--radius-sm);
  background: var(--surface);
  border-left: 3px solid var(--crimson);
}

/* ------------------------------------------------------------------ buttons */

.btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-family: inherit;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 1.1px;
  padding: 14px 32px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  cursor: pointer;
  transition: background .15s ease, color .15s ease, border-color .15s ease;
}
.btn:disabled { cursor: default; opacity: .55; }

.btn-primary {
  color: var(--surface);
  background: var(--clay);
}
.btn-primary:hover:not(:disabled) { background: #a94530; }

.btn-ghost {
  color: var(--ink-2);
  background: var(--surface);
  border-color: var(--rule);
  padding: 9px 16px;
  letter-spacing: .4px;
  font-size: 12px;
}
.btn-ghost:hover:not(:disabled) { color: var(--ink); border-color: var(--rule-strong); }
.btn-ghost kbd {
  font-family: var(--mono);
  font-size: 10px;
  padding: 2px 6px;
  margin-left: 8px;
  border-radius: 4px;
  background: var(--surface-2);
  color: var(--ink-3);
}

.btn-spinner {
  display: none;
  position: absolute;
  right: 15px; top: 50%;
  width: 13px; height: 13px;
  margin-top: -7px;
  border-radius: 50%;
  border: 2px solid rgba(250, 247, 240, .35);
  border-top-color: var(--surface);
  animation: spin .7s linear infinite;
}
.btn.is-loading { padding-right: 44px; }
.btn.is-loading .btn-spinner { display: block; }

@keyframes spin { to { transform: rotate(360deg); } }

/* -------------------------------------------------------------- game footer */

.game-footer {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-top: 12px;
  flex-wrap: wrap;
}
.footer-note { margin: 0; font-size: 11.5px; color: var(--ink-3); }

/* ---------------------------------------------------------------- AI panel */

.ai-panel {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 18px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--rule);
  border-top: 3px solid var(--clay);
  position: sticky;
  top: 20px;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--rule);
}
.panel-head h2 {
  margin: 0;
  font-size: 12px;
  letter-spacing: 1.2px;
  font-weight: 800;
  color: var(--ink);
}

.mic-status {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 10px;
  letter-spacing: .8px;
  font-weight: 700;
  padding: 5px 10px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--ink-3);
}
.mic-dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; }

.mic-status[data-state="active"]  { color: var(--pine);    background: var(--pine-soft); }
.mic-status[data-state="loading"] { color: var(--ochre);   background: var(--ochre-soft); }
.mic-status[data-state="ready"]   { color: var(--ink-2);   background: var(--surface-2); }
.mic-status[data-state="error"]   { color: var(--crimson); background: var(--crimson-soft); }

.mic-status[data-state="active"] .mic-dot { animation: blink 1.5s steps(1, end) infinite; }
@keyframes blink { 0%, 60% { opacity: 1; } 61%, 100% { opacity: .25; } }

/* mic level meter */
.mic-meter { display: flex; align-items: center; gap: 10px; }
.meter-label {
  font-size: 9.5px;
  letter-spacing: 1px;
  color: var(--ink-3);
  font-weight: 700;
}
.meter-track {
  flex: 1;
  height: 6px;
  border-radius: 3px;
  background: var(--surface-2);
  overflow: hidden;
}
.meter-fill {
  height: 100%;
  width: 0%;
  background: var(--pine);
  transition: width .07s linear;
}

/* detection card */
.detect-card {
  padding: 16px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  border-left: 3px solid var(--rule-strong);
  transition: background .2s ease, border-color .2s ease;
}
.detect-card[data-state="good"]  { background: var(--pine-soft);    border-left-color: var(--pine); }
.detect-card[data-state="warn"]  { background: var(--ochre-soft);   border-left-color: var(--ochre); }
.detect-card[data-state="error"] { background: var(--crimson-soft); border-left-color: var(--crimson); }

.detect-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 6px;
}
.detect-label {
  font-size: 10px;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: var(--ink-3);
  font-weight: 700;
  white-space: nowrap;
}
.detect-value {
  font-family: var(--mono);
  font-size: 21px;
  font-weight: 700;
  letter-spacing: -0.3px;
  text-align: right;
  color: var(--ink);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.detect-value-sm { font-size: 16px; }

.detect-card[data-state="good"]  .detect-value { color: var(--pine); }
.detect-card[data-state="warn"]  .detect-value { color: #966d16; }
.detect-card[data-state="error"] .detect-value { color: var(--crimson); }
.detect-card[data-state="idle"]  .detect-value { color: var(--ink-2); }

.conf-track {
  position: relative;
  height: 9px;
  margin: 11px 0 12px;
  border-radius: 4px;
  background: var(--surface-3);
  overflow: hidden;
}
.conf-fill {
  height: 100%;
  width: 0%;
  background: var(--ink-3);
  transition: width .1s linear, background .2s ease;
}
.detect-card[data-state="good"]  .conf-fill { background: var(--pine); }
.detect-card[data-state="warn"]  .conf-fill { background: var(--ochre); }
.detect-card[data-state="error"] .conf-fill { background: var(--crimson); }

.conf-threshold {
  position: absolute;
  top: 0; bottom: 0;
  width: 2px;
  background: var(--ink);
  z-index: 2;
}

.detect-action {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  padding-top: 11px;
  border-top: 1px solid var(--rule);
}
.action-value {
  font-size: 13px;
  font-weight: 700;
  text-align: right;
  color: var(--ink);
}

/* movement state card */
.state-card {
  padding: 13px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--rule);
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.state-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.state-label {
  font-size: 10px;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: var(--ink-3);
  font-weight: 700;
}
.state-pill {
  font-family: var(--mono);
  font-size: 11.5px;
  font-weight: 700;
  letter-spacing: .3px;
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--ink-2);
}
.state-pill[data-state="MOVE_LEFT"],
.state-pill[data-state="MOVE_RIGHT"] { color: var(--surface); background: var(--clay); }
.state-pill[data-state="STOP"]       { color: var(--ink-2);   background: var(--surface-3); }

.state-pill.pulse { animation: pop .26s ease; }
@keyframes pop {
  0%   { transform: scale(1); }
  45%  { transform: scale(1.07); }
  100% { transform: scale(1); }
}

.panel-note {
  font-size: 11.5px;
  line-height: 1.6;
  color: var(--ink-3);
  padding: 11px 13px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}
.panel-note b { color: var(--ink-2); }

/* ------------------------------------------------------------------ footer */

.app-footer {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-top: 26px;
  padding-top: 16px;
  border-top: 1px solid var(--rule);
  font-size: 11.5px;
  color: var(--ink-3);
}
.app-footer code {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--ink-2);
  word-break: break-all;
}

/* -------------------------------------------------------------- responsive */

/* Sidebar drops under the game once there is no room beside it. */
@media (max-width: 1120px) {
  .layout { grid-template-columns: 1fr; }
  .ai-panel { position: static; }
}

/* Tablets: a slightly taller playfield keeps the runner a usable size. */
@media (max-width: 900px) {
  .canvas-wrap { aspect-ratio: 3 / 2; }
  .app { padding: 22px 18px 32px; }
  .overlay-title { font-size: 38px; }
}

/* ---------------------------------------------------------------- phones */
@media (max-width: 640px) {
  .app { padding: 14px 12px 26px; }

  /* A near-square canvas. The game reads the element's real aspect ratio and
     zooms the world to match, so this shows less of the level but draws the
     runner roughly twice the size it would be in a letterboxed 16:9 strip. */
  .canvas-wrap { aspect-ratio: 1 / 1; }

  .app-header {
    gap: 12px;
    margin-bottom: 14px;
    padding-bottom: 14px;
  }
  .brand { gap: 11px; }
  .brand-mark { width: 38px; height: 38px; gap: 2px; }
  .wave-bar { width: 3px; }
  .brand-text h1 { font-size: 18px; }
  .brand-text p { font-size: 12px; line-height: 1.45; }
  .brand-badges { display: none; }          /* decorative only */

  /* Stats wrap onto two tidy rows instead of overflowing. */
  .statbar {
    flex-wrap: wrap;
    gap: 12px 18px;
    padding: 11px 13px;
    margin-bottom: 10px;
  }
  .stat-value { font-size: 16px; }
  .stat-grow { flex-basis: 100%; order: 5; min-width: 0; }
  .stat-optional { display: none; }         /* timer is nice-to-have */

  .ai-panel { padding: 15px; gap: 12px; }
  .panel-head { padding-bottom: 11px; }
  .detect-card { padding: 14px; }
  .detect-value { font-size: 19px; }

  .overlay { padding: 18px 14px; }
  .overlay-title { font-size: 27px; letter-spacing: -.6px; margin-bottom: 8px; }
  .overlay-sub { font-size: 13px; margin-bottom: 14px; }
  .overlay-eyebrow { margin-bottom: 10px; font-size: 9.5px; }
  .overlay-note { display: none; }          /* rules already shown in chips */
  .overlay-fineprint { font-size: 10.5px; margin-top: 12px; }

  .cmd-legend { gap: 6px; margin-bottom: 14px; }
  .cmd-chip { padding: 8px 13px; flex: 1 1 0; min-width: 0; }
  .cmd-key { font-size: 13px; }
  .cmd-desc { font-size: 10px; }

  .result { min-width: 88px; padding: 10px 12px; }
  .result b { font-size: 17px; }
  .result-grid { gap: 8px; margin-bottom: 18px; }

  .btn { width: 100%; }
  .game-footer { margin-top: 10px; }
  .btn-ghost { width: 100%; justify-content: center; }
  .app-footer { margin-top: 18px; font-size: 10.5px; gap: 6px; }
}

/* Very short landscape phones: give the canvas the full width, no letterbox. */
@media (max-height: 460px) and (orientation: landscape) {
  .canvas-wrap { aspect-ratio: 2 / 1; }
  .app-header, .app-footer { display: none; }
  .app { padding: 10px; }
}

/* --------------------------------------------------------------- touch */

/* Comfortable hit targets and no 300ms tap delay / double-tap zoom. */
.btn, .mic-status, .state-pill { touch-action: manipulation; }

@media (pointer: coarse) {
  .btn { min-height: 48px; }
  .btn-ghost { min-height: 44px; }

  /* The keyboard hint is meaningless without a keyboard. */
  .btn-ghost kbd { display: none; }
}

/* Stop the canvas from being selected/dragged during play. */
#game-canvas {
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
  }
}
