/* ============================================================ tokens */
:root {
  --bg: #f5f2ec;
  --surface: #fffdf9;
  --surface-2: #f0ebe2;
  --text: #1f1d1a;
  --muted: #6b665e;
  --border: #e3ddd2;
  --border-strong: #c9c1b3;
  --accent: #2f4b8f;
  --accent-ink: #ffffff;
  --accent-soft: #e4e9f6;
  --again: #c0432e;
  --hard: #b0731a;
  --good: #2f7d4f;
  --easy: #2f6c9e;
  --new: #2f6c9e;
  --learn: #c0432e;
  --due: #2f7d4f;
  --cover-a: #ece6db;
  --cover-b: #e4ddd0;
  --shadow: 0 1px 2px rgb(40 30 10 / 6%), 0 8px 28px rgb(40 30 10 / 8%);
  --radius: 16px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-ja: "Hiragino Kaku Gothic ProN", "Hiragino Sans", "Noto Sans JP", "Yu Gothic", "Meiryo", sans-serif;
  --font-ja-serif: "Hiragino Mincho ProN", "Noto Serif JP", "Yu Mincho", serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14151a;
    --surface: #1d1f26;
    --surface-2: #262933;
    --text: #ece8e1;
    --muted: #a39e95;
    --border: #2f323c;
    --border-strong: #454956;
    --accent: #8ea8f0;
    --accent-ink: #10131c;
    --accent-soft: #263152;
    --again: #f0806b;
    --hard: #e3a857;
    --good: #6cc48f;
    --easy: #79b3e6;
    --new: #79b3e6;
    --learn: #f0806b;
    --due: #6cc48f;
    --cover-a: #2a2d36;
    --cover-b: #242730;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 10px 30px rgb(0 0 0 / 35%);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #14151a;
  --surface: #1d1f26;
  --surface-2: #262933;
  --text: #ece8e1;
  --muted: #a39e95;
  --border: #2f323c;
  --border-strong: #454956;
  --accent: #8ea8f0;
  --accent-ink: #10131c;
  --accent-soft: #263152;
  --again: #f0806b;
  --hard: #e3a857;
  --good: #6cc48f;
  --easy: #79b3e6;
  --new: #79b3e6;
  --learn: #f0806b;
  --due: #6cc48f;
  --cover-a: #2a2d36;
  --cover-b: #242730;
  --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 10px 30px rgb(0 0 0 / 35%);
  color-scheme: dark;
}

/* ============================================================ base */
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 var(--font);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}
[lang="ja"] { font-family: var(--font-ja); }
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5rem; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 .75rem; }
hr { border: 0; border-top: 1px solid var(--border); margin: 1rem 0; }
kbd {
  font: 12px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 5px;
  padding: 2px 5px;
  background: var(--surface);
}
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.row { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.stack > * + * { margin-top: 1rem; }
.hidden { display: none !important; }

/* ============================================================ topbar */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 1rem;
  padding: .6rem 1.25rem;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: .6rem; font-weight: 700; color: var(--text); text-decoration: none; white-space: nowrap; }
.brand-mark {
  display: grid; place-items: center; width: 32px; height: 32px; border-radius: 9px;
  background: var(--accent); color: var(--accent-ink); font-size: 18px;
}
.topnav { display: flex; align-items: center; gap: .25rem; margin-left: auto; flex-wrap: wrap; }
.topnav a {
  color: var(--muted); text-decoration: none; padding: .4rem .7rem; border-radius: 8px; font-weight: 500;
}
.topnav a:hover { color: var(--text); background: var(--surface-2); }
.topnav a.active { color: var(--accent); background: var(--accent-soft); }

main { max-width: 1120px; margin: 0 auto; padding: 1.5rem 1.25rem 4rem; outline: none; }

/* ============================================================ controls */
.btn, .icon-btn {
  font: inherit; font-weight: 600; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text);
  padding: .5rem .9rem; border-radius: 10px; text-decoration: none; line-height: 1.2;
  transition: background .15s, border-color .15s, transform .05s;
}
.btn:hover, .icon-btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn:focus-visible, .icon-btn:focus-visible, .fi-hidden:focus-visible, .fi-audio:focus-visible, .flashcard:focus-visible,
input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--again); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.sm { padding: .3rem .6rem; font-size: .85rem; border-radius: 8px; }
.btn[disabled] { opacity: .45; cursor: not-allowed; }
.icon-btn { padding: 0; width: 34px; height: 34px; border-radius: 9px; }
.btn.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

