/* ── Design Tokens ── */
:root {
  --bg:         #F2F2F7;
  --surface:    #FFFFFF;
  --surface-2:  #F2F2F7;
  --surface-3:  #E5E5EA;
  --text:       #1C1C1E;
  --text-2:     #48484A;
  --text-3:     #8E8E93;
  --border:     rgba(60,60,67,0.13);
  --accent:     #5E5CE6;
  --accent-fg:  #FFFFFF;
  --ok:         #34C759;
  --ok-bg:      #EBF9EF;
  --ok-text:    #166534;
  --err:        #FF3B30;
  --err-bg:     #FFF0EF;
  --err-text:   #B91C1C;
  --sh-sm: 0 0 0 0.5px rgba(0,0,0,0.06), 0 1px 4px rgba(0,0,0,0.07);
  --sh-md: 0 0 0 0.5px rgba(0,0,0,0.06), 0 4px 20px rgba(0,0,0,0.09);
  --r-sm: 8px; --r-md: 12px; --r-lg: 16px; --r-xl: 22px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:        #000000;
    --surface:   #1C1C1E;
    --surface-2: #2C2C2E;
    --surface-3: #3A3A3C;
    --text:      #FFFFFF;
    --text-2:    rgba(235,235,245,0.82);
    --text-3:    rgba(235,235,245,0.48);
    --border:    rgba(255,255,255,0.10);
    --accent:    #7C7AE8;
    --ok:        #30D158;
    --ok-bg:     rgba(48,209,88,0.16);
    --ok-text:   #4ADE80;
    --err:       #FF453A;
    --err-bg:    rgba(255,69,58,0.16);
    --err-text:  #F87171;
    --sh-sm: 0 0 0 0.5px rgba(255,255,255,0.05), 0 1px 4px rgba(0,0,0,0.35);
    --sh-md: 0 0 0 0.5px rgba(255,255,255,0.05), 0 4px 20px rgba(0,0,0,0.50);
  }
}

/* ── Base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { -webkit-text-size-adjust: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Noto Sans JP', 'Hiragino Sans', 'Yu Gothic', -apple-system, BlinkMacSystemFont, sans-serif;
  -webkit-font-smoothing: antialiased;
  min-height: 100svh;
}

#app {
  max-width: 480px;
  margin: 0 auto;
  padding: max(env(safe-area-inset-top), 16px) 16px calc(env(safe-area-inset-bottom, 0px) + 32px);
}

.sr-only { position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0,0,0,0); }

/* ── App Header ── */
.app-header {
  text-align: center;
  padding-bottom: 16px;
}

.app-header h1 {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-3);
}

/* ── Mode Tabs (segmented control) ── */
.top-tabs {
  background: var(--surface-3);
  border-radius: 13px;
  padding: 3px;
  display: flex;
  gap: 2px;
  margin-bottom: 14px;
}

.top-tab {
  flex: 1;
  padding: 9px 8px;
  border: none;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-3);
  background: transparent;
  cursor: pointer;
  transition: all 0.2s ease;
  min-height: 38px;
  font-family: inherit;
}

.top-tab.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--sh-sm);
  font-weight: 600;
}

/* ── Filter Card ── */
.filter-card {
  background: var(--surface);
  border-radius: var(--r-xl);
  margin-bottom: 14px;
  box-shadow: var(--sh-sm);
  overflow: hidden;
}

.filter-section {
  padding: 14px 16px;
}

.filter-section + .filter-section {
  border-top: 0.5px solid var(--border);
}

.filter-label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-3);
  margin-bottom: 10px;
  display: block;
}

/* ── Kana Mode Toggle ── */
.kana-toggle {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.ktog {
  padding: 9px 4px;
  border: 1.5px solid var(--border);
  border-radius: var(--r-md);
  font-size: clamp(11px, 3vw, 13px);
  font-weight: 500;
  color: var(--text-3);
  background: var(--surface-2);
  cursor: pointer;
  transition: all 0.15s;
  text-align: center;
  min-height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: inherit;
}

.ktog.on {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: var(--accent);
}

/* ── Row Chips ── */
.row-controls {
  display: flex;
  gap: 12px;
  margin-bottom: 10px;
}

.rc-btn {
  background: none;
  border: none;
  font-size: 13px;
  font-weight: 500;
  color: var(--accent);
  cursor: pointer;
  padding: 0;
  font-family: inherit;
  min-height: 28px;
}

.row-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 5px;
}

