/* ===== 基礎重置與變量 ===== */
:root {
  --bg-primary: #0f172a;
  --bg-secondary: #1e293b;
  --bg-tertiary: #334155;
  --bg-card: #1e293b;
  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --accent-primary: #3b82f6;
  --accent-success: #10b981;
  --accent-warning: #f59e0b;
  --accent-danger: #ef4444;
  --accent-info: #06b6d4;
  --accent-purple: #8b5cf6;
  --border-color: #334155;
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.5);
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --transition: all 0.2s ease;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft JhengHei", sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  padding: 20px;
  line-height: 1.6;
}

/* 滾動條樣式 */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: var(--bg-secondary);
  border-radius: 4px;
}

::-webkit-scrollbar-thumb {
  background: var(--bg-tertiary);
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: #475569;
}

/* ===== 容器佈局 ===== */
.container {
  max-width: 1200px;
  margin: 0 auto;
}

/* ===== 標題區域 ===== */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 2px solid var(--border-color);
}

.app-header h1 {
  font-size: 28px;
  font-weight: 700;
  background: linear-gradient(135deg, #60a5fa, #a78bfa);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.status-badge {
  padding: 8px 16px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 20px;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-secondary);
  transition: var(--transition);
}

.status-badge.connected {
  background: rgba(16, 185, 129, 0.15);
  border-color: var(--accent-success);
  color: var(--accent-success);
}

/* ===== 主要面板 ===== */
.main-panel {
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-color);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}

/* ===== 控制區域 ===== */
.control-section {
  padding: 24px;
  background: linear-gradient(180deg, rgba(59, 130, 246, 0.05), transparent);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 16px;
}

.btn-group {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

/* ===== 按鈕樣式 ===== */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
  border: none;
  border-radius: var(--radius-md);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: var(--transition);
  box-shadow: var(--shadow-sm);
}

.btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.btn:active:not(:disabled) {
  transform: translateY(0);
}

.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn-icon {
  font-size: 16px;
}

.btn-primary {
  background: linear-gradient(135deg, #3b82f6, #2563eb);
  color: white;
}

.btn-secondary {
  background: linear-gradient(135deg, #8b5cf6, #7c3aed);
  color: white;
}

.btn-success {
  background: linear-gradient(135deg, #10b981, #059669);
  color: white;
}

.btn-danger {
  background: linear-gradient(135deg, #ef4444, #dc2626);
  color: white;
}

.btn-info {
  background: linear-gradient(135deg, #06b6d4, #0891b2);
  color: white;
}

/* ===== 切換開關 ===== */
.toggle-switch {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  margin-left: auto;
}

.toggle-switch input {
  display: none;
}

.toggle-slider {
  position: relative;
  width: 52px;
  height: 28px;
  background: var(--bg-tertiary);
  border-radius: 14px;
  transition: var(--transition);
}

.toggle-slider::before {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  background: white;
  border-radius: 50%;
  transition: var(--transition);
  box-shadow: var(--shadow-sm);
}

.toggle-switch input:checked + .toggle-slider {
  background: var(--accent-success);
}

.toggle-switch input:checked + .toggle-slider::before {
  transform: translateX(24px);
}

.toggle-label {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-secondary);
}

/* ===== 提示信息 ===== */
.info-tip {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 24px;
  background: rgba(59, 130, 246, 0.08);
  border-left: 4px solid var(--accent-primary);
  font-size: 14px;
  color: var(--text-secondary);
}

.tip-icon {
  font-size: 18px;
}

/* ===== 區域標題 ===== */
.section-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.section-title::before {
  content: '';
  width: 4px;
  height: 18px;
  background: var(--accent-primary);
  border-radius: 2px;
}

.subsection-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 12px;
}

/* ===== 設置區域 ===== */
.settings-section {
  padding: 24px;
  border-bottom: 1px solid var(--border-color);
}

.input-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 16px;
}

.input-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.input-group label {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
}

.input-group input {
  padding: 10px 12px;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 14px;
  transition: var(--transition);
}

.input-group input:focus {
  outline: none;
  border-color: var(--accent-primary);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
}

/* ===== 預覽區域 ===== */
.preview-section {
  padding: 24px;
  border-bottom: 1px solid var(--border-color);
}

.source-preview {
  width: 100%;
  max-height: 200px;
  background: var(--bg-primary);
  border: 2px dashed var(--border-color);
  border-radius: var(--radius-md);
  object-fit: contain;
}

/* ===== 結果區域 ===== */
.result-section {
  padding: 24px;
  border-bottom: 1px solid var(--border-color);
  background: rgba(15, 23, 42, 0.5);
}

.result-layout {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 32px;
  align-items: start;
}

.crop-preview canvas {
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  background: var(--bg-primary);
  max-height: 100px;
}

.ocr-results {
  display: grid;
  gap: 12px;
}

.result-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--bg-secondary);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-color);
}

