:root {
  --red: #c0392b;
  --red-dark: #96281b;
  --gold: #f1c40f;
  --bg: #fdf6ee;
  --border: #eee0d5;
  --text: #2d2d2d;
  --muted: #8a8a8a;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Segoe UI", "Microsoft YaHei", "PingFang SC", sans-serif;
  background: var(--bg);
  color: var(--text);
}

.site-header {
  background: linear-gradient(135deg, var(--red), var(--red-dark));
  color: #fff;
  text-align: center;
  padding: 28px 16px 20px;
}
.site-header h1 { margin: 0 0 6px; font-size: 28px; }
.site-header .zh { color: var(--gold); margin-right: 8px; }
.site-header p { margin: 0; opacity: .92; font-size: 15px; }

.tabs {
  display: flex;
  justify-content: center;
  gap: 8px;
  flex-wrap: wrap;
  padding: 14px 12px 0;
}
.tabs button {
  border: 1px solid var(--border);
  background: #fff;
  padding: 9px 20px;
  border-radius: 999px;
  font-size: 15px;
  cursor: pointer;
}
.tabs button.active {
  background: var(--red);
  border-color: var(--red);
  color: #fff;
  font-weight: 600;
}

main { max-width: 860px; margin: 18px auto 40px; padding: 0 14px; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

.card {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 18px 20px;
  margin-bottom: 16px;
  box-shadow: 0 2px 6px rgba(192, 57, 43, .06);
}
.card h2 { margin-top: 0; color: var(--red-dark); font-size: 20px; }

.toolbar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 12px;
  align-items: center;
}
.toolbar select,
.toolbar input {
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 15px;
  background: #fff;
}
.toolbar input[type="search"] { flex: 1; min-width: 200px; }
.toolbar.center { justify-content: center; }