.rchip-header {
  grid-column: span 4;
  font-size: 10px;
  font-weight: 600;
  color: var(--text-3);
  padding: 6px 2px 0;
  letter-spacing: 0.06em;
}

.rchip {
  padding: 8px 4px;
  border: 1.5px solid var(--border);
  border-radius: var(--r-sm);
  font-size: clamp(11px, 2.8vw, 12px);
  font-weight: 500;
  color: var(--text-3);
  background: var(--surface-2);
  cursor: pointer;
  transition: all 0.15s;
  text-align: center;
  min-height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: inherit;
}

.rchip.on {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: var(--accent);
}

/* ── Sections ── */
.section { display: none; }
.section.visible { display: block; }

/* ── Stats Row ── */
.stats-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 10px;
}

.stat {
  background: var(--surface);
  border-radius: var(--r-lg);
  padding: 12px 8px 10px;
  text-align: center;
  box-shadow: var(--sh-sm);
}

.stat-num {
  font-size: clamp(22px, 6vw, 28px);
  font-weight: 600;
  color: var(--text);
  line-height: 1;
  display: block;
}

.stat-label {
  font-size: 11px;
  color: var(--text-3);
  font-weight: 500;
  margin-top: 3px;
  display: block;
}

/* ── Progress Bar ── */
.pbar {
  height: 3px;
  background: var(--surface-3);
  border-radius: 2px;
  margin-bottom: 16px;
  overflow: hidden;
}

.pfill {
  height: 100%;
  background: var(--accent);
  border-radius: 2px;
  transition: width 0.45s cubic-bezier(0.4, 0, 0.2, 1);
  width: 0%;
}

/* ── Kana Card ── */
.card-wrap {
  display: flex;
  justify-content: center;
  margin-bottom: 14px;
}

.kcard {
  width: min(62vw, 218px);
  height: min(62vw, 218px);
  background: var(--surface);
  border-radius: 24px;
  box-shadow: var(--sh-md);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  position: relative;
}

.kcard.shake { animation: kshake .35s ease; }
.kcard.pop   { animation: kpop .25s cubic-bezier(.34,1.56,.64,1); }

@keyframes kshake {
  0%,100% { transform:translateX(0) }
  20% { transform:translateX(-9px) }
  40% { transform:translateX(9px) }
  60% { transform:translateX(-5px) }
  80% { transform:translateX(5px) }
}

@keyframes kpop {
  0%   { transform:scale(1) }
  60%  { transform:scale(1.09) }
  100% { transform:scale(1) }
}

.kana-big {
  font-size: clamp(60px, 17vw, 92px);
  line-height: 1;
  color: var(--text);
  user-select: none;
}

.kana-rom {
  font-size: clamp(17px, 5vw, 22px);
  font-weight: 500;
  color: var(--text-3);
}

.ktag-tl, .ktag-tr {
  position: absolute;
  top: 12px;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-3);
  letter-spacing: 0.02em;
}

.ktag-tl { left: 14px; }
.ktag-tr { right: 14px; }

/* ── Weak Bar ── */
.weak-bar {
  background: var(--surface);
  border-radius: var(--r-lg);
  padding: 11px 14px;
  margin-bottom: 12px;
  font-size: 12px;
  color: var(--text-3);
  box-shadow: var(--sh-sm);
}

.wchips { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 6px; }

.wchip {
  padding: 3px 10px;
  border-radius: 20px;
  background: var(--err-bg);
  color: var(--err-text);
  font-size: 12px;
  font-weight: 600;
}

/* ── Flashcard Buttons ── */
.fc-btns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 10px;
}

.fc-btn {
  padding: clamp(14px, 4vw, 17px) 8px;
  border: none;
  border-radius: var(--r-lg);
  font-size: clamp(14px, 4vw, 15px);
  font-weight: 600;
  cursor: pointer;
  min-height: 54px;
  transition: transform 0.1s, opacity 0.1s;
  font-family: inherit;
}

.fc-btn:active { transform: scale(0.97); opacity: 0.82; }

.btn-again { background: var(--err-bg); color: var(--err-text); }
.btn-know  { background: var(--ok-bg);  color: var(--ok-text);  }

