:root {
  --primary: #4f46e5;
  --bg: #f4f5fa;
  --card: #ffffff;
  --text: #1f2937;
  --muted: #5b6472;
  --line: #e5e7eb;
  --ok: #16a34a;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

/* The hidden attribute must always win over author display rules (e.g.
 * .hsk-badge uses display: inline-block), otherwise empty badges/tags with
 * hidden set still render as blank pills. */
[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", "Noto Sans SC", sans-serif;
  background: var(--bg);
  color: var(--text);
  overscroll-behavior-y: none;
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: linear-gradient(135deg, #4f46e5, #7c3aed);
  color: #fff;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  font-size: 17px;
  font-weight: 700;
  box-shadow: 0 2px 8px rgba(79, 70, 229, 0.25);
}

.topbar-title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin: 0;
  font-size: inherit;
  font-weight: inherit;
}

.topbar-logo {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.98);
  box-shadow: 0 2px 6px rgba(30, 41, 59, 0.18);
}

.topbar-logo svg {
  display: block;
  width: 26px;
  height: 26px;
  border-radius: 7px;
}

.container {
  max-width: 560px;
  margin: 0 auto;
  padding: 14px 14px 30px;
}

.topbar-link {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: inherit;
  text-decoration: none;
}

.page h1 {
  margin: 0 0 12px;
  font-size: 22px;
  line-height: 1.35;
}

.page h2 {
  margin: 22px 0 8px;
  font-size: 17px;
}

.page p,
.page li {
  font-size: 15px;
  line-height: 1.7;
}

.page p {
  margin: 0 0 12px;
}

.page ul {
  margin: 0 0 12px;
  padding-left: 20px;
}

.page li {
  margin-bottom: 6px;
}

.footer-nav {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px 14px;
  margin-bottom: 8px;
}

.panel {
  background: var(--card);
  border-radius: 18px;
  padding: 16px;
  box-shadow: 0 2px 10px rgba(30, 41, 59, 0.06);
  margin-bottom: 14px;
}

.input-row {
  display: flex;
  gap: 10px;
}

#wordInput {
  flex: 1;
  min-width: 0;
  font-size: 17px;
  padding: 13px 14px;
  border: 2px solid var(--line);
  border-radius: 14px;
  outline: none;
  background: #fafbff;
}

#wordInput:focus {
  border-color: var(--primary);
}

.btn {
  border: 0;
  border-radius: 12px;
  font-size: 15px;
  padding: 12px 14px;
  background: #eef0f6;
  color: #374151;
  touch-action: manipulation;
  cursor: pointer;
  font-family: inherit;
}

.btn:active {
  transform: scale(0.97);
}

.btn:disabled {
  opacity: 0.55;
}

.btn.primary {
  background: var(--primary);
  color: #fff;
  font-weight: 600;
}

.btn.off {
  opacity: 0.5;
}

.menu {
  position: relative;
  flex: 0 0 auto;
}

.menu summary {
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
  list-style: none;
  color: #fff;
  background: rgba(255, 255, 255, 0.16);
  border: 1px solid rgba(255, 255, 255, 0.35);
  border-radius: 999px;
  padding: 6px 12px;
  line-height: 1.3;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}

.menu summary::-webkit-details-marker {
  display: none;
}

.menu[open] summary {
  background: rgba(255, 255, 255, 0.26);
}

.menu-dropdown {
  display: none;
}

.menu[open] .menu-dropdown {
  display: block;
  position: absolute;
  right: 0;
  top: calc(100% + 10px);
  width: 302px;
  max-width: calc(100vw - 20px);
  max-height: calc(100vh - 90px);
  overflow-y: auto;
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 6px 16px 14px;
  box-shadow: 0 10px 30px rgba(30, 41, 59, 0.22);
  -webkit-user-select: none;
  user-select: none;
}

.menu-dropdown input,
.menu-dropdown textarea {
  -webkit-user-select: text;
  user-select: text;
}

