:root {
  color-scheme: light;
  --background: #f4f1ea;
  --surface: #ffffff;
  --surface-soft: #faf8f3;
  --text: #1f2933;
  --muted: #60758d;
  --border: #d9d7d0;
  --accent: #9f3d2d;
  --accent-dark: #7d2c20;
  --correct: #177245;
  --correct-soft: #e7f6ee;
  --incorrect: #b42318;
  --incorrect-soft: #fff0ee;
  --focus: #2563eb;
  --shadow: 0 18px 50px rgba(49, 43, 35, 0.1);
  --tabs-surface: rgba(255, 255, 255, 0.72);
  --panel-surface: rgba(255, 255, 255, 0.94);
  --panel-border: rgba(255, 255, 255, 0.72);
  --selected-surface: #fff8f5;
  --picker-hover: #fffaf7;
  --summary-surface: #f2f5f7;
  --hover-border: #aaa59c;
  --picker-border: #c7c2b8;
  --accent-text: #ffffff;
  --correct-text: #0f5d38;
  --incorrect-text: #8f1a12;
  --correct-border: #b8d9c9;
  --incorrect-border: #efc4bf;
  --error-border: #f2b8b5;
  --progress-track: #eceae5;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --background: #10151e;
  --surface: #1b2432;
  --surface-soft: #222e3d;
  --text: #edf1f7;
  --muted: #a9b8cc;
  --border: #405069;
  --accent: #ffab91;
  --accent-dark: #ffc4ae;
  --accent-text: #27150f;
  --correct: #7ee2ad;
  --correct-soft: #173c2c;
  --incorrect: #ffaba3;
  --incorrect-soft: #482725;
  --focus: #8bbcff;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.25);
  --tabs-surface: #18212e;
  --panel-surface: #1b2432;
  --panel-border: #34445a;
  --selected-surface: #3b2d2d;
  --picker-hover: #30303b;
  --summary-surface: #222e3d;
  --hover-border: #8d9db4;
  --picker-border: #63738c;
  --correct-text: #9ff0c3;
  --incorrect-text: #ffc2bc;
  --correct-border: #3e8b63;
  --incorrect-border: #aa6762;
  --error-border: #aa6762;
  --progress-track: #405069;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: radial-gradient(circle at top left, rgba(159, 61, 45, 0.1), transparent 28rem), var(--background);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 16px;
  line-height: 1.55;
}

button, input, select { font: inherit; }
button { cursor: pointer; }
a { color: inherit; }
[hidden] { display: none !important; }

button:focus-visible,
input:focus-visible,
select:focus-visible,
a:focus-visible,
summary:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--focus) 45%, transparent);
  outline-offset: 3px;
}

.app-shell {
  width: min(1120px, calc(100% - 32px));
  margin: 0 auto;
  padding: 48px 0 70px;
}

.page-header { margin-bottom: 28px; }
.header-actions { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 12px; margin-bottom: 16px; }
.header-actions .eyebrow { margin: 0; }
.theme-toggle { gap: 10px; font-size: 0.875rem; }
.theme-toggle span { min-width: 2.5em; padding: 2px 6px; border-radius: 5px; background: var(--surface-soft); color: var(--muted); }
.theme-toggle[aria-pressed="true"] { border-color: var(--accent); }
.theme-toggle[aria-pressed="true"] span { background: var(--accent); color: var(--accent-text); }
.eyebrow {
  margin: 0 0 8px;
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

h1, h2, h3, p { margin-top: 0; }
h1 {
  margin-bottom: 10px;
  font-size: clamp(2.5rem, 6vw, 4.25rem);
  line-height: 1.03;
  letter-spacing: -0.052em;
}
h2 { margin-bottom: 8px; font-size: 1.45rem; }
h3 { font-size: 1.1rem; }
.subtitle, .section-heading p { margin-bottom: 0; color: var(--muted); line-height: 1.65; }

.section-tabs {
  display: flex;
  gap: 8px;
  margin-top: 26px;
  padding: 6px;
  width: fit-content;
  border: 1px solid var(--border);
  border-radius: 13px;
  background: var(--tabs-surface);
}
.section-tabs a {
  padding: 9px 18px;
  border-radius: 8px;
  color: var(--muted);
  font-size: 0.92rem;
  font-weight: 750;
  text-decoration: none;
}
.section-tabs a:hover { color: var(--text); background: var(--surface); }
.section-tabs a[aria-current="page"] { color: var(--accent-text); background: var(--accent); }

.panel,
.flashcard {
  border: 1px solid var(--panel-border);
  border-radius: 24px;
  background: var(--panel-surface);
  box-shadow: var(--shadow);
}
.setup-panel, .list-panel, .progress-panel, .history-panel, .results-panel { padding: clamp(24px, 5vw, 44px); }
.section-heading h2 { margin-bottom: 7px; }

.file-picker {
  display: grid;
  gap: 6px;
  margin-top: 28px;
  padding: 26px 30px;
  border: 2px dashed var(--picker-border);
  border-radius: 18px;
  background: var(--surface-soft);
  transition: border-color 160ms ease, background 160ms ease;
}
.file-picker:hover { border-color: var(--accent); background: var(--picker-hover); }
.file-picker-title { font-weight: 800; }
.file-picker-help { color: var(--muted); font-size: 0.92rem; }
.built-in-file { color: var(--correct); font-size: 0.88rem; font-weight: 700; }
.file-picker input { width: min(100%, 580px); margin-top: 8px; }
.template-download { margin-top: 18px; }
.template-download .secondary-button { max-width: 100%; padding-block: 10px; text-align: center; }
.template-download p { margin: 10px 0 0; color: var(--muted); font-size: 0.875rem; line-height: 1.6; }

.mapping-section { margin-top: 32px; padding-top: 30px; border-top: 1px solid var(--border); }
.mapping-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 20px; }
.mapping-grid label,
.list-toolbar label {
  display: grid;
  gap: 8px;
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 750;
}
select,
input[type="search"],
input[type="number"] {
  width: 100%;
  min-height: 54px;
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
}

