:root {
  --bg-primary: #0f0f1a;
  --bg-secondary: #1a1a2e;
  --bg-tertiary: #16213e;
  --bg-card: #1e2a4a;
  --text-primary: #e0e0e0;
  --text-secondary: #a0a0b0;
  --accent: #4a9eff;
  --accent-hover: #6bb5ff;
  --green: #2ecc71;
  --red: #e74c3c;
  --orange: #f39c12;
  --border: #2a2a4a;
  --suit-red: #e74c3c;
  --suit-black: #2c3e50;
  --card-bg: #fafafa;
  --card-shadow: 0 2px 8px rgba(0,0,0,0.3);
  --radius: 8px;
  --transition: 0.2s ease;
}

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

body {
  font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  user-select: none;
}

.app { display: flex; flex-direction: column; min-height: 100vh; }

/* Tabs */
.tab-bar {
  display: flex;
  background: var(--bg-secondary);
  border-bottom: 2px solid var(--border);
  padding: 0 16px;
}
.tab {
  padding: 12px 24px;
  cursor: pointer;
  color: var(--text-secondary);
  border-bottom: 3px solid transparent;
  transition: var(--transition);
  font-size: 14px;
  font-weight: 500;
}
.tab:hover { color: var(--text-primary); background: var(--bg-tertiary); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }

/* Panels */
.panel { display: none; padding: 20px; flex: 1; }
.panel.active { display: block; }

