/* =============================================
   C++ 编程游戏 - 全局样式 (Scratch 风格配色)
   ============================================= */

/* --- CSS 变量 / 色板 --- */
:root {
  --scratch-orange:  #ff8c1a;
  --scratch-yellow:  #ffab19;
  --scratch-purple:  #9966ff;
  --scratch-blue:    #4c97ff;
  --scratch-green:   #59c059;
  --scratch-teal:    #5cb1d6;
  --scratch-red:     #ff6680;
  --scratch-bg:      #e6f0ff;
  --scratch-panel:   #ffffff;
  --scratch-dark:    #2a3f5f;
  --scratch-gray:    #f8f9fa;
  --scratch-border:  #d0ddf5;
  --font-main:       'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  --radius:          14px;
  --shadow:          0 4px 18px rgba(76,151,255,.18);
}

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

body {
  font-family: var(--font-main);
  background: var(--scratch-bg);
  color: var(--scratch-dark);
  min-height: 100vh;
}

a { text-decoration: none; color: inherit; }

/* --- 顶部导航 --- */
.navbar {
  background: var(--scratch-purple);
  padding: 0 32px;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 3px 12px rgba(0,0,0,.18);
}

.navbar .logo {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 1.4rem;
  font-weight: 800;
  color: #fff;
}

.navbar .logo-icon {
  width: 40px; height: 40px;
  background: var(--scratch-yellow);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.3rem;
}

.navbar .nav-back {
  color: #fff;
  font-size: .95rem;
  display: flex; align-items: center; gap: 6px;
  opacity: .85;
  transition: opacity .2s;
}
.navbar .nav-back:hover { opacity: 1; }

/* --- 首页英雄区 --- */
.hero {
  text-align: center;
  padding: 60px 20px 30px;
}
.hero h1 {
  font-size: 2.2rem;
  font-weight: 900;
  color: var(--scratch-purple);
  line-height: 1.2;
  margin-bottom: 12px;
}
.hero p {
  font-size: 1.1rem;
  color: #5566aa;
  max-width: 520px;
  margin: 0 auto;
}

/* --- 章节卡片网格 --- */
.chapters {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 28px;
  max-width: 900px;
  margin: 40px auto;
  padding: 0 28px;
}

.chapter-card {
  background: var(--scratch-panel);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 32px 28px;
  cursor: pointer;
  transition: transform .22s, box-shadow .22s;
  border: 3px solid transparent;
  position: relative;
  overflow: hidden;
}
.chapter-card::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 6px;
  border-radius: var(--radius) var(--radius) 0 0;
}
.chapter-card.ch1::before { background: var(--scratch-orange); }
.chapter-card.ch2::before { background: var(--scratch-green); }
.chapter-card.ch3::before { background: var(--scratch-blue); }
.chapter-card.ch4::before { background: var(--scratch-red); }

.chapter-card:hover {
  transform: translateY(-6px);
  box-shadow: 0 10px 32px rgba(76,151,255,.28);
}

.chapter-num {
  font-size: .82rem;
  font-weight: 700;
  letter-spacing: .06em;
  text-transform: uppercase;
  margin-bottom: 10px;
}
.ch1 .chapter-num { color: var(--scratch-orange); }
.ch2 .chapter-num { color: var(--scratch-green); }
.ch3 .chapter-num { color: var(--scratch-blue); }
.ch4 .chapter-num { color: var(--scratch-red); }

.chapter-title {
  font-size: 1.4rem;
  font-weight: 800;
  margin-bottom: 8px;
  color: var(--scratch-dark);
}

.chapter-desc {
  font-size: .95rem;
  color: #7788aa;
  line-height: 1.55;
  margin-bottom: 20px;
}

