:root {
  --bg: #faf9f5;
  --surface: rgba(255, 255, 255, 0.92);
  --surface-solid: #ffffff;
  --ink: #18181b;
  --ink-soft: #52525b;
  --ink-muted: #a1a1aa;
  --border: #e4e4e7;
  --up: #059669;
  --up-bg: rgba(5, 150, 105, 0.12);
  --down: #dc2626;
  --down-bg: rgba(220, 38, 38, 0.12);
  --accent: #2563eb;
  --boost: #ea580c;
  --shadow: 0 8px 28px rgba(15, 23, 42, 0.08);
  --radius: 12px;
  --mono: ui-monospace, 'SFMono-Regular', Menlo, Consolas, monospace;
  --sans: Inter, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

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

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
}

body {
  display: flex;
  flex-direction: column;
}

/* Top Bar */
.topbar {
  height: 54px;
  background: var(--surface-solid);
  border-bottom: 1px solid var(--border);
  padding: 0 14px;
  display: flex;
  align-items: center;
  gap: 12px;
  z-index: 30;
  flex-shrink: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 7px;
  font-weight: 800;
  font-size: 15px;
  letter-spacing: -0.02em;
  white-space: nowrap;
  user-select: none;
}

.brand-icon {
  font-size: 19px;
}

.brand-name span {
  color: var(--down);
}

.search-wrap {
  position: relative;
  flex: 0 1 290px;
  min-width: 180px;
}

.search-wrap input {
  width: 100%;
  height: 34px;
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 0 30px 0 11px;
  font-size: 13px;
  font-family: var(--sans);
  color: var(--ink);
  background: #f8fafc;
  outline: none;
  transition: border-color 0.15s, background 0.15s;
}

.search-wrap input:focus {
  border-color: var(--ink);
  background: #ffffff;
}

.spinner {
  position: absolute;
  right: 9px;
  top: 9px;
  width: 15px;
  height: 15px;
  border: 2px solid var(--border);
  border-top-color: var(--ink);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

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

.search-dropdown {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  min-width: 310px;
  background: var(--surface-solid);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  overflow: hidden;
  z-index: 100;
}

.search-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 9px 12px;
  cursor: pointer;
  border-bottom: 1px solid #f4f4f5;
  font-size: 13px;
}

.search-item:last-child {
  border-bottom: none;
}

.search-item:hover,
.search-item.selected {
  background: #f4f4f5;
}