input[type="text"], input[type="number"], input[type="search"], input[type="date"], input[type="password"], select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border-strong); border-radius: 9px; padding: .45rem .6rem; max-width: 100%;
}
textarea { width: 100%; resize: vertical; }
label { cursor: pointer; }
.field { display: grid; gap: .3rem; margin-bottom: .8rem; }
.field > span { font-weight: 600; font-size: .9rem; }
.check { display: inline-flex; align-items: center; gap: .4rem; }

.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1.1rem 1.2rem; box-shadow: var(--shadow);
}
.panel + .panel { margin-top: 1rem; }
.panel-head { display: flex; align-items: center; gap: .5rem; margin-bottom: .8rem; flex-wrap: wrap; }
.panel-head h2, .panel-head h3 { margin: 0; }

.page-head { display: flex; align-items: flex-end; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.25rem; }
.page-head .sub { color: var(--muted); margin: .2rem 0 0; }
.crumb { color: var(--muted); text-decoration: none; font-size: .9rem; }
.crumb:hover { color: var(--accent); }

.pill { display: inline-flex; align-items: center; gap: .3rem; padding: .1rem .55rem; border-radius: 999px; font-size: .8rem; font-weight: 600; background: var(--surface-2); color: var(--muted); }
.pill.new { color: var(--new); }
.pill.learning { color: var(--learn); }
.pill.due { color: var(--due); }

/* ============================================================ home */
.deck-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 1rem; }
.deck-card { display: flex; flex-direction: column; gap: .8rem; }
.deck-card h2 { font-size: 1.1rem; margin: 0; }
.deck-stats { display: flex; gap: 1.2rem; }
.stat b { display: block; font-size: 1.4rem; line-height: 1.1; }
.stat span { font-size: .8rem; color: var(--muted); }
.stat.new b { color: var(--new); }
.stat.due b { color: var(--due); }
.stat.learning b { color: var(--learn); }
.progress { height: 6px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.progress > i { display: block; height: 100%; background: var(--good); border-radius: inherit; }
.deck-actions { display: flex; gap: .4rem; flex-wrap: wrap; margin-top: auto; }
.deck-new {
  display: grid; place-items: center; gap: .5rem; text-align: center; min-height: 200px;
  border: 2px dashed var(--border-strong); border-radius: var(--radius); color: var(--muted);
  background: transparent; cursor: pointer; font: inherit;
}
.deck-new:hover { border-color: var(--accent); color: var(--accent); }
.deck-new b { font-size: 2rem; line-height: 1; }

/* ============================================================ calendar */
main:has(.cal-layout) { max-width: 1320px; }
.cal-layout { display: grid; grid-template-columns: minmax(0, 1fr) 330px; gap: 1.25rem; align-items: start; }
.cal-head { display: flex; align-items: center; gap: .5rem; margin-bottom: .9rem; flex-wrap: wrap; }
.cal-head h1 { margin: 0; min-width: 10.5rem; text-align: center; font-size: 1.4rem; }
.cal {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: hidden;
}
.cal-dow, .cal-week { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); }
.cal-dow div {
  padding: .5rem .2rem; text-align: center; font-size: .75rem; font-weight: 700; letter-spacing: .05em;
  color: var(--muted); background: var(--surface-2);
}
.cal-dow .sun { color: var(--again); }
.cal-cell {
  position: relative; min-width: 0; min-height: 118px; padding: .35rem .4rem 2rem;
  display: flex; flex-direction: column; gap: .25rem;
  border-top: 1px solid var(--border); border-left: 1px solid var(--border);
  cursor: pointer; transition: background .12s;
}
.cal-cell:first-child { border-left: 0; }
.cal-cell:hover { background: var(--bg); }
.cal-cell.out { background: color-mix(in srgb, var(--surface-2) 50%, transparent); }
.cal-cell.out .num { opacity: .45; }
.cal-cell.selected { box-shadow: inset 0 0 0 2px var(--accent); background: color-mix(in srgb, var(--accent-soft) 40%, var(--surface)); }
.cal-cell.drop-over { background: var(--accent-soft); outline: 2px dashed var(--accent); outline-offset: -4px; }
.cal-day { display: flex; align-items: center; justify-content: space-between; gap: .2rem; }
.num {
  display: inline-grid; place-items: center; min-width: 26px; height: 26px; padding: 0 .3rem; border-radius: 99px;
  font-weight: 700; font-size: .85rem; font-variant-numeric: tabular-nums;
}
.cal-cell.today .num { background: var(--accent); color: var(--accent-ink); }
.cal-log { font-size: .7rem; font-weight: 700; color: var(--good); white-space: nowrap; }
.cal-decks { display: flex; flex-direction: column; gap: .2rem; min-width: 0; }
.cal-chip {
  display: flex; align-items: center; gap: .3rem; min-width: 0; padding: .14rem .4rem; border-radius: 6px;
  font-size: .76rem; font-weight: 600; line-height: 1.3; text-decoration: none;
  background: var(--accent-soft); color: var(--accent); border-left: 3px solid var(--accent); cursor: grab;
}
.cal-chip:hover { filter: brightness(.96); }
.cal-chip:active { cursor: grabbing; }
.cal-chip .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cal-chip .n { font-size: .68rem; opacity: .75; font-variant-numeric: tabular-nums; }
.cal-chip.has-due { border-left-color: var(--due); }
.cal-chip.has-due .n::after { content: " •"; color: var(--due); }
.cal-more { font-size: .72rem; color: var(--muted); padding-left: .2rem; }
.cal-add {
  position: absolute; right: .35rem; bottom: .35rem; width: 24px; height: 24px; border-radius: 7px;
  border: 1px dashed var(--border-strong); background: var(--surface); color: var(--muted);
  font: 700 15px/1 var(--font); cursor: pointer; opacity: 0; transition: opacity .12s;
}
.cal-cell:hover .cal-add, .cal-cell.selected .cal-add, .cal-add:focus-visible { opacity: 1; }
.cal-add:hover { color: var(--accent); border-color: var(--accent); }
.undated { margin-top: 1rem; }
.undated.drop-over { outline: 2px dashed var(--accent); outline-offset: -4px; background: var(--accent-soft); }
.undated .cal-chip { display: inline-flex; max-width: 260px; font-size: .85rem; padding: .3rem .6rem; }