/* Layout */
.equity-layout { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.replay-layout { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }

/* Cards */
.card {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 48px; height: 68px;
  background: var(--card-bg);
  border-radius: 6px;
  font-size: 18px;
  font-weight: bold;
  cursor: pointer;
  box-shadow: var(--card-shadow);
  transition: var(--transition);
  position: relative;
}
.card:hover { transform: translateY(-2px); }
.card.red { color: var(--suit-red); }
.card.black { color: var(--suit-black); }
.card.selected { outline: 3px solid var(--accent); transform: translateY(-2px); }
.card.empty { background: var(--bg-tertiary); border: 2px dashed var(--border); color: var(--text-secondary); font-size: 24px; }
.card.back { background: linear-gradient(135deg, #4a148c, #7b1fa2); color: #fff; font-size: 12px; }
.card.small { width: 36px; height: 50px; font-size: 14px; }
.card.large { width: 60px; height: 84px; font-size: 22px; }

.card-suit { font-size: 0.85em; margin-left: 1px; }

/* Card Picker Grid */
.card-picker {
  display: grid;
  grid-template-columns: repeat(13, 1fr);
  gap: 4px;
  padding: 12px;
  background: var(--bg-tertiary);
  border-radius: var(--radius);
}
.card-picker .card { width: 100%; height: 40px; font-size: 14px; }
.card-picker .card.disabled { opacity: 0.3; cursor: not-allowed; }

/* Hand Range Grid (13x13) */
.range-grid {
  display: grid;
  grid-template-columns: repeat(13, 1fr);
  gap: 2px;
  padding: 8px;
  background: var(--bg-tertiary);
  border-radius: var(--radius);
}
.range-cell {
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  border-radius: 3px;
  background: var(--bg-card);
  color: var(--text-secondary);
  transition: var(--transition);
}
.range-cell:hover { transform: scale(1.1); z-index: 1; }
.range-cell.in-range { background: var(--accent); color: #fff; }
.range-cell.pair { border: 1px solid var(--orange); }

/* Board */
.board-area { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* Buttons */
.btn {
  padding: 10px 20px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: var(--transition);
}
.btn:hover { background: var(--accent-hover); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.secondary { background: var(--bg-tertiary); border: 1px solid var(--border); }
.btn.secondary:hover { background: var(--bg-card); border-color: var(--accent); }
.btn.danger { background: var(--red); }
.btn.success { background: var(--green); }

/* Section */
.section {
  background: var(--bg-secondary);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
  border: 1px solid var(--border);
}
.section-title {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 12px;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  gap: 8px;
}
.section-title .icon { font-size: 16px; }

/* Equity Results */
.equity-bar {
  height: 24px;
  border-radius: 4px;
  display: flex;
  overflow: hidden;
  margin: 8px 0;
}
.equity-bar .win-bar { background: var(--green); }
.equity-bar .tie-bar { background: var(--orange); }
.equity-bar .lose-bar { background: var(--red); }

.equity-result-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}
.equity-result-row:last-child { border-bottom: none; }

.player-label { font-size: 14px; font-weight: 600; min-width: 60px; }
.equity-value { font-size: 18px; font-weight: bold; color: var(--accent); }
.equity-detail { font-size: 12px; color: var(--text-secondary); }

/* Progress */
.progress-bar {
  width: 100%;
  height: 6px;
  background: var(--bg-tertiary);
  border-radius: 3px;
  overflow: hidden;
}
.progress-bar .fill {
  height: 100%;
  background: var(--accent);
  transition: width 0.1s ease;
}

/* AI Panel */
.ai-response {
  background: var(--bg-tertiary);
  border-radius: var(--radius);
  padding: 16px;
  white-space: pre-wrap;
  font-size: 14px;
  line-height: 1.8;
  color: var(--text-primary);
  max-height: 600px;
  overflow-y: auto;
}
.ai-response h1, .ai-response h2, .ai-response h3 { margin: 12px 0 8px; }
.ai-response code { background: var(--bg-primary); padding: 2px 6px; border-radius: 3px; font-size: 13px; }
.ai-response pre { background: var(--bg-primary); padding: 12px; border-radius: var(--radius); overflow-x: auto; }
.ai-response strong { color: var(--accent); }
.ai-response ul, .ai-response ol { padding-left: 20px; margin: 8px 0; }
.ai-response li { margin: 4px 0; }

.loading-spinner {
  display: inline-block;
  width: 16px; height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Settings */
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 13px; margin-bottom: 6px; color: var(--text-secondary); }
.form-group input, .form-group select, .form-group textarea {
  width: 100%;
  padding: 8px 12px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 14px;
  font-family: inherit;
}
.form-group input:focus, .form-group select:focus, .form-group textarea:focus {
  outline: none;
  border-color: var(--accent);
}
.form-group textarea { resize: vertical; min-height: 120px; }

/* Status badge */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 600;
}
.badge.connected { background: rgba(46,204,113,0.2); color: var(--green); }
.badge.disconnected { background: rgba(231,76,60,0.2); color: var(--red); }

/* Toast */
.toast {
  position: fixed;
  top: 20px; right: 20px;
  padding: 12px 20px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 14px;
  z-index: 1000;
  box-shadow: 0 4px 12px rgba(0,0,0,0.4);
  animation: slideIn 0.3s ease;
}
@keyframes slideIn { from { transform: translateX(100%); } to { transform: translateX(0); } }
.toast.error { border-color: var(--red); }
.toast.success { border-color: var(--green); }

/* Replay timeline */
.timeline {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 0;
}
.timeline-step {
  padding: 6px 14px;
  background: var(--bg-tertiary);
  border-radius: 20px;
  font-size: 12px;
  cursor: pointer;
  transition: var(--transition);
}
.timeline-step.active { background: var(--accent); color: #fff; }
.timeline-step.done { background: rgba(46,204,113,0.2); color: var(--green); }
.timeline-arrow { color: var(--text-secondary); }

/* Action log */
.action-log {
  background: var(--bg-tertiary);
  border-radius: var(--radius);
  padding: 12px;
  max-height: 200px;
  overflow-y: auto;
  font-size: 13px;
}
.action-log-entry {
  padding: 4px 0;
  border-bottom: 1px solid var(--border);
}
.action-log-entry:last-child { border-bottom: none; }

/* Vision Result */
.vision-result-card {
  background: var(--bg-tertiary);
  border-radius: var(--radius);
  padding: 12px;
  font-size: 14px;
  line-height: 1.8;
}
.vision-result-card div { padding: 2px 0; }

/* Compare Cards */
.compare-card {
  border-left: 3px solid var(--accent);
}
.compare-card .section-title { justify-content: space-between; }

/* Capture controls */
.capture-controls { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.capture-preview {
  background: var(--bg-tertiary);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  min-height: 200px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.capture-status {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: var(--text-secondary);
}
.capture-status .dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--red);
}
.capture-status.active .dot { background: var(--green); animation: pulse 1.5s infinite; }
.dot.pulsing { animation: pulse 1s infinite; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.4; } }

/* Model selector grid */
.model-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 12px; }

/* Pipeline */
.pipeline-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--bg-secondary);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  margin-bottom: 16px;
}
.pipeline-status-text {
  font-size: 14px;
  font-weight: 600;
  min-width: 120px;
}
.pipeline-dot {
  display: inline-block;
  width: 10px; height: 10px;
  border-radius: 50%;
  background: var(--text-secondary);
  margin-right: 6px;
}
.pipeline-dot.pulsing { background: var(--accent); animation: pulse 1s infinite; }

/* Capture canvas */
#capture-canvas {
  width: 100%;
  height: 300px;
  background: #1a1a2e;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  display: block;
}
#capture-canvas.crosshair { cursor: crosshair; }

/* Active button */
.btn.active { background: var(--orange); }

/* Pipeline controls */
.pipeline-controls { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.pipeline-controls input { width: 80px; }

/* Split layout */
.split-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.split-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 16px; }

/* Mini stat */
.mini-stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 8px;
  background: var(--bg-tertiary);
  border-radius: 6px;
}
.mini-stat .label { font-size: 11px; color: var(--text-secondary); }
.mini-stat .value { font-size: 18px; font-weight: bold; color: var(--accent); }
.action-log-entry .player { font-weight: 600; color: var(--accent); }
.action-log-entry .action { color: var(--text-secondary); }
.action-log-entry .amount { color: var(--orange); }

