:root {
  --bg: #f5f5f5; --surface: #fff; --text: #1a1a2e; --dim: #555; --dimmer: #999; --border: #e0e0e0;
  --radius: 10px; --shadow: 0 2px 8px rgba(0, 0, 0, .08);
  --accent: #3949ab; --accent-soft: #e8eaf6;
  --good: #2e7d32; --good-bg: #e8f5e9; --fair: #e65100; --fair-bg: #fff3e0; --poor: #c62828; --poor-bg: #ffebee;
  --rec: #d32f2f;
  --jp: "Hiragino Kaku Gothic Pro", "Hiragino Sans", "Yu Gothic", "YuGothic", "Meiryo", sans-serif;
  --en: Georgia, "Times New Roman", serif;
  --ipa: "Charis SIL", "Doulos SIL", "Segoe UI", "Lucida Sans Unicode", "Arial Unicode MS", "Hiragino Sans", sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font-family: var(--jp); font-size: 15px; line-height: 1.6; -webkit-text-size-adjust: 100%; }
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.dim { color: var(--dim); }
.small { font-size: 13px; }
.center { text-align: center; }

/* ---------- top bar */
.topbar { position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 10px; min-height: 52px;
  padding: 8px max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left)); background: var(--surface); border-bottom: 1px solid var(--border); }
.topbar h1 { flex: 1; margin: 0; font-size: 17px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-back { border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 6px 12px; cursor: pointer; font-size: 14px; }
.topbar-progress { color: var(--dim); font-size: 14px; white-space: nowrap; }

.screen { max-width: 720px; margin: 0 auto; padding: 14px 12px 40px; }
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 16px; }
.section-title { font-size: 15px; margin: 22px 2px 10px; color: var(--dim); font-weight: 700; }
.intro p { margin: 4px 0; }