/* ── Ghost / Action Button ── */
.action-btn {
  display: block;
  width: 100%;
  padding: 13px;
  background: transparent;
  border: 0.5px solid var(--border);
  border-radius: var(--r-lg);
  font-size: 13px;
  font-weight: 500;
  color: var(--text-3);
  cursor: pointer;
  margin-bottom: 8px;
  min-height: 44px;
  transition: background 0.15s, color 0.15s;
  font-family: inherit;
}

.action-btn:hover { background: var(--surface-3); color: var(--text-2); }

/* ── Note ── */
.note {
  font-size: 12px;
  color: var(--text-3);
  text-align: center;
  margin-top: 2px;
}

/* ── Quiz Sub-Tabs ── */
.quiz-sub-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-bottom: 12px;
}

.qstab {
  padding: 9px 8px;
  border: 0.5px solid var(--border);
  border-radius: var(--r-md);
  font-size: 13px;
  font-weight: 500;
  color: var(--text-3);
  background: var(--surface);
  cursor: pointer;
  transition: all 0.15s;
  min-height: 40px;
  box-shadow: var(--sh-sm);
  font-family: inherit;
}

.qstab.active {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: var(--accent);
  font-weight: 600;
}

/* ── Feedback ── */
.feedback {
  min-height: 24px;
  font-size: 14px;
  font-weight: 500;
  text-align: center;
  margin-bottom: 12px;
  color: var(--text-3);
}

.feedback.ok { color: var(--ok-text); }
.feedback.no { color: var(--err-text); }

/* ── Type Input ── */
.input-row {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}

#qa-inp {
  flex: 1;
  font-size: 17px;
  font-family: inherit;
  padding: 0 14px;
  height: 52px;
  border-radius: var(--r-lg);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s, border-width 0.15s;
  min-width: 0;
  box-shadow: var(--sh-sm);
}

#qa-inp:focus { border-color: var(--accent); border-width: 1.5px; }
#qa-inp.ok    { border-color: var(--ok);     border-width: 1.5px; color: var(--ok-text); }
#qa-inp.no    { border-color: var(--err);    border-width: 1.5px; color: var(--err-text); }

#qa-btn {
  padding: 0 clamp(16px, 4.5vw, 22px);
  height: 52px;
  background: var(--accent);
  color: var(--accent-fg);
  border: none;
  border-radius: var(--r-lg);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
  min-width: 72px;
  transition: opacity 0.15s;
  font-family: inherit;
}

#qa-btn:active { opacity: 0.8; }

/* ── MC Grid ── */
.mc-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 10px;
}

.mc-opt {
  padding: clamp(14px, 4vw, 18px) 8px;
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  font-size: clamp(16px, 5vw, 20px);
  font-weight: 600;
  color: var(--text);
  background: var(--surface);
  cursor: pointer;
  text-align: center;
  min-height: clamp(60px, 16vw, 70px);
  transition: background 0.12s, border-color 0.12s, transform 0.1s;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--sh-sm);
  font-family: inherit;
}

.mc-opt:active { transform: scale(0.96); }

.mc-opt.opt-correct { background: var(--ok-bg);  border-color: var(--ok);  color: var(--ok-text);  }
.mc-opt.opt-wrong   { background: var(--err-bg); border-color: var(--err); color: var(--err-text); }
.mc-opt.opt-reveal  { background: var(--ok-bg);  border-color: var(--ok);  color: var(--ok-text);  }
.mc-opt[disabled]   { cursor: default; pointer-events: none; }

/* ── 五十音總表 ── */
.kana-table-wrap {
  background: var(--surface);
  border-radius: var(--r-xl);
  box-shadow: var(--sh-sm);
  overflow: hidden;
  margin-bottom: 12px;
}

#kana-table {
  display: grid;
  grid-template-columns: 30px repeat(5, 1fr);
  gap: 0.5px;
  background: var(--border);
}

.ktc {
  background: var(--surface);
  padding: 7px 3px 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  min-height: 52px;
  text-align: center;
}

.ktc-corner,
.ktc-head,
.ktc-label { background: var(--surface-2); }

.ktc-head {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-3);
  letter-spacing: 0.04em;
}

.ktc-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-3);
}

.ktc-empty {
  color: var(--surface-3);
  font-size: 13px;
}

.kt-h {
  font-size: clamp(18px, 5vw, 22px);
  line-height: 1;
  color: var(--text);
  font-weight: 500;
}

