/* ========== 全局基础样式 ========== */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: "PingFang SC", "Microsoft YaHei", "Noto Sans SC", sans-serif;
  background: linear-gradient(135deg, #a8edea 0%, #fed6e3 50%, #d4fc79 100%);
  background-attachment: fixed;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 16px;
  overflow-x: hidden;
}

.app {
  width: 100%;
  max-width: 600px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  position: relative;
}

/* ========== 图标通用样式 ========== */
.icon-title { width: 32px; height: 32px; vertical-align: middle; }
.icon-sm { width: 18px; height: 18px; vertical-align: -3px; margin-right: 3px; }
.icon-label { width: 20px; height: 20px; vertical-align: -3px; margin-right: 2px; }
.icon-btn { width: 18px; height: 18px; vertical-align: -3px; margin-right: 4px; }
.icon-placeholder { width: 22px; height: 22px; vertical-align: -4px; margin-right: 6px; opacity: 0.5; }
.icon-toggle { width: 20px; height: 20px; vertical-align: -4px; margin-right: 5px; }
.icon-backspace { width: 20px; height: 20px; vertical-align: middle; }

.footer-icon {
  width: 24px; height: 24px;
  display: inline-block;
  vertical-align: middle;
  animation: floatIcon 3s ease-in-out infinite;
}
.footer-icon:nth-child(odd) { animation-delay: -1s; }
.footer-icon:nth-child(even) { animation-delay: -2s; }

@keyframes floatIcon {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-4px); }
}

/* ========== 顶部标题 ========== */
.header {
  text-align: center;
  width: 100%;
}

.title-row {
  font-size: 26px;
  color: #e74c3c;
  text-shadow: 2px 2px 0 #fff, 3px 3px 0 rgba(0,0,0,0.05);
  letter-spacing: 4px;
  margin-bottom: 8px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.score-board {
  display: flex;
  justify-content: center;
  gap: 16px;
  font-size: 15px;
  color: #6c3483;
  font-weight: 600;
}

.score-item {
  background: rgba(255,255,255,0.75);
  padding: 5px 14px;
  border-radius: 20px;
  display: inline-flex;
  align-items: center;
  backdrop-filter: blur(4px);
}

/* ========== 年级选择器（三行稳定布局）========== */
.grade-selector {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  background: rgba(255,255,255,0.55);
  padding: 12px 20px;
  border-radius: 20px;
  backdrop-filter: blur(6px);
  box-shadow: 0 2px 12px rgba(0,0,0,0.06);
}

.grade-selector > .selector-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  width: 100%;
}

.grade-selector > .action-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
}

.selector-group {
  display: flex;
  align-items: center;
  gap: 6px;
}

.selector-group label {
  font-size: 14px;
  font-weight: 700;
  color: #6c3483;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 3px;
}

.selector-group select {
  padding: 7px 14px;
  border: 2px solid #f39c12;
  border-radius: 14px;
  background: #fff;
  font-size: 14px;
  font-weight: 700;
  color: #e67e22;
  cursor: pointer;
  outline: none;
  appearance: auto;
  transition: border-color 0.2s, box-shadow 0.2s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
  min-width: 100px;
}

.selector-group select:focus {
  border-color: #3498db;
  box-shadow: 0 0 0 3px rgba(52,152,219,0.15);
}

.char-count {
  font-size: 13px;
  color: #555;
  background: rgba(255,255,255,0.75);
  padding: 7px 14px;
  border-radius: 14px;
  font-weight: 600;
  white-space: nowrap;
}

/* ========== 课时选择器 ========== */
.selector-lesson select {
  min-width: 140px;
  border-color: #f9a825;
  color: #e65100;
}

.selector-lesson select:focus {
  border-color: #f57c00;
  box-shadow: 0 0 0 3px rgba(249,168,37,0.15);
}