.day-panel { position: sticky; top: 76px; }
.dp-date { font-size: 1.5rem; margin: .1rem 0 .6rem; font-variant-numeric: tabular-nums; }
.day-panel h3 { margin-top: .9rem; }
.dp-list { display: grid; gap: .55rem; }
.dp-deck { display: grid; gap: .35rem; padding: .65rem .7rem; border: 1px solid var(--border); border-radius: 12px; background: var(--bg); }
.dp-name { font-weight: 700; color: var(--text); text-decoration: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dp-name:hover { color: var(--accent); }
.c-new { color: var(--new); }
.c-due { color: var(--due); }

@media (max-width: 1000px) {
  .cal-layout { grid-template-columns: minmax(0, 1fr); }
  .day-panel { position: static; }
}
@media (max-width: 600px) {
  .hide-sm, #helpBtn { display: none; }
  .topnav { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; gap: .1rem; }
  .topnav a { white-space: nowrap; }
  /* day details right under the calendar, "Chưa xếp ngày" after it */
  .cal-main { display: contents; }
  .day-panel { order: 1; }
  .undated { order: 2; margin-top: 0; }
  .cal-layout { gap: .75rem; }
  .cal-head { margin-bottom: 0; }
  .cal-head h1 { min-width: 0; font-size: 1.15rem; }
  .cal-head .spacer + .small { flex-basis: 100%; }
  .cal-cell { min-height: 58px; padding: .2rem .15rem .3rem; gap: .15rem; }
  .cal-dow div { padding: .4rem 0; font-size: .68rem; }
  .num { min-width: 22px; height: 22px; font-size: .78rem; padding: 0; }
  .cal-day { flex-direction: column; gap: 0; }
  .cal-log { font-size: .58rem; }
  /* on phones decks become small bars; tap the day to see them in the panel below */
  .cal-chip { height: 5px; padding: 0; border: 0; border-radius: 3px; pointer-events: none; background: var(--accent); }
  .cal-chip.has-due { background: var(--due); }
  .cal-chip .name, .cal-chip .n { display: none; }
  .cal-more { display: none; }
  .cal-add { display: none; }
  .undated .cal-chip { height: auto; padding: .3rem .6rem; pointer-events: auto; background: var(--accent-soft); }
  .undated .cal-chip .name, .undated .cal-chip .n { display: inline; }
}

/* ============================================================ flashcard */
.study { max-width: 760px; margin: 0 auto; }
.study-top { display: flex; align-items: center; gap: .75rem; margin-bottom: 1rem; flex-wrap: wrap; }
.study-top h1 { font-size: 1.15rem; margin: 0; }
.counts { display: flex; gap: .6rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.counts .c-new { color: var(--new); }
.counts .c-learning { color: var(--learn); }
.counts .c-due { color: var(--due); }
.counts .cur { text-decoration: underline; text-underline-offset: 4px; }
.study-progress { height: 4px; background: var(--surface-2); border-radius: 99px; margin-bottom: 1rem; overflow: hidden; }
.study-progress > i { display: block; height: 100%; background: var(--accent); transition: width .3s; }

.flashcard { perspective: 1800px; cursor: pointer; border-radius: 22px; -webkit-tap-highlight-color: transparent; }
.fc-inner {
  display: grid; transition: transform .55s cubic-bezier(.2, .7, .2, 1); transform-style: preserve-3d;
  min-height: 360px;
}
.flashcard.flipped .fc-inner { transform: rotateY(180deg); }
.fc-face {
  grid-area: 1 / 1; backface-visibility: hidden; -webkit-backface-visibility: hidden;
  background: var(--surface); border: 1px solid var(--border); border-radius: 22px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 2.2rem 1.5rem 2.6rem; position: relative; text-align: center;
}
.fc-face::before {
  content: attr(data-side); position: absolute; top: .8rem; left: 1rem;
  font-size: .72rem; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); font-weight: 700;
}
.fc-back { transform: rotateY(180deg); }
.flashcard:not(.flipped) .fc-back, .flashcard.flipped .fc-front { pointer-events: none; }
.fc-hint { position: absolute; bottom: .8rem; left: 0; right: 0; font-size: .78rem; color: var(--muted); }
.fc-items { display: flex; flex-direction: column; align-items: center; gap: .9rem; width: 100%; }
.fc-echo { opacity: .75; width: 100%; }
.fc-echo .fc-items { gap: .4rem; }
.fc-divider { width: 60%; margin: 1.1rem auto; border-top: 1px dashed var(--border-strong); }
.fc-empty { color: var(--muted); font-style: italic; }

.fi { max-width: 100%; overflow-wrap: anywhere; }
.fi-label { display: block; font-size: .72rem; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); margin-bottom: .15rem; font-family: var(--font); }
.fi-value { display: block; }
.size-s .fi-value { font-size: 1rem; }
.size-m .fi-value { font-size: 1.4rem; }
.size-l .fi-value { font-size: 2rem; font-weight: 600; }
.size-xl .fi-value { font-size: clamp(2.8rem, 9vw, 4.6rem); font-weight: 600; line-height: 1.15; }
.size-xl .fi-value[lang="ja"] { font-family: var(--font-ja-serif); font-weight: 500; }
.size-s .fi-value { color: var(--muted); }
.fi-value ruby rt { font-size: .55em; color: var(--muted); }
.size-s .fi-value ruby rt { font-size: .62em; }