.quiz-mode-group { margin: 28px 0 0; padding: 0; border: 0; }
.quiz-mode-group legend, .quiz-order-group legend { margin-bottom: 12px; color: var(--muted); font-size: 0.88rem; font-weight: 800; }
.mode-options { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.mode-card {
  display: flex;
  gap: 16px;
  align-items: flex-start;
  min-height: 126px;
  padding: 22px;
  border: 2px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  cursor: pointer;
  transition: border-color 120ms ease, background 120ms ease, transform 120ms ease;
}
.mode-card:hover { border-color: var(--hover-border); transform: translateY(-1px); }
.mode-card:has(input:checked) { border-color: var(--accent); background: var(--selected-surface); }
.mode-card input { width: 20px; height: 20px; margin: 2px 0 0; accent-color: var(--accent); flex: 0 0 auto; }
.mode-card span { display: grid; gap: 6px; }
.mode-card strong { color: var(--text); font-size: 1.02rem; }
.mode-card small { color: var(--muted); font-size: 0.9rem; line-height: 1.5; }
.quiz-order-group { margin: 26px 0 0; padding: 0; border: 0; }
.order-options { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.order-card { display: flex; gap: 12px; align-items: flex-start; padding: 15px 17px; border: 1px solid var(--border); border-radius: 13px; background: var(--surface); cursor: pointer; }
.order-card:has(input:checked) { border-color: var(--accent); background: var(--selected-surface); box-shadow: inset 0 0 0 1px var(--accent); }
.order-card input { width: 18px; height: 18px; margin: 3px 0 0; accent-color: var(--accent); flex: 0 0 auto; }
.order-card span { display: grid; gap: 2px; }
.order-card strong { color: var(--text); }
.order-card small { color: var(--muted); font-size: 0.84rem; }

.file-summary {
  margin: 24px 0 20px;
  padding: 16px 18px;
  border-radius: 13px;
  background: var(--summary-surface);
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.55;
}
.setup-actions, .results-actions, .progress-actions { display: flex; gap: 12px; flex-wrap: wrap; }

.primary-button,
.secondary-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 48px;
  padding: 0 20px;
  border-radius: 11px;
  font-weight: 800;
  text-decoration: none;
  transition: transform 120ms ease, background 120ms ease, border-color 120ms ease;
}
.primary-button { border: 1px solid var(--accent); background: var(--accent); color: var(--accent-text); }
.primary-button:hover { border-color: var(--accent-dark); background: var(--accent-dark); transform: translateY(-1px); }
.secondary-button { border: 1px solid var(--border); background: var(--surface); color: var(--text); }
.secondary-button:hover { border-color: var(--hover-border); transform: translateY(-1px); }
button:disabled { cursor: default; opacity: 0.48; transform: none !important; }

.message { margin: 18px 0; padding: 12px 14px; border-radius: 12px; }
.error-message { border: 1px solid var(--error-border); background: var(--incorrect-soft); color: var(--incorrect); }

.quiz-area { display: grid; gap: 18px; }
.quiz-toolbar { display: flex; align-items: flex-end; justify-content: space-between; gap: 24px; padding: 0 4px; }
.quiz-stats, .quiz-actions { display: flex; align-items: flex-end; gap: 22px; }
.quiz-stats > div { display: grid; gap: 2px; }
.quiz-actions { margin-left: auto; }
.toolbar-label { color: var(--muted); font-size: 0.75rem; font-weight: 800; letter-spacing: 0.08em; text-transform: uppercase; }
.jump-control { display: grid; gap: 4px; }
.jump-input-group { display: flex; gap: 7px; }
.jump-input-group input { width: 116px; min-height: 44px; padding: 0 11px; border-radius: 10px; }
.jump-input-group input[aria-invalid="true"] { border-color: var(--incorrect); background: var(--incorrect-soft); }
.jump-input-group .secondary-button { min-height: 44px; padding-inline: 14px; }
.jump-range { color: var(--muted); font-size: 0.75rem; }
.jump-feedback { min-height: 20px; margin: -10px 4px 0; color: var(--incorrect); font-size: 0.84rem; font-weight: 700; text-align: right; }

.flashcard { padding: clamp(24px, 6vw, 48px); }
.word-block { padding: 12px 10px 32px; text-align: center; }
.word-label { margin-bottom: 18px; color: var(--muted); font-size: 0.8rem; font-weight: 800; letter-spacing: 0.08em; text-transform: uppercase; }
#kanjiText {
  margin-bottom: 8px;
  font-family: "Yu Mincho", "Hiragino Mincho ProN", "Noto Serif JP", serif;
  font-size: clamp(3.5rem, 12vw, 6.8rem);
  font-weight: 700;
  line-height: 1.05;
  letter-spacing: 0.03em;
  overflow-wrap: anywhere;
}
.word-hint { margin-bottom: 0; color: var(--accent); font-family: "Yu Gothic", "Hiragino Kaku Gothic ProN", sans-serif; font-size: clamp(1.2rem, 4vw, 1.75rem); font-weight: 700; letter-spacing: 0.06em; }
.options-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.option-button {
  min-height: 76px;
  padding: 16px 18px;
  border: 2px solid var(--border);
  border-radius: 15px;
  background: var(--surface);
  color: var(--text);
  font-weight: 700;
  line-height: 1.4;
  text-align: left;
  overflow-wrap: anywhere;
  transition: transform 120ms ease, border-color 120ms ease, background 120ms ease;
}
.option-button:hover:not(:disabled) { border-color: var(--hover-border); transform: translateY(-2px); }
.option-button:disabled { opacity: 1; }
.option-button.correct { border-color: var(--correct); background: var(--correct-soft); color: var(--correct-text); }
.option-button.incorrect { border-color: var(--incorrect); background: var(--incorrect-soft); color: var(--incorrect-text); }
.feedback { min-height: 28px; margin: 20px 0 0; font-weight: 800; text-align: center; }
.correct-feedback { color: var(--correct); }
.incorrect-feedback { color: var(--incorrect); }
.next-button { display: block; min-width: 150px; margin: 14px auto 0; }

