:root {
  --bg: #ffffff;
  --fg: #111111;
  --muted: #666666;
  --rule: #e5e5e5;
  --border: #111111;
  --soft-border: #cccccc;
  --accent: #d46b4c;
  --accent-soft: #fff5f2;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--fg);
  font: 14px/1.4 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  user-select: none;
  touch-action: none;
}

#stage {
  display: block;
  width: 100%;
  height: 100%;
  cursor: crosshair;
}

#stage.tool-pan {
  cursor: grab;
}

#stage.tool-pan.dragging {
  cursor: grabbing;
}

#stage.tool-eraser {
  cursor: cell;
}

button,
input,
select {
  font: inherit;
  color: var(--fg);
}

button,
select {
  background: var(--bg);
  border: 1px solid var(--soft-border);
  border-radius: 5px;
  padding: 6px 11px;
  cursor: pointer;
  line-height: 1.2;
  white-space: nowrap;
}

button {
  appearance: none;
}

button:hover,
select:hover {
  border-color: var(--fg);
}

button:focus-visible,
input:focus-visible,
select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

button.btn-primary {
  background: var(--fg);
  border-color: var(--fg);
  color: var(--bg);
  font-weight: 500;
}

button.btn-primary:hover {
  background: #2b2b2b;
}

button.active {
  background: var(--fg);
  border-color: var(--fg);
  color: var(--bg);
}

button.active-accent {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--bg);
}

/* Top HUD */
.hud-top {
  position: fixed;
  top: 14px;
  left: 16px;
  right: 16px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  pointer-events: none;
  z-index: 20;
}

.hud-brand {
  pointer-events: auto;
  background: var(--bg);
  border: 1px solid var(--rule);
  border-radius: 5px;
  padding: 8px 12px;
}

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

.brand-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--accent);
  flex-shrink: 0;
}

.hud-brand h1 {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.curve-select {
  margin-left: 4px;
  padding: 2px 7px;
  font-size: 13px;
  font-weight: 500;
  border-color: var(--soft-border);
}

.telemetry {
  margin-top: 4px;
  font-size: 13px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  align-items: center;
}

.telemetry strong {
  color: var(--fg);
  font-weight: 600;
}

.telemetry .sep {
  color: #bbbbbb;
}

.hud-actions {
  pointer-events: auto;
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

/* Milestone Unlock Toast */
.milestone-toast {
  position: fixed;
  top: 84px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg);
  border: 1px solid var(--fg);
  border-left: 4px solid var(--accent);
  border-radius: 5px;
  padding: 10px 16px;
  min-width: 240px;
  max-width: min(92vw, 440px);
  z-index: 25;
  pointer-events: none;
  opacity: 0;
  transition: opacity 140ms ease, transform 160ms ease;
}

.milestone-toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.milestone-toast.entering {
  transform: translateX(-50%) translateY(-6px);
}

.milestone-meta {
  font-size: 12px;
  color: var(--accent);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.milestone-title {
  margin-top: 2px;
  font-size: 16px;
  font-weight: 600;
  color: var(--fg);
  letter-spacing: -0.01em;
}

/* Start prompt */
.start-banner {
  position: fixed;
  top: 84px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg);
  border: 1px solid var(--fg);
  border-radius: 5px;
  padding: 13px 16px;
  display: flex;
  align-items: center;
  gap: 16px;
  max-width: min(92vw, 500px);
  z-index: 22;
}

.start-banner[hidden] {
  display: none;
}

.start-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--fg);
}

.start-sub {
  margin-top: 3px;
  font-size: 13px;
  color: var(--muted);
}

.start-banner .btn-primary {
  padding: 9px 18px;
  font-size: 14px;
}

/* Minimap */
.minimap-wrap {
  position: fixed;
  right: 16px;
  bottom: 70px;
  padding: 6px;
  background: var(--bg);
  border: 1px solid var(--rule);
  border-radius: 5px;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
  z-index: 18;
}

.minimap-wrap:hover {
  border-color: var(--fg);
}

#minimap {
  display: block;
  width: 144px;
  height: 96px;
}

.minimap-label {
  font-size: 11px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

/* Touch lean buttons */
.touch-controls {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: 84px;
  display: flex;
  justify-content: space-between;
  gap: 8px;
  pointer-events: none;
  z-index: 19;
}

.touch-controls[hidden] {
  display: none;
}

.touch-controls button {
  pointer-events: auto;
  padding: 10px 14px;
  border-color: var(--fg);
  font-weight: 500;
}

/* Bottom Toolbar */
.toolbar {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%);
  background: var(--bg);
  border: 1px solid var(--fg);
  border-radius: 5px;
  padding: 5px 7px;
  display: flex;
  align-items: center;
  gap: 6px;
  max-width: calc(100vw - 20px);
  overflow-x: auto;
  z-index: 20;
}

