:root {
  --bg: #0b1120;
  --surface: #151e32;
  --surface-2: #0f172a;
  --border: rgba(255, 255, 255, 0.1);
  --text: #f1f5f9;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --orange: #f59e0b;
  --red: #ef4444;
  --green: #22c55e;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: radial-gradient(circle at 20% 0%, #1e293b 0%, #0b1120 65%);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", "Noto Sans TC", sans-serif;
  padding: max(12px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) max(20px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
  touch-action: pan-y;
  -webkit-user-select: none;
  user-select: none;
}

.app {
  max-width: 1260px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 12px 16px;
}

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

.back-link {
  color: var(--muted);
  text-decoration: none;
  font-size: 0.88rem;
  font-weight: 600;
  padding: 8px 12px;
  min-height: 40px;
  display: inline-flex;
  align-items: center;
  border-radius: 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
}

.back-link:hover { color: var(--text); border-color: var(--accent); }
.brand h1 { margin: 0; font-size: 1.2rem; }
.subtitle { margin: 2px 0 0; font-size: 0.82rem; color: var(--muted); }

.top-stats { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

.stat-chip {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 0.84rem;
  color: var(--muted);
}

.stat-chip strong { color: var(--accent); }

.fx-toggle {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 14px;
  min-height: 40px;
  font-size: 0.84rem;
  font-weight: 700;
  color: var(--muted);
  cursor: pointer;
  transition: all 0.15s ease;
}

.fx-toggle.is-on {
  background: rgba(56, 189, 248, 0.16);
  border-color: var(--accent);
  color: #bae6fd;
}

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

.stage-card,
.controls-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.dual-views {
  display: grid;
  grid-template-columns: 1.25fr 0.75fr;
  gap: 12px;
}

.view-pane {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.view-label {
  font-size: 0.84rem;
  font-weight: 700;
  color: #cbd5e1;
  text-align: center;
}

.canvas-wrap {
  position: relative;
  width: 100%;
  background: #070b14;
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  touch-action: none;
}

canvas {
  width: 100%;
  height: auto;
  display: block;
  touch-action: none;
}

/* Dual-Task Overlay */
.dual-task-box {
  position: absolute;
  right: 12px;
  bottom: 54px;
  width: min(230px, calc(100% - 24px));
  background: rgba(15, 23, 42, 0.95);
  border: 2px solid var(--accent);
  border-radius: 12px;
  padding: 10px;
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.55);
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.dt-header {
  font-size: 0.76rem;
  font-weight: 700;
  color: var(--accent);
}

.dt-prompt {
  font-size: 1.05rem;
  font-weight: 800;
  color: var(--text);
  text-align: center;
}

.dt-options {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.dt-opt-btn {
  min-height: 44px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #1e293b;
  color: var(--text);
  font-size: 1rem;
  font-weight: 800;
  cursor: pointer;
}

.dt-opt-btn:active {
  background: var(--accent);
  color: #082f49;
}

.brake-bar {
  display: grid;
  grid-template-columns: 1fr 1.6fr;
  gap: 10px;
}

.btn {
  padding: 14px 16px;
  min-height: 52px;
  border-radius: 12px;
  border: none;
  font-size: 1rem;
  font-weight: 800;
  cursor: pointer;
  transition: transform 0.08s, opacity 0.15s;
}

.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn:active:not(:disabled) { transform: scale(0.98); }
.btn.primary { background: var(--accent); color: #082f49; }

.btn.brake {
  background: linear-gradient(145deg, #ef4444, #b91c1c);
  color: white;
  box-shadow: 0 8px 20px rgba(239, 68, 68, 0.3);
}

.status-banner {
  background: rgba(56, 189, 248, 0.08);
  border: 1px solid rgba(56, 189, 248, 0.25);
  border-radius: 10px;
  padding: 10px 14px;
  text-align: center;
  font-size: 0.92rem;
  color: #e2e8f0;
}

.status-banner.warn {
  background: rgba(239, 68, 68, 0.16);
  border-color: rgba(239, 68, 68, 0.45);
  color: #fecaca;
}

.status-banner.ok {
  background: rgba(34, 197, 94, 0.16);
  border-color: rgba(34, 197, 94, 0.45);
  color: #bbf7d0;
}

.result-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.result-head { display: flex; justify-content: space-between; align-items: center; }
.result-head h2 { margin: 0; font-size: 1.1rem; }

.badge {
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 800;
}

.badge.ok { background: rgba(34, 197, 94, 0.2); color: #86efac; }
.badge.bad { background: rgba(239, 68, 68, 0.22); color: #fca5a5; }

.metrics-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.metric {
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.metric span { font-size: 0.74rem; color: var(--muted); }
.metric strong { font-size: 1.05rem; }
.metric.orange strong { color: var(--orange); }
.metric.red strong { color: var(--red); }
.metric.accent strong { color: var(--accent); }

.dist-bar-wrap { display: flex; flex-direction: column; gap: 6px; }
.dist-bar-labels { display: flex; justify-content: space-between; font-size: 0.8rem; color: var(--muted); }

.dist-bar {
  position: relative;
  height: 18px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
  overflow: visible;
  display: flex;
}

.bar-seg { height: 100%; transition: width 0.35s ease; }
.bar-seg.orange { background: var(--orange); border-radius: 999px 0 0 999px; }
.bar-seg.red { background: var(--red); border-radius: 0 999px 999px 0; }
.bar-seg.red.has-remain { border-radius: 0; }
.bar-seg.green {
  background: rgba(34, 197, 94, 0.72);
  border-radius: 0;
  color: #f8fafc;
  font-size: 0.7rem;
  font-weight: 700;
  line-height: 18px;
  text-align: center;
  overflow: hidden;
  white-space: nowrap;
}

.bar-safe-line {
  position: absolute;
  top: -4px;
  bottom: -4px;
  width: 3px;
  background: var(--accent);
  border-radius: 2px;
  box-shadow: 0 0 8px rgba(56, 189, 248, 0.8);
  transition: left 0.3s ease;
}

.dist-legend { display: flex; flex-wrap: wrap; gap: 14px; font-size: 0.78rem; color: var(--muted); }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 5px; }
.dot.orange { background: var(--orange); }
.dot.red { background: var(--red); }
.dot.green { background: var(--green); }
.dot.blue { background: var(--accent); }

.advice-box {
  background: rgba(56, 189, 248, 0.08);
  border-left: 3px solid var(--accent);
  border-radius: 6px;
  padding: 10px 12px;
  font-size: 0.86rem;
  line-height: 1.5;
  color: #cbd5e1;
}

/* Analytics */
.analytics-grid {
  display: grid;
  grid-template-columns: 1fr 1.2fr;
  gap: 12px;
}

.analytics-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

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

.analytics-head h3 { margin: 0; font-size: 0.92rem; }
.chart-sub { font-size: 0.76rem; color: var(--muted); }

.mini-btn {
  padding: 5px 10px;
  min-height: 32px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #1e293b;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 700;
  cursor: pointer;
}

.compare-table {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  flex: 1;
}

.cmp-col {
  background: rgba(56, 189, 248, 0.06);
  border: 1px solid rgba(56, 189, 248, 0.2);
  border-radius: 10px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.82rem;
  color: var(--muted);
}

.cmp-col.warn {
  background: rgba(245, 158, 11, 0.06);
  border-color: rgba(245, 158, 11, 0.22);
}

.cmp-title { font-weight: 700; color: var(--text); font-size: 0.82rem; }
.cmp-col strong { color: var(--accent); }
.cmp-col.warn strong { color: var(--orange); }

.chart-wrap {
  width: 100%;
  height: 190px;
  background: #070b14;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  touch-action: none;
}

/* Controls */
.controls-card h2 { margin: 0; font-size: 1.05rem; }

.control-block {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.control-block label,
.label-title { font-size: 0.85rem; color: #cbd5e1; }

.control-block input[type="range"] {
  width: 100%;
  min-height: 28px;
  accent-color: var(--accent);
  cursor: pointer;
}

.preset-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.seg-2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px; }
.seg-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }

.preset-btn,
.road-btn,
.veh-btn,
.drv-btn,
.lead-btn,
.dist-btn {
  padding: 8px 4px;
  min-height: 44px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #1e293b;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
  cursor: pointer;
  text-align: center;
}

.preset-btn.active,
.road-btn.active,
.veh-btn.active,
.drv-btn.active,
.lead-btn.active,
.dist-btn.active {
  background: rgba(56, 189, 248, 0.2);
  color: var(--text);
  border-color: var(--accent);
}

.rule-hint { font-size: 0.82rem; color: var(--muted); }
.rule-hint strong { color: var(--accent); }

.check-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.82rem;
  color: #cbd5e1;
  cursor: pointer;
  padding-top: 2px;
}

.check-row input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
}

.formula-card {
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
}

.formula-card h3 { margin: 0 0 8px; font-size: 0.92rem; }
.formula-card ul { margin: 0; padding-left: 18px; display: flex; flex-direction: column; gap: 6px; font-size: 0.8rem; color: var(--muted); line-height: 1.45; }

.hidden { display: none !important; }

@media (max-width: 1024px) {
  .layout { grid-template-columns: 1fr; }
  .analytics-grid { grid-template-columns: 1fr; }
  .metrics-grid { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 640px) {
  .dual-views { grid-template-columns: 1fr; }
}
