/* =========================================================
   Design tokens (light theme)
   ========================================================= */
:root {
  --bg: #f4f3ef;
  --surface: #ffffff;
  --surface-2: #f7f6f2;
  --surface-3: #eceae4;
  --border: #e3e1da;
  --border-strong: #cfccc3;

  --text: #1c1b18;
  --text-2: #5c5a53;
  --text-3: #8b887e;

  --accent: #2f5bea;
  --accent-hover: #2449c4;
  --accent-soft: #e8eefd;
  --red: #e03131;
  --red-soft: #fdecec;
  --green: #2b8a3e;
  --green-soft: #e9f6ec;
  --amber: #f59f00;
  --amber-text: #9a6200;
  --amber-soft: #fff4dc;

  --radius-sm: 6px;
  --radius: 10px;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;

  --topbar-h: 56px;
  --shadow: 0 1px 2px rgba(28, 27, 24, 0.06), 0 4px 16px rgba(28, 27, 24, 0.06);

  --font: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

body {
  user-select: none;
}

button,
input,
select {
  font: inherit;
  color: inherit;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.hidden {
  display: none !important;
}

.hidden-view {
  visibility: hidden;
  pointer-events: none;
}

/* Mode and debug visibility */
body.mode-sim .game-only,
body.mode-game .sim-only,
body:not(.debug-on) .debug-only {
  display: none !important;
}

body.mode-game {
  cursor: none;
}

body.mode-game #topbar,
body.mode-game #topbar * {
  cursor: default;
}

/* =========================================================
   Buttons
   ========================================================= */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  height: 34px;
  padding: 0 var(--space-3);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: none;
  font-weight: 500;
  font-size: 13px;
  white-space: nowrap;
  cursor: pointer;
  transition: background-color 0.12s ease, border-color 0.12s ease, color 0.12s ease;
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent);
  color: #ffffff;
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-secondary {
  background: var(--surface);
  border-color: var(--border-strong);
}

.btn-secondary:hover:not(:disabled) {
  background: var(--surface-2);
}

.btn-ghost {
  color: var(--text-2);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--surface-3);
  color: var(--text);
}

.btn-ghost[aria-pressed='true'] {
  background: var(--surface-3);
  color: var(--text);
}

.btn-link {
  height: auto;
  padding: 0;
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.btn-sm {
  height: 28px;
  padding: 0 var(--space-2);
  font-size: 12px;
}

.btn-square {
  width: 34px;
  padding: 0;
}

.rec-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--red);
}

#btn-record.recording {
  background: var(--red-soft);
  color: var(--red);
}

#btn-record.recording .rec-dot {
  animation: blink 1s steps(2, start) infinite;
}

@keyframes blink {
  to {
    visibility: hidden;
  }
}

/* =========================================================
   Top bar
   ========================================================= */
#topbar {
  position: fixed;
  inset: 0 0 auto 0;
  height: var(--topbar-h);
  z-index: 40;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: var(--space-4);
  padding: 0 var(--space-4);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar-left,
.topbar-right {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.topbar-left {
  gap: var(--space-4);
}

.wordmark {
  font-weight: 700;
  font-size: 15px;
  letter-spacing: -0.01em;
}

.tabs {
  display: flex;
  padding: 3px;
  gap: 2px;
  background: var(--surface-3);
  border-radius: 8px;
}

.tab {
  height: 28px;
  padding: 0 var(--space-3);
  border: 0;
  border-radius: var(--radius-sm);
  background: none;
  color: var(--text-2);
  font-weight: 500;
  font-size: 13px;
  cursor: pointer;
}

.tab:hover {
  color: var(--text);
}

.tab.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(28, 27, 24, 0.12);
}

.topbar-center {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-3);
  min-width: 0;
}

.status {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
  color: var(--text-2);
}

.status::before {
  content: '';
  display: inline-block;
  width: 7px;
  height: 7px;
  margin-right: var(--space-2);
  border-radius: 50%;
  background: var(--text-3);
  vertical-align: 1px;
}

.status[data-tone='ok']::before {
  background: var(--green);
}

.status[data-tone='error']::before,
.status[data-tone='rec']::before {
  background: var(--red);
}

.status[data-tone='warn']::before {
  background: var(--amber);
}