/* hidden (tap to reveal) box */
.fi-hidden {
  font: inherit; color: inherit; cursor: pointer; position: relative;
  min-width: min(14rem, 100%); padding: .55rem 1.1rem; border-radius: 12px;
  border: 1.5px dashed var(--border-strong); background: transparent;
  transition: border-color .2s, background .2s;
}
.fi-hidden .fi-value, .fi-hidden .fi-label { visibility: hidden; }
.fi-cover {
  position: absolute; inset: 0; border-radius: 10px;
  display: flex; align-items: center; justify-content: center; gap: .4rem; padding: 0 .6rem;
  background: repeating-linear-gradient(-45deg, var(--cover-a) 0 9px, var(--cover-b) 9px 18px);
  color: var(--muted); font-size: .85rem; font-weight: 600; font-family: var(--font);
}
.fi-cover::before { content: "👁"; filter: grayscale(1); opacity: .7; }
.fi-cover small { font-weight: 500; opacity: .8; }
.btn.primary kbd { background: transparent; color: inherit; border-color: currentColor; opacity: .75; }
.fi-hidden:hover .fi-cover { color: var(--accent); }
.fi-hidden.revealed { border-color: transparent; cursor: default; }
.fi-hidden.revealed .fi-cover { display: none; }
.fi-hidden.revealed .fi-value, .fi-hidden.revealed .fi-label { visibility: visible; animation: reveal .25s ease-out; }
@keyframes reveal { from { opacity: 0; transform: translateY(3px); } to { opacity: 1; transform: none; } }

