:root {
  color-scheme: dark;
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-border: #334155;
  --text: #f8fafc;
  --muted: #94a3b8;
  --glucose: #f43f5e;
  --insulin: #38bdf8;
  --glucagon: #fb923c;
  --adrenaline: #facc15;
  --atp: #a78bfa;
  --green: #10b981;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans TC", sans-serif;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  touch-action: pan-y;
}

.app {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  max-width: 1400px;
  margin: 0 auto;
  padding:
    max(10px, env(safe-area-inset-top))
    max(12px, env(safe-area-inset-right))
    max(10px, env(safe-area-inset-bottom))
    max(12px, env(safe-area-inset-left));
  gap: 10px;
}

.card,
.topbar {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  padding: 10px 14px;
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.title-group {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.title-group h1 {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 700;
}

.badge {
  font-size: 0.82rem;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(56, 189, 248, 0.16);
  border: 1px solid rgba(56, 189, 248, 0.35);
  color: #7dd3fc;
}

.top-metrics {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.stat-pill {
  display: flex;
  align-items: baseline;
  gap: 6px;
  background: #0f172a;
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 5px 10px;
}

.stat-label {
  font-size: 0.76rem;
  color: var(--muted);
}

.stat-val {
  font-size: 0.98rem;
  font-variant-numeric: tabular-nums;
}

.stat-val.high { color: #fb923c; }
.stat-val.low { color: #f43f5e; }
.stat-val.ok { color: #34d399; }

.top-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1.65fr) minmax(300px, 1fr);
  gap: 10px;
  flex: 1;
  align-items: start;
}

.visual-column {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}

.card-header h2 {
  margin: 0;
  font-size: 0.96rem;
}

.legend-inline {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  font-size: 0.78rem;
  color: var(--muted);
}

.mol-toggle {
  display: inline-flex;
  align-items: center;
  min-height: 34px;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid var(--panel-border);
  background: #0f172a;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  opacity: 0.45;
  text-decoration: line-through;
  transition: opacity 0.15s ease, border-color 0.15s ease, background-color 0.15s ease;
}

.mol-toggle.active {
  opacity: 1;
  color: var(--text);
  background: rgba(51, 65, 85, 0.55);
  border-color: #64748b;
  text-decoration: none;
}

.mol-toggle.all-btn {
  opacity: 1;
  text-decoration: none;
  color: #93c5fd;
  border-color: rgba(59, 130, 246, 0.45);
  background: rgba(59, 130, 246, 0.14);
}

.dot {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.dot::before {
  content: "";
  width: 9px;
  height: 9px;
  border-radius: 50%;
  display: inline-block;
}

.dot.glucose::before { background: var(--glucose); }
.dot.insulin::before { background: var(--insulin); }
.dot.glucagon::before { background: var(--glucagon); }
.dot.adrenaline::before { background: var(--adrenaline); }
.dot.atp::before { background: var(--atp); }

.hint-chip {
  font-size: 0.78rem;
  padding: 3px 10px;
  border-radius: 999px;
  background: #0f172a;
  border: 1px solid var(--panel-border);
  color: #cbd5e1;
}

.canvas-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9.5;
  background: #020617;
  border-radius: 10px;
  border: 1px solid var(--panel-border);
  overflow: hidden;
  touch-action: none;
}

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

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

.gameover-banner {
  position: absolute;
  inset: 0;
  background: rgba(2, 6, 23, 0.82);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

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

.gameover-box {
  background: var(--panel);
  border: 1px solid var(--glucose);
  border-radius: 14px;
  padding: 20px 24px;
  text-align: center;
  max-width: 360px;
}

.gameover-box h3 {
  margin: 0 0 8px;
  color: #fda4af;
}

.gameover-box p {
  margin: 0 0 14px;
  font-size: 0.9rem;
  color: #cbd5e1;
}

.control-card {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.control-card h2 {
  margin: 0;
  font-size: 0.98rem;
}

.toggle-row {
  display: flex;
}

.hormone-bars {
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: #0f172a;
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 10px;
}

.bar-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.bar-label {
  display: flex;
  justify-content: space-between;
  font-size: 0.82rem;
  color: #cbd5e1;
}

.bar-track {
  width: 100%;
  height: 8px;
  background: #1e293b;
  border-radius: 999px;
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  width: 15%;
  transition: width 0.12s linear;
}

.bar-fill.insulin { background: var(--insulin); }
.bar-fill.glucagon { background: var(--glucagon); }
.bar-fill.adrenaline { background: var(--adrenaline); }

.action-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.btn {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 44px;
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--panel-border);
  background: #334155;
  color: var(--text);
  font-size: 0.86rem;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.08s ease, background-color 0.15s ease;
}

.btn small {
  font-size: 0.72rem;
  font-weight: 400;
  opacity: 0.85;
}

.btn:active { transform: scale(0.97); }
.btn-primary { background: #2563eb; border-color: #3b82f6; flex-direction: row; }
.btn-secondary { flex-direction: row; }
.btn-secondary.active { background: rgba(59, 130, 246, 0.25); border-color: #3b82f6; color: #bfdbfe; }
.btn-ghost { background: transparent; flex-direction: row; }
.btn-toggle { width: 100%; flex-direction: row; background: #0f172a; }
.btn-toggle.active { background: rgba(16, 185, 129, 0.2); border-color: var(--green); color: #a7f3d0; }

.btn-eat { background: rgba(244, 63, 94, 0.22); border-color: rgba(244, 63, 94, 0.5); }
.btn-exercise { background: rgba(167, 139, 250, 0.22); border-color: rgba(167, 139, 250, 0.5); }
.btn-insulin { background: rgba(56, 189, 248, 0.22); border-color: rgba(56, 189, 248, 0.5); }
.btn-glucagon { background: rgba(251, 146, 60, 0.22); border-color: rgba(251, 146, 60, 0.5); }
.btn-adrenaline { grid-column: 1 / -1; background: rgba(250, 204, 21, 0.18); border-color: rgba(250, 204, 21, 0.5); }

.sliders-box {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.slider-item input[type="range"] {
  width: 100%;
  accent-color: #38bdf8;
}

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

.org-chip {
  background: #0f172a;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 6px 8px;
  font-size: 0.78rem;
  text-align: center;
  color: var(--muted);
}

.org-chip strong {
  display: block;
  color: var(--text);
  font-size: 0.92rem;
  font-variant-numeric: tabular-nums;
}

@media (max-width: 960px) {
  .workspace {
    grid-template-columns: 1fr;
  }
}
