:root {
  --bg-dark: #0f172a;
  --bg-panel: #1e293b;
  --bg-card: #263349;
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --border-color: #334155;
  --accent-green: #10b981;
  --accent-lime: #84cc16;
  --accent-amber: #f59e0b;
  --accent-cyan: #06b6d4;
  --accent-red: #f43f5e;
  --accent-purple: #a855f7;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  overscroll-behavior: none;
}

body {
  font-family: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", -apple-system, BlinkMacSystemFont, sans-serif;
  background-color: var(--bg-dark);
  color: var(--text-main);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
}

.app-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-width: 1720px;
  margin: 0 auto;
  width: 100%;
  padding: 10px 16px;
  gap: 10px;
}

/* 頂部標題與狀態列 */
.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--bg-panel);
  border: 1px solid var(--border-color);
  border-radius: 14px;
  padding: 12px 18px;
  flex-shrink: 0;
  flex-wrap: wrap;
  gap: 14px;
}

.header-title {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
  min-width: 320px;
}

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

.badge {
  display: inline-block;
  background: rgba(16, 185, 129, 0.15);
  color: #34d399;
  border: 1px solid rgba(16, 185, 129, 0.4);
  font-size: 0.74rem;
  font-weight: 700;
  padding: 2px 9px;
  border-radius: 999px;
}

.status-badge {
  display: inline-block;
  font-size: 0.76rem;
  font-weight: 700;
  padding: 2px 10px;
  border-radius: 999px;
  transition: all 0.3s ease;
}

.status-badge.healthy {
  background: rgba(16, 185, 129, 0.2);
  color: #6ee7b7;
  border: 1px solid rgba(16, 185, 129, 0.5);
}

.status-badge.warning {
  background: rgba(245, 158, 11, 0.22);
  color: #fcd34d;
  border: 1px solid rgba(245, 158, 11, 0.55);
}

.status-badge.danger {
  background: rgba(244, 63, 94, 0.25);
  color: #fda4af;
  border: 1px solid rgba(244, 63, 94, 0.65);
  animation: pulseDanger 1.2s infinite ease-in-out;
}

@keyframes pulseDanger {
  0%, 100% { box-shadow: 0 0 0 0 rgba(244, 63, 94, 0.4); }
  50% { box-shadow: 0 0 12px 2px rgba(244, 63, 94, 0.65); }
}

.app-header h1 {
  font-size: 1.28rem;
  font-weight: 800;
  letter-spacing: 0.01em;
  color: #fff;
}

.subtitle {
  font-size: 0.83rem;
  color: var(--text-muted);
  line-height: 1.4;
}

.subtitle strong {
  color: #e2e8f0;
}

/* 四大即時指標卡 */
.stats-bar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  padding: 8px 12px;
  border-radius: 11px;
  min-width: 148px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.stat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  font-size: 0.78rem;
}

.stat-label {
  color: #cbd5e1;
  flex: 1;
}

.stat-header strong {
  font-size: 0.95rem;
  font-variant-numeric: tabular-nums;
}

.stat-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  flex-shrink: 0;
}