.results-panel { text-align: center; }
.final-score { display: block; margin: 14px auto 24px; color: var(--accent); font-size: clamp(3.5rem, 10vw, 6rem); line-height: 1; }
.score-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; max-width: 660px; margin: 0 auto 24px; }
.score-grid div, .progress-grid div { padding: 20px; border: 1px solid var(--border); border-radius: 14px; background: var(--surface-soft); }
.score-grid strong, .progress-grid strong { display: block; font-size: 1.8rem; }
.score-grid span, .progress-grid span { color: var(--muted); font-size: 0.88rem; }
.results-actions { justify-content: center; margin-top: 24px; }

.page-section-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 24px; }
.page-section-heading > strong { color: var(--accent); font-size: 1.15rem; }
.list-toolbar { display: grid; grid-template-columns: minmax(0, 1fr) 240px; gap: 18px; margin: 28px 0 16px; }
.list-summary { color: var(--muted); font-size: 0.9rem; }
.word-list { margin: 0; padding: 0; border: 1px solid var(--border); border-radius: 14px; background: var(--surface); list-style: none; overflow: hidden; }
.word-row { display: grid; grid-template-columns: 44px minmax(0, 1fr) minmax(0, 1fr) 160px; gap: 20px; align-items: start; padding: 22px 24px; border-bottom: 1px solid var(--border); }
.word-row:last-child { border-bottom: 0; }
.row-number { padding-top: 7px; color: var(--muted); font-size: 0.8rem; }
.japanese-word { display: block; margin-bottom: 3px; font: 700 1.65rem/1.35 "Yu Mincho", "Hiragino Mincho ProN", serif; overflow-wrap: anywhere; }
.reading { display: block; color: var(--accent); font-size: 0.94rem; overflow-wrap: anywhere; }
.meaning { padding-top: 5px; overflow-wrap: anywhere; }
.meaning strong, .meaning span { display: block; }
.meaning span { margin-top: 4px; color: var(--muted); font-size: 0.9rem; }
.word-history { padding-top: 5px; text-align: right; }
.word-history small { display: block; margin-top: 7px; color: var(--muted); font-size: 0.78rem; }
.status { display: inline-block; padding: 4px 8px; border: 1px solid var(--border); border-radius: 999px; color: var(--muted); font-size: 0.76rem; }
.status.correct { border-color: var(--correct-border); background: var(--correct-soft); color: var(--correct); }
.status.incorrect { border-color: var(--incorrect-border); background: var(--incorrect-soft); color: var(--incorrect); }
.word-row details { grid-column: 2 / -1; color: var(--muted); font-size: 0.88rem; }
.word-row summary { cursor: pointer; }
.extra-field { white-space: pre-wrap; overflow-wrap: anywhere; }
.empty-state { padding: 56px 20px; border: 1px dashed var(--border); border-radius: 14px; text-align: center; }

