:root {
  --green: #2d8a4e;
  --green-dark: #1e6b3a;
  --bamboo: #4caf7a;
  --bg: #f4faf6;
  --card: #fff;
  --text: #1a2e22;
  --muted: #5a6b60;
  --border: #cfe8d8;
  --gold: #c9a227;
  --radius: 14px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.55;
  min-height: 100vh;
}

.site-header {
  background: linear-gradient(145deg, var(--green) 0%, var(--green-dark) 100%);
  color: #fff;
  padding: 20px 16px 22px;
  text-align: center;
}

.tag { font-size: .85rem; opacity: .85; }
.site-header h1 { font-size: clamp(1.2rem, 4vw, 1.75rem); margin: 6px 0; }
.sub { font-size: .92rem; opacity: .9; }

.toolbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 10px 12px;
  box-shadow: 0 2px 12px rgba(0,0,0,.06);
}

#searchInput {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 1rem;
  margin-bottom: 10px;
}

.tabs {
  display: flex;
  gap: 6px;
  overflow-x: auto;
}

.tab {
  flex: 0 0 auto;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg);
  cursor: pointer;
  font-size: .88rem;
  white-space: nowrap;
}

.tab.active {
  background: var(--green);
  color: #fff;
  border-color: var(--green);
}

.tab-short { display: none; }

#app { padding: 14px; max-width: 960px; margin: 0 auto; }

.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
  border: 1px solid var(--border);
  box-shadow: 0 2px 8px rgba(45,138,78,.08);
}

.card h2 { font-size: 1.05rem; margin-bottom: 6px; }
.muted { font-size: .9rem; color: var(--muted); }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 10px;
}

.rad-item {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 8px;
  text-align: center;
  cursor: pointer;
  transition: transform .15s, box-shadow .15s;
}