.search-item-left {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.search-sym {
  font-family: var(--mono);
  font-weight: 700;
  font-size: 12.5px;
  color: var(--ink);
}

.search-name {
  font-size: 11.5px;
  color: var(--ink-soft);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.search-exch {
  font-size: 10px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 5px;
  background: #f4f4f5;
  color: var(--ink-soft);
  white-space: nowrap;
}

.search-exch.private {
  background: #fef3c7;
  color: #b45309;
}

.presets-bar {
  display: flex;
  align-items: center;
  gap: 5px;
  overflow-x: auto;
  scrollbar-width: none;
}

.presets-bar::-webkit-scrollbar {
  display: none;
}

.presets-bar button,
.timeframe-bar button {
  height: 30px;
  padding: 0 9px;
  border-radius: 8px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--ink-soft);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.12s;
}

.presets-bar button:hover,
.timeframe-bar button:hover {
  background: #f4f4f5;
  color: var(--ink);
}

.presets-bar button.active {
  background: var(--ink);
  color: #ffffff;
}

.timeframe-bar {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 2px;
  background: #f4f4f5;
  padding: 3px;
  border-radius: 9px;
  flex-shrink: 0;
}

.timeframe-bar button {
  height: 26px;
  padding: 0 8px;
  font-family: var(--mono);
  font-size: 11.5px;
}

.timeframe-bar button.active {
  background: #ffffff;
  color: var(--ink);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

.timeframe-bar.disabled {
  opacity: 0.45;
  pointer-events: none;
}

/* Main Stage */
.stage {
  position: relative;
  flex: 1;
  width: 100%;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

#gameCanvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

/* Zoom Controls */
.zoom-controls {
  position: absolute;
  bottom: 74px;
  left: 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  z-index: 25;
}

.zoom-controls button {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  backdrop-filter: blur(8px);
  color: var(--ink);
  font-size: 17px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  justify-content: center;
}

.zoom-controls button:hover {
  background: #ffffff;
}

#gameCanvas.cursor-grabbing {
  cursor: grabbing;
}

#gameCanvas.cursor-crosshair {
  cursor: crosshair;
}

/* Telemetry HUD Card */
.hud-card {
  position: absolute;
  top: 14px;
  left: 14px;
  background: var(--surface);
  backdrop-filter: blur(8px);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 11px 14px;
  min-width: 220px;
  max-width: 300px;
  box-shadow: var(--shadow);
  pointer-events: none;
  user-select: none;
}

.hud-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.hud-symbol {
  font-family: var(--mono);
  font-weight: 800;
  font-size: 14px;
  letter-spacing: -0.01em;
}

.hud-badge {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.04em;
  padding: 2px 6px;
  border-radius: 5px;
  background: #f4f4f5;
  color: var(--ink-soft);
}

.hud-badge.live {
  background: var(--up-bg);
  color: var(--up);
}

.hud-badge.private {
  background: #fef3c7;
  color: #b45309;
}

.hud-name {
  font-size: 11.5px;
  color: var(--ink-soft);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.hud-price-row {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-top: 6px;
}

.hud-price {
  font-family: var(--mono);
  font-size: 22px;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.hud-pct {
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 6px;
}

.hud-pct.up {
  background: var(--up-bg);
  color: var(--up);
}

.hud-pct.down {
  background: var(--down-bg);
  color: var(--down);
}

.hud-date {
  font-size: 11.5px;
  color: var(--ink-soft);
  margin-top: 2px;
}

.hud-stats {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
  padding-top: 7px;
  border-top: 1px solid #f1f1f4;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--ink-soft);
}

.hud-stats .dot {
  color: var(--ink-muted);
}

.status-ready {
  color: var(--ink-soft);
  font-weight: 700;
}

.status-riding {
  color: var(--up);
  font-weight: 700;
}

.status-bailed {
  color: var(--down);
  font-weight: 700;
}

.status-finished {
  color: var(--accent);
  font-weight: 700;
}

.hud-note {
  margin-top: 7px;
  font-size: 11px;
  line-height: 1.35;
  color: #92400e;
  background: #fffbeb;
  border: 1px solid #fde68a;
  border-radius: 7px;
  padding: 5px 7px;
}

/* Controls Hint */
.controls-hint {
  position: absolute;
  top: 14px;
  right: 14px;
  background: var(--surface);
  backdrop-filter: blur(8px);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 12px;
  color: var(--ink-soft);
  display: flex;
  gap: 12px;
  box-shadow: var(--shadow);
  pointer-events: none;
  user-select: none;
}

.controls-hint kbd {
  display: inline-block;
  padding: 1px 5px;
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 700;
  color: var(--ink);
  background: #f4f4f5;
  border: 1px solid #d4d4d8;
  border-radius: 4px;
  margin-right: 2px;
}

/* Toast */
.toast {
  position: absolute;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: #ffffff;
  font-size: 12.5px;
  font-weight: 600;
  padding: 8px 14px;
  border-radius: 9px;
  box-shadow: var(--shadow);
  z-index: 40;
  pointer-events: none;
}

/* Tune Popover */
.tune-popover {
  position: absolute;
  bottom: 74px;
  right: 18px;
  width: 248px;
  background: var(--surface-solid);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
  z-index: 35;
  display: flex;
  flex-direction: column;
  gap: 11px;
}

.tune-title {
  font-size: 12.5px;
  font-weight: 700;
  color: var(--ink);
}

.tune-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--ink-soft);
}

.tune-row span {
  display: flex;
  justify-content: space-between;
}

.tune-row strong {
  font-family: var(--mono);
  color: var(--ink);
}

.tune-row input[type='range'] {
  width: 100%;
  accent-color: var(--ink);
}

.tune-check {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12.5px;
  color: var(--ink);
  cursor: pointer;
}

.tune-btn {
  height: 32px;
  border: 1px solid var(--border);
  background: #f8fafc;
  color: var(--ink);
  border-radius: 8px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.tune-btn:hover {
  background: #f1f5f9;
}

/* Bottom Floating Toolbar */
.toolbar {
  position: absolute;
  bottom: 14px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 6px 8px;
  display: flex;
  align-items: center;
  gap: 6px;
  box-shadow: var(--shadow);
  max-width: calc(100vw - 24px);
  overflow-x: auto;
  z-index: 30;
}

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

.toolbar button {
  height: 34px;
  padding: 0 10px;
  border: 1px solid transparent;
  border-radius: 9px;
  background: transparent;
  color: var(--ink);
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
}

.toolbar button:hover {
  background: #f4f4f5;
}

.toolbar button.active {
  background: var(--ink);
  color: #ffffff;
}

.toolbar button.btn-primary {
  background: var(--up);
  color: #ffffff;
  padding: 0 13px;
}

.toolbar button.btn-primary:hover {
  background: #047857;
}

.toolbar button.btn-primary.playing {
  background: #d97706;
}

.toolbar button.btn-primary.playing:hover {
  background: #b45309;
}

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

.mode-toggle {
  display: flex;
  background: #f4f4f5;
  padding: 3px;
  border-radius: 9px;
  gap: 2px;
}

.mode-toggle button {
  height: 28px;
  padding: 0 10px;
  font-size: 12px;
  border-radius: 7px;
  color: var(--ink-soft);
}

.mode-toggle button.active {
  background: #ffffff;
  color: var(--ink);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.hidden {
  display: none !important;
}

@media (max-width: 960px) {
  .presets-bar {
    display: none;
  }
  .controls-hint {
    display: none;
  }
}