.btn {
  border: 1px solid var(--red);
  background: #fff;
  color: var(--red);
  padding: 9px 16px;
  border-radius: 10px;
  font-size: 15px;
  cursor: pointer;
}
.btn:hover { background: #fdeceb; }
.btn-primary { background: var(--red); color: #fff; }
.btn-primary:hover { background: var(--red-dark); }
.btn-ghost { border-color: var(--border); color: var(--muted); }
.btn-known { background: #eafaf1; border-color: #27ae60; color: #1e8449; }

.muted { color: var(--muted); font-size: 14px; }
.hidden { display: none !important; }

/* Intro tab */
.guide li { margin: 6px 0; }
.tone-row,
.sentence-row {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 4px;
  border-bottom: 1px dashed var(--border);
}
.tone-row:last-child,
.sentence-row:last-child { border-bottom: none; }
.tone-main { min-width: 170px; }
.tone-name { font-weight: 600; margin-right: 10px; }
.tone-example { font-size: 20px; color: var(--red-dark); }
.tone-desc { flex: 1; color: var(--muted); font-size: 14px; }
.sentence-left { min-width: 260px; }
.sentence-hanzi { font-size: 20px; }
.sentence-pinyin { color: var(--muted); font-size: 13px; }
.sentence-meaning { flex: 1; }

.speak-btn {
  border: none;
  background: #fdeceb;
  color: var(--red);
  width: 34px;
  height: 34px;
  border-radius: 50%;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.speak-btn:hover { background: var(--red); color: #fff; }

/* Vocab tab */
.vocab-list { display: flex; flex-direction: column; gap: 8px; }
.vocab-row {
  display: flex;
  align-items: center;
  gap: 14px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 14px;
}
.vocab-hanzi { font-size: 26px; min-width: 90px; }
.vocab-mid { flex: 1; }
.vocab-pinyin { color: var(--red-dark); font-size: 14px; }
.vocab-meaning { font-size: 15px; }
.known-tag {
  background: #eafaf1;
  color: #1e8449;
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 999px;
  white-space: nowrap;
}

/* Flashcard tab */
.flashcard { perspective: 1000px; height: 300px; margin: 10px 0 16px; cursor: pointer; }
.flashcard-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transition: transform .5s;
  transform-style: preserve-3d;
}
.flashcard.flipped .flashcard-inner { transform: rotateY(180deg); }
.flashcard-face {
  position: absolute;
  inset: 0;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  border-radius: 16px;
  border: 1px solid var(--border);
}
.flashcard-front { background: #fff; }
.flashcard-back { background: var(--red); color: #fff; transform: rotateY(180deg); }
.fc-hanzi { font-size: 84px; }
.fc-hanzi.small { font-size: 44px; }
.fc-pinyin { font-size: 24px; color: var(--gold); }
.flashcard-back .fc-meaning { font-size: 26px; }
.fc-hint { color: var(--muted); font-size: 14px; }
.fc-controls { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }

/* Quiz tab */
.quiz-box { text-align: center; }
.quiz-label { color: var(--muted); margin-bottom: 8px; }
.quiz-prompt { font-size: 30px; font-weight: 600; margin-bottom: 6px; }
.quiz-prompt.zh { font-size: 56px; }
.quiz-options {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 14px 0;
}
.quiz-option {
  padding: 16px;
  font-size: 17px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #fff;
  cursor: pointer;
}
.quiz-option.zh { font-size: 24px; }
.quiz-option:hover:not(:disabled) { border-color: var(--red); }
.quiz-option.correct { background: #eafaf1; border-color: #27ae60; color: #1e8449; font-weight: 600; }
.quiz-option.wrong { background: #fdedec; border-color: var(--red); color: var(--red); }
.quiz-option:disabled { cursor: default; }
.quiz-nav { text-align: center; }

.site-footer { text-align: center; color: var(--muted); font-size: 13px; padding: 20px; }

@media (max-width: 600px) {
  .fc-hanzi { font-size: 60px; }
  .quiz-prompt.zh { font-size: 40px; }
  .quiz-options { grid-template-columns: 1fr; }
  .vocab-hanzi { min-width: 64px; font-size: 22px; }
  .sentence-left { min-width: 0; }
}

/* Pinyin chart */
.card h3 { color: var(--red-dark); margin: 14px 0 10px; font-size: 16px; }
.pinyin-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
  gap: 8px;
  margin-bottom: 14px;
}
.py-cell {
  padding: 10px 4px;
  font-size: 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fff;
  cursor: pointer;
  text-align: center;
}
.py-cell:hover { border-color: var(--red); color: var(--red); }

/* Listening tab */
.big-speak {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  border: none;
  background: var(--red);
  color: #fff;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin: 10px 0;
}
.big-speak svg { width: 34px; height: 34px; }
.big-speak:hover { background: var(--red-dark); }
.ls-reveal { margin-top: 10px; font-size: 18px; }
.ls-reveal .py { color: var(--red-dark); }

/* Tips tab */
.tip { display: flex; gap: 16px; align-items: flex-start; }
.tip-num {
  background: var(--red);
  color: #fff;
  font-weight: 700;
  min-width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.tip h3 { margin: 0 0 6px; font-size: 17px; }
.tip p { margin: 0; }

/* Stats card */
.stats-row { display: flex; gap: 12px; flex-wrap: wrap; }
.stat {
  flex: 1;
  min-width: 110px;
  background: var(--bg);
  border-radius: 12px;
  padding: 14px;
  text-align: center;
}
.stat-num { font-size: 28px; font-weight: 700; color: var(--red); }
.stat-label { color: var(--muted); font-size: 13px; }

/* Speaking tab */
.sp-word { margin: 14px 0; }
.sp-pinyin { font-size: 22px; color: var(--red-dark); }
.sp-result { min-height: 26px; font-size: 17px; font-weight: 600; margin: 12px 0 4px; }
.sp-result.ok { color: #1e8449; }
.sp-result.bad { color: var(--red); }
#sp-speak.recording { background: #e74c3c; border-color: #e74c3c; animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .55; } }

/* Matching tab */
.match-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  margin: 14px 0;
}
.match-card {
  min-height: 74px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #fff;
  cursor: pointer;
  font-size: 17px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
  text-align: center;
}
.match-card.zh { font-size: 26px; }
.match-card.revealed { border-color: var(--gold); background: #fff8d6; }
.match-card.matched { border-color: #27ae60; background: #eafaf1; cursor: default; }
@media (max-width: 600px) {
  .match-grid { grid-template-columns: repeat(3, 1fr); }
}

/* Fill-in-the-blank quiz question */
.quiz-prompt.sent { font-size: 32px; }

/* Radicals chart */
.rad-ex {
  cursor: pointer;
  margin-right: 12px;
  font-size: 20px;
  color: var(--red-dark);
}
.rad-ex:hover { color: var(--red); }

/* Writing tab */
.wr-head {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 14px;
  margin: 10px 0;
  flex-wrap: wrap;
}
.wr-hanzi { font-size: 40px; font-weight: 600; }
.wr-board { display: flex; justify-content: center; margin: 10px 0; }
#wr-target {
  width: 260px;
  height: 260px;
  border: 2px solid #e5b8b2;
  border-radius: 8px;
  background-image:
    linear-gradient(to right, transparent 49.5%, #f0d5d1 49.5%, #f0d5d1 50.5%, transparent 50.5%),
    linear-gradient(to bottom, transparent 49.5%, #f0d5d1 49.5%, #f0d5d1 50.5%, transparent 50.5%);
  cursor: crosshair;
}

/* Writing tab: clearer reading + meaning */
.wr-meaning { font-size: 17px; }
.wr-head .speak-btn { width: 40px; height: 40px; }

/* Dictionary tab */
.dict-input {
  width: 100%;
  padding: 12px 14px;
  font-size: 17px;
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 8px;
}
.dict-input:focus { outline: none; border-color: var(--red); }

/* Free writing pad (speaking tab) */
.free-board { position: relative; width: 300px; height: 300px; margin: 10px auto; }
.fw-samples {
  display: flex;
  justify-content: center;
  gap: 14px;
  flex-wrap: wrap;
  min-height: 112px;
  margin: 8px 0;
}
.fw-sample-char {
  width: 110px;
  height: 110px;
  border: 1px dashed var(--border);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 60px;
  color: rgba(192, 57, 43, .35);
}
#fw-canvas {
  position: relative;
  width: 300px;
  height: 300px;
  border: 2px solid #e5b8b2;
  border-radius: 8px;
  cursor: crosshair;
  touch-action: none;
}

/* Word popup: click any hanzi anywhere */
[data-zh] { cursor: pointer; }
[data-zh]:hover { color: var(--red); }
.wp-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
}
.wp-modal {
  background: #fff;
  border-radius: 16px;
  padding: 24px;
  max-width: 420px;
  width: 100%;
  text-align: center;
  position: relative;
  box-shadow: 0 10px 40px rgba(0, 0, 0, .25);
  max-height: 90vh;
  overflow-y: auto;
}
.wp-close {
  position: absolute;
  top: 8px;
  right: 12px;
  border: none;
  background: none;
  font-size: 28px;
  cursor: pointer;
  color: var(--muted);
  line-height: 1;
}
.wp-close:hover { color: var(--red); }
.wp-head {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 14px;
  flex-wrap: wrap;
}
#wp-hanzi { font-size: 52px; font-weight: 600; }
#wp-pinyin { font-size: 22px; color: var(--red-dark); }
#wp-meaning { font-size: 19px; margin: 8px 0 2px; }

/* HSK level badge */
.level-tag {
  background: #fdf0d5;
  color: #b9770e;
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 999px;
  white-space: nowrap;
}

/* Global level bar */
.level-bar {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  padding: 10px 12px 0;
  font-size: 14px;
  color: var(--muted);
}
.level-bar button {
  border: 1px solid var(--border);
  background: #fff;
  padding: 5px 14px;
  border-radius: 999px;
  font-size: 13px;
  cursor: pointer;
}
.level-bar button.active {
  background: var(--red-dark);
  border-color: var(--red-dark);
  color: #fff;
  font-weight: 600;
}

/* Auth screen, user chip, history */
.site-header { position: relative; }
.user-chip {
  position: absolute;
  top: 14px;
  right: 16px;
  display: flex;
  gap: 8px;
  align-items: center;
  color: #fff;
  font-size: 14px;
}
.user-chip button {
  background: rgba(255, 255, 255, .18);
  border: 1px solid rgba(255, 255, 255, .5);
  color: #fff;
  padding: 4px 12px;
  border-radius: 999px;
  cursor: pointer;
  font-size: 13px;
}
.auth-tabs {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin-bottom: 14px;
}
.auth-tabs button {
  border: 1px solid var(--border);
  background: #fff;
  padding: 8px 18px;
  border-radius: 999px;
  cursor: pointer;
  font-size: 15px;
}
.auth-tabs button.active {
  background: var(--red);
  border-color: var(--red);
  color: #fff;
  font-weight: 600;
}
.auth-submit { width: 100%; padding: 12px; font-size: 16px; margin-top: 4px; }
.history-box { margin-top: 14px; }
.history-box h3 { margin: 0 0 8px; }
.history-row {
  display: flex;
  justify-content: space-between;
  padding: 6px 4px;
  border-bottom: 1px dashed var(--border);
  font-size: 14px;
}
.history-row:last-child { border-bottom: none; }
.history-score { font-weight: 600; color: var(--red-dark); }