/* Score (play mode) */
.score-line {
  display: flex;
  align-items: baseline;
  gap: var(--space-5);
  margin-right: var(--space-3);
}

.score-item {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
}

.score-num {
  font-weight: 700;
  font-size: 20px;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
}

.score-label {
  font-size: 12px;
  color: var(--text-3);
}

/* =========================================================
   Calibrate view
   ========================================================= */
#simulator-view {
  position: fixed;
  top: var(--topbar-h);
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 15;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: var(--space-4);
  padding: var(--space-4);
}

body.debug-on #simulator-view {
  grid-template-columns: minmax(0, 1fr) 380px;
}

body.mode-game #simulator-view {
  display: none;
}

.stage {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.stage-toolbar {
  display: flex;
  align-items: center;
  gap: var(--space-4);
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--border);
}

.spacer {
  flex: 1;
}

.stage-canvas {
  position: relative;
  flex: 1;
  min-height: 0;
}

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

/* Coach bar (step-by-step calibration) */
.coach {
  position: relative;
  display: flex;
  align-items: center;
  gap: var(--space-4);
  padding: var(--space-3) var(--space-4);
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.coach-progress {
  position: absolute;
  top: -1px;
  left: 0;
  height: 2px;
  background: var(--accent);
  transition: width 0.06s linear;
}

.coach-main {
  flex: 1;
  min-width: 0;
}

.coach-meta {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  margin-bottom: 2px;
  font-size: 12px;
  color: var(--text-3);
}

.coach-steps {
  display: flex;
  gap: 3px;
}

.coach-steps i {
  width: 14px;
  height: 3px;
  border-radius: 2px;
  background: var(--surface-3);
}

.coach-steps i.done {
  background: var(--accent);
}

.coach-steps i.active {
  background: var(--text);
}

.hand-state {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.hand-state::before {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--green);
}

.hand-state.no-hand {
  color: var(--amber-text);
}

.hand-state.no-hand::before {
  background: var(--amber);
}

.coach-title {
  font-weight: 600;
  font-size: 15px;
}

.coach-text {
  color: var(--text-2);
  font-size: 13px;
}

.coach-text strong {
  color: var(--text);
  font-weight: 600;
}

.coach-result {
  margin-top: var(--space-1);
  font-size: 12px;
  color: var(--green);
}

.coach-result.warn {
  color: var(--amber-text);
}

.coach-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex-shrink: 0;
}

kbd {
  display: inline-block;
  min-width: 20px;
  padding: 1px 5px;
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 4px;
  background: var(--surface);
  font-family: var(--font);
  font-size: 11px;
  font-weight: 500;
  line-height: 16px;
  text-align: center;
  color: var(--text-2);
}

.btn-primary kbd {
  border-color: rgba(255, 255, 255, 0.35);
  background: rgba(255, 255, 255, 0.12);
  color: #ffffff;
}

/* Sidebar */
.sidebar {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  min-height: 0;
  overflow-y: auto;
}

.panel {
  flex-shrink: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  height: 40px;
  padding: 0 var(--space-3);
  border-bottom: 1px solid var(--border);
}

.panel-head h2 {
  font-size: 13px;
  font-weight: 600;
}

.panel-note {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-3);
}

.key-swatch {
  width: 8px;
  height: 8px;
  margin-left: 4px;
  border-radius: 50%;
  background: var(--c);
}

#sim-hand-canvas,
#sim-scope-canvas {
  display: block;
  width: 100%;
  height: auto;
}

/* Tuning */
.panel-body {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  padding: var(--space-3);
}

.panel-body.collapsed {
  display: none;
}

.field {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.field-label {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  color: var(--text);
}

.field-label output {
  color: var(--text-2);
  font-variant-numeric: tabular-nums;
}

.field-hint {
  font-size: 12px;
  color: var(--text-3);
}

select {
  height: 32px;
  padding: 0 var(--space-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 13px;
  cursor: pointer;
}

input[type='range'] {
  width: 100%;
  accent-color: var(--accent);
  cursor: pointer;
}

.check {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
}

.check input {
  width: 15px;
  height: 15px;
  accent-color: var(--accent);
  cursor: pointer;
}

/* Report */
.report {
  padding: var(--space-3);
  font-size: 13px;
  color: var(--text-2);
}

.report-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-3) var(--space-4);
}