.rad-item:hover { transform: translateY(-2px); box-shadow: 0 4px 12px rgba(45,138,78,.15); }
.rad-item.featured { border-color: var(--gold); background: #fffbeb; }

.speak-btn {
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  padding: 2px 4px;
  border-radius: 6px;
  opacity: .75;
}

.speak-btn:hover { opacity: 1; background: #ecfdf3; }
.speak-btn.playing { opacity: 1; background: #d1fae5; }

.audio-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-bottom: 8px;
}

.audio-row .speak-btn { font-size: 1.4rem; }

.quiz-card .speak-btn {
  display: block;
  margin: 0 auto 10px;
  font-size: 1.5rem;
}

.rad-char {
  font-family: "Noto Serif SC", SimSun, serif;
  font-size: 2.2rem;
  line-height: 1.1;
}

.rad-py { font-size: .78rem; color: var(--muted); margin-top: 4px; }
.rad-vi { font-size: .82rem; font-weight: 600; color: var(--green-dark); }

.big-char {
  font-family: "Noto Serif SC", SimSun, serif;
  font-size: 5rem;
  text-align: center;
  line-height: 1;
  margin: 8px 0;
}

.detail-meta { text-align: center; color: var(--muted); margin-top: 8px; }

.detail-panel {
  border: 2px solid #333;
  border-radius: 4px;
  padding: 14px;
  margin: 12px 0;
  background: #fff;
}

.detail-head {
  display: flex;
  align-items: baseline;
  gap: 12px;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}

.detail-char {
  font-family: "Noto Serif SC", SimSun, serif;
  font-size: 1.6rem;
}

.detail-bothu {
  font-weight: 700;
  font-size: 1.1rem;
  letter-spacing: .02em;
}

.detail-fields { display: grid; gap: 10px; }

.detail-row {
  display: grid;
  grid-template-columns: minmax(120px, 42%) 1fr;
  gap: 8px;
  align-items: center;
  font-size: .95rem;
}

.detail-row dt {
  font-weight: 600;
  color: var(--text);
}

.detail-row dd { margin: 0; }

.audio-cell .speak-btn { font-size: 1.35rem; }

/* Layout học — tham khảo chinese.edu.vn */
.learn-card.edu-style {
  display: grid;
  grid-template-columns: minmax(160px, 220px) 1fr;
  gap: 20px;
  margin: 12px 0;
  padding: 18px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  background: #fff;
  box-shadow: none;
}

.learn-visual {
  display: flex;
  align-items: flex-start;
  justify-content: center;
}

.char-box {
  position: relative;
  width: 100%;
  max-width: 220px;
  aspect-ratio: 1;
  border: 2px solid #c8c8c8;
  border-radius: 4px;
  background: #fff;
  overflow: hidden;
}

/* Lưới chỉ khi tập viết / xem nét */
.char-box.mode-write,
.char-box.mode-strokes {
  border-color: #aaa;
  background:
    linear-gradient(to right, transparent calc(50% - .5px), #e8e8e8 calc(50% - .5px), #e8e8e8 calc(50% + .5px), transparent calc(50% + .5px)),
    linear-gradient(to bottom, transparent calc(50% - .5px), #e8e8e8 calc(50% - .5px), #e8e8e8 calc(50% + .5px), transparent calc(50% + .5px)),
    #fff;
}

.char-box.mode-write::before,
.char-box.mode-strokes::before,
.char-box.mode-write::after,
.char-box.mode-strokes::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 0;
}

.char-box.mode-write::before,
.char-box.mode-strokes::before {
  background: linear-gradient(135deg, transparent calc(50% - .5px), #efefef calc(50% - .5px), #efefef calc(50% + .5px), transparent calc(50% + .5px));
}

.char-box.mode-write::after,
.char-box.mode-strokes::after {
  background: linear-gradient(45deg, transparent calc(50% - .5px), #efefef calc(50% - .5px), #efefef calc(50% + .5px), transparent calc(50% + .5px));
}

.ghost-char {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: "Noto Serif SC", SimSun, serif;
  font-size: clamp(5rem, 32vw, 8.5rem);
  color: #1a1a1a;
  z-index: 0;
  pointer-events: none;
  user-select: none;
  opacity: 0;
  transition: opacity .15s, color .15s;
}

.ghost-char.show {
  opacity: 1;
}

.learn-info {
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-width: 0;
  justify-content: space-between;
}

.info-table {
  width: 100%;
  border-collapse: collapse;
  font-size: .95rem;
}

.info-table td {
  padding: 6px 4px;
  vertical-align: middle;
}

.info-ico-cell {
  width: 40px;
  text-align: center;
}

.info-label-cell {
  width: 100px;
  font-weight: 600;
  white-space: nowrap;
}

.info-label-cell.vi { color: #c41e3a; }
.info-label-cell.py { color: #3b82f6; }
.info-label-cell.hz { color: #6b7280; }

.info-val-cell {
  color: #111;
  font-weight: 500;
}

.subtle-row .info-val-cell {
  font-weight: 500;
}

.hanzi-val {
  display: flex;
  align-items: center;
  gap: 6px;
}

.hanzi-val .speak-btn {
  font-size: 1.1rem;
  color: #2563eb;
}

.hanzi-big {
  font-family: "Noto Serif SC", SimSun, serif;
  font-size: 1.35rem;
  color: #1d4ed8;
  font-weight: 700;
}

.info-ico {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  font-size: .95rem;
  border-radius: 4px;
  background: #f5f5f5;
}

.info-ico.abc {
  font-size: .65rem;
  font-weight: 700;
  color: #2563eb;
}

.info-ico.han {
  font-family: "Noto Serif SC", serif;
  color: #ea580c;
  font-weight: 700;
}

.info-ico.bothu {
  font-size: .8rem;
  color: #666;
}

.learn-actions {
  display: flex;
  gap: 10px;
  margin-top: 8px;
}

.btn-action {
  flex: 1;
  padding: 11px 16px;
  border: none;
  border-radius: 22px;
  background: #8b2942;
  color: #fff;
  font-size: .9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s, box-shadow .15s;
  box-shadow: 0 2px 6px rgba(139, 41, 66, .25);
}

.btn-action:hover {
  background: #6d1f33;
  box-shadow: 0 3px 10px rgba(139, 41, 66, .35);
}

.btn-action.active {
  background: #5a1829;
  box-shadow: inset 0 2px 6px rgba(0, 0, 0, .25);
  outline: 2px solid #22c55e;
  outline-offset: 2px;
}

.writer-host {
  position: absolute;
  inset: 0;
  margin: auto;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
}

.writer-host[hidden] {
  display: none !important;
}

.mode-write .writer-host:not([hidden]) svg,
.mode-strokes .writer-host:not([hidden]) svg {
  max-width: 100%;
  max-height: 100%;
}

.mode-write .ghost-char,
.mode-strokes .ghost-char {
  opacity: 0;
}

@media (max-width: 560px) {
  .learn-card.edu-style {
    grid-template-columns: 1fr;
  }
  .char-box {
    max-width: 240px;
    margin: 0 auto;
  }
}

.rad-meta { display: grid; gap: 8px; font-size: .95rem; }
.rad-meta dt { font-weight: 600; color: var(--muted); font-size: .8rem; }

.note-label {
  display: block;
  margin-top: 14px;
  font-size: .85rem;
  color: var(--muted);
}

.rad-note {
  width: 100%;
  min-height: 72px;
  margin-top: 6px;
  padding: 10px;
  border: 1px dashed var(--border);
  border-radius: 10px;
  font-family: inherit;
  resize: vertical;
}

.nav-row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-top: 12px;
}

.btn {
  padding: 10px 18px;
  border: none;
  border-radius: 10px;
  background: var(--green);
  color: #fff;
  cursor: pointer;
  font-size: .95rem;
}

.btn.secondary { background: var(--bg); color: var(--text); border: 1px solid var(--border); }

.detail-top-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: space-between;
  margin-bottom: 8px;
}

.rad-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  min-height: 28px;
  gap: 4px;
}

.weak-btn {
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  color: var(--muted);
  padding: 2px 4px;
}

.weak-btn.on { color: var(--gold); }
.rad-item.is-weak { border-color: #e6c35c; }

.game-hub .game-start-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 14px;
}

.game-start-btn { flex: 1 1 160px; }

.weak-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 6px;
}

.weak-head h3 {
  font-size: 1rem;
  color: var(--green-dark);
}

.game-screen {
  max-width: 720px;
  margin: 0 auto;
}

.game-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
}

.game-progress {
  background: #1a1228;
  color: #fff;
  padding: 6px 14px;
  border-radius: 999px;
  font-weight: 600;
  font-size: .9rem;
}

.game-score {
  font-weight: 600;
  color: var(--green-dark);
  min-width: 3.5rem;
  text-align: right;
}

.game-exit { padding: 8px 12px; font-size: .85rem; }

.game-question-card {
  display: flex;
  align-items: center;
  gap: 16px;
  background: linear-gradient(145deg, #3a1f4f 0%, #2a1638 100%);
  color: #fff;
  border-radius: 16px;
  padding: 18px;
  margin-bottom: 14px;
}

.game-char-wrap {
  flex: 0 0 auto;
  text-align: center;
}

.game-char-box {
  position: relative;
  width: 148px;
  height: 148px;
  background:
    linear-gradient(rgba(45, 138, 78, .08) 1px, transparent 1px) 0 0 / 24px 24px,
    linear-gradient(90deg, rgba(45, 138, 78, .08) 1px, transparent 1px) 0 0 / 24px 24px,
    linear-gradient(160deg, #eef7ff 0%, #d8ecf8 55%, #cfe4f5 100%);
  color: #1a2e22;
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: "Noto Serif SC", SimSun, serif;
  font-size: 4.4rem;
  font-weight: 700;
  box-shadow:
    inset 0 0 0 3px #fff,
    0 8px 22px rgba(0, 0, 0, .28);
  overflow: hidden;
  cursor: pointer;
  isolation: isolate;
}

.game-char-box::before {
  content: "";
  position: absolute;
  inset: 10px;
  border: 1px dashed rgba(45, 138, 78, .28);
  border-radius: 10px;
  pointer-events: none;
  z-index: 1;
}

.game-char-box .ghost-char {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: .18;
  color: #2d8a4e;
  z-index: 0;
  pointer-events: none;
}

.game-char-box .writer-host {
  z-index: 2;
}

.game-char-box.mode-strokes .ghost-char {
  opacity: .12;
}

.game-stroke-hint {
  margin-top: 8px;
  font-size: .72rem;
  letter-spacing: .04em;
  color: rgba(255, 255, 255, .72);
}

.game-q-text {
  font-size: clamp(1.15rem, 4vw, 1.6rem);
  font-weight: 700;
  line-height: 1.35;
}

.game-options {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.game-opt {
  position: relative;
  border: none;
  border-radius: 14px;
  background: var(--opt-color, var(--green));
  color: #fff;
  min-height: 72px;
  padding: 14px 36px 14px 14px;
  cursor: pointer;
  text-align: left;
  font-size: .95rem;
  font-weight: 600;
  box-shadow: 0 4px 0 rgba(0,0,0,.15);
  transition: transform .12s, opacity .12s;
}

.game-opt:hover:not(:disabled) { transform: translateY(-2px); }
.game-opt:disabled { cursor: default; }

.game-opt-num {
  position: absolute;
  top: 8px;
  right: 10px;
  width: 22px;
  height: 22px;
  border-radius: 6px;
  background: rgba(255,255,255,.25);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: .8rem;
}

.game-opt.is-correct {
  outline: 3px solid #fff;
  box-shadow: 0 0 0 3px #2e7d32;
}

.game-opt.is-wrong {
  outline: 3px solid #fff;
  box-shadow: 0 0 0 3px #c62828;
  opacity: .85;
}

.game-opt.is-dim { opacity: .45; }

.game-feedback {
  margin-top: 14px;
  padding: 12px 14px;
  border-radius: 12px;
  text-align: center;
  font-size: .95rem;
}

.game-feedback.ok { background: #e8f5e9; color: #1b5e20; }
.game-feedback.bad { background: #ffebee; color: #b71c1c; }

.game-next {
  display: block;
  width: 100%;
  margin-top: 12px;
  padding: 14px;
  font-size: 1rem;
}

.game-result-score {
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--green-dark);
  margin: 12px 0;
}

.quiz-card { text-align: center; padding: 24px 16px; }

.quiz-char {
  font-family: "Noto Serif SC", SimSun, serif;
  font-size: 4.5rem;
  margin: 12px 0;
}

.quiz-answer {
  margin-top: 16px;
  padding: 14px;
  background: #ecfdf3;
  border-radius: 10px;
  display: none;
}

.quiz-answer.show { display: block; }

.quiz-meaning-vi {
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--text);
  margin-top: 4px;
}

.stroke-group h3 {
  font-size: 1rem;
  color: var(--green-dark);
  margin: 16px 0 8px;
  padding-bottom: 4px;
  border-bottom: 2px solid var(--bamboo);
}

.poem-day {
  margin-bottom: 14px;
}

.poem-day h3 {
  font-size: 1rem;
  color: var(--green-dark);
  margin-bottom: 10px;
  padding-bottom: 4px;
  border-bottom: 2px solid var(--bamboo);
}

.poem-lines {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.poem-line {
  font-family: "Noto Serif SC", "Segoe UI", serif;
  font-size: .98rem;
  line-height: 1.7;
  padding: 8px 12px;
  background: #f8fcf9;
  border-left: 3px solid var(--bamboo);
  border-radius: 0 8px 8px 0;
}

.poem-name {
  color: #1a5c9e;
  font-weight: 700;
}

.poem-meaning {
  color: #8a5a12;
}

.poem-char {
  font-family: "Noto Serif SC", serif;
  font-weight: 700;
  font-size: 1.15em;
  color: var(--green-dark);
  cursor: pointer;
  padding: 0 3px;
  margin: 0 1px;
  border-radius: 4px;
  text-decoration: none;
  border-bottom: 2px solid var(--bamboo);
  background: #e8f6ee;
}

.poem-char:hover,
.poem-char:focus {
  background: #fffbeb;
  border-bottom-color: var(--gold);
  outline: none;
}

.char-popup-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(20, 40, 28, .55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.char-popup {
  width: min(420px, 100%);
  max-height: min(90vh, 640px);
  overflow: auto;
  background: var(--card);
  border-radius: 16px;
  border: 1px solid var(--border);
  box-shadow: 0 16px 40px rgba(0, 0, 0, .22);
}

.char-popup-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.char-popup-title {
  font-family: "Noto Serif SC", serif;
  font-size: 1.15rem;
  color: var(--green-dark);
}

.char-popup-close {
  border: none;
  background: #eef6f1;
  color: var(--text);
  width: 36px;
  height: 36px;
  border-radius: 999px;
  font-size: 1.1rem;
  cursor: pointer;
  flex: 0 0 auto;
}

.char-popup-close:hover {
  background: #dceee3;
}

.char-popup-body {
  padding: 16px;
  text-align: center;
}

.char-popup-body .popup-box {
  width: min(260px, 70vw);
  height: min(260px, 70vw);
  margin: 0 auto 12px;
}

.char-popup-meaning {
  color: var(--muted);
  margin-bottom: 12px;
  font-size: .95rem;
}

.char-popup-body .learn-actions {
  justify-content: center;
  margin-bottom: 10px;
}

.char-popup-body .speak-btn {
  margin-top: 4px;
}

.site-footer {
  text-align: center;
  padding: 20px;
  font-size: .85rem;
  color: var(--muted);
}

.site-footer a { color: var(--green); }

@media (max-width: 480px) {
  .rad-char { font-size: 1.8rem; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); }

  .tab-long { display: none; }
  .tab-short { display: inline; }
  .tab[data-view="poem"] { padding-left: 12px; padding-right: 12px; }

  .game-options { grid-template-columns: 1fr; }
  .game-question-card { flex-direction: column; text-align: center; }
  .game-char-box { width: 132px; height: 132px; font-size: 3.8rem; }
}