.tool-group {
  display: flex;
  align-items: center;
  gap: 4px;
}

.tool-divider {
  width: 1px;
  height: 22px;
  background: var(--rule);
  flex-shrink: 0;
}

.swatch {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  margin-right: 5px;
  vertical-align: middle;
}

.swatch-accent {
  background: var(--accent);
}

.tool-btn.active-accent .swatch-accent {
  background: var(--bg);
}

/* Subtle footer */
.site-footer {
  position: fixed;
  left: 50%;
  bottom: 6px;
  transform: translateX(-50%);
  font-size: 11px;
  color: #888888;
  white-space: nowrap;
  pointer-events: none;
  z-index: 15;
}

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(17, 17, 17, 0.28);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 40;
}

.modal-backdrop[hidden] {
  display: none;
}

.modal {
  background: var(--bg);
  border: 1px solid var(--fg);
  border-radius: 5px;
  width: 100%;
  max-width: 460px;
  max-height: min(86vh, 640px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  user-select: text;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 13px 16px;
  border-bottom: 1px solid var(--rule);
}

.modal-header h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}

.btn-close {
  border: 1px solid transparent;
  padding: 2px 8px;
  font-size: 20px;
  line-height: 1;
}

.btn-close:hover {
  border-color: var(--soft-border);
}

/* Finish Section */
.finish-section {
  padding: 16px;
  border-bottom: 1px solid var(--rule);
}

.finish-section[hidden] {
  display: none;
}

.finish-headline {
  display: flex;
  align-items: center;
  gap: 12px;
}

.finish-clip {
  font-size: 32px;
  line-height: 1;
}

.finish-kicker {
  font-size: 13px;
  color: var(--accent);
  font-weight: 600;
}

.finish-time {
  font-size: 28px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  line-height: 1.15;
}

.finish-bonus {
  margin-top: 3px;
  font-size: 13px;
  font-weight: 600;
  color: var(--fg);
}

.finish-stats {
  margin-top: 10px;
  font-size: 13px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.score-form {
  margin-top: 14px;
  display: flex;
  gap: 8px;
}

.score-form input {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--soft-border);
  border-radius: 5px;
  padding: 8px 10px;
  background: var(--bg);
}

.score-form input:focus {
  border-color: var(--fg);
}

.form-feedback {
  margin-top: 8px;
  font-size: 13px;
  color: var(--accent);
  font-weight: 500;
}

.form-feedback.error {
  color: #b91c1c;
}

.finish-footer {
  margin-top: 12px;
  display: flex;
  justify-content: flex-end;
}

/* Leaderboard Section */
.leaderboard-section {
  padding: 12px 16px 16px;
  display: flex;
  flex-direction: column;
  min-height: 0;
  flex: 1;
}

.lb-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
}

.lb-tabs {
  display: flex;
  gap: 4px;
}

.lb-tab {
  padding: 5px 10px;
  font-size: 13px;
}

.lb-count {
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.lb-table-wrap {
  overflow-y: auto;
  border-top: 1px solid var(--rule);
}

.lb-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.lb-table th {
  text-align: left;
  font-weight: 600;
  color: var(--muted);
  padding: 7px 6px;
  border-bottom: 1px solid var(--rule);
  position: sticky;
  top: 0;
  background: var(--bg);
}

.lb-table td {
  padding: 7px 6px;
  border-bottom: 1px solid var(--rule);
}

.lb-table th.num,
.lb-table td.num {
  text-align: right;
}

.lb-table tr.mine {
  background: var(--accent-soft);
}

.lb-table tr.mine td:first-child,
.lb-table tr.mine td:nth-child(2) {
  color: var(--accent);
  font-weight: 600;
}

.lb-empty {
  text-align: center;
  color: var(--muted);
  padding: 20px 8px;
}

.tag-custom {
  color: var(--accent);
}

.tag-muted {
  color: var(--muted);
}

@media (max-width: 680px) {
  .hud-top {
    top: 10px;
    left: 10px;
    right: 10px;
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
  }

  .hud-actions {
    justify-content: flex-start;
  }

  .milestone-toast,
  .start-banner {
    top: 122px;
  }

  .minimap-wrap {
    display: none;
  }

  .start-banner {
    flex-direction: column;
    align-items: flex-start;
  }

  .start-banner .btn-primary {
    width: 100%;
  }

  .toolbar {
    bottom: 24px;
  }
}
