/* --- CSS Variables ---
 * 基底面（背景、文字、面板、邊框）接站上 /theme.css 的 mist tokens；
 * 訊號色（球的藍、開始/暫停/重設的綠黃紅）是功能色，留在頁內。 */
:root {
  --primary-color: var(--accent);
  --primary-dark: color-mix(in srgb, var(--accent) 82%, #000);
  --primary-light: color-mix(in srgb, var(--accent) 38%, #fff);
  --accent-color: #ef4444;
  --success-color: #22c55e;
  --warning-color: #f59e0b;
  --bg-secondary: var(--panel);
  --bg-tertiary: var(--panel-hi);
  --text-color: var(--ink);
  --text-secondary: var(--dim);
  --border-color: var(--line);
  --shadow-lg: 0 8px 30px rgba(0, 0, 0, 0.12);
  --radius: 12px;
}

/* --- General Layout --- */
body {
  display: flex;
  flex-direction: column;
  align-items: center;
  /* theme.css 的 body{height:100%} 會讓 flex column 壓縮固定高度的子元素
     （模擬區/圖表區被 flex-shrink 壓扁），改回 auto 由內容撐高。 */
  height: auto;
  min-height: 100vh;
  padding: 16px 16px 30px;
  margin: 0;
}

h1 {
  color: var(--text-color);
  margin: 0 0 12px 0;
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.5px;
  text-align: center;
}

/* --- Controls Area --- */
.controls {
  padding: 16px 20px;
  background: var(--bg-secondary);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border-color);
  display: grid;
  grid-template-columns: 115px 1fr 65px;
  gap: 10px 12px;
  align-items: center;
  width: 600px;
  margin-top: 12px;
}

.controls label {
  font-weight: 500;
  color: var(--text-secondary);
  font-size: 0.8rem;
  text-align: right;
}

.controls > div:not(.control-buttons) {
  grid-column: 2 / 4;
  display: flex;
  align-items: center;
  gap: 12px;
}

/* --- Custom Range Slider --- */
.controls input[type="range"] {
  flex-grow: 1;
  height: 6px;
  border-radius: 3px;
  background: linear-gradient(to right, #e2e8f0, #cbd5e1);
  outline: none;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
  border: none;
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);
}

.controls input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--primary-color), var(--primary-dark));
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(59, 130, 246, 0.35);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  border: 2px solid white;
}

.controls input[type="range"]::-webkit-slider-thumb:hover {
  transform: scale(1.1);
  box-shadow: 0 3px 8px rgba(59, 130, 246, 0.45);
}

.controls input[type="range"]::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--primary-color), var(--primary-dark));
  cursor: pointer;
  border: 2px solid white;
  box-shadow: 0 2px 6px rgba(59, 130, 246, 0.35);
}

.controls input[type="range"]:focus {
  outline: none;
}

.value-display {
  min-width: 55px;
  text-align: right;
  font-weight: 600;
  color: var(--primary-dark);
  font-size: 0.8rem;
  font-family: 'SF Mono', 'Consolas', 'Monaco', monospace;
  background: linear-gradient(135deg, #eff6ff, #dbeafe);
  padding: 4px 8px;
  border-radius: 4px;
  border: 1px solid var(--primary-light);
}

/* --- Control Buttons --- */
.control-buttons {
  grid-column: 1 / 4;
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border-color);
}

.control-buttons button {
  padding: 8px 24px;
  font-size: 0.85rem;
  font-weight: 600;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s ease;
  min-width: 90px;
  letter-spacing: 0.3px;
}

#startButton {
  background: linear-gradient(135deg, var(--success-color), #16a34a);
  box-shadow: 0 2px 8px rgba(34, 197, 94, 0.3);
}

#pauseButton {
  background: linear-gradient(135deg, var(--warning-color), #d97706);
  box-shadow: 0 2px 8px rgba(245, 158, 11, 0.3);
  color: #fff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

#resetButton {
  background: linear-gradient(135deg, var(--accent-color), #dc2626);
  box-shadow: 0 2px 8px rgba(239, 68, 68, 0.3);
}

#startButton:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(34, 197, 94, 0.4);
}

