:root {
    --bg: #fff6ec;
    --ink: #3b3340;
    --muted: #8a7f8e;
    --card: #ffffff;
    --line: #f0dccf;
    --pink: #ff8fb1;
    --pink-dark: #e56f93;
    --pink-soft: #ffe1ea;
    --mint: #5fcfa6;
    --mint-dark: #43b18a;
    --mint-soft: #dcf7ec;
    --note: #fff1df;
    --note-ink: #8a5a2b;
    --guide: rgba(230, 90, 90, .28);
    --radius: 22px;
    --shadow: 0 6px 0 rgba(59, 51, 64, .06), 0 12px 30px rgba(255, 143, 177, .15);
    /* Fredoka's tone-marked vowels (ī, ǚ…) render badly, so pinyin gets Nunito. */
    --pinyin-font: 'Nunito', system-ui, sans-serif;
    --hanzi-font: 'Noto Sans SC', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
    min-height: 100vh;
    background:
        radial-gradient(circle at 10% 0%, #ffe9f1 0, transparent 40%),
        radial-gradient(circle at 100% 35%, #e3f8ef 0, transparent 35%),
        var(--bg);
    background-attachment: fixed;
    color: var(--ink);
    font-family: 'Fredoka', system-ui, sans-serif;
    font-size: 17px;
}
main { max-width: 520px; margin: 0 auto; padding: 16px 16px 32px; }
.screen { animation: pop .25s ease; }
@keyframes pop { from { opacity: 0; transform: translateY(6px); } }
.hanzi { font-family: var(--hanzi-font); }
.pinyin, .word .py, #wd-pinyin { font-family: var(--pinyin-font); }
.spacer { width: 60px; }

/* Brand + panda */
.brand { font-family: 'Ma Shan Zheng', var(--hanzi-font); font-weight: 400; font-size: 52px; margin: 4px 0 0; text-align: center; }
.brand span { display: block; font-family: 'Fredoka', sans-serif; font-weight: 600; font-size: 20px; color: var(--pink); }
.brand.small { font-size: 30px; text-align: left; margin: 0; }
.brand.small span { display: inline; font-size: 18px; margin-left: 8px; }
.tagline { text-align: center; color: var(--muted); margin: 6px 0 18px; }
.panda svg { display: block; width: 110px; height: auto; margin: 8px auto 0; animation: bob 3s ease-in-out infinite; }
@keyframes bob { 50% { transform: translateY(-5px) rotate(-2deg); } }

/* Cards, forms, buttons */
.card { background: var(--card); border: 2px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
label { display: block; font-weight: 600; font-size: 15px; margin: 0 0 12px; }
input, textarea {
    display: block; width: 100%; margin-top: 6px; padding: 12px 14px;
    font: inherit; color: var(--ink); background: #fffdfb;
    border: 2px solid var(--line); border-radius: 14px;
}
input:focus, textarea:focus { outline: none; border-color: var(--pink); }
.row { display: flex; gap: 10px; flex-wrap: wrap; }
.row > * { flex: 1; }
.btn {
    font: inherit; font-weight: 600; color: var(--ink); background: #fff;
    border: none; border-radius: 16px; padding: 13px 16px; cursor: pointer;
    box-shadow: inset 0 0 0 2px var(--line), 0 4px 0 var(--line);
    transition: transform .08s;
}
.btn:active:not(:disabled) { transform: translateY(3px); box-shadow: inset 0 0 0 2px var(--line), 0 1px 0 var(--line); }
.btn.primary { background: var(--pink); color: #fff; box-shadow: 0 4px 0 var(--pink-dark); }
.btn.primary:active:not(:disabled) { box-shadow: 0 1px 0 var(--pink-dark); }
.btn.mint { background: var(--mint); color: #fff; box-shadow: 0 4px 0 var(--mint-dark); }
.btn.mint:active:not(:disabled) { box-shadow: 0 1px 0 var(--mint-dark); }
.btn.pink { background: var(--pink-soft); box-shadow: 0 4px 0 #f5c1d0; }
.btn.danger { color: #d64a6b; }
.btn.small { padding: 9px 10px; font-size: 15px; }
.btn.wide { width: 100%; margin-top: 14px; }
.btn:disabled { opacity: .45; cursor: default; }
.link { background: none; border: none; font: inherit; color: var(--muted); cursor: pointer; padding: 6px; }
.msg { color: #d64a6b; min-height: 1.2em; margin: 10px 0 0; }
.note { background: var(--note); color: var(--note-ink); border-radius: 12px; padding: 8px 12px; font-size: 14px; }
.muted { color: var(--muted); margin: 4px 0 0; }

/* Home */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 14px; }
.topbar h2 { margin: 0; font-size: 20px; flex: 1; text-align: center; }
.hero { display: flex; align-items: center; gap: 14px; margin-bottom: 14px; }
.hero .panda svg { width: 84px; margin: 0; }
.big-count { font-size: 26px; font-weight: 600; margin: 0; }
.row.big .btn { font-size: 19px; padding: 16px; }
.word-list { list-style: none; padding: 0; margin: 18px 0 0; display: grid; gap: 10px; }
.word {
    width: 100%; display: grid; grid-template-columns: auto 1fr; column-gap: 14px; align-items: center;
    text-align: left; font: inherit; color: var(--ink); cursor: pointer;
    background: var(--card); border: 2px solid var(--line); border-radius: 18px; padding: 10px 14px;
}
.word .hanzi { grid-row: span 2; font-size: 30px; font-weight: 700; }
.word .py { color: var(--pink-dark); font-weight: 600; }
.word .mn { color: var(--muted); font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.word.again { border-color: var(--pink); background: #fff7fa; }

/* Add word */
.building { display: flex; align-items: center; justify-content: center; min-height: 76px; margin-bottom: 12px; padding: 10px; }
.building .hanzi { font-size: 40px; font-weight: 700; letter-spacing: 4px; }
.placeholder { color: var(--muted); }
.pad-wrap {
    position: relative; width: 100%; max-width: 420px; aspect-ratio: 1; margin: 0 auto;
    border: 3px solid #f2b8b8; border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow);
    background-color: #fffaf3;
    /* 米字格: centre cross + both diagonals */
    background-image:
        linear-gradient(var(--guide), var(--guide)),
        linear-gradient(var(--guide), var(--guide)),
        linear-gradient(to top right, transparent calc(50% - 1px), var(--guide) 50%, transparent calc(50% + 1px)),
        linear-gradient(to bottom right, transparent calc(50% - 1px), var(--guide) 50%, transparent calc(50% + 1px));
    background-size: 100% 2px, 2px 100%, 100% 100%, 100% 100%;
    background-position: center;
    background-repeat: no-repeat;
}
#pad { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; color: var(--ink); cursor: crosshair; }
.pad-status {
    position: absolute; inset: 0; width: 100%; border: none; font: inherit; font-weight: 600;
    background: rgba(255, 250, 243, .88); color: var(--muted); cursor: pointer;
}
.candidates { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; min-height: 60px; margin: 14px 0 6px; }
.cand {
    width: 56px; height: 56px; font-size: 30px; color: var(--ink); cursor: pointer;
    background: #fff; border: 2px solid var(--line); border-radius: 16px; box-shadow: 0 3px 0 var(--line);
}
.cand:first-child { border-color: var(--mint); background: var(--mint-soft); }
.tools { margin-top: 8px; }

/* Word dialog */
dialog {
    width: min(92vw, 420px); padding: 20px; border: none; border-radius: var(--radius);
    color: var(--ink); background: var(--card); box-shadow: 0 20px 60px rgba(59, 51, 64, .25);
}
dialog::backdrop { background: rgba(59, 51, 64, .35); }
.hanzi.big { font-size: 54px; font-weight: 700; text-align: center; margin: 0 0 8px; }

/* Play */
.count-options { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.btn.count { font-size: 30px; padding: 26px 10px; }
.btn.count small { display: block; font-size: 14px; font-weight: 400; color: var(--muted); }
.progress { font-weight: 600; color: var(--muted); }
.flip { width: 100%; max-width: 420px; aspect-ratio: 3 / 4; margin: 0 auto 18px; perspective: 1200px; cursor: pointer; outline: none; }
.flip-inner { position: relative; width: 100%; height: 100%; transform-style: preserve-3d; transition: transform .5s cubic-bezier(.3, 1.4, .5, 1); }
.flip.flipped .flip-inner { transform: rotateY(180deg); }
.flip.instant .flip-inner { transition: none; }
.face {
    position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
    padding: 24px; text-align: center; border: 3px solid var(--line); border-radius: 28px; box-shadow: var(--shadow);
    backface-visibility: hidden; -webkit-backface-visibility: hidden;
}
.face.front { background: linear-gradient(160deg, #fff 0%, #fff1f5 100%); }
.face.back { background: linear-gradient(160deg, #fff 0%, #eafaf3 100%); transform: rotateY(180deg); }
#card-hanzi { font-size: calc(min(84vw, 420px) * .75 / max(var(--len, 1), 1.5)); font-weight: 700; line-height: 1.1; }
.hint { position: absolute; bottom: 18px; font-size: 14px; color: var(--muted); }
#card-hanzi-small { font-size: 44px; font-weight: 700; }
.pinyin { font-size: 30px; font-weight: 600; color: var(--pink-dark); }
.meaning { font-size: 18px; max-height: 40%; overflow: auto; }
.grade .btn { font-size: 20px; padding: 18px; }
.invisible { visibility: hidden; }

/* Results */
#screen-results { text-align: center; }
#res-score { font-size: 48px; margin: 10px 0 0; }
#screen-results h3 { margin: 22px 0 0; text-align: left; }
#screen-results .row { margin-top: 20px; }

.toast {
    position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 10;
    max-width: calc(100vw - 32px); padding: 12px 18px; border-radius: 999px;
    background: var(--ink); color: #fff; font-weight: 600; box-shadow: 0 8px 24px rgba(0, 0, 0, .2);
}
footer { text-align: center; color: var(--muted); font-size: 12px; padding: 0 16px 24px; }
footer a { color: inherit; }