.kt-k {
  font-size: clamp(11px, 3vw, 13px);
  line-height: 1;
  color: var(--text-2);
}

.kt-r {
  font-size: clamp(8px, 2.1vw, 10px);
  line-height: 1;
  color: var(--text-3);
  margin-top: 2px;
  letter-spacing: 0.02em;
}

/* Table display-mode toggles */
#sec-table.show-hira .kt-k { display: none; }
#sec-table.show-kata .kt-h { display: none; }
#sec-table.show-kata .kt-k {
  font-size: clamp(18px, 5vw, 22px);
  color: var(--text);
}

/* ── 濁音・半濁音 table (6-col: label + 5) ── */
#kana-table-daku {
  display: grid;
  grid-template-columns: 30px repeat(5, 1fr);
  gap: 0.5px;
  background: var(--border);
}

/* ── 拗音 table (4-col: label + ya/yu/yo) ── */
#kana-table-you {
  display: grid;
  grid-template-columns: 36px repeat(3, 1fr);
  gap: 0.5px;
  background: var(--border);
}

.ktc-section {
  grid-column: 1 / -1;
  background: var(--surface-3);
  font-size: 10px;
  font-weight: 600;
  color: var(--text-3);
  padding: 4px 8px;
  letter-spacing: 0.06em;
  display: flex;
  align-items: center;
  min-height: 20px;
}

.table-section-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-3);
  letter-spacing: 0.06em;
  padding: 10px 4px 4px;
}

/* ── Stroke Order ── */
.stroke-wrap {
  background: var(--surface);
  border-radius: var(--r-lg);
  box-shadow: var(--sh-sm);
  padding: 10px 14px 9px;
  margin-bottom: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.stroke-wrap:active { opacity: 0.75; }

.stroke-label {
  align-self: flex-start;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-3);
}

.stroke-svg-box {
  width: 116px;
  height: 116px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text);
}

.stroke-svg-box svg { width: 100%; height: 100%; }

.stroke-loading { font-size: 12px; color: var(--text-3); }

.stroke-hint {
  font-size: 11px;
  color: var(--text-3);
  letter-spacing: 0.02em;
}

@keyframes fcStroke { to { stroke-dashoffset: 0; } }

/* ── 單字練習 ── */
.vc-tag-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 12px;
}

.vtag {
  flex-shrink: 0;
  padding: 7px 14px;
  border-radius: 20px;
  border: 1.5px solid var(--border);
  background: var(--surface);
  font-size: 13px;
  font-weight: 500;
  color: var(--text-2);
  cursor: pointer;
  min-height: 36px;
  display: flex;
  align-items: center;
  font-family: inherit;
  white-space: nowrap;
  transition: all 0.15s;
  box-shadow: var(--sh-sm);
}

.vtag.on {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: var(--accent);
  box-shadow: none;
}

#vc-inp {
  flex: 1;
  font-size: 17px;
  font-family: inherit;
  padding: 0 14px;
  height: 52px;
  border-radius: var(--r-lg);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s, border-width 0.15s;
  min-width: 0;
  box-shadow: var(--sh-sm);
}

#vc-inp:focus { border-color: var(--accent); border-width: 1.5px; }
#vc-inp.ok    { border-color: var(--ok);     border-width: 1.5px; color: var(--ok-text); }
#vc-inp.no    { border-color: var(--err);    border-width: 1.5px; color: var(--err-text); }

#vc-btn {
  padding: 0 clamp(16px, 4.5vw, 22px);
  height: 52px;
  background: var(--accent);
  color: var(--accent-fg);
  border: none;
  border-radius: var(--r-lg);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
  min-width: 72px;
  transition: opacity 0.15s;
  font-family: inherit;
}

#vc-btn:active { opacity: 0.8; }

.vc-speak-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 13px;
  font-weight: 700;
  color: var(--accent);
  font-family: inherit;
  padding: 4px 6px;
  border-radius: var(--r-sm);
  transition: opacity 0.15s;
  line-height: 1;
}
.vc-speak-btn:active { opacity: 0.5; }

.vc-meaning-blur-instant {
  filter: blur(7px);
  user-select: none;
}

.vc-meaning-blur {
  filter: blur(7px);
  user-select: none;
  transition: filter 0.25s;
}