.menu-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 8px 0;
}

.menu-item {
  border: 0;
  background: transparent;
  font: inherit;
  font-size: 14px;
  color: var(--text);
  text-align: left;
  padding: 12px 14px;
  border-radius: 10px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.menu-item:active {
  background: #eef0f6;
}

.menu-view {
  padding-top: 2px;
}

.menu-view .setting-row:first-of-type {
  margin-top: 6px;
}

.setting-row {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 0;
  border-top: 1px dashed var(--line);
}

.setting-row:first-of-type {
  margin-top: 10px;
}

.setting-label {
  flex: 1;
  font-size: 13px;
  color: #374151;
}

.feedback-body {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 6px 0 10px;
}

.feedback-field {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.feedback-label {
  font-size: 12.5px;
  color: var(--muted);
}

.feedback-field input,
.feedback-field textarea {
  font: inherit;
  font-size: 14px;
  padding: 10px 12px;
  border: 1.5px solid var(--line);
  border-radius: 10px;
  background: #fafbff;
  outline: none;
  width: 100%;
  resize: vertical;
  min-height: 44px;
}

.feedback-field input.invalid {
  border-color: #dc2626;
}

.field-error {
  font-size: 12px;
  color: #dc2626;
  line-height: 1.3;
}

.feedback-field textarea {
  min-height: 84px;
}

.feedback-field input:focus,
.feedback-field textarea:focus {
  border-color: var(--primary);
}

.feedback-submit {
  width: 100%;
  padding: 11px 14px;
  font-size: 14px;
}

.switch-row input {
  width: 44px;
  height: 26px;
  accent-color: var(--primary);
}

.seg {
  display: flex;
  background: #eef0f6;
  border-radius: 10px;
  padding: 3px;
}

.seg button {
  border: 0;
  background: transparent;
  padding: 7px 12px;
  border-radius: 8px;
  font-size: 12px;
  color: #4b5563;
  font-family: inherit;
  cursor: pointer;
}

.seg button.active {
  background: #fff;
  color: var(--primary);
  font-weight: 600;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
}

.card {
  background: var(--card);
  border-radius: 20px;
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: 0 2px 10px rgba(30, 41, 59, 0.06);
  border: 2px solid transparent;
  scroll-margin-top: 70px;
}

#practice {
  scroll-margin-top: 70px;
}

.card.complete {
  border-color: #86efac;
}

.card-head {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 8px;
}

.char-big {
  font-size: 52px;
  line-height: 1;
  font-weight: 700;
  display: flex;
  align-items: flex-start;
  gap: 2px;
}

.speak-btn {
  border: 0;
  background: transparent;
  padding: 4px 4px 4px 8px;
  margin-top: 4px;
  font-size: 19px;
  line-height: 1;
  cursor: pointer;
  border-radius: 8px;
  opacity: 0.8;
  transition: transform 0.12s ease, opacity 0.12s ease;
}

.speak-btn:active {
  transform: scale(0.82);
  opacity: 1;
}

.speak-btn.speaking {
  animation: speak-pulse 0.35s ease 2;
}

@keyframes speak-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.3); }
}

.char-meta {
  min-width: 0;
}

.char-pinyin {
  color: var(--muted);
  font-size: 15px;
  min-height: 20px;
}

.char-en {
  color: var(--muted);
  font-size: 12.5px;
  line-height: 1.35;
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}

.char-count {
  color: var(--muted);
  font-size: 13px;
  margin-top: 3px;
}

.char-side {
  margin-left: auto;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 5px;
  flex-shrink: 0;
}

.char-no {
  color: var(--muted);
  font-size: 13px;
  flex-shrink: 0;
}

.hsk-badge {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.4px;
  line-height: 1.5;
  background: #eef0ff;
  color: var(--primary);
  border: 1px solid #c7d2fe;
  white-space: nowrap;
}