.result-label {
  font-size: 13px;
  color: var(--text-secondary);
  font-weight: 500;
}

.result-value {
  font-size: 15px;
  font-weight: 600;
  font-family: 'Consolas', monospace;
}

.value-success { color: var(--accent-success); }
.value-info { color: var(--accent-primary); }
.value-warning { color: var(--accent-warning); }
.value-accent { color: #f472b6; }

/* ===== 統計信息卡片區 ===== */
.stats-section {
  padding: 24px;
  border-bottom: 1px solid var(--border-color);
  background: rgba(15, 23, 42, 0.3);
}

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

.stat-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  overflow: hidden;
  transition: var(--transition);
}

.stat-card:hover {
  border-color: var(--accent-primary);
  box-shadow: var(--shadow-md);
}

.stat-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  background: rgba(59, 130, 246, 0.1);
  border-bottom: 1px solid var(--border-color);
}

.stat-icon {
  font-size: 18px;
}

.stat-title {
  flex: 1;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
}

.reset-btn {
  padding: 4px 10px;
  background: var(--accent-primary);
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: var(--transition);
}

.reset-btn:hover {
  background: #2563eb;
  transform: scale(1.05);
}

.stat-body {
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

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

.stat-label {
  font-size: 12px;
  color: var(--text-secondary);
  font-weight: 500;
}

.stat-value {
  font-size: 13px;
  color: var(--text-primary);
  font-weight: 600;
  font-family: 'Consolas', monospace;
  text-align: right;
}

.stat-value.highlight {
  color: var(--accent-success);
}

/* 卡片特定顏色 */
.status-card .stat-header {
  background: rgba(59, 130, 246, 0.1);
}

.exp-card .stat-header {
  background: rgba(245, 158, 11, 0.1);
}

.rate-card .stat-header {
  background: rgba(16, 185, 129, 0.1);
}

.time-card .stat-header {
  background: rgba(139, 92, 246, 0.1);
}

/* 響應式：統計卡片 */
@media (max-width: 1024px) {
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

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

/* ===== 日誌區域 ===== */
.log-section {
  padding: 24px;
}

.log-container {
  height: 280px;
  overflow-y: auto;
  background: #020617;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 16px;
  font-family: 'Consolas', 'Monaco', monospace;
  font-size: 13px;
  line-height: 1.7;
  color: #cbd5e1;
}

/* ===== 頁腳提示 ===== */
.app-footer {
  margin-top: 24px;
}

.notice-box {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  padding: 20px 24px;
  background: linear-gradient(135deg, rgba(245, 158, 11, 0.1), rgba(245, 158, 11, 0.05));
  border: 1px solid rgba(245, 158, 11, 0.3);
  border-radius: var(--radius-md);
}

.notice-icon {
  font-size: 24px;
  flex-shrink: 0;
}

.notice-content {
  flex: 1;
}

.notice-content strong {
  display: block;
  margin-bottom: 8px;
  color: var(--accent-warning);
  font-size: 15px;
}

.notice-content ul {
  margin: 0;
  padding-left: 20px;
  color: var(--text-secondary);
  font-size: 14px;
}

.notice-content li {
  margin: 4px 0;
}

/* ===== 放大選區彈窗 ===== */
.zoom-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.92);
  z-index: 9999;
  display: none;
  overflow: hidden;
}

.zoom-container {
  width: 100%;
  height: 100%;
  position: relative;
}

#zoomCanvas {
  position: absolute;
  top: 0;
  left: 0;
  cursor: crosshair;
}

#zoomSelectBox {
  position: absolute;
  border: 3px solid #ef4444;
  background: rgba(239, 68, 68, 0.15);
  pointer-events: none;
  display: none;
  z-index: 99999;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.5);
}

.zoom-close-btn {
  position: fixed;
  top: 24px;
  right: 24px;
  z-index: 100000;
  padding: 12px 24px;
  background: rgba(239, 68, 68, 0.9);
  color: white;
  border: none;
  border-radius: var(--radius-md);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: var(--transition);
  backdrop-filter: blur(10px);
}