.selector-lesson select:focus {
  border-color: #f57c00;
  box-shadow: 0 0 0 3px rgba(249,168,37,0.15);
}

/* ========== 查看字库按钮 ========== */
.view-chars-btn {
  padding: 6px 16px;
  border: 2px solid #9b59b6;
  border-radius: 14px;
  background: linear-gradient(180deg, #f3e5f5 0%, #e1bee7 100%);
  font-size: 13px;
  font-weight: 700;
  color: #7b1fa2;
  cursor: pointer;
  outline: none;
  transition: all 0.25s;
  display: inline-flex;
  align-items: center;
  box-shadow: 0 2px 6px rgba(155,89,182,0.15);
}

.view-chars-btn:hover {
  transform: scale(1.05) translateY(-1px);
  box-shadow: 0 4px 12px rgba(155,89,182,0.3);
  background: linear-gradient(180deg, #e1bee7 0%, #ce93d8 100%);
}

/* ========== 主内容区 ========== */
.main-area {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

/* ========== 拼音四线三格 ========== */
.pinyin-staff {
  width: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 0 10px;
}

.pinyin-grid {
  display: flex;
  align-items: center;
  justify-content: center;
}

.pinyin-text {
  font-size: 36px;
  font-weight: 700;
  color: #1565c0;
  letter-spacing: 2px;
  font-family: "Times New Roman", serif;
  white-space: nowrap;
  display: inline-flex;
  align-items: baseline;
  gap: 1px;
}

.py-char {
  display: inline-block;
  line-height: 1;
}

.py-mid, .py-top-mid, .py-mid-bottom, .py-three, .py-dot-mid {
  transform: translateY(0);
}

.py-space {
  display: inline-block;
  width: 10px;
}

.speak-btn {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  min-width: 44px;
  border: 2px solid #FF6B6B;
  border-radius: 50%;
  background: #FFE5E5;
  cursor: pointer;
  transition: all 0.2s ease;
  color: #D63031;
  flex: 0 0 44px;
}

.speak-btn:hover {
  background: #FF6B6B;
  transform: scale(1.08);
  box-shadow: 0 4px 12px rgba(214, 48, 49, 0.3);
}

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

.icon-speak {
  width: 22px;
  height: 22px;
}

/* ========== 米字格 ========== */
.mi-grid-container {
  display: flex;
  justify-content: center;
}

.mi-grid {
  position: relative;
  width: 180px;
  height: 180px;
  background: #fffdf7;
  border-radius: 12px;
  box-shadow:
    0 6px 28px rgba(231,76,60,0.12),
    0 0 0 4px rgba(255,224,219,0.6),
    inset 0 0 30px rgba(255,200,180,0.08);
}

.mi-grid-lines {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
}

.mi-grid-char {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 120px;
  font-weight: 400;
  color: #2c3e50;
  font-family: "KaiTi", "STKaiti", "楷体", serif;
  z-index: 1;
  user-select: none;
}

/* ========== 输入反馈区 ========== */
.input-feedback {
  width: 100%;
  text-align: center;
  scroll-margin-bottom: 220px;
  position: relative;
}

.input-display {
  background: rgba(255,255,255,0.9);
  border: 3px dashed #f39c12;
  border-radius: 16px;
  padding: 10px 20px;
  min-height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  font-weight: 700;
  color: #2c3e50;
  letter-spacing: 4px;
  transition: border-color 0.3s, background 0.3s, transform 0.2s;
  backdrop-filter: blur(4px);
}

.input-display.correct {
  border-color: #27ae60;
  background: rgba(39,174,96,0.08);
  transform: scale(1.02);
}

.input-display.wrong {
  border-color: #e74c3c;
  background: rgba(231,76,60,0.08);
  animation: shake 0.4s ease;
}

.input-placeholder {
  font-size: 15px;
  color: #bbb;
  font-weight: 400;
  letter-spacing: 2px;
  display: flex;
  align-items: center;
}

.mobile-input-tip {
  display: none;
  margin-top: 8px;
  font-size: 12px;
  color: #6c757d;
}

.feedback-message {
  margin-top: 8px;
  font-size: 17px;
  font-weight: 700;
  min-height: 26px;
  transition: all 0.3s;
}

.feedback-message.success { color: #27ae60; }
.feedback-message.error { color: #e74c3c; }

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-8px); }
  40% { transform: translateX(8px); }
  60% { transform: translateX(-6px); }
  80% { transform: translateX(6px); }
}

/* ========== 键盘切换按钮 ========== */
.keyboard-toggle { display: flex; gap: 10px; }

.toggle-btn {
  padding: 8px 20px;
  border: 2px solid #ddd;
  border-radius: 22px;
  background: rgba(255,255,255,0.85);
  font-size: 14px;
  font-weight: 600;
  color: #666;
  cursor: pointer;
  transition: all 0.25s;
  outline: none;
  display: inline-flex;
  align-items: center;
  backdrop-filter: blur(4px);
}

.toggle-btn:hover {
  background: #fff;
  transform: scale(1.04);
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}

.toggle-btn.active {
  border-color: #3498db;
  color: #2980b9;
  background: linear-gradient(180deg, #e3f2fd 0%, #bbdefb 100%);
  box-shadow: 0 2px 10px rgba(52,152,219,0.25);
}

/* ========== QWERTY 键盘 ========== */
.keyboard { width: 100%; }

.hidden { display: none !important; }

.key-row {
  display: flex;
  justify-content: center;
  gap: 4px;
  margin-bottom: 4px;
}

.key {
  width: 48px; height: 48px;
  border: none; border-radius: 10px;
  background: linear-gradient(180deg, #ffffff 0%, #f5f5f5 100%);
  box-shadow: 0 3px 0 #dcdcdc, 0 4px 8px rgba(0,0,0,0.08);
  display: flex;
  align-items: center; justify-content: center;
  font-size: 17px; font-weight: 700; color: #333;
  cursor: pointer;
  transition: all 0.1s;
  user-select: none;
}

.key:active, .key.pressed {
  transform: translateY(3px);
  box-shadow: 0 0 0 #ccc, 0 1px 2px rgba(0,0,0,0.1);
  background: linear-gradient(180deg, #fff8e1 0%, #ffecb3 100%);
  color: #e65100;
}

.key.highlight {
  background: linear-gradient(180deg, #c8e6c9 0%, #a5d6a7 100%);
  color: #1b5e20;
  box-shadow: 0 3px 0 #66bb6a, 0 4px 8px rgba(102,187,106,0.35);
}

.wide-key { width: 72px; font-size: 19px; }
.space-key { width: 240px; font-size: 13px; color: #999; }

/* ========== 九宫格键盘 ========== */
.nine-keyboard {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.nine-row { display: flex; justify-content: center; gap: 8px; margin-bottom: 8px; }

.nine-key {
  width: 80px; height: 64px;
  border: none; border-radius: 14px;
  background: linear-gradient(180deg, #ffffff 0%, #f5f5f5 100%);
  box-shadow: 0 4px 0 #dcdcdc, 0 5px 10px rgba(0,0,0,0.08);
  display: flex;
  flex-direction: column;
  align-items: center; justify-content: center;
  font-size: 22px; font-weight: 700; color: #333;
  cursor: pointer;
  transition: all 0.1s;
  user-select: none;
}

.nine-key:active, .nine-key.pressed {
  transform: translateY(3px);
  box-shadow: 0 0 0 #ccc, 0 1px 3px rgba(0,0,0,0.1);
  background: linear-gradient(180deg, #fff8e1 0%, #ffecb3 100%);
  color: #e65100;
}

.nine-key.highlight {
  background: linear-gradient(180deg, #c8e6c9 0%, #a5d6a7 100%);
  color: #1b5e20;
  box-shadow: 0 4px 0 #66bb6a, 0 5px 10px rgba(102,187,106,0.35);
}

.nine-letters { font-size: 10px; font-weight: 600; color: #aaa; letter-spacing: 1px; margin-top: 2px; }
.nine-space { width: 172px; font-size: 14px; color: #999; flex-direction: row; }

/* ========== 底部装饰 ========== */
.footer {
  text-align: center;
  padding: 10px 0;
  opacity: 0.65;
  display: flex;
  justify-content: center;
  gap: 8px;
}

/* ========== 成功动画 ========== */
@keyframes pop {
  0% { transform: scale(1); }
  50% { transform: scale(1.2); }
  100% { transform: scale(1); }
}
.mi-grid-char.pop { animation: pop 0.4s ease; }

@keyframes celebrate {
  0% { transform: scale(0) rotate(0deg); opacity: 1; }
  100% { transform: scale(1.5) rotate(360deg); opacity: 0; }
}
.celebrate-emoji {
  position: fixed; font-size: 40px;
  pointer-events: none; z-index: 100;
  animation: celebrate 0.8s ease forwards;
}

/* ========== 字库弹窗 ========== */
.char-library-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.45);
  z-index: 999;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s;
  backdrop-filter: blur(4px);
}
.char-library-overlay.show { opacity: 1; pointer-events: auto; }

.char-library-panel {
  width: 90%; max-width: 560px;
  max-height: 80vh;
  background: #fff;
  border-radius: 20px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 12px 48px rgba(0,0,0,0.2);
  animation: panelSlideIn 0.3s ease-out;
}

@keyframes panelSlideIn {
  from { transform: translateY(30px) scale(0.95); opacity: 0; }
  to   { transform: translateY(0) scale(1); opacity: 1; }
}

.char-library-header {
  padding: 16px 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
  display: flex; align-items: center; gap: 12px;
  position: relative;
}
.char-library-header h2 { font-size: 19px; letter-spacing: 2px; }
.char-library-title {
  font-size: 13px; opacity: 0.85;
  background: rgba(255,255,255,0.2);
  padding: 2px 10px; border-radius: 10px;
}

.close-btn {
  margin-left: auto;
  width: 30px; height: 30px;
  border: none; border-radius: 50%;
  background: rgba(255,255,255,0.22);
  color: #fff;
  cursor: pointer;
  transition: background 0.2s;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
}
.close-btn:hover { background: rgba(255,255,255,0.38); }

.char-library-body {
  flex: 1; overflow-y: auto;
  padding: 16px 20px;
}

/* ========== 课时分组（字库弹窗）========== */
.lesson-group {
  margin-bottom: 18px;
}

.lesson-group:last-child { margin-bottom: 0; }

.lesson-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: #5d4037;
  margin-bottom: 8px;
  padding-bottom: 6px;
  border-bottom: 2px dashed #e0d5c7;
}

.lesson-header strong {
  color: #4e342e;
  font-size: 15px;
}

.lesson-badge {
  font-size: 11px;
  background: linear-gradient(135deg, #ffecb3, #ffe082);
  color: #e65100;
  padding: 1px 8px;
  border-radius: 10px;
  font-weight: 700;
}

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

.char-card {
  background: linear-gradient(180deg, #fffef5 0%, #fef9e7 100%);
  border: 2px solid #f5d77a;
  border-radius: 12px;
  text-align: center;
  padding: 8px 4px;
  transition: transform 0.15s, box-shadow 0.15s, border-color 0.15s;
  cursor: default;
}
.char-card:hover {
  transform: translateY(-3px) scale(1.06);
  box-shadow: 0 6px 16px rgba(245,215,122,0.45);
  border-color: #f0a500;
  z-index: 1;
}
.card-char {
  font-size: 28px;
  font-family: "KaiTi", "STKaiti", "楷体", serif;
  color: #2c3e50; line-height: 1.2;
}
.card-pinyin { font-size: 13px; color: #1976d2; font-weight: 600; margin-top: 2px; }
.card-input { font-size: 11px; color: #bdbdbd; margin-top: 1px; }

.char-library-footer {
  padding: 12px 20px;
  border-top: 1px solid #f0f0f0;
  display: flex;
  align-items: center; justify-content: space-between;
  font-size: 13px; color: #999;
  background: #fafafa;
}

/* ========== 响应式适配 ========== */

/* ========== 手机端：隐藏虚拟键盘，优化输入体验 ========== */
@media (hover: none) and (pointer: coarse), (max-width: 768px) {
  .keyboard { display: none !important; }
  .keyboard-toggle { display: none !important; }

  body { padding: 10px; padding-bottom: env(safe-area-inset-bottom, 20px); }
  .app { gap: 10px; }
  .app.keyboard-active { padding-bottom: 36vh; }
  .header h1 { font-size: 22px; }
  .title-row { font-size: 22px; gap: 4px; letter-spacing: 3px; }
  .icon-title { width: 26px; height: 26px; }

  /* 三行布局：年级学期 | 课时 | 字数按钮 */
  .grade-selector { gap: 8px; padding: 10px 14px; }
  .selector-row { gap: 10px; flex-wrap: nowrap; }
  .selector-row:first-child { gap: 12px; }
  .action-row { gap: 12px; }

  .selector-group label { font-size: 13px; }
  .selector-group select { font-size: 13px; padding: 6px 8px; min-width: 80px; border-radius: 12px; }
  .selector-lesson select { min-width: 0; width: 100%; max-width: 160px; }
  .view-chars-btn { font-size: 12px; padding: 6px 12px; border-radius: 12px; white-space: nowrap; }

  .pinyin-text { font-size: 30px; }
  .speak-btn { width: 38px; height: 38px; border-width: 1.5px; }
  .icon-speak { width: 18px; height: 18px; }
  .mi-grid { width: 160px; height: 160px; border-radius: 10px; }
  .mi-grid-char { font-size: 100px; }
  .input-feedback {
    position: sticky;
    bottom: calc(env(safe-area-inset-bottom, 0px) + 8px);
    z-index: 20;
  }
  .input-display { padding: 14px 16px; min-height: 56px; font-size: 22px; border-radius: 18px; letter-spacing: 3px; }
  .input-placeholder { font-size: 15px; }
  .mobile-input-tip { display: block; }
  .feedback-message { font-size: 16px; margin-top: 6px; }

  .char-library-panel { width: 95%; max-height: 85vh; border-radius: 16px; }
  .char-library-header { padding: 14px 16px; }
  .char-library-header h2 { font-size: 17px; }
  .char-library-body { padding: 12px 14px; }
  .char-grid { grid-template-columns: repeat(auto-fill, minmax(58px, 1fr)); gap: 6px; }
  .card-char { font-size: 24px; }
  .lesson-header { font-size: 13px; }
  .lesson-badge { font-size: 10px; }

  .score-board { gap: 10px; font-size: 13px; }
  .score-item { padding: 4px 10px; border-radius: 16px; }
}

@media (max-width: 380px) {
  .title-row { font-size: 19px; gap: 2px; }
  .icon-title { width: 22px; height: 22px; }
  .pinyin-text { font-size: 26px; }
  .speak-btn { width: 36px; height: 36px; border-width: 1.5px; }
  .icon-speak { width: 16px; height: 16px; }
  .mi-grid { width: 140px; height: 140px; }
  .mi-grid-char { font-size: 86px; }
  .input-display { min-height: 50px; font-size: 20px; padding: 12px 14px; }
  .grade-selector { gap: 6px; padding: 8px 12px; }
  .selector-row { gap: 8px; }
  .action-row { gap: 8px; }
  .selector-group select { padding: 5px 7px; min-width: 70px; font-size: 12px; }
}