.chapter-badge {
  display: inline-block;
  padding: 4px 12px;
  border-radius: 20px;
  font-size: .82rem;
  font-weight: 700;
  margin-bottom: 12px;
}
.ch1 .chapter-badge { background: #fff3e0; color: var(--scratch-orange); }
.ch2 .chapter-badge { background: #e8f5e9; color: var(--scratch-green); }
.ch3 .chapter-badge { background: #e3f2fd; color: var(--scratch-blue); }
.ch4 .chapter-badge { background: #fce4ec; color: var(--scratch-red); }

.chapter-icon {
  font-size: 3rem;
  margin-bottom: 16px;
  display: block;
}

.btn-start {
  display: inline-block;
  padding: 11px 28px;
  border-radius: 30px;
  font-size: 1rem;
  font-weight: 700;
  color: #fff;
  border: none;
  cursor: pointer;
  transition: filter .18s, transform .15s;
}
.btn-start:hover { filter: brightness(1.1); transform: scale(1.04); }
.ch1 .btn-start { background: var(--scratch-orange); }
.ch2 .btn-start { background: var(--scratch-green); }
.ch3 .btn-start { background: var(--scratch-blue); }
.ch4 .btn-start { background: var(--scratch-red); }
.chapter-card.ch5::before { background: #9c27b0; }
.ch5 .chapter-num { color: #9c27b0; }
.ch5 .chapter-badge { background: #f3e5f5; color: #9c27b0; }
.ch5 .btn-start { background: #9c27b0; }

/* =============================================
   第五章专属：变量收纳师 — 紫色系
   ============================================= */
.ch5 .story-box     { border-left-color: #ce93d8; }
.ch5 .code-panel    { border: 1px solid rgba(156,39,176,0.25); }
.ch5 .canvas-panel  { border: 1px solid rgba(156,39,176,0.2);  }

/* =============================================
   第六章专属：变量改造工坊 — 橙色系
   ============================================= */
.chapter-card.ch6::before { background: #ff9800; }
.ch6 .chapter-num { color: #ff9800; }
.ch6 .chapter-badge { background: #fff3e0; color: #ff9800; }
.ch6 .btn-start { background: #ff9800; }

/* =============================================
   第六章专属：变量改造工坊 — 橙色系（游戏页面）
   ============================================= */
.ch6 .story-box     { border-left-color: #ffb74d; }
.ch6 .code-panel    { border: 1px solid rgba(255,152,0,0.25); }
.ch6 .canvas-panel  { border: 1px solid rgba(255,152,0,0.2); }

/* =============================================
   第七章卡片 — 青色系（首页）
   ============================================= */
.chapter-card.ch7::before { background: #00acc1; }
.ch7 .chapter-num { color: #00acc1; }
.ch7 .chapter-badge { background: #e0f7fa; color: #00acc1; }
.ch7 .btn-start { background: #00acc1; }

/* =============================================
   第七章专属：变量加工厂 — 青色系（游戏页面）
   ============================================= */
.ch7 .story-box     { border-left-color: #4dd0e1; }
.ch7 .code-panel    { border: 1px solid rgba(0,172,193,0.25); }
.ch7 .canvas-panel  { border: 1px solid rgba(0,172,193,0.2); }

/* =============================================
   第八章卡片 — 橙红色系（首页）
   ============================================= */
.chapter-card.ch8::before { background: #f4511e; }
.ch8 .chapter-num { color: #f4511e; }
.ch8 .chapter-badge { background: #fbe9e7; color: #f4511e; }
.ch8 .btn-start { background: #f4511e; }

/* =============================================
   第八章专属：条件判断 — 橙红色系（游戏页面）
   ============================================= */
.ch8 .story-box     { border-left-color: #ff7043; }
.ch8 .code-panel    { border: 1px solid rgba(244,81,30,0.25); }
.ch8 .canvas-panel  { border: 1px solid rgba(244,81,30,0.2); }

/* =============================================
   第九章卡片 — 青绿色系（首页）
   ============================================= */
.chapter-card.ch9::before { background: #00796b; }
.ch9 .chapter-num { color: #00796b; }
.ch9 .chapter-badge { background: #e0f2f1; color: #00796b; }
.ch9 .btn-start { background: #00796b; }

/* =============================================
   第九章专属：for 循环 — 青绿色系（游戏页面）
   ============================================= */
.ch9 .story-box     { border-left-color: #26a69a; }
.ch9 .code-panel    { border: 1px solid rgba(0,121,107,0.25); }
.ch9 .canvas-panel  { border: 1px solid rgba(0,121,107,0.2); }

/* =============================================
   游戏页面通用
   ============================================= */

.game-layout {
  display: flex;
  flex-direction: column;
  min-height: calc(100vh - 64px);
}

.game-content {
  display: flex;
  gap: 0;
  flex: 1;
  min-height: 0;           /* 允许 flex 子元素收缩，height:100% 才有效 */
}

/* --- 左侧代码面板 --- */
.code-panel {
  width: 420px;
  min-width: 340px;
  background: #1e2d40;
  padding: 24px 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-width: thin;
  scrollbar-color: rgba(255,255,255,.15) transparent;
}

.code-panel::-webkit-scrollbar {
  width: 6px;
}
.code-panel::-webkit-scrollbar-track {
  background: transparent;
}
.code-panel::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,.15);
  border-radius: 3px;
}

.code-panel h2 {
  color: #fff;
  font-size: 1.05rem;
  font-weight: 700;
  display: flex; align-items: center; gap: 8px;
}

.story-box {
  background: rgba(255,255,255,.07);
  border-radius: 10px;
  padding: 14px 16px;
  color: #b0c8ff;
  font-size: 1rem;
  line-height: 1.6;
  border-left: 4px solid var(--scratch-blue);
}

.task-box {
  background: rgba(255,255,255,.07);
  border-radius: 10px;
  padding: 14px 16px;
  color: #ffe7a0;
  font-size: .92rem;
  line-height: 1.6;
  border-left: 4px solid var(--scratch-yellow);
}

.code-editor-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.code-editor-wrap label {
  color: #8899cc;
  font-size: .82rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .06em;
}

textarea.code-input {
  flex: 1;
  min-height: 200px;
  background: #11192a;
  color: #e0edff;
  border: 2px solid #2e4060;
  border-radius: 10px;
  padding: 14px 16px;
  font-family: 'Consolas', 'Courier New', monospace;
  font-size: .95rem;
  line-height: 1.7;
  resize: vertical;
  outline: none;
  transition: border-color .2s;
  tab-size: 4;
}
textarea.code-input:focus { border-color: var(--scratch-blue); }

.btn-run {
  padding: 13px 0;
  border-radius: 10px;
  background: var(--scratch-blue);
  color: #fff;
  font-size: 0.8rem;
  font-weight: 700;
  border: none;
  cursor: pointer;
  transition: filter .18s;
  display: flex; align-items: center; justify-content: center; gap: 8px;
}
.btn-run:hover { filter: brightness(1.12); }
.btn-run:active { filter: brightness(.95); }

/* --- 右侧游戏画布区 --- */
.canvas-panel {
  flex: 1;
  background: #1a2b44;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  gap: 16px;
  min-width: 0;            /* 防止被 canvas 撑开 */
}

/* --- 桌面端锁定视口，左侧独立滚动（仅游戏页） --- */
@media (min-width: 769px) {
  body.game-page { overflow: hidden; }
  .game-layout {
    height: calc(100vh - 64px);
    min-height: unset;
  }
  .game-content {
    height: 100%;
    overflow: hidden;
  }
  .code-panel { height: 100%; }
  .canvas-panel { overflow: hidden; }
}

#game-canvas {
  border-radius: 16px;
  box-shadow: 0 6px 30px rgba(0,0,0,.4);
  display: block;
}

/* 关卡进度 */
.level-bar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: center;
}
.level-dot {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: rgba(255,255,255,.12);
  border: 2.5px solid rgba(255,255,255,.25);
  display: flex; align-items: center; justify-content: center;
  font-size: .8rem;
  color: #8899cc;
  font-weight: 700;
  transition: all .3s;
}
.level-dot.done {
  background: var(--scratch-green);
  border-color: var(--scratch-green);
  color: #fff;
}
.level-dot.active {
  background: var(--scratch-yellow);
  border-color: var(--scratch-yellow);
  color: #333;
  transform: scale(1.15);
}

/* --- Toast 提示 --- */
.toast {
  position: fixed;
  bottom: 40px; left: 50%;
  transform: translateX(-50%) translateY(30px);
  background: #1e2d40;
  color: #fff;
  padding: 14px 28px;
  border-radius: 30px;
  font-size: 1rem;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity .3s, transform .3s;
  z-index: 999;
  box-shadow: 0 4px 20px rgba(0,0,0,.3);
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}
.toast.success { border-left: 5px solid var(--scratch-green); }
.toast.error   { border-left: 5px solid var(--scratch-red); }

/* --- 模态框 --- */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.55);
  display: flex; align-items: center; justify-content: center;
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
  transition: opacity .3s;
}
.modal-overlay.show { opacity: 1; pointer-events: auto; }

.modal {
  background: #fff;
  border-radius: 20px;
  padding: 40px 36px;
  max-width: 420px;
  width: 90%;
  text-align: center;
  box-shadow: 0 10px 50px rgba(0,0,0,.3);
  transform: scale(.9);
  transition: transform .3s;
}
.modal-overlay.show .modal { transform: scale(1); }

.modal .modal-icon { font-size: 4rem; margin-bottom: 16px; display: block; }
.modal h2 { font-size: 1.6rem; font-weight: 900; margin-bottom: 10px; color: var(--scratch-dark); }
.modal p  { color: #667; font-size: 1rem; margin-bottom: 24px; line-height: 1.5; }

.modal-btns { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; }

.btn-modal {
  padding: 12px 28px;
  border-radius: 30px;
  border: none;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  transition: filter .18s;
}
.btn-modal:hover { filter: brightness(1.1); }
.btn-modal.primary { background: var(--scratch-blue); color: #fff; }
.btn-modal.success { background: var(--scratch-green); color: #fff; }
.btn-modal.secondary { background: #eef; color: var(--scratch-dark); }

/* --- 进度条 (第二章) --- */
.progress-track {
  width: 100%;
  max-width: 500px;
}
.progress-track .progress-label {
  color: #8899cc;
  font-size: .85rem;
  margin-bottom: 6px;
}
.progress-bar-wrap {
  background: rgba(255,255,255,.1);
  border-radius: 20px;
  height: 14px;
  overflow: hidden;
}
.progress-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--scratch-blue), var(--scratch-green));
  border-radius: 20px;
  transition: width .4s ease;
  width: 0%;
}

/* --- 响应式 --- */
@media (max-width: 768px) {
  body { overflow: auto; }
  .game-content { flex-direction: column; height: auto; overflow: visible; }
  .code-panel { width: 100%; min-width: unset; height: auto; overflow-y: visible; }
  .canvas-panel { height: auto; overflow: visible; }
  #game-canvas { width: 100%; height: auto; }
}