.zoom-close-btn:hover {
  background: var(--accent-danger);
  transform: scale(1.05);
}

.zoom-tip {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  color: white;
  font-size: 14px;
  background: rgba(0, 0, 0, 0.7);
  padding: 12px 24px;
  border-radius: 24px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

/* ===== 響應式設計 ===== */
@media (max-width: 768px) {
  body {
    padding: 12px;
  }

  .app-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }

  .app-header h1 {
    font-size: 22px;
  }

  .control-section {
    flex-direction: column;
    align-items: stretch;
  }

  .btn-group {
    justify-content: center;
  }

  .btn {
    flex: 1;
    justify-content: center;
  }

  .toggle-switch {
    margin-left: 0;
    justify-content: center;
    width: 100%;
  }

  .input-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .timer-inputs {
    grid-template-columns: 1fr;
  }

  .result-layout {
    grid-template-columns: 1fr;
    gap: 24px;
  }

  .crop-preview {
    text-align: center;
  }

  .crop-preview canvas {
    max-width: 100%;
  }
}

/* ===== 定時提醒區域 ===== */
.timer-reminder-section {
  padding: 24px;
  border-bottom: 1px solid var(--border-color);
  background: linear-gradient(180deg, rgba(139, 92, 246, 0.05), transparent);
}

.timer-reminder-content {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.timer-row {
  display: flex;
  flex-wrap: wrap;
  gap: 24px;
  align-items: center;
}

.timer-row .toggle-switch {
  margin-left: 0;
}

.timer-inputs {
  display: grid;
  grid-template-columns: 160px 1fr;
  gap: 16px;
  align-items: start;
}

.timer-text-group {
  flex: 1;
}

.timer-text-group input {
  width: 100%;
}

.timer-status {
  padding: 10px 16px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  font-size: 13px;
  color: var(--text-secondary);
  font-weight: 500;
}

.timer-status.active {
  background: rgba(16, 185, 129, 0.15);
  border-color: var(--accent-success);
  color: var(--accent-success);
}

.timer-status.error {
  background: rgba(239, 68, 68, 0.15);
  border-color: var(--accent-danger);
  color: var(--accent-danger);
}

.timer-test {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.timer-hint {
  font-size: 12px;
  color: var(--text-muted);
  font-style: italic;
}

/* 語速控制 */
.timer-rate-control {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 16px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
}

.timer-rate-control label {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: 8px;
}

#timerRateValue {
  color: var(--accent-primary);
  font-weight: 600;
  font-family: 'Consolas', monospace;
}

.timer-rate-control input[type="range"] {
  width: 100%;
  height: 8px;
  -webkit-appearance: none;
  appearance: none;
  background: var(--bg-tertiary);
  border-radius: 4px;
  outline: none;
  cursor: pointer;
}

.timer-rate-control input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 20px;
  height: 20px;
  background: linear-gradient(135deg, var(--accent-primary), #2563eb);
  border-radius: 50%;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: var(--transition);
}

.timer-rate-control input[type="range"]::-webkit-slider-thumb:hover {
  transform: scale(1.2);
  box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.3);
}

.timer-rate-control input[type="range"]::-moz-range-thumb {
  width: 20px;
  height: 20px;
  background: linear-gradient(135deg, var(--accent-primary), #2563eb);
  border-radius: 50%;
  cursor: pointer;
  border: none;
  box-shadow: var(--shadow-sm);
}

.rate-labels {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  color: var(--text-muted);
  padding: 0 4px;
}

/* Number 輸入框美化 */
input[type="number"] {
  -moz-appearance: textfield;
  appearance: textfield;
}

input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.input-group {
  position: relative;
}

.input-group input[type="number"] {
  padding-right: 32px;
}

.number-controls {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.number-btn {
  width: 18px;
  height: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-radius: 3px;
  color: var(--text-secondary);
  font-size: 10px;
  cursor: pointer;
  transition: var(--transition);
  user-select: none;
}

.number-btn:hover {
  background: var(--accent-primary);
  border-color: var(--accent-primary);
  color: white;
}

.number-btn:active {
  transform: scale(0.95);
}

/* ===== 動畫效果 ===== */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.main-panel {
  animation: fadeIn 0.3s ease;
}

/* ===== 舊版兼容樣式（保持功能不變） ===== */
#log {
  white-space: pre-wrap;
}

#previewCanvas {
  max-width: 100%;
  height: auto;
}