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

:root {
  --primary: #5C6BC0;
  --primary-dark: #3949AB;
  --accent: #FF7043;
  --bg: #F0F4FF;
  --surface: #fff;
  --border: #E0E7FF;
  --text: #1A237E;
  --text-muted: #6E7CB8;
  --radius: 14px;
  --shadow: 0 4px 24px rgba(92,107,192,0.12);
  --highlight-color: rgba(255,220,50,0.55);
}

body { background: var(--bg); font-family: 'Inter', sans-serif; color: var(--text); min-height: 100vh; }

#cbn-app { max-width: 1100px; margin: 0 auto; padding: 16px; }

/* ── SCREENS ── */
.screen { display: none; }
.screen.active { display: block; }

/* ── HEADER ── */
.cbn-header {
  display: flex; align-items: center; gap: 12px;
  margin-bottom: 20px; flex-wrap: wrap;
}
.cbn-header h1 { font-size: 22px; font-weight: 800; color: var(--text); flex: 1; }
.cbn-back-btn {
  display: flex; align-items: center; gap: 6px;
  background: none; border: 2px solid var(--border);
  border-radius: 10px; padding: 7px 14px;
  font-size: 14px; font-weight: 600; color: var(--text-muted);
  cursor: pointer; transition: all .18s;
}
.cbn-back-btn:hover { border-color: var(--primary); color: var(--primary); background: var(--border); }

/* ── TEMPLATE SELECT SCREEN ── */
.select-title { font-size: 26px; font-weight: 800; margin-bottom: 6px; }
.select-subtitle { color: var(--text-muted); font-size: 15px; margin-bottom: 24px; }

.template-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 18px;
}

.template-card {
  background: var(--surface); border-radius: var(--radius);
  border: 2px solid var(--border); overflow: hidden;
  cursor: pointer; transition: all .2s;
  display: flex; flex-direction: column;
}
.template-card:hover { border-color: var(--primary); box-shadow: var(--shadow); transform: translateY(-3px); }

.template-preview {
  background: #f8faff; aspect-ratio: 4/3;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden; padding: 8px;
}
.template-preview svg { width: 100%; height: 100%; }