/* ---------- menu */
.set-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.set-card { text-align: left; border: 1px solid var(--border); background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 12px 14px; cursor: pointer; min-height: 72px; display: flex; flex-direction: column; justify-content: center; gap: 2px; }
.set-card:active { transform: scale(.99); }
.set-card b { font-size: 15px; }
.set-card span { font-family: var(--en); color: var(--dim); font-size: 14px; }
.set-card .best { font-family: var(--jp); font-size: 12px; color: var(--accent); }
.custom textarea { width: 100%; border: 1px solid var(--border); border-radius: 8px; padding: 10px; font-family: var(--en); font-size: 17px; resize: vertical; background: #fafafa; }
.custom-row { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-top: 8px; }
.custom-row .btn { flex-shrink: 0; white-space: nowrap; }
.free-intro { margin: 0 0 4px; }
.free-card { margin-top: 10px; }
.free-label { display: block; font-size: 13px; color: var(--dim); margin-bottom: 6px; font-weight: 700; }
.target.hint { font-family: var(--jp); font-size: 16px; color: var(--dim); line-height: 1.7; }

.gate { margin-top: 24px; text-align: center; }
.gate p { margin: 6px 0 14px; }
.gate-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.gate-actions a.btn { text-decoration: none; display: inline-flex; align-items: center; }
.btn { border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 10px 18px; cursor: pointer; font-size: 15px; min-height: 44px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 700; }
.btn:disabled { opacity: .4; cursor: default; }

/* ---------- practice */
.practice-grid { display: flex; flex-direction: column; gap: 12px; }
.target-card { padding: 18px 16px; }
.target { font-family: var(--en); font-size: 22px; line-height: 1.75; word-spacing: 2px; }
.w { cursor: default; border-radius: 4px; padding: 0 1px; }
.w.done { cursor: pointer; }
.w.good { color: var(--good); }
.w.fair { color: var(--fair); text-decoration: underline dotted 2px; text-underline-offset: 5px; }
.w.poor { color: var(--poor); background: var(--poor-bg); text-decoration: underline 2px; text-underline-offset: 5px; }
.w.skip { color: var(--dimmer); text-decoration: line-through; }
.w.sel { outline: 2px solid var(--accent); outline-offset: 1px; }

.word-detail { margin-top: 10px; }
.wd-head { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.wd-word { font-family: var(--en); font-size: 24px; font-weight: 700; }
.badge { font-size: 13px; padding: 1px 8px; border-radius: 10px; font-weight: 700; }
.badge.good { color: var(--good); background: var(--good-bg); } .badge.fair { color: var(--fair); background: var(--fair-bg); } .badge.poor { color: var(--poor); background: var(--poor-bg); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0 6px; }
.chip { font-family: var(--ipa); font-size: 18px; min-width: 38px; text-align: center; padding: 4px 8px; border-radius: 8px; border: 1px solid transparent; }
.chip.good { background: var(--good-bg); color: var(--good); } .chip.fair { background: var(--fair-bg); color: var(--fair); border-color: #ffcc80; } .chip.poor { background: var(--poor-bg); color: var(--poor); border-color: #ef9a9a; }
.chip.extra { background: #fff; color: var(--poor); border: 1px dashed var(--poor); font-family: var(--jp); font-size: 14px; }
.msgs { list-style: none; margin: 6px 0 0; padding: 0; }
.msgs li { margin: 6px 0; padding: 8px 10px; background: #fafafa; border-radius: 8px; border-left: 3px solid var(--fair); }
.msgs li.poor { border-left-color: var(--poor); }
.msgs .hint { display: block; color: var(--dim); font-size: 14px; }
.wd-actions { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.wd-good { color: var(--good); margin: 8px 0 0; }

.controls { display: flex; align-items: center; justify-content: center; gap: 22px; padding: 6px 0; }
.btn.round-sm { width: 64px; height: 64px; min-height: 0; border-radius: 50%; padding: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0; font-size: 11px; white-space: nowrap; box-shadow: var(--shadow); }
.round-sm svg { flex: none; width: 24px; height: 24px; fill: var(--accent); }
.btn.rec { position: relative; width: 84px; height: 84px; min-height: 0; border-radius: 50%; border: none; background: var(--rec); color: #fff; padding: 0; box-shadow: 0 3px 10px rgba(211, 47, 47, .35); display: flex; flex-direction: column; align-items: center; justify-content: center; }
.rec .rec-dot { width: 22px; height: 22px; border-radius: 50%; background: #fff; transition: border-radius .15s; }
.rec .rec-label { font-size: 12px; margin-top: 4px; font-weight: 700; }
.rec.on .rec-dot { border-radius: 4px; }
.rec-ring { position: absolute; inset: -8px; border-radius: 50%; border: 4px solid rgba(211, 47, 47, .35); transform: scale(1); opacity: 0; pointer-events: none; }
.rec.on .rec-ring { opacity: 1; }
.rec.busy { background: #9e9e9e; box-shadow: none; }
.status { text-align: center; min-height: 1.6em; margin: 2px 0 0; color: var(--dim); }
.file-row { display: block; text-align: center; font-size: 13px; color: var(--dim); }

.result { padding: 16px; }
.score-row { display: flex; align-items: center; gap: 16px; }
.score-ring { position: relative; width: 104px; height: 104px; flex: none; }
.score-ring svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring-bg { fill: none; stroke: #eee; stroke-width: 10; }
.ring-fg { fill: none; stroke: var(--accent); stroke-width: 10; stroke-linecap: round; stroke-dasharray: 326.7; stroke-dashoffset: 326.7; transition: stroke-dashoffset .6s ease; }
.score-num { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1.1; }
.score-num span { font-size: 32px; font-weight: 700; font-family: var(--en); }
.score-num small { font-size: 12px; color: var(--dim); }
.bars { flex: 1; min-width: 0; }
.bar-row { display: grid; grid-template-columns: 6.5em 1fr 2.2em; align-items: center; gap: 8px; font-size: 13px; margin: 5px 0; }
.bar { height: 8px; background: #eee; border-radius: 4px; overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--accent); border-radius: 4px; transition: width .6s ease; }
.bar-val { text-align: right; font-weight: 700; }
.result-note { margin: 10px 0 0; font-size: 14px; }
.issues { margin-top: 8px; }
.issue { display: block; width: 100%; text-align: left; border: 1px solid var(--border); background: #fafafa; border-radius: 8px; padding: 8px 10px; margin-top: 6px; cursor: pointer; }
.issue b { font-family: var(--en); font-size: 16px; margin-right: 6px; }
.issue .hint { display: block; color: var(--dim); font-size: 13px; }
.issues-title { font-size: 13px; color: var(--dim); margin: 12px 0 0; font-weight: 700; }
.nav-row { display: flex; gap: 10px; justify-content: flex-end; margin-top: 12px; }
.nav-row .btn { flex: 1; max-width: 220px; }

.loader { position: fixed; inset: 0; background: rgba(245, 245, 245, .92); z-index: 50; display: flex; align-items: center; justify-content: center; padding: 20px; }
.loader-box { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px 22px; width: min(420px, 100%); text-align: center; }
.loader-bar { height: 10px; background: #eee; border-radius: 5px; overflow: hidden; margin: 12px 0 6px; }
.loader-bar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .2s; }

/* ---------- iPhone landscape: two columns, less vertical chrome */
@media (orientation: landscape) and (max-height: 520px) {
  .topbar { min-height: 40px; padding-top: 4px; padding-bottom: 4px; }
  .topbar h1 { font-size: 15px; }
  .screen { max-width: 980px; padding: 8px 12px 20px; }
  .set-grid { grid-template-columns: repeat(3, 1fr); }
  .practice-grid { display: grid; grid-template-columns: 1.05fr .95fr; gap: 12px; align-items: start; }
  .target { font-size: 19px; line-height: 1.6; }
  .target-card { padding: 12px 14px; }
  .controls { gap: 16px; padding: 2px 0; }
  .btn.rec { width: 62px; height: 62px; }
  .rec .rec-dot { width: 18px; height: 18px; }
  .rec .rec-label { font-size: 11px; margin-top: 2px; }
  .btn.round-sm { width: 50px; height: 50px; font-size: 10px; }
  .round-sm svg { width: 20px; height: 20px; }
  .status { margin: 0; font-size: 13px; }
  .score-ring { width: 84px; height: 84px; }
  .score-num span { font-size: 24px; }
  .score-num small { font-size: 10px; }
  .result { padding: 12px; }
  /* each pane scrolls on its own so the record button never leaves the screen */
  .pane-left, .pane-right { max-height: calc(100vh - 52px); overflow-y: auto; padding-bottom: 8px; }
  .nav-row { margin-top: 8px; }
  .nav-row .btn { min-height: 44px; }
}

/* ---------- tablets (both orientations): larger type and targets */
@media (min-width: 768px) and (min-height: 600px) {
  html, body { font-size: 17px; }
  .topbar { min-height: 60px; }
  .topbar h1 { font-size: 20px; }
  .topbar-back, .btn { font-size: 17px; min-height: 56px; padding: 12px 22px; }
  .section-title { font-size: 17px; }
  .set-card { min-height: 88px; padding: 14px 18px; }
  .set-card b { font-size: 18px; }
  .set-card span { font-size: 16px; }
  .custom textarea { font-size: 20px; }
  .target { font-size: 28px; }
  .target-card { padding: 24px 26px; }
  .wd-word { font-size: 30px; }
  .chip { font-size: 22px; min-width: 46px; padding: 6px 10px; }
  .msgs li { font-size: 17px; }
  .msgs .hint { font-size: 16px; }
  .btn.rec { width: 104px; height: 104px; }
  .rec .rec-dot { width: 28px; height: 28px; }
  .rec .rec-label { font-size: 14px; }
  .btn.round-sm { width: 80px; height: 80px; font-size: 13px; }
  .round-sm svg { width: 30px; height: 30px; }
  .score-ring { width: 130px; height: 130px; }
  .score-num span { font-size: 40px; }
  .score-num small { font-size: 14px; }
  .bar-row { font-size: 15px; grid-template-columns: 7em 1fr 2.4em; }
  .bar { height: 10px; }
  .issue b { font-size: 19px; }
  .issue { padding: 10px 14px; font-size: 16px; }
  .issue .hint { font-size: 15px; }
  .nav-row .btn { min-height: 72px; max-width: 280px; }
}
/* iPad portrait: one wide column */
@media (min-width: 768px) and (min-height: 600px) and (orientation: portrait) {
  .screen { max-width: 880px; padding: 20px 24px 48px; }
  .set-grid { grid-template-columns: repeat(3, 1fr); gap: 14px; }
}
/* iPad landscape: two panes, results stay in view */
@media (min-width: 1000px) and (min-height: 600px) and (orientation: landscape) {
  .screen { max-width: 1240px; padding: 20px 28px 40px; }
  .set-grid { grid-template-columns: repeat(3, 1fr); gap: 14px; }
  #screen-menu { max-width: 1100px; }
  .practice-grid { display: grid; grid-template-columns: 1.15fr .85fr; gap: 20px; align-items: start; }
  .pane-right { position: sticky; top: 80px; }
  .target { font-size: 30px; }
}