.progress-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 14px; margin: 28px 0; }
.coverage { margin: 30px 0; }
.coverage > div { display: flex; justify-content: space-between; gap: 16px; margin-bottom: 10px; }
.coverage span { color: var(--muted); }
progress { width: 100%; height: 11px; border: 0; border-radius: 99px; overflow: hidden; accent-color: var(--accent); }
progress::-webkit-progress-bar { background: var(--progress-track); }
progress::-webkit-progress-value { background: var(--accent); }
.progress-actions { margin-top: 24px; }
.history-panel { margin-top: 24px; }
.table-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
th, td { padding: 14px 12px; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
th { color: var(--muted); font-size: 0.78rem; letter-spacing: 0.04em; text-transform: uppercase; }
.empty-history { margin-bottom: 0; color: var(--muted); }

footer { width: min(1120px, calc(100% - 32px)); margin: -38px auto 0; padding: 0 0 30px; color: var(--muted); font-size: 0.82rem; text-align: center; }
footer p { margin: 0; line-height: 1.6; }
footer p + p { margin-top: 4px; }
footer a { color: inherit; text-decoration: underline; text-underline-offset: 3px; overflow-wrap: anywhere; }

@media (max-width: 860px) {
  .mapping-grid { grid-template-columns: 1fr; }
  .progress-grid { grid-template-columns: repeat(2, 1fr); }
  .word-row { grid-template-columns: 34px minmax(0, 1fr) minmax(0, 1fr); }
  .word-history { grid-column: 2 / -1; text-align: left; }
  .word-row details { grid-column: 2 / -1; }
}

@media (max-width: 720px) {
  .app-shell { width: min(100% - 20px, 1120px); padding-top: 28px; }
  h1 { font-size: clamp(2.35rem, 12vw, 3.7rem); }
  .section-tabs { width: 100%; }
  .section-tabs a { flex: 1; padding-inline: 8px; text-align: center; }
  .mode-options, .order-options, .options-grid, .score-grid, .list-toolbar { grid-template-columns: 1fr; }
  .quiz-toolbar, .quiz-actions { align-items: stretch; }
  .quiz-toolbar { flex-direction: column; gap: 16px; }
  .quiz-stats { width: 100%; justify-content: space-between; }
  .quiz-actions { width: 100%; margin-left: 0; }
  .jump-control { flex: 1; }
  .jump-input-group input { width: 100%; }
  .quiz-actions > .secondary-button { align-self: end; }
  .jump-feedback { margin-top: -8px; text-align: left; }
  .page-section-heading { flex-direction: column; gap: 8px; }
  .word-row { grid-template-columns: 28px minmax(0, 1fr); padding: 18px 14px; gap: 10px 14px; }
  .meaning, .word-history, .word-row details { grid-column: 2; }
  .progress-grid { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 480px) {
  .quiz-actions, .setup-actions, .results-actions, .progress-actions { flex-direction: column; }
  .quiz-actions > .secondary-button, .setup-actions > *, .results-actions > *, .progress-actions > * { width: 100%; }
  .progress-grid { grid-template-columns: 1fr; }
  .file-picker { padding: 22px 18px; }
  .setup-panel, .list-panel, .progress-panel, .history-panel, .results-panel { padding: 22px 18px; }
}

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; transition: none !important; }
}