.template-info { padding: 12px 14px; }
.template-name { font-size: 15px; font-weight: 700; margin-bottom: 4px; }
.template-meta { display: flex; gap: 8px; align-items: center; }
.difficulty-badge {
  font-size: 11px; font-weight: 600; padding: 2px 8px;
  border-radius: 20px; text-transform: uppercase; letter-spacing: .4px;
}
.difficulty-badge.oson { background: #E8F5E9; color: #2E7D32; }
.difficulty-badge[data-diff="o'rta"] { background: #FFF3E0; color: #E65100; }
.difficulty-badge.qiyin { background: #FCE4EC; color: #C62828; }
.region-count { font-size: 12px; color: var(--text-muted); }

/* ── WORKSPACE ── */
.workspace {
  display: grid;
  grid-template-columns: 1fr 260px;
  gap: 16px;
  align-items: start;
}

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

/* ── CANVAS AREA ── */
.canvas-area {
  background: var(--surface); border-radius: var(--radius);
  border: 2px solid var(--border); overflow: hidden;
  position: relative;
}

.svg-viewport {
  overflow: hidden; cursor: grab; user-select: none;
  touch-action: none;
  background: repeating-conic-gradient(#e8ecf5 0% 25%, #f4f6fb 0% 50%) 0 0 / 24px 24px;
  min-height: 420px;
  height: clamp(420px, 60vh, 680px);
  position: relative;
}
.svg-viewport.grabbing { cursor: grabbing; }

.svg-wrapper {
  transform-origin: 0 0;
  transition: none;
  will-change: transform;
  display: inline-block;
  line-height: 0;
  width: 100%;
}

.cbn-svg {
  display: block;
  width: 100%;
  height: auto;
}

.cbn-svg .region {
  stroke: #333;
  stroke-width: 1.5;
  fill: white;
  cursor: pointer;
  transition: filter .15s;
}

.cbn-svg .region:hover { filter: brightness(0.92); }

.cbn-svg .region.filled { cursor: default; }
.cbn-svg .region.filled:hover { filter: none; }

.cbn-svg .region.highlighted {
  filter: drop-shadow(0 0 4px rgba(255,200,0,0.9));
  stroke: #F9A825;
  stroke-width: 2.5;
}

.cbn-svg .region.shake {
  animation: regionShake .4s ease;
}

@keyframes regionShake {
  0%,100% { transform: translateX(0); }
  20% { transform: translateX(-5px); }
  40% { transform: translateX(5px); }
  60% { transform: translateX(-3px); }
  80% { transform: translateX(3px); }
}

.zoom-bar {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  background: #fafbff;
}
.zoom-btn {
  width: 32px; height: 32px; border-radius: 8px;
  border: 1.5px solid var(--border); background: var(--surface);
  font-size: 18px; font-weight: 700; color: var(--text);
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  transition: all .15s;
}
.zoom-btn:hover { background: var(--border); border-color: var(--primary); }
.zoom-label { font-size: 12px; color: var(--text-muted); margin-left: auto; }

/* ── SIDEBAR ── */
.sidebar {
  display: flex; flex-direction: column; gap: 14px;
}

.sidebar-card {
  background: var(--surface); border-radius: var(--radius);
  border: 2px solid var(--border); padding: 14px;
}

.card-title {
  font-size: 12px; font-weight: 700; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: .5px; margin-bottom: 10px;
}

/* Progress */
.progress-bar-wrap {
  background: #E8ECFF; border-radius: 100px; height: 10px;
  overflow: hidden; margin-bottom: 6px;
}
.progress-bar-fill {
  height: 100%; background: linear-gradient(90deg, var(--primary), #7986CB);
  border-radius: 100px; transition: width .4s ease;
  width: 0%;
}
.progress-text { font-size: 13px; color: var(--text-muted); }
.progress-text strong { color: var(--text); }

/* Palette */
.palette-grid {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px;
}

.palette-item {
  display: flex; flex-direction: column; align-items: center;
  gap: 4px; cursor: pointer; padding: 6px 4px; border-radius: 10px;
  border: 2px solid transparent; transition: all .18s;
}
.palette-item:hover { background: var(--bg); border-color: var(--border); }
.palette-item.active { border-color: var(--primary); background: var(--border); }

.palette-swatch {
  width: 38px; height: 38px; border-radius: 50%;
  border: 2px solid rgba(0,0,0,0.12);
  display: flex; align-items: center; justify-content: center;
  font-size: 15px; font-weight: 800; color: #fff;
  text-shadow: 0 1px 3px rgba(0,0,0,.4);
  transition: transform .15s;
  position: relative;
}
.palette-item.active .palette-swatch { transform: scale(1.15); box-shadow: 0 0 0 3px var(--primary); }
.palette-item.completed .palette-swatch::after {
  content: '✓';
  position: absolute; inset: 0;
  background: rgba(255,255,255,0.5);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; font-weight: 900; color: #333;
  text-shadow: none;
}
.palette-label { font-size: 10px; color: var(--text-muted); text-align: center; line-height: 1.2; }

/* Remaining info */
.remaining-info { font-size: 13px; color: var(--text-muted); min-height: 20px; }
.remaining-info span { font-weight: 700; color: var(--primary); }

/* Buttons */
.btn {
  padding: 10px 16px; border-radius: 10px; font-size: 14px; font-weight: 600;
  cursor: pointer; border: none; transition: all .18s; width: 100%;
  display: flex; align-items: center; justify-content: center; gap: 6px;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-outline { background: none; border: 2px solid var(--border); color: var(--text-muted); }
.btn-outline:hover { border-color: var(--primary); color: var(--primary); }
.btn-danger { background: none; border: 2px solid #FFCDD2; color: #C62828; }
.btn-danger:hover { background: #FFEBEE; }

/* ── TOAST ── */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(80px);
  background: #263238; color: #fff; padding: 10px 20px; border-radius: 100px;
  font-size: 14px; font-weight: 500; z-index: 9999;
  transition: transform .25s ease, opacity .25s ease;
  opacity: 0; pointer-events: none; white-space: nowrap;
  max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { transform: translateX(-50%) translateY(0); opacity: 1; }

/* ── COMPLETION CARD (inline, below workspace) ── */
.completion-card {
  margin-top: 20px;
  background: linear-gradient(135deg, #EEF2FF 0%, #FAF5FF 100%);
  border: 2px solid #C7D2FE;
  border-radius: 20px;
  overflow: hidden;
  animation: completionSlide .4s ease;
}
@keyframes completionSlide {
  from { opacity: 0; transform: translateY(20px); }
  to   { opacity: 1; transform: translateY(0); }
}
.completion-inner {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0;
  min-height: 280px;
}
.completion-left {
  background: #fff;
  border-right: 2px solid #C7D2FE;
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
  overflow: hidden;
}
.completion-left svg { width: 100%; height: auto; max-height: 340px; display: block; }

.completion-right {
  padding: 32px 28px;
  display: flex; flex-direction: column; justify-content: center; gap: 12px;
}
.completion-emoji { font-size: 52px; line-height: 1; }
.completion-title { font-size: 26px; font-weight: 800; color: var(--text); }
.completion-sub { color: var(--text-muted); font-size: 15px; line-height: 1.5; }
.completion-actions { display: flex; flex-direction: column; gap: 10px; margin-top: 8px; }

/* ── HELPER MODE ── */
.helper-toggle {
  display: flex; align-items: center; gap: 8px; cursor: pointer;
  font-size: 13px; color: var(--text-muted);
}
.helper-toggle input { accent-color: var(--primary); width: 16px; height: 16px; cursor: pointer; }

/* ── RESPONSIVE ── */
@media (max-width: 600px) {
  .completion-inner { grid-template-columns: 1fr; }
  .completion-left { border-right: none; border-bottom: 2px solid #C7D2FE; max-height: 220px; }
  .completion-right { padding: 20px 18px; }
}
@media (max-width: 480px) {
  #cbn-app { padding: 10px; }
  .select-title { font-size: 20px; }
  .palette-grid { grid-template-columns: repeat(3, 1fr); gap: 6px; }
  .palette-swatch { width: 32px; height: 32px; font-size: 13px; }
}