#pauseButton:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(245, 158, 11, 0.4);
}

#resetButton:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(239, 68, 68, 0.4);
}

button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none !important;
  box-shadow: none !important;
  filter: grayscale(30%);
}

/* --- Simulation Area --- */
#simulationArea {
  position: relative;
  width: 600px;
  height: 400px;
  background: linear-gradient(180deg, #f8fafc 0%, #ffffff 100%);
  border: 1px solid var(--border-color);
  border-bottom: none;
  border-radius: var(--radius) var(--radius) 0 0;
  overflow: hidden;
  box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.08);
}

.peg {
  position: absolute;
  width: 8px;
  height: 8px;
  background: radial-gradient(circle at 30% 30%, #94a3b8, #475569);
  border-radius: 50%;
  transform: translate(-50%, -50%);
  box-shadow:
    inset 0 -2px 4px rgba(0, 0, 0, 0.2),
    0 1px 3px rgba(0, 0, 0, 0.15);
}

.ball {
  position: absolute;
  width: 10px;
  height: 10px;
  background: radial-gradient(circle at 30% 30%, #fca5a5, var(--accent-color));
  border-radius: 50%;
  left: 0;
  top: 0;
  will-change: transform;
  z-index: 10;
  box-shadow:
    inset 0 -3px 6px rgba(0, 0, 0, 0.15),
    0 2px 8px rgba(239, 68, 68, 0.35);
}

/* --- Bar Chart Area --- */
#barChartWrapper {
  position: relative;
  width: 600px;
  height: 280px;
  border-radius: 0 0 var(--radius) var(--radius);
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

#barChartArea {
  display: flex;
  justify-content: flex-start;
  align-items: flex-end;
  width: 100%;
  height: 100%;
  background: linear-gradient(to bottom, #ffffff 0%, #f8fafc 100%);
  border: 1px solid var(--border-color);
  border-top: none;
  padding: 0;
  box-sizing: border-box;
  position: absolute;
  top: 0;
  left: 0;
}

.bar-container {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  height: 100%;
}

.bar {
  background: linear-gradient(to top, var(--primary-dark), var(--primary-color), var(--primary-light));
  border: none;
  border-radius: 4px 4px 0 0;
  width: 10px;
  margin: 0 1px;
  height: 0;
  position: relative;
  box-sizing: border-box;
  flex-shrink: 0;
  transition: height 0.1s ease-out;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.3),
    0 2px 8px rgba(59, 130, 246, 0.2);
}

.bar-label {
  position: absolute;
  bottom: -22px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 10px;
  color: var(--text-secondary);
  white-space: nowrap;
  font-weight: 600;
}

/* --- Count Labels Container (above curve) --- */
#countLabelsContainer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 10;
  display: flex;
  align-items: flex-end;
}

.bar-count {
  position: absolute;
  transform: translateX(-50%);
  font-size: 10px;
  color: var(--text-color);
  font-weight: 600;
  white-space: nowrap;
  font-family: 'SF Mono', 'Consolas', 'Monaco', monospace;
}

/* --- SVG Overlay for Theoretical Curve --- */
#theoreticalCurveSvg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 5;
}

#theoreticalCurvePath {
  fill: none;
  stroke: var(--accent-color);
  stroke-width: 2.5;
  stroke-linejoin: round;
  stroke-linecap: round;
  filter: drop-shadow(0 1px 2px rgba(239, 68, 68, 0.3));
}

/* --- Responsive --- */
@media (max-width: 640px) {
  .controls {
    width: 100%;
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .controls label {
    text-align: left;
  }

  .controls > div {
    grid-column: 1;
  }

  .control-buttons {
    grid-column: 1;
    flex-wrap: wrap;
  }

  #simulationArea,
  #barChartWrapper {
    width: 100%;
  }
}
