/* ============================================================
   Kudzu — responsive, accessible styles
   Theming via custom properties; light/dark + forced overrides.
   ============================================================ */

:root {
  --bg: #0b1020;
  --surface: #141a30;
  --surface-2: #1c2444;
  --text: #e8ecff;
  --muted: #9aa3c7;
  --border: #2a3358;
  --accent: #34d399;          /* kudzu */
  --accent-2: #10b981;
  --food: #f87171;            /* food */
  --focus: #ffd166;
  --danger: #ef4444;
  --shadow: 0 10px 30px rgb(0 0 0 / 0.45);
  --radius: 14px;
  --speed-hud: 1fr;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #eef1fb;
    --surface: #ffffff;
    --surface-2: #e6eaf8;
    --text: #17203d;
    --muted: #5a6486;
    --border: #c9d1ea;
    --shadow: 0 10px 30px rgb(23 32 61 / 0.12);
  }
}

/* Forced high contrast (user setting or OS preference) */
:root.hc,
:root.hc-light {
  --bg: #000;
  --surface: #000;
  --surface-2: #111;
  --text: #fff;
  --muted: #fff;
  --border: #fff;
  --accent: #00ff85;
  --food: #ff5252;
  --focus: #ffff00;
  --shadow: none;
}
:root.hc {
  color-scheme: dark;
}
:root.hc-light {
  --bg: #fff;
  --surface: #fff;
  --surface-2: #f2f2f2;
  --text: #000;
  --border: #000;
  --accent: #007a33;
  --food: #c62828;
  color-scheme: light;
}

/* Colorblind-friendly palette */
:root.cb {
  --accent: #4ea8ff;
  --accent-2: #2f7fd6;
  --food: #ffa63d;
}

* { box-sizing: border-box; }

/* The hidden attribute must always win over component display values. */
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  margin: -1px; padding: 0; border: 0;
  clip-path: inset(50%);
  overflow: hidden;
  white-space: nowrap;
}

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  background: var(--accent);
  color: #04120a;
  padding: 0.6rem 1rem;
  border-radius: 0 0 var(--radius) 0;
  z-index: 100;
}
.skip-link:focus { left: 0; }

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
  border-radius: 4px;
}

/* ---------- Header ---------- */

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
  padding: 0.75rem clamp(0.75rem, 3vw, 2rem);
}

.app-title {
  font-size: clamp(1.25rem, 2.5vw, 1.75rem);
  margin: 0;
  letter-spacing: 0.02em;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

/* ---------- Controls ---------- */

.btn, .icon-btn, .control, .tab {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.45rem 0.9rem;
  cursor: pointer;
  transition: transform 120ms ease, background-color 120ms ease;
}
.btn:hover, .tab:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn-primary {
  background: var(--accent);
  border-color: transparent;
  color: #04120a;
  font-weight: 700;
}
.btn-primary:hover { filter: brightness(1.08); background: var(--accent); }
.btn-danger { color: var(--danger); border-color: var(--danger); }
.icon-btn {
  padding: 0.45rem 0.6rem;
  line-height: 1;
}
.icon-btn[aria-pressed="false"] .sound-icon { opacity: 0.45; }
.control { cursor: default; }

/* ---------- HUD ---------- */

.hud {
  display: grid;
  grid-template-columns: repeat(4, minmax(4.5rem, 1fr));
  gap: 0.5rem;
  width: min(92vw, 560px);
  margin: 0 auto 0.6rem;
}
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.35rem 0.6rem;
  text-align: center;
}
.stat dt {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}
.stat dd {
  margin: 0;
  font-size: clamp(1rem, 2.4vw, 1.3rem);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

/* ---------- Board ---------- */

.board-wrap {
  position: relative;
  width: min(92vw, 62dvh, 560px);
  aspect-ratio: 1 / 1;
  margin: 0 auto;
  container-type: inline-size;
}

#board {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: var(--radius);
  border: 2px solid var(--border);
  background: var(--surface);
  touch-action: none; /* we handle swipes ourselves */
}

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: color-mix(in srgb, var(--bg) 55%, transparent);
  backdrop-filter: blur(3px);
  border-radius: var(--radius);
  opacity: 1;
  transition: opacity 180ms ease;
}
.overlay[hidden] { display: none; }

.overlay-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem 1.5rem;
  text-align: center;
  max-width: min(90%, 26rem);
}
.overlay-title { margin: 0 0 0.25rem; font-size: 1.4rem; }
.overlay-text { margin: 0 0 0.9rem; color: var(--muted); }
.overlay-score-line {
  margin: 0 0 0.9rem;
  font-size: 1.15rem;
  font-weight: 800;
}
.overlay-actions {
  display: flex;
  gap: 0.5rem;
  justify-content: center;
  flex-wrap: wrap;
}
.submit-row {
  display: flex;
  gap: 0.5rem;
  justify-content: center;
  flex-wrap: wrap;
  margin-bottom: 0.9rem;
}
.submit-row .control { max-width: 12rem; }
.mode-note { margin: 0.75rem 0 0; }
.badge {
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.1rem 0.7rem;
  font-size: 0.8rem;
  color: var(--muted);
}

/* ---------- Touch controls ---------- */