/* audio-only item */
.fi-audio {
  font: inherit; cursor: pointer; color: var(--accent); background: var(--accent-soft);
  border: 0; border-radius: 999px; padding: .9rem 1.6rem; display: inline-flex; align-items: center; gap: .6rem;
}
.fi-audio svg { width: 1.6em; height: 1.6em; }
.fi-audio.size-xl, .fi-audio.size-l { font-size: 1.3rem; padding: 1.1rem 2rem; }
.fi-audio .fi-label { margin: 0; color: inherit; }

.study-tools { display: flex; justify-content: center; gap: .4rem; margin: .9rem 0; flex-wrap: wrap; }
.answer-bar { display: grid; grid-template-columns: repeat(4, 1fr); gap: .5rem; margin-top: .5rem; }
.answer-bar.two { grid-template-columns: 1fr 1fr; }
.answer-bar.one { grid-template-columns: 1fr; max-width: 360px; margin-left: auto; margin-right: auto; }
.ans { flex-direction: column; gap: .1rem; padding: .65rem .5rem; }
.ans small { font-weight: 500; color: var(--muted); font-size: .78rem; }
.ans.again { color: var(--again); }
.ans.hard { color: var(--hard); }
.ans.good { color: var(--good); }
.ans.easy { color: var(--easy); }
.ans.again:hover { border-color: var(--again); }
.ans.hard:hover { border-color: var(--hard); }
.ans.good:hover { border-color: var(--good); }
.ans.easy:hover { border-color: var(--easy); }
.ans kbd { margin-top: .2rem; }
.nav-bar { display: flex; align-items: center; justify-content: space-between; gap: .5rem; margin-top: .5rem; }
.nav-bar .pos { font-variant-numeric: tabular-nums; color: var(--muted); font-weight: 600; }

.done { text-align: center; padding: 2.5rem 1rem; }
.done .big { font-size: 3rem; }
.done-stats { display: flex; justify-content: center; gap: 1.5rem; margin: 1rem 0 1.5rem; flex-wrap: wrap; }

.flashcard.flipped .fc-front .fi-audio, .flashcard:not(.flipped) .fc-back .fi-audio { visibility: hidden; }

@media (prefers-reduced-motion: reduce) {
  .fc-inner { transition: none; }
  .fi-hidden.revealed .fi-value { animation: none; }
}

/* ============================================================ layout editor */
.layout-grid { display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); gap: 1.25rem; align-items: start; }
.layout-preview { position: sticky; top: 76px; }
.presets { display: flex; gap: .4rem; flex-wrap: wrap; }
.face-panel h3 { display: flex; align-items: center; gap: .5rem; }
.face-panel h3 .tag { font-size: .72rem; padding: .1rem .5rem; border-radius: 6px; background: var(--accent-soft); color: var(--accent); }
.li-list { display: grid; gap: .45rem; margin-bottom: .6rem; }
.li-row {
  display: grid; grid-template-columns: minmax(0, 1.5fr) minmax(0, 1.1fr) minmax(0, .8fr) auto auto; gap: .4rem; align-items: center;
  padding: .45rem; border: 1px solid var(--border); border-radius: 10px; background: var(--bg);
}
.li-row select { width: 100%; }
.li-row .li-btns { display: flex; gap: .15rem; }
.li-row .li-btns .btn { padding: .25rem .45rem; }
.li-row.mode-hidden { border-left: 4px solid var(--hard); }
.li-row.mode-audio { border-left: 4px solid var(--easy); }
.li-row.mode-show { border-left: 4px solid var(--good); }
.li-empty { color: var(--muted); font-style: italic; padding: .4rem; }
.legend { display: flex; gap: 1rem; flex-wrap: wrap; font-size: .82rem; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: .3rem; vertical-align: -1px; }