.hsk-badge-phrase {
  background: #ffffff;
  font-size: 11px;
  padding: 2px 8px;
}

.word-chip {
  display: inline-block;
  margin-left: 8px;
  padding: 2px 10px;
  border: 1px solid var(--primary);
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  color: var(--primary);
  background: #eef0ff;
  vertical-align: middle;
}

.word-chip .chip-speak {
  border: 0;
  background: transparent;
  padding: 0 0 0 6px;
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
  opacity: 0.85;
}

.word-chip .chip-speak:active {
  transform: scale(0.8);
}

.phrase-bar {
  margin-bottom: 12px;
}

.word-card {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 7px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--text);
  text-align: left;
}

.word-card-char {
  font-size: 22px;
  font-weight: 600;
  min-width: 44px;
  flex-shrink: 0;
  text-align: center;
}

.word-card-meta {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
  flex: 1;
}

.word-card-pinyin {
  font-size: 12px;
  color: var(--muted);
  line-height: 1.3;
}

.word-card-en {
  font-size: 11.5px;
  color: var(--muted);
  line-height: 1.3;
}

.phrase-speak {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  padding: 0;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  line-height: 1;
  background: #f1f3ff;
  color: var(--primary);
  border: 1px solid #dbe0f5;
  opacity: 0.9;
}

.phrase-speak:active {
  transform: scale(0.85);
  opacity: 1;
}

.phrase-speak.speaking {
  animation: speak-pulse 0.35s ease 2;
}

.grid-box {
  position: relative;
  border: 1px solid rgba(79, 70, 229, 0.35);
  border-radius: 12px;
  overflow: hidden;
  background: #fff;
}

.grid-box .writer svg {
  display: block;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.grid-tian {
  background-image:
    repeating-linear-gradient(to right, rgba(79, 70, 229, 0.4) 0 5px, transparent 5px 10px),
    repeating-linear-gradient(to bottom, rgba(79, 70, 229, 0.4) 0 5px, transparent 5px 10px);
  background-size: 100% 1px, 1px 100%;
  background-repeat: no-repeat;
  background-position: center;
}

.grid-mi {
  background-image:
    repeating-linear-gradient(to right, rgba(79, 70, 229, 0.4) 0 5px, transparent 5px 10px),
    repeating-linear-gradient(to bottom, rgba(79, 70, 229, 0.4) 0 5px, transparent 5px 10px);
  background-size: 100% 1px, 1px 100%;
  background-repeat: no-repeat;
  background-position: center;
}

/* The diagonal strokes of the Mi Zi Ge use the same dashed pattern as the
 * Tian Zi Ge cross (5px on, 5px off): a full-width dashed strip, rotated
 * 45° so it runs corner to corner. The element is doubled in size so the
 * rotated strip still reaches every corner of the box. A 1px-tall strip
 * rotated 45° shortens the apparent gaps, so the strip uses 6px on / 8px
 * off: projected onto the diagonal that lands on 5px on / 5px off. */
.grid-mi::before,
.grid-mi::after {
  content: "";
  position: absolute;
  inset: -50%;
  background-image: repeating-linear-gradient(to right, rgba(79, 70, 229, 0.4) 0 6px, transparent 6px 14px);
  background-size: 100% 1px;
  background-repeat: no-repeat;
  background-position: center;
  pointer-events: none;
}

.grid-mi::before {
  transform: rotate(45deg);
}

.grid-mi::after {
  transform: rotate(-45deg);
}

.grid-none {
  background-image: none;
  border-color: rgba(100, 116, 139, 0.35);
}

.practice-grid {
  width: 100%;
  aspect-ratio: 1 / 1;
}

.practice-btns {
  display: flex;
  gap: 8px;
  margin-top: 12px;
}

.practice-btns .btn {
  flex: 1;
  padding: 12px 6px;
  font-size: 14px;
}

.practice-btns .demo-btn {
  background: #eef0ff;
  color: var(--primary);
  border: 1px solid #dbe0f5;
  font-weight: 600;
}

.practice-btns .demo-btn:active {
  background: #e2e6ff;
}

.practice-btns .practice-btn {
  background: var(--primary);
  color: #fff;
  font-weight: 600;
}

.practice-btns .practice-btn:active {
  background: #4338ca;
}


.card.no-data .char-big {
  opacity: 0.55;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 90px;
  transform: translateX(-50%);
  background: rgba(17, 24, 39, 0.88);
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 14px;
  z-index: 100;
  max-width: 80vw;
  text-align: center;
  pointer-events: none;
}

.footer {
  text-align: center;
  color: var(--muted);
  font-size: 12px;
  padding: 0 16px 30px;
}

.footer-link {
  color: var(--primary);
  text-decoration: underline;
}

.footer-link:hover {
  color: var(--text);
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

.card.shake {
  animation: shake 0.25s ease;
}

/* Pinyin input conversion */
#pinyinCompose {
  margin-top: 10px;
}

.compose-box {
  background: #f8f9ff;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 10px 12px;
}

.compose-sheet {
  margin-top: 10px;
  border-top: 1px dashed var(--line);
  padding-top: 10px;
  max-height: 55vh;
  overflow-y: auto;
}

.compose-sheet-head {
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.compose-close {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  padding: 2px 6px;
}

.compose-close:hover {
  color: var(--text);
}

.compose-sheet-tip {
  font-size: 13px;
  color: #b91c1c;
}

.compose-sheet-tip.is-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: var(--muted);
}

.compose-sheet-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 18px 12px;
  color: var(--muted);
  font-size: 13px;
}