.touch-controls {
  display: none;
  grid-template-columns: repeat(3, 4rem);
  grid-template-rows: repeat(2, 4rem);
  gap: 0.4rem;
  justify-content: center;
  margin: 0.9rem auto 0;
}
.dpad {
  font-size: 1.2rem;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
}
.dpad:active { background: var(--surface-2); }
.dpad-up { grid-column: 2; grid-row: 1; }
.dpad-left { grid-column: 1; grid-row: 2; }
.dpad-down { grid-column: 2; grid-row: 2; }
.dpad-right { grid-column: 3; grid-row: 2; }

@media (pointer: coarse) {
  .touch-controls { display: grid; }
  .keyboard-hint { display: none; }
}

.keyboard-hint {
  text-align: center;
  color: var(--muted);
  font-size: 0.85rem;
  margin: 0.8rem auto 0;
}

/* ---------- Dialogs ---------- */

.dialog {
  width: min(92vw, 30rem);
  max-height: 85dvh;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  padding: 1.25rem;
  overscroll-behavior: contain;
}
.dialog::backdrop {
  background: rgb(0 0 0 / 0.55);
}
.dialog h2 { margin-top: 0; }
.dialog-form fieldset {
  border: 1px solid var(--border);
  border-radius: 10px;
  margin: 0 0 0.9rem;
}
.dialog-form legend {
  padding: 0 0.4rem;
  color: var(--muted);
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.field-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.35rem 0.5rem;
}
.field-row label { flex: 1; }
.switch { width: 1.2rem; height: 1.2rem; accent-color: var(--accent); }
.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 0.75rem;
}
.hint { color: var(--muted); font-size: 0.82rem; }

/* Avatar settings row */
.avatar-row { display: flex; gap: 0.9rem; align-items: center; padding: 0.4rem 0.5rem; }
.avatar-preview {
  width: 56px; height: 56px;
  border-radius: 50%;
  border: 2px solid var(--border);
  object-fit: cover;
}
.avatar-actions .hint { margin: 0.35rem 0 0; }

/* Cropper */
.crop-stage { display: grid; place-items: center; margin: 0.5rem 0; }
#crop-canvas {
  border-radius: var(--radius);
  border: 1px solid var(--border);
  max-width: 100%;
  touch-action: none;
  cursor: grab;
}
#crop-canvas:active { cursor: grabbing; }

/* Leaderboard */
.tabs { display: flex; gap: 0.4rem; margin-bottom: 0.6rem; }
.tab[aria-selected="true"] {
  background: var(--accent);
  color: #04120a;
  font-weight: 700;
  border-color: transparent;
}
.lb-table {
  width: 100%;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
}
.lb-table th, .lb-table td {
  text-align: left;
  padding: 0.4rem 0.5rem;
  border-bottom: 1px solid var(--border);
}
.lb-table th {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.lb-table td:nth-child(3), .lb-table th:nth-child(3) { text-align: right; }
.lb-empty { text-align: center; color: var(--muted); padding: 1.2rem 0; }
tr.you { background: var(--surface-2); font-weight: 700; }

/* Career Stats */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.6rem;
  margin-bottom: 1rem;
}
.stats-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.6rem 0.75rem;
  text-align: center;
}
.stats-label {
  display: block;
  font-size: 0.75rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.stats-value {
  display: block;
  font-size: 1.3rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  margin-top: 0.2rem;
}
.stats-subtitle {
  font-size: 0.95rem;
  margin: 0.75rem 0 0.4rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.stats-table th[scope="row"] {
  font-weight: 600;
  color: var(--text);
}

/* Debug Telemetry HUD */
.debug-hud {
  position: fixed;
  bottom: 0.5rem;
  left: 0.5rem;
  background: rgba(11, 16, 32, 0.85);
  color: #34d399;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.25rem 0.5rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.72rem;
  z-index: 1000;
  pointer-events: none;
  backdrop-filter: blur(4px);
}

/* RTL support */
[dir="rtl"] .skip-link {
  left: auto;
  right: -9999px;
  border-radius: 0 0 0 var(--radius);
}
[dir="rtl"] .skip-link:focus {
  left: auto;
  right: 0;
}
[dir="rtl"] .debug-hud {
  left: auto;
  right: 0.5rem;
}
[dir="rtl"] .lb-table th, [dir="rtl"] .lb-table td {
  text-align: right;
}
[dir="rtl"] .lb-table td:nth-child(3), [dir="rtl"] .lb-table th:nth-child(3) {
  text-align: left;
}

/* ---------- Footer ---------- */

.app-footer {
  margin-top: auto;
  padding: 0.9rem;
  text-align: center;
  color: var(--muted);
  font-size: 0.8rem;
}
.release-info {
  margin-top: 0.35rem;
  font-size: 0.72rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  opacity: 0.75;
  letter-spacing: 0.02em;
}

/* ---------- Motion preferences ---------- */

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

/* ---------- Small screens / large screens ---------- */

@media (max-width: 480px) {
  .hud { grid-template-columns: repeat(2, 1fr); width: min(94vw, 30rem); }
  .app-header { justify-content: center; }
}

@media (min-width: 900px) {
  main { display: grid; grid-template-columns: minmax(0, 1fr); justify-items: center; }
  .board-wrap { width: min(60vw, 70dvh, 640px); }
}