.mucus-dot { background: #22d3ee; box-shadow: 0 0 6px #22d3ee; }
.cell-dot  { background: #34d399; box-shadow: 0 0 6px #34d399; }
.scfa-dot  { background: #fbbf24; box-shadow: 0 0 6px #fbbf24; }
.immune-dot { background: #f43f5e; box-shadow: 0 0 6px #f43f5e; }

.progress-track {
  width: 100%;
  height: 6px;
  background: rgba(15, 23, 42, 0.7);
  border-radius: 999px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  border-radius: 999px;
  transition: width 0.25s ease, background-color 0.3s ease;
}

.mucus-fill { background: linear-gradient(90deg, #06b6d4, #22d3ee); }
.cell-fill  { background: linear-gradient(90deg, #10b981, #34d399); }
.scfa-fill  { background: linear-gradient(90deg, #f59e0b, #fbbf24); }
.immune-fill { background: linear-gradient(90deg, #f43f5e, #fb7185); }

/* 主版面配置 */
.main-layout {
  display: grid;
  grid-template-columns: 1fr 405px;
  gap: 12px;
  flex: 1;
  min-height: 0;
}

.canvas-wrapper {
  position: relative;
  background: #131c2e;
  border-radius: 16px;
  border: 2px solid var(--border-color);
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
}

#sim-canvas {
  width: 100%;
  height: 100%;
  display: block;
  cursor: pointer;
  touch-action: none;
}

/* 畫布浮動提示 */
.canvas-overlay-top-left {
  position: absolute;
  top: 12px;
  left: 12px;
  pointer-events: none;
  z-index: 10;
}

.flow-indicator {
  background: rgba(15, 23, 42, 0.84);
  backdrop-filter: blur(6px);
  color: #f8fafc;
  padding: 6px 13px;
  border-radius: 999px;
  font-size: 0.82rem;
  font-weight: 600;
  border: 1px solid rgba(255, 255, 255, 0.18);
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
}

.arrow-pulse {
  display: inline-block;
  color: #4ade80;
  font-weight: 900;
  animation: slideRight 1.2s infinite ease-in-out;
}

@keyframes slideRight {
  0%, 100% { transform: translateX(-2px); opacity: 0.7; }
  50% { transform: translateX(4px); opacity: 1; }
}

.canvas-overlay-top-right {
  position: absolute;
  top: 12px;
  right: 12px;
  left: 360px;
  display: flex;
  justify-content: flex-end;
  pointer-events: none;
  z-index: 10;
}

.leaky-alert {
  background: rgba(153, 27, 27, 0.92);
  backdrop-filter: blur(6px);
  color: #fef2f2;
  border: 1.5px solid #f87171;
  padding: 7px 14px;
  border-radius: 10px;
  font-size: 0.82rem;
  font-weight: 700;
  box-shadow: 0 6px 20px rgba(220, 38, 38, 0.45);
  animation: shakeAlert 2s infinite;
}

.leaky-alert.hidden {
  display: none;
}

@keyframes shakeAlert {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(2px); }
}

.canvas-overlay-bottom-left {
  position: absolute;
  bottom: 12px;
  left: 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  z-index: 10;
}

button, input, label, .mode-btn, .btn-action, .btn-secondary, .toggle-chip {
  touch-action: manipulation;
}

.toggle-chip {
  background: rgba(15, 23, 42, 0.84);
  backdrop-filter: blur(6px);
  color: #f1f5f9;
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 0.82rem;
  display: flex;
  align-items: center;
  gap: 7px;
  cursor: pointer;
  user-select: none;
  border: 1px solid rgba(255, 255, 255, 0.18);
  transition: background 0.2s;
}

.toggle-chip:hover {
  background: rgba(30, 41, 59, 0.95);
}

.toggle-chip input {
  width: 17px;
  height: 17px;
  accent-color: var(--accent-green);
  cursor: pointer;
}

.structure-tooltip {
  position: absolute;
  pointer-events: none;
  background: rgba(15, 23, 42, 0.95);
  color: #fff;
  padding: 10px 13px;
  border-radius: 10px;
  border: 1px solid rgba(34, 211, 238, 0.55);
  font-size: 0.8rem;
  max-width: 270px;
  line-height: 1.45;
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.45);
  z-index: 25;
  transition: opacity 0.15s ease;
}

.structure-tooltip.hidden {
  opacity: 0;
  visibility: hidden;
}

/* 右側控制面板 */
.control-panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
  padding-right: 4px;
}

.panel-card {
  background: var(--bg-panel);
  border: 1px solid var(--border-color);
  border-radius: 14px;
  padding: 12px 15px;
  display: flex;
  flex-direction: column;
  gap: 9px;
}

.panel-card h2 {
  font-size: 0.95rem;
  font-weight: 700;
  color: #f1f5f9;
  border-left: 3px solid var(--accent-green);
  padding-left: 8px;
}

.card-desc {
  font-size: 0.78rem;
  color: var(--text-muted);
  line-height: 1.38;
}

/* 飲食模式按鈕 */
.diet-mode-Grid {
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.mode-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  background: var(--bg-card);
  border: 1.5px solid var(--border-color);
  border-radius: 10px;
  padding: 8px 11px;
  color: var(--text-main);
  cursor: pointer;
  text-align: left;
  transition: all 0.18s ease;
}

.mode-btn:hover {
  background: #2d3d56;
}

.mode-btn.active[data-mode="high"] {
  border-color: #10b981;
  background: rgba(16, 185, 129, 0.14);
  box-shadow: 0 0 12px rgba(16, 185, 129, 0.2);
}

.mode-btn.active[data-mode="none"] {
  border-color: #f43f5e;
  background: rgba(244, 63, 94, 0.16);
  box-shadow: 0 0 12px rgba(244, 63, 94, 0.25);
}

.mode-icon {
  font-size: 1.45rem;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(15, 23, 42, 0.45);
  border-radius: 8px;
  flex-shrink: 0;
}

.mode-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.mode-info strong {
  font-size: 0.85rem;
  color: #fff;
}

.mode-info span {
  font-size: 0.73rem;
  color: #cbd5e1;
}

.manual-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 7px;
}

.btn-action {
  border: none;
  border-radius: 9px;
  padding: 10px 11px;
  min-height: 42px;
  font-weight: 700;
  font-size: 0.8rem;
  cursor: pointer;
  color: #fff;
  transition: transform 0.12s, filter 0.15s;
}

.btn-action:hover {
  filter: brightness(1.1);
  transform: translateY(-1px);
}

.btn-action:active {
  transform: translateY(0);
}

.fiber-btn {
  background: linear-gradient(135deg, #15803d, #16a34a);
}

.scfa-btn {
  background: linear-gradient(135deg, #b45309, #d97706);
}

.auto-controls {
  display: flex;
  flex-direction: column;
  gap: 7px;
  padding-top: 2px;
}

.control-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.79rem;
  color: #cbd5e1;
}

.control-row input[type="range"] {
  width: 145px;
  height: 28px;
  accent-color: var(--accent-green);
  cursor: pointer;
}

.btn-group {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 7px;
}

.btn-secondary {
  background: var(--bg-card);
  color: #e2e8f0;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 8px 10px;
  min-height: 40px;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}

.btn-secondary:hover {
  background: #334155;
}

/* 生理與病理四階段追蹤 */
.stage-timeline {
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.stage-item {
  display: flex;
  gap: 9px;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 9px;
  padding: 7px 9px;
  opacity: 0.55;
  transition: all 0.25s ease;
}

.stage-item.active {
  opacity: 1;
  border-color: #10b981;
  background: rgba(16, 185, 129, 0.11);
}

.stage-item.warning-stage.active {
  border-color: #f59e0b;
  background: rgba(245, 158, 11, 0.14);
}

.stage-item.danger-stage.active {
  border-color: #f43f5e;
  background: rgba(244, 63, 94, 0.16);
}

.stage-num {
  width: 21px;
  height: 21px;
  border-radius: 50%;
  background: #334155;
  color: #fff;
  font-size: 0.74rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin-top: 2px;
}

.stage-item.active .stage-num {
  background: #10b981;
  color: #0f172a;
}

.stage-item.warning-stage.active .stage-num {
  background: #f59e0b;
  color: #0f172a;
}

.stage-item.danger-stage.active .stage-num {
  background: #f43f5e;
  color: #fff;
}

.stage-body strong {
  font-size: 0.81rem;
  display: block;
  margin-bottom: 2px;
  color: #f8fafc;
}

.stage-body p {
  font-size: 0.73rem;
  color: #cbd5e1;
  line-height: 1.4;
}

/* 構造說明與圖例 */
.info-box {
  background: var(--bg-card);
  border: 1px solid rgba(34, 211, 238, 0.35);
  border-radius: 10px;
  padding: 9px 11px;
}

.info-title {
  font-size: 0.84rem;
  font-weight: 700;
  color: #38bdf8;
  margin-bottom: 3px;
}

.info-desc {
  font-size: 0.75rem;
  color: #e2e8f0;
  line-height: 1.44;
}

.legend-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 5px 8px;
  margin-top: 2px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.73rem;
  color: #cbd5e1;
}

.dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
}

.l-fiber     { background: #22c55e; border: 1px solid #dcfce7; }
.l-scfa      { background: #fbbf24; border: 1px solid #fef08a; box-shadow: 0 0 5px #fbbf24; }
.l-good-bac  { background: #38bdf8; border: 1px solid #e0f2fe; }
.l-mucin-bac { background: #a855f7; border: 1px solid #f3e8ff; }
.l-mucin     { background: #2dd4bf; border: 1px solid #ccfbf1; }
.l-lps       { background: #ef4444; border: 1px solid #fecaca; }
.l-immune    { background: #f97316; border: 1px solid #ffedd5; }
.l-cytokine  { background: #ec4899; border: 1px solid #fce7f3; }

@media (max-width: 1120px) {
  .app-container {
    height: auto;
  }
  .main-layout {
    grid-template-columns: 1fr;
  }
  .canvas-wrapper {
    height: 600px;
  }
}
