:root {
  --bg: #f7f4ed;
  --surface: #ffffff;
  --surface-soft: #f4efe4;
  --ink: #1d1b18;
  --muted: #5f574f;
  --line: #ddd2c0;
  --brand: #1f6b62;
  --accent: #8f5f14;
  --shadow: 0 14px 36px rgba(56, 45, 30, 0.08);
}

:root[data-theme="dark"] {
  --bg: #161a1d;
  --surface: #21272c;
  --surface-soft: #2a3137;
  --ink: #f3efe6;
  --muted: #bdb5aa;
  --line: #394148;
  --brand: #77c8be;
  --accent: #e2bf7d;
  --shadow: 0 18px 42px rgba(0, 0, 0, 0.34);
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  color: var(--ink);
  background: linear-gradient(180deg, color-mix(in srgb, var(--bg) 82%, white 18%) 0%, var(--bg) 100%);
  font-family: "Source Serif 4", "Iowan Old Style", "Palatino Linotype", "Hiragino Mincho ProN", serif;
  font-size: 17px;
  line-height: 1.6;
}

button,
input,
textarea {
  font: inherit;
}

.page-shell {
  width: min(1080px, calc(100% - 2rem));
  margin: 0 auto;
  padding: 0 0 3rem;
}

.site-header {
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, color-mix(in srgb, var(--bg) 74%, var(--surface) 26%) 0%, color-mix(in srgb, var(--bg) 86%, var(--surface) 14%) 100%);
  margin-bottom: 2rem;
}

.site-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  padding: 1rem 0;
}

.site-nav-brand,
.site-nav a,
.level-card,
.progress-card a {
  color: inherit;
  text-decoration: none;
}

.site-nav-links a:hover {
  color: var(--brand);
}

.site-nav-brand {
  font-weight: 700;
}

.site-nav-links a[aria-current="page"] {
  color: var(--brand);
  font-weight: 700;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.site-nav-links {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}

.site-nav-actions {
  display: flex;
  align-items: center;
  gap: 0.9rem;
}

.home-hero {
  padding: 2.5rem 0 2rem;
  max-width: 660px;
}

.level-hero {
  padding: 1rem 0 1.35rem;
}

.level-hero-copy {
  display: grid;
  gap: 0.7rem;
}

.level-heading-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.9rem 1.5rem;
  flex-wrap: wrap;
}

.eyebrow {
  margin: 0 0 0.5rem;
  font-size: 0.78rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--accent);
}

h1 {
  margin: 0;
  font-size: clamp(2rem, 4vw, 3.2rem);
  line-height: 1.02;
}

.hero-text {
  max-width: 54rem;
  color: var(--muted);
  line-height: 1.65;
}

.home-progress,
.level-grid,
.entry-list {
  display: grid;
  gap: 1rem;
}

.home-list {
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.home-list-header,
.home-list-row {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) 120px 130px minmax(170px, 0.9fr);
  gap: 1rem;
  align-items: center;
  padding: 0.95rem 1rem;
}

.home-list-header {
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  background: color-mix(in srgb, var(--surface-soft) 72%, transparent);
  border-bottom: 1px solid var(--line);
}

.home-list-row {
  color: inherit;
  text-decoration: none;
  border-bottom: 1px solid var(--line);
  transition: background 0.12s ease;
}

.home-list-row:last-child {
  border-bottom: none;
}

.home-list-row:hover,
.home-list-row:focus-visible {
  background: color-mix(in srgb, var(--brand) 8%, var(--surface));
}

.home-list-name {
  display: grid;
  gap: 0.18rem;
  min-width: 0;
}

.home-list-name strong {
  font-size: 1rem;
}

.home-list-label {
  color: var(--muted);
}

.home-list-count {
  color: var(--muted);
  white-space: nowrap;
}

.home-list-progress {
  display: grid;
  gap: 0.15rem;
  color: var(--muted);
}

.hero-summary {
  display: flex;
  gap: 2.5rem;
  flex-wrap: wrap;
  margin-top: 1.75rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--line);
}

.level-card,
.progress-card,
.toolbar,
.level-summary,
.grammar-entry,
.content-block {
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  border-radius: 12px;
}

.hero-metric {
  background: none;
  border: none;
  box-shadow: none;
  padding: 0;
}

.hero-parts {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.hero-parts a {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 2rem;
  padding: 0.35rem 0.8rem;
  border-radius: 999px;
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--ink);
  text-decoration: none;
  background: var(--surface);
  border: 1px solid var(--line);
}

.hero-parts a:hover {
  color: var(--brand);
  border-color: var(--brand);
}

.hero-parts a[aria-current="page"] {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
}

.theme-toggle {
  border: 1px solid var(--line);
  background: var(--surface-soft);
  color: var(--ink);
  border-radius: 8px;
  padding: 0.4rem 0.8rem;
  cursor: pointer;
  font-size: 0.85rem;
  white-space: nowrap;
}