.report-metric dt {
  font-size: 12px;
  color: var(--text-3);
}

.report-metric dd {
  font-weight: 500;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.tag {
  display: inline-block;
  margin-left: 4px;
  padding: 0 6px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 500;
}

.tag-good {
  background: var(--green-soft);
  color: var(--green);
}

.tag-warn {
  background: var(--amber-soft);
  color: var(--amber-text);
}

.report-foot {
  margin-top: var(--space-3);
  padding-top: var(--space-2);
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-3);
}

code {
  font-family: ui-monospace, 'SF Mono', Menlo, monospace;
  font-size: 12px;
}

/* =========================================================
   Play view
   ========================================================= */
#game-container {
  position: fixed;
  top: var(--topbar-h);
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1;
}

#pip-container {
  position: fixed;
  right: var(--space-4);
  bottom: var(--space-4);
  width: 220px;
  z-index: 25;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface-3);
  border: 3px solid var(--surface);
  box-shadow: var(--shadow);
}

body.camera-off #pip-container {
  display: none !important;
}

#webcam-video {
  display: none;
}

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

/* Reticle */
#reticle {
  position: fixed;
  top: 0;
  left: 0;
  width: 40px;
  height: 40px;
  margin: -20px 0 0 -20px;
  pointer-events: none;
  z-index: 30;
  will-change: transform;
}

.reticle-ring {
  position: absolute;
  inset: 4px;
  border: 2px solid var(--text);
  border-radius: 50%;
  box-shadow: 0 0 0 1.5px rgba(255, 255, 255, 0.85), inset 0 0 0 1.5px rgba(255, 255, 255, 0.85);
  transition: transform 0.08s ease, border-color 0.08s ease;
}

.reticle-dot {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 6px;
  height: 6px;
  margin: -3px 0 0 -3px;
  border-radius: 50%;
  background: var(--red);
  box-shadow: 0 0 0 1.5px #ffffff;
}

#reticle.target-lock .reticle-ring {
  border-color: var(--red);
  transform: scale(0.82);
}

#reticle.recoil .reticle-ring {
  transform: scale(1.35);
}

/* Score popups */
#popups-container {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 28;
}

.score-popup {
  position: absolute;
  transform: translate(-50%, -50%);
  font-weight: 700;
  font-size: 20px;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  text-shadow: 0 0 3px #ffffff, 0 0 3px #ffffff;
  animation: float-up 0.75s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

.score-popup.headshot {
  font-size: 24px;
  color: var(--red);
}

@keyframes float-up {
  0% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(0.8);
  }
  25% {
    transform: translate(-50%, -80%) scale(1.08);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -140%) scale(1);
  }
}

/* =========================================================
   Shortcuts dialog
   ========================================================= */
#help-dialog {
  margin: auto;
  width: min(420px, calc(100vw - 32px));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 12px 40px rgba(28, 27, 24, 0.18);
}

#help-dialog::backdrop {
  background: rgba(28, 27, 24, 0.25);
}

.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-3) var(--space-3) var(--space-2) var(--space-4);
}

.dialog-head h2 {
  font-size: 15px;
  font-weight: 600;
}

.shortcuts {
  display: grid;
  grid-template-columns: 72px 1fr;
  gap: var(--space-2) var(--space-3);
  padding: 0 var(--space-4) var(--space-3);
  font-size: 13px;
}

.shortcuts dd {
  color: var(--text-2);
}

.dialog-foot {
  padding: var(--space-3) var(--space-4);
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-3);
}

/* =========================================================
   Narrow screens
   ========================================================= */
@media (max-width: 1100px) {
  .wordmark {
    display: none;
  }

  .score-line {
    gap: var(--space-3);
  }

  .score-label {
    display: none;
  }

  #simulator-view,
  body.debug-on #simulator-view {
    grid-template-columns: minmax(0, 1fr) 300px;
  }

  .coach {
    flex-wrap: wrap;
  }
}

@media (max-width: 760px) {
  .topbar-center {
    display: none;
  }

  #simulator-view,
  body.debug-on #simulator-view {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1fr) auto;
  }

  .sidebar {
    max-height: 40vh;
  }
}
