:root{
    --bg: #f5f7fb;
    --card: #ffffff;
    --line: #e5e8ef;
    --text: #1c1f26;
    --sub: #6b7280;
    --brand: #2563eb;
    --brand-strong: #1d4ed8;
    --ok: #16a34a;
    --close: #d97706;
    --ng: #dc2626;
    --shadow: 0 1px 3px rgba(0,0,0,0.05), 0 1px 2px rgba(0,0,0,0.06);
}
*{box-sizing:border-box}
html,body{margin:0;padding:0;background:var(--bg);color:var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans", "Yu Gothic", "Meiryo", sans-serif;
    -webkit-tap-highlight-color: transparent;
    overscroll-behavior: contain;
}
button{font-family:inherit;font-size:15px;padding:10px 16px;border-radius:10px;border:1px solid var(--line);background:#fff;cursor:pointer;color:var(--text)}
button.primary{background:var(--brand);color:#fff;border-color:var(--brand)}
button.primary:hover{background:var(--brand-strong)}
button.secondary{background:#fff}
button.danger{background:#fff;color:var(--ng);border-color:var(--ng)}
button.big{padding:14px 28px;font-size:17px}
button:disabled{opacity:.4;cursor:not-allowed}
input[type="text"],input[type="search"],select,input[type="file"]{
    font:inherit;padding:9px 12px;border:1px solid var(--line);border-radius:10px;background:#fff;color:var(--text);min-width:0;
}
select{padding-right:32px}
a{color:var(--brand);text-decoration:none}
a:hover{text-decoration:underline}

/* --- Header --- */
.topbar{position:sticky;top:0;background:#fff;border-bottom:1px solid var(--line);z-index:10}
.topbar .brand{padding:12px 16px 4px;font-weight:700;font-size:16px}
.tabs{display:flex;gap:4px;padding:0 12px 8px;overflow-x:auto}
.tab{padding:8px 14px;border-radius:999px;border:1px solid var(--line);background:#fff;font-size:14px;white-space:nowrap}
.tab.active{background:var(--brand);color:#fff;border-color:var(--brand)}

/* --- Layout --- */
#app{max-width:960px;margin:0 auto;padding:16px}
.view{animation:fade .15s}
@keyframes fade{from{opacity:0}to{opacity:1}}
h2{margin:8px 0 12px;font-size:20px}
h3{margin:0 0 8px;font-size:16px}
.card{background:var(--card);border:1px solid var(--line);border-radius:14px;padding:16px;margin-bottom:14px;box-shadow:var(--shadow)}
.row{display:flex;align-items:center;gap:10px;flex-wrap:wrap;margin:8px 0}
.label{color:var(--sub);font-size:13px}
.note{color:var(--sub);font-size:13px;line-height:1.5}
.segmented{display:inline-flex;background:#eef1f7;border-radius:10px;padding:3px}
.seg{background:transparent;border:none;padding:8px 14px;border-radius:8px;font-size:14px;color:var(--sub)}
.seg.active{background:#fff;color:var(--text);box-shadow:var(--shadow);font-weight:600}
.stat-grid{display:grid;grid-template-columns:repeat(3,1fr);gap:14px}
.stat-grid > div{display:flex;flex-direction:column;gap:4px;text-align:center}
.stat-grid .k{color:var(--sub);font-size:12px}
.stat-grid .v{font-size:22px;font-weight:700}
.btn-link{display:inline-block;padding:10px 18px;background:var(--brand);color:#fff;border-radius:10px;text-decoration:none;font-weight:600;font-size:15px}
.btn-link:hover{background:var(--brand-strong);text-decoration:none}
.toggle{display:inline-flex;align-items:center;gap:6px;font-size:14px;color:var(--sub)}
code{background:#eef1f7;padding:1px 6px;border-radius:4px;font-size:13px}

/* --- Test --- */
.test-header{display:flex;justify-content:space-between;align-items:center;gap:10px;margin-bottom:8px}
.progress{font-weight:700;font-size:18px;color:var(--sub)}
.progress #q-idx{color:var(--brand)}
.question{background:#fff;border:1px solid var(--line);border-radius:14px;padding:20px;margin-bottom:10px;text-align:center;box-shadow:var(--shadow)}
.q-cat{color:var(--sub);font-size:13px}
.q-ja{font-size:34px;font-weight:700;margin:8px 0 4px;line-height:1.2;word-break:keep-all}
.q-hint{color:var(--sub);font-size:13px}
.canvas-wrap{position:relative;background:#fff;border:1px solid var(--line);border-radius:14px;overflow:hidden;box-shadow:var(--shadow)}
#pad{display:block;width:100%;height:auto;touch-action:none;background:
    repeating-linear-gradient(to bottom, transparent 0 59px, rgba(0,0,0,.04) 59px 60px)}
.baseline{display:none}
.pad-actions{display:flex;gap:8px;flex-wrap:wrap;margin:10px 0;align-items:center}
#text-input{flex:1;min-width:200px;font-size:22px;letter-spacing:.02em}
.nav-buttons{display:flex;justify-content:space-between;gap:8px;margin-top:8px}
.thumb-strip{display:grid;grid-template-columns:repeat(auto-fill,minmax(60px,1fr));gap:6px;margin-top:14px}
.thumb{aspect-ratio:1;border-radius:8px;border:1px solid var(--line);background:#fff;font-size:12px;color:var(--sub);display:flex;align-items:center;justify-content:center;cursor:pointer;position:relative}
.thumb.active{border-color:var(--brand);color:var(--brand);font-weight:700;background:#eef4ff}
.thumb.answered::after{content:"";position:absolute;right:4px;top:4px;width:6px;height:6px;border-radius:999px;background:var(--brand)}

/* --- Result --- */
.result-summary{display:flex;align-items:baseline;justify-content:center;gap:12px;margin:10px 0 18px}
.big-score{font-size:44px;font-weight:800}
.big-score #score-correct{color:var(--ok)}
.score-pct{color:var(--sub);font-size:20px}
.result-list{display:flex;flex-direction:column;gap:8px}
.result-item{display:grid;grid-template-columns:44px 1fr auto;gap:10px;align-items:center;background:#fff;border:1px solid var(--line);border-radius:12px;padding:10px 12px}
.result-item .badge{width:32px;height:32px;border-radius:999px;display:flex;align-items:center;justify-content:center;font-weight:700;color:#fff}
.badge.correct{background:var(--ok)}
.badge.close{background:var(--close)}
.badge.wrong{background:var(--ng)}
.result-item .body{display:flex;flex-direction:column;gap:2px;min-width:0}
.result-item .ja{font-weight:600}
.result-item .answers{color:var(--sub);font-size:13px}
.result-item .answers .yours{color:var(--text)}
.result-item .answers .expected{margin-left:10px;color:var(--ok);font-weight:600}
.result-item .miniimg{width:120px;height:36px;object-fit:contain;background:#fafbfd;border:1px solid var(--line);border-radius:6px}

/* --- Word list --- */
.word-list{display:grid;grid-template-columns:repeat(auto-fill,minmax(260px,1fr));gap:8px}
.word-card{background:#fff;border:1px solid var(--line);border-radius:12px;padding:10px 12px;display:flex;justify-content:space-between;align-items:center;gap:10px;cursor:pointer}
.word-card:hover{border-color:var(--brand)}
.word-card .no{color:var(--sub);font-size:12px;width:32px}
.word-card .ja{flex:1;font-weight:600}
.word-card .en{color:var(--sub);font-size:13px}
.word-card .cat{font-size:11px;color:var(--brand);background:#eef4ff;padding:2px 6px;border-radius:999px}

/* --- History --- */
.history-list{display:flex;flex-direction:column;gap:8px}
.history-item{background:#fff;border:1px solid var(--line);border-radius:12px;padding:12px 14px}
.history-head{display:flex;justify-content:space-between;align-items:center;gap:10px;flex-wrap:wrap}
.history-date{font-weight:700}
.history-score{font-size:14px}
.history-score .ok{color:var(--ok);font-weight:700}
.history-detail{margin-top:8px;color:var(--sub);font-size:13px;line-height:1.6}
.history-detail .w{display:inline-block;background:#fdecec;color:var(--ng);padding:2px 8px;border-radius:999px;margin:2px 4px 2px 0;font-size:12px}
.history-detail .c{display:inline-block;background:#e8f7ee;color:var(--ok);padding:2px 8px;border-radius:999px;margin:2px 4px 2px 0;font-size:12px}
.history-actions{margin-top:8px;text-align:right}

/* --- Toast & loading --- */
[hidden]{display:none !important}
.toast{position:fixed;bottom:24px;left:50%;transform:translateX(-50%);background:#1c1f26;color:#fff;padding:10px 16px;border-radius:999px;font-size:14px;z-index:100;box-shadow:var(--shadow)}
.loading{position:fixed;inset:0;background:rgba(255,255,255,.75);display:flex;flex-direction:column;align-items:center;justify-content:center;gap:14px;z-index:200}
.spinner{width:36px;height:36px;border-radius:999px;border:3px solid var(--line);border-top-color:var(--brand);animation:spin .8s linear infinite}
@keyframes spin{to{transform:rotate(360deg)}}

@media (max-width:640px){
    .q-ja{font-size:26px}
    #pad{background:repeating-linear-gradient(to bottom, transparent 0 39px, rgba(0,0,0,.04) 39px 40px)}
    .stat-grid .v{font-size:18px}
    .result-item{grid-template-columns:36px 1fr;grid-template-rows:auto auto}
    .result-item .miniimg{grid-column:1 / -1;width:100%;height:44px}
    .word-list{grid-template-columns:1fr}
}