.loading-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(79, 70, 229, 0.25);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: loading-spin 0.8s linear infinite;
  flex-shrink: 0;
}

@keyframes loading-spin {
  to { transform: rotate(360deg); }
}

.compose-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 45vh;
  overflow-y: auto;
}

.cand-row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 7px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--text);
  cursor: pointer;
  text-align: left;
}

.cand-row .cand-main {
  font-size: 22px;
  font-weight: 600;
  min-width: 44px;
  flex-shrink: 0;
  text-align: center;
}

.cand-row .cand-meta {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}

.cand-row .cand-py {
  font-size: 12px;
  color: var(--muted);
  line-height: 1.3;
}

.cand-row .cand-en {
  font-size: 11.5px;
  color: var(--muted);
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.cand-row.active {
  border-color: var(--primary);
  background: #eef0ff;
  color: var(--primary);
  font-weight: 700;
}

.cand-row.active .cand-main,
.cand-row.active .cand-py,
.cand-row.active .cand-en {
  color: var(--primary);
}

.cand {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 58px;
  min-height: 60px;
  padding: 5px 8px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--text);
  cursor: pointer;
}

.cand .cand-main {
  font-size: 20px;
  font-weight: 600;
  line-height: 1.25;
}

.cand .cand-meta {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 2px;
  max-width: 120px;
}

.cand .cand-py {
  font-size: 10.5px;
  color: var(--muted);
  line-height: 1.2;
}

.cand .cand-en {
  font-size: 10px;
  color: var(--muted);
  line-height: 1.25;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 120px;
}

.cand.word {
  min-width: 0;
  max-width: 170px;
  padding: 5px 10px;
}

.cand.word .cand-main {
  font-size: 17px;
  letter-spacing: 1px;
}

.cand.active {
  border-color: var(--primary);
  background: #eef0ff;
  color: var(--primary);
  font-weight: 700;
}

.back-to-top {
  position: fixed;
  right: 18px;
  bottom: 26px;
  z-index: 30;
  width: 46px;
  height: 46px;
  border: none;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 6px 18px rgba(79, 70, 229, 0.35);
  -webkit-tap-highlight-color: transparent;
}

.back-to-top:active {
  transform: scale(0.92);
}