/* Responsive */
@media (max-width: 900px) {
  .equity-layout, .replay-layout { grid-template-columns: 1fr; }
  .card-picker { grid-template-columns: repeat(7, 1fr); }
}

/* ==================== Decision Popup ==================== */
.decision-popup-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10000;
  animation: fadeIn 0.2s ease;
}
.decision-popup-overlay.hidden { display: none; }

.decision-popup {
  background: var(--bg-primary);
  border: 2px solid var(--border);
  border-radius: 16px;
  padding: 0;
  width: 420px;
  max-width: 90%;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  animation: popIn 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  overflow: hidden;
}

.decision-popup-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.decision-popup-header .title {
  font-size: 16px;
  font-weight: 700;
  color: var(--text-primary);
}
.decision-popup-header .time {
  font-size: 12px;
  color: var(--text-secondary);
}
.decision-popup-header .close {
  cursor: pointer;
  color: var(--text-secondary);
  font-size: 20px;
  line-height: 1;
  padding: 4px 8px;
  border-radius: 4px;
  transition: all 0.2s;
}
.decision-popup-header .close:hover { background: var(--bg-tertiary); color: var(--red); }

.decision-popup-body { padding: 20px; }

.decision-action-box {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 20px;
  border-radius: 12px;
  margin-bottom: 16px;
}
.decision-action-icon {
  width: 64px; height: 64px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  font-weight: 900;
  color: #fff;
  flex-shrink: 0;
}
.decision-action-text { flex: 1; }
.decision-action-text .action-name {
  font-size: 24px;
  font-weight: 800;
}
.decision-action-text .action-confidence {
  font-size: 14px;
  color: var(--text-secondary);
  margin-top: 4px;
}
.decision-action-text .action-confidence .bar {
  display: inline-block;
  width: 80px;
  height: 6px;
  background: var(--bg-tertiary);
  border-radius: 3px;
  margin-left: 8px;
  vertical-align: middle;
  overflow: hidden;
}
.decision-action-text .action-confidence .bar-fill {
  height: 100%;
  border-radius: 3px;
  transition: width 0.5s ease;
}

.decision-detail-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 12px;
}
.decision-detail-item {
  display: flex;
  flex-direction: column;
  padding: 8px 12px;
  background: var(--bg-tertiary);
  border-radius: 8px;
}
.decision-detail-item .label {
  font-size: 11px;
  color: var(--text-secondary);
  margin-bottom: 2px;
}
.decision-detail-item .value {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
}

.decision-reason {
  padding: 12px;
  background: var(--bg-secondary);
  border-radius: 8px;
  font-size: 14px;
  color: var(--text-primary);
  line-height: 1.6;
  margin-bottom: 12px;
}

.decision-alternative {
  padding: 8px 12px;
  background: rgba(74, 158, 255, 0.1);
  border-left: 3px solid var(--accent);
  border-radius: 0 8px 8px 0;
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.5;
}

.decision-popup-footer {
  display: flex;
  gap: 8px;
  padding: 12px 20px;
  border-top: 1px solid var(--border);
  justify-content: flex-end;
}
.decision-popup-footer .btn { padding: 8px 20px; font-size: 14px; }

.decision-history-list {
  max-height: 120px;
  overflow-y: auto;
  padding: 8px 12px;
  background: var(--bg-tertiary);
  border-radius: 8px;
  margin-bottom: 12px;
}
.decision-history-entry {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
  font-size: 12px;
  border-bottom: 1px solid var(--border);
}
.decision-history-entry:last-child { border-bottom: none; }
.decision-history-entry .time { color: var(--text-secondary); }
.decision-history-entry .action-badge {
  padding: 2px 8px;
  border-radius: 4px;
  font-weight: 600;
  color: #fff;
  font-size: 11px;
}

.decision-toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  background: var(--bg-primary);
  border: 2px solid;
  border-radius: 12px;
  padding: 16px 20px;
  display: flex;
  align-items: center;
  gap: 12px;
  z-index: 9999;
  box-shadow: 0 8px 32px rgba(0,0,0,0.4);
  animation: slideInRight 0.3s ease;
  cursor: pointer;
  min-width: 200px;
}
.decision-toast .toast-icon {
  width: 40px; height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 900;
  color: #fff;
  flex-shrink: 0;
}
.decision-toast .toast-text { flex: 1; }
.decision-toast .toast-action {
  font-size: 18px;
  font-weight: 800;
}
.decision-toast .toast-model {
  font-size: 11px;
  color: var(--text-secondary);
}
.decision-toast .toast-close {
  cursor: pointer;
  color: var(--text-secondary);
  font-size: 16px;
}

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes popIn { from { transform: scale(0.8); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes slideInRight { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
@keyframes slideOutRight { from { transform: translateX(0); opacity: 1; } to { transform: translateX(120%); opacity: 0; } }