/* ============================================================ cards table */
.toolbar { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; margin-bottom: .8rem; }
.toolbar input[type="search"] { flex: 1; min-width: 180px; }
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 12px; }
table { width: 100%; border-collapse: collapse; background: var(--surface); }
th, td { padding: .5rem .65rem; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: .78rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); background: var(--surface-2); position: sticky; top: 0; white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: var(--bg); }
td.ja { font-family: var(--font-ja); }
td.clip { max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
td.actions { white-space: nowrap; text-align: right; }
.star { background: none; border: 0; cursor: pointer; font-size: 1.1rem; color: var(--border-strong); padding: 0 .2rem; }
.star.on { color: #e0a800; }
.field-chips { display: flex; gap: .4rem; flex-wrap: wrap; }
.chip { display: inline-flex; align-items: center; gap: .35rem; border: 1px solid var(--border-strong); border-radius: 999px; padding: .2rem .35rem .2rem .7rem; background: var(--surface); font-size: .9rem; }
.chip small { color: var(--muted); }
.chip button { border: 0; background: none; cursor: pointer; color: var(--muted); padding: .1rem .3rem; border-radius: 99px; font: inherit; }
.chip button:hover { background: var(--surface-2); color: var(--text); }

/* ============================================================ import */
.import-grid { display: grid; grid-template-columns: minmax(0, 1fr); gap: 1rem; }
.map-table select { min-width: 140px; }
.drop {
  border: 2px dashed var(--border-strong); border-radius: 12px; padding: 1rem; text-align: center; color: var(--muted);
}
.drop.over { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
/* ============================================================ AI */
.ai-error { color: var(--again); }
.ai-cols { display: flex; flex-wrap: wrap; gap: .3rem; align-items: center; padding-bottom: .2rem; }
.ai-setup { border: 2px solid var(--accent); }
.ai-setup.compact { border: 1px solid var(--border); padding-top: .8rem; padding-bottom: .8rem; }
.ai-row { align-items: flex-end; gap: .75rem; margin-bottom: .6rem; }
.ai-row .field { margin: 0; min-width: 0; }
.ai-row .field:first-child { flex: 1 1 220px; }
.ai-row .field:nth-child(2) { flex: 1 1 240px; }
.ai-row select { width: 100%; }
.ai-table td { padding: .25rem .3rem; }
.ai-table td:first-child { width: 2.5rem; text-align: center; font-variant-numeric: tabular-nums; }
.ai-table textarea {
  min-width: 8rem; width: 100%; resize: vertical; field-sizing: content; min-height: 2.1rem;
  border: 1px solid transparent; background: transparent; padding: .3rem .4rem; border-radius: 7px; line-height: 1.4;
}
.ai-table textarea:hover { border-color: var(--border); }
.ai-table textarea:focus { border-color: var(--accent); background: var(--surface); }
.ai-table tr.dup td { background: color-mix(in srgb, var(--again) 7%, transparent); }
.ai-chat { display: grid; gap: .4rem; margin-top: 1rem; }
.ai-chat .msg { padding: .45rem .7rem; border-radius: 10px; font-size: .9rem; max-width: 90%; }
.ai-chat .msg.user { background: var(--accent-soft); justify-self: end; }
.ai-chat .msg.ai { background: var(--surface-2); }
.ai-chat .msg.error { background: color-mix(in srgb, var(--again) 12%, transparent); color: var(--again); }
.tip { background: var(--accent-soft); border-radius: 10px; padding: .7rem .9rem; margin-bottom: .9rem; }
.tip ul { margin: .3rem 0 0; padding-left: 1.1rem; }
pre.example { background: var(--surface-2); padding: .7rem .9rem; border-radius: 10px; overflow-x: auto; font-size: .85rem; margin: .4rem 0 .8rem; }

/* ============================================================ modal + toast */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 50; background: rgb(10 10 15 / 45%);
  display: grid; place-items: center; padding: 1rem; animation: fade .15s;
}
.modal {
  background: var(--surface); color: var(--text); border-radius: var(--radius); box-shadow: var(--shadow);
  width: min(560px, 100%); max-height: calc(100vh - 2rem); overflow: auto; padding: 1.3rem 1.4rem;
  border: 1px solid var(--border);
}
.modal.wide { width: min(760px, 100%); }
.modal-foot { display: flex; gap: .5rem; justify-content: flex-end; margin-top: 1rem; flex-wrap: wrap; }
@keyframes fade { from { opacity: 0; } }
#toasts { position: fixed; bottom: 1rem; left: 50%; transform: translateX(-50%); z-index: 60; display: grid; gap: .4rem; }
.toast { background: var(--text); color: var(--bg); padding: .55rem 1rem; border-radius: 10px; box-shadow: var(--shadow); font-weight: 500; animation: fade .2s; }
.toast.error { background: var(--again); color: #fff; }
.shortcuts { display: grid; grid-template-columns: auto 1fr; gap: .45rem 1rem; align-items: center; }

/* ============================================================ responsive */
@media (max-width: 860px) {
  .layout-grid { grid-template-columns: minmax(0, 1fr); }
  .layout-preview { position: static; order: -1; }
}
@media (max-width: 600px) {
  main { padding: 1rem 16px 3rem; }
  .topbar { padding: .5rem 16px; gap: .5rem; }
  .brand > span:last-child { display: none; }
  .topnav a { padding: .35rem .5rem; font-size: .9rem; }
  .fc-inner { min-height: 300px; }
  .answer-bar { grid-template-columns: repeat(2, 1fr); }
  .li-row { grid-template-columns: 1fr 1fr; }
  .li-row .li-btns { grid-column: 1 / -1; justify-content: flex-end; }
  .deck-grid { grid-template-columns: minmax(0, 1fr); }
}

/* ============================================================ sync badge */
#syncBadge { font-weight: 600; white-space: nowrap; }
@media (max-width: 600px) { #syncBadge .sync-text { display: none; } }

/* ============================================================ touch & phone */
.flashcard { touch-action: pan-y; position: relative; }
.flashcard:not(.swiping) { transition: transform .2s ease-out; }
.fc-swipe-hint {
  position: absolute; top: 1rem; z-index: 3; pointer-events: none; opacity: 0;
  padding: .3rem .8rem; border-radius: 8px; border: 2px solid currentColor;
  background: var(--surface); font-weight: 800; font-size: 1.1rem;
}
.fc-swipe-hint[data-dir="right"] { right: 1rem; color: var(--good); }
.fc-swipe-hint[data-dir="left"] { left: 1rem; color: var(--again); }
.swipe-tip { display: none; text-align: center; margin: .4rem 0 0; }
@media (hover: none) and (pointer: coarse) { .swipe-tip { display: block; } }
.cal-chip { -webkit-touch-callout: none; user-select: none; }
.ios-hint { margin-bottom: 1rem; }

@media (max-width: 600px) {
  .topbar { padding-top: max(.5rem, env(safe-area-inset-top)); }
  .fc-inner { min-height: min(360px, 52dvh); }
  .study-tools .btn { min-height: 40px; }
  #answers {
    position: sticky; bottom: 0; z-index: 5; background: var(--bg);
    margin: 0 -16px; padding: .5rem 16px calc(.5rem + env(safe-area-inset-bottom));
    box-shadow: 0 -6px 16px rgb(0 0 0 / 8%);
  }
  .ans { min-height: 52px; }
  .nav-bar .btn { min-height: 44px; }
}

/* ============================================================ sign-in screen */
body.gated .topnav { display: none; }
.login-screen { min-height: calc(100dvh - 140px); display: grid; place-items: center; padding: 1rem 0; }
.login-card { width: min(440px, 100%); text-align: center; padding: 2rem 1.6rem; }
.login-card h1 { font-size: 1.6rem; }
.login-logo {
  width: 72px; height: 72px; margin: 0 auto 1rem; border-radius: 18px; display: grid; place-items: center;
  background: var(--accent); color: var(--accent-ink); font-size: 40px; font-family: var(--font-ja-serif);
}
.login-action { display: grid; justify-items: center; gap: .6rem; min-height: 48px; margin: 1.2rem 0 .6rem; }
.login-dev { justify-content: center; margin-bottom: .6rem; }
.login-foot { margin-top: 1rem; }