.theme-toggle:hover,
.theme-toggle:focus-visible,
#reset-progress:hover,
#reset-progress:focus-visible,
.search-field input:focus-visible,
.notes-box:focus-visible {
  outline: 2px solid color-mix(in srgb, var(--brand) 60%, transparent);
  outline-offset: 2px;
}

.hero-metric {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.hero-metric span,
.entry-kicker,
.entry-meta,
.level-summary,
.search-field,
.toggle-field,
.progress-card p,
.section-heading p,
.level-card p,
.content-block li,
.content-block p,
.example-english,
.example-reading {
  color: var(--muted);
}

.hero-metric strong {
  font-size: 1.6rem;
  line-height: 1;
  color: var(--ink);
}

.home-section {
  margin-bottom: 2rem;
}

.section-heading {
  margin-bottom: 1rem;
}

.section-heading h2,
.level-card h2,
.progress-card h3,
.grammar-entry h2,
.content-block h3 {
  margin: 0 0 0.5rem;
}

.level-grid {
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
}

/* JLPT level accent colours */
.level-card[data-jlpt="N5"] { border-top: 3px solid #4caf86; }
.level-card[data-jlpt="N4"] { border-top: 3px solid #2e8bc0; }
.level-card[data-jlpt="N3"] { border-top: 3px solid #7c5cbf; }
.level-card[data-jlpt="N2"] { border-top: 3px solid #c0762e; }
.level-card[data-jlpt="N1"] { border-top: 3px solid #bf3b3b; }

.level-card {
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}

.level-card:hover,
.level-card:focus-visible {
  transform: translateY(-3px);
  box-shadow: 0 20px 48px rgba(56, 45, 30, 0.14);
}

:root[data-theme="dark"] .level-card:hover {
  box-shadow: 0 20px 48px rgba(0, 0, 0, 0.44);
}

.progress-card {
  padding: 1.2rem;
}

.level-card-stats {
  display: flex;
  gap: 1rem;
  margin: 1rem 0;
}

.level-card-stats dt {
  font-size: 0.8rem;
  color: var(--muted);
}

.level-card-stats dd {
  margin: 0.2rem 0 0;
  font-weight: 700;
}

.level-card-label {
  font-size: 0.82rem;
  color: var(--muted);
  margin: 0.2rem 0 0.75rem;
}

.level-card-link {
  color: var(--brand);
  font-weight: 700;
  margin-top: auto;
  padding-top: 1rem;
}

.home-progress {
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
}

.toolbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 1rem;
  align-items: center;
  padding: 1rem;
  margin-bottom: 1rem;
}

.search-field {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.search-field input,
.notes-box {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.7rem 0.9rem;
  background: var(--surface-soft);
}

#reset-progress {
  border: 1px solid var(--line);
  background: var(--surface-soft);
  color: var(--ink);
  border-radius: 8px;
  padding: 0.7rem 1rem;
  cursor: pointer;
}

.level-summary {
  padding: 1rem;
  margin-bottom: 1rem;
}

.entry-list {
  gap: 1.25rem;
}

.grammar-entry {
  padding: 1.2rem;
}

.grammar-entry-header {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 1rem;
  align-items: start;
  margin-bottom: 1rem;
}

.entry-toggle,
.example-track,
.toggle-field {
  display: inline-flex;
  align-items: center;
  gap: 0.55rem;
}

.entry-toggle input,
.example-track input,
.toggle-field input {
  accent-color: var(--brand);
}

.content-block {
  padding: 1rem;
  margin-top: 1rem;
}

.content-block ul,
.example-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  gap: 0.65rem;
}

.example-disclosure summary {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  cursor: pointer;
  color: var(--brand);
  font-weight: 700;
  margin-bottom: 0.85rem;
}

.example-disclosure summary:hover {
  color: color-mix(in srgb, var(--brand) 82%, var(--ink) 18%);
}

.example-disclosure[open] summary {
  margin-bottom: 1rem;
}

.example-disclosure summary::marker,
.example-disclosure summary::-webkit-details-marker {
  color: var(--brand);
}

.notes-box {
  min-height: 6rem;
  resize: vertical;
}

.example-item {
  padding: 1rem;
  border: 1px solid var(--line);
  gap: 0.75rem;
  border-radius: 8px;
  background: var(--surface-soft);
}

.example-japanese {
  margin: 0;
  font-family: "Hiragino Kaku Gothic ProN", "Hiragino Sans", "Yu Gothic", "BIZ UDPGothic", "Noto Sans JP", sans-serif;
  font-weight: 400;
  font-size: 1.12rem;
  line-height: 1.85;
  padding-top: 0.55rem;
}

.example-japanese ruby {
  ruby-align: center;
}

.example-japanese rt {
  font-size: 0.82rem;
  color: var(--muted);
}

.example-reading,
.example-english {
  margin: 0;
  line-height: 1.55;
}

.example-reading {
  font-family: "Hiragino Kaku Gothic ProN", "Hiragino Sans", "Yu Gothic", "BIZ UDPGothic", "Noto Sans JP", sans-serif;
  font-weight: 400;
}

.example-audio {
  display: none;
}

.audio-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.3rem 0.75rem 0.3rem 0.55rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  cursor: pointer;
  font-size: 0.82rem;
  font-family: inherit;
  transition: border-color 0.1s, color 0.1s, background 0.1s;
  line-height: 1;
}

.audio-btn:hover {
  border-color: var(--brand);
  color: var(--brand);
  background: var(--surface-soft);
}

.audio-btn.is-playing {
  border-color: var(--brand);
  color: var(--brand);
}

.audio-btn svg {
  flex-shrink: 0;
  width: 12px;
  height: 12px;
}

@media (max-width: 900px) {
  .home-list-header {
    display: none;
  }

  .home-list-row {
    grid-template-columns: 1fr;
    gap: 0.4rem;
  }

    .level-heading-row {
      flex-direction: column;
      align-items: flex-start;
    }
  .level-hero,
  .toolbar,
  .grammar-entry-header {
    grid-template-columns: 1fr;
  }

  .site-nav {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.5rem;
  }

  .site-nav-actions {
    width: 100%;
    justify-content: space-between;
    flex-wrap: wrap;
  }
}

/* ── Level page layout (sidebar + main) ───────────────────── */

.level-layout {
  width: min(1360px, calc(100% - 2rem));
  margin: 0 auto;
  display: grid;
  grid-template-columns: 250px minmax(0, 1fr);
  gap: 2rem;
  padding: 0 0 3rem;
  align-items: start;
}

.level-sidebar {
  position: sticky;
  top: 1rem;
  max-height: calc(100vh - 2rem);
  overflow-y: auto;
  scrollbar-width: thin;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 1rem;
}

.sidebar-parts {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-bottom: 0.75rem;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid var(--line);
}

.sidebar-parts a {
  padding: 0.3rem 0.55rem;
  border-radius: 6px;
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--ink);
  text-decoration: none;
  background: var(--surface-soft);
  border: 1px solid var(--line);
  transition: color 0.1s, background 0.1s;
}

.sidebar-parts a:hover {
  color: var(--brand);
  background: color-mix(in srgb, var(--brand) 8%, transparent);
}

.sidebar-parts a[aria-current="page"] {
  background: var(--brand);
  color: #fff;
  border-color: var(--brand);
}

:root[data-theme="dark"] .sidebar-parts a[aria-current="page"] {
  color: var(--bg);
}

.sidebar-heading {
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 0 0 0.5rem;
  padding: 0 0.25rem;
}

.sidebar-toggle {
  display: none;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  padding: 0.25rem 0.25rem;
  font-size: 0.82rem;
  color: var(--brand);
  cursor: pointer;
  margin-bottom: 0.25rem;
  font-family: inherit;
}

.sidebar-index {
  list-style: none;
  padding: 0;
  margin: 0;
  display: block;
}

.sidebar-index li a {
  display: block;
  padding: 0.3rem 0.4rem;
  font-size: 0.92rem;
  color: var(--ink);
  text-decoration: none;
  border-radius: 5px;
  line-height: 1.35;
  border-left: 2px solid transparent;
  transition: color 0.1s, background 0.1s, border-color 0.1s;
}

.sidebar-index li a:hover {
  color: var(--brand);
  background: color-mix(in srgb, var(--brand) 6%, transparent);
}

.sidebar-index li a.is-active {
  color: var(--brand);
  font-weight: 600;
  background: color-mix(in srgb, var(--brand) 8%, transparent);
  border-left-color: var(--brand);
}

/* ── Page pagination nav ──────────────────────────────────── */

.page-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-top: 2rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--line);
}

.page-nav-btn {
  display: inline-block;
  padding: 0.6rem 1.1rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--brand);
  font-weight: 700;
  text-decoration: none;
  box-shadow: var(--shadow);
}

.page-nav-btn:hover {
  background: var(--surface-soft);
}

.page-nav-label {
  color: var(--muted);
  font-size: 0.9rem;
}

/* ── Mobile: sidebar stacks above content ─────────────────── */

@media (max-width: 900px) {
  .level-layout {
    grid-template-columns: 1fr;
    gap: 1rem;
  }

  .level-sidebar {
    position: static;
    max-height: none;
  }

  .sidebar-toggle {
    display: block;
  }

  .sidebar-index {
    display: none;
  }

  .sidebar-index.is-open {
    display: block;
  }
}