:root {
  --paper: #f4efe6;
  --paper-deep: #ebe4d6;
  --ink: #241f1a;
  --muted: #74695d;
  --line: #e3d8c8;
  --card: #fffdf9;
  --teal: #0e6b61;
  --teal-deep: #0a4a43;
  --teal-soft: #e5f3f1;
  --good: #1a7346;
  --good-bg: #e8f6ee;
  --bad: #9d3d36;
  --bad-bg: #fdeeee;
  --part: #8d5d10;
  --part-bg: #fbf4e4;
  --shadow: 0 12px 32px rgba(55, 40, 18, 0.06);
  --sans: "Fira Sans", "Segoe UI", sans-serif;
  --serif: "Noto Serif", "Liberation Serif", Georgia, serif;
  color: var(--ink);
  background:
    radial-gradient(1200px 480px at 10% -10%, #fff9ef 0%, transparent 60%),
    var(--paper);
  font-family: var(--sans);
  font-size: 16.5px;
  line-height: 1.5;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: hidden;
}
a { color: inherit; }
button, input { font: inherit; color: inherit; }

.topbar {
  display: flex;
  align-items: center;
  gap: 1.1rem;
  min-height: 3.4rem;
  padding:
    calc(0.7rem + env(safe-area-inset-top))
    calc(1.15rem + env(safe-area-inset-right))
    0.7rem
    calc(1.15rem + env(safe-area-inset-left));
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--paper) 94%, white);
  position: sticky;
  top: 0;
  z-index: 4;
  box-shadow: 0 8px 24px rgba(55, 40, 18, 0.04);
}
.brand, .topbar-back {
  color: var(--teal-deep);
  font-weight: 600;
  text-decoration: none;
  letter-spacing: -0.01em;
}
.topbar-back { padding: 0.25rem 0; }
.topbar-title {
  color: var(--muted);
  font-size: 0.92rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.logout { margin-left: auto; }
.logout .text-button { min-height: 44px; }
.notice.bad { background: var(--bad-bg); color: var(--bad); }
.login-screen {
  min-height: calc(100dvh - 8rem);
  display: grid;
  align-items: center;
  justify-items: center;
}
.login-panel { width: min(100%, 26rem); }
.login-panel h1 { font-size: clamp(1.55rem, 5vw, 2.05rem); margin-bottom: 0.9rem; }
.login-card {
  display: grid;
  gap: 0.85rem;
  margin: 0;
  padding: 1.15rem 1.1rem 1.1rem;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 22px;
  box-shadow: var(--shadow);
}
.login-card label { display: grid; gap: 0.35rem; color: var(--muted); font-size: 0.82rem; }
.login-card input {
  width: 100%;
  min-height: 48px;
  padding: 0.7rem 0.85rem;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #fff;
  font-size: 16px;
}
.login-card input:focus { outline: 2px solid #b7ddd8; border-color: transparent; }
.login-card .button { width: 100%; min-height: 48px; }

main {
  padding:
    1.75rem
    calc(1.35rem + env(safe-area-inset-right))
    calc(3.5rem + env(safe-area-inset-bottom))
    calc(1.35rem + env(safe-area-inset-left));
}
.home, .quiz-home { max-width: 980px; margin: 0 auto; }
.sheet { max-width: 760px; margin: 0 auto; }

.eyebrow {
  margin: 0 0 0.45rem;
  color: var(--teal);
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}
h1 {
  font-family: var(--serif);
  font-size: clamp(1.85rem, 3vw, 2.55rem);
  font-weight: 600;
  letter-spacing: -0.025em;
  line-height: 1.18;
  margin: 0 0 0.7rem;
}
.lede { color: var(--muted); max-width: 36rem; margin: 0 0 1.6rem; }

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 1rem; }
.card {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  min-height: 230px;
  padding: 1.35rem 1.35rem 1.2rem;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 22px;
  box-shadow: var(--shadow);
  text-decoration: none;
  transition: transform 0.16s ease, border-color 0.16s ease, box-shadow 0.16s ease;
}
.card:hover {
  transform: translateY(-2px);
  border-color: #b9ddd8;
  box-shadow: 0 18px 36px rgba(55, 40, 18, 0.08);
}
.card h2 {
  font-family: var(--serif);
  font-size: 1.55rem;
  font-weight: 600;
  line-height: 1.25;
  margin: 0.15rem 0 0.2rem;
}
.card-kicker, .card-meta, .card-progress { margin: 0; color: var(--muted); }
.card-kicker { font-size: 0.82rem; letter-spacing: 0.04em; text-transform: uppercase; color: var(--teal); font-weight: 600; }
.card-go { margin-top: auto; padding-top: 0.8rem; color: var(--teal-deep); font-weight: 600; }

.stats {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 0.65rem;
  margin: 1.25rem 0 1.15rem;
}
.stats div {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 0.8rem 0.9rem 0.7rem;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.7) inset;
}
.stats dt { color: var(--muted); font-size: 0.75rem; letter-spacing: 0.03em; text-transform: uppercase; }
.stats dd { margin: 0.2rem 0 0; font-family: var(--serif); font-size: 1.65rem; letter-spacing: -0.03em; }
.notice {
  background: var(--part-bg);
  color: #6d4a0c;
  border-radius: 14px;
  padding: 0.8rem 1rem;
  margin: 0 0 0.4rem;
}

.actions, .sheet-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.button[hidden] { display: none; }
.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 999px;
  padding: 0.62rem 1.05rem;
  text-decoration: none;
  cursor: pointer;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.8) inset;
  transition: background 0.14s ease, border-color 0.14s ease, transform 0.14s ease;
}
.button:hover { border-color: #b7ddd8; background: #fff; }
.button:active { transform: translateY(1px); }
.button.primary { background: var(--teal); border-color: var(--teal); color: white; box-shadow: none; }
.button.primary:hover { background: var(--teal-deep); border-color: var(--teal-deep); }
.button.quiet {
  background: transparent;
  box-shadow: none;
  color: var(--muted);
}
.button.quiet:hover { color: var(--bad); border-color: #f0c7c3; background: var(--bad-bg); }

.jump {
  display: flex;
  gap: 0.55rem;
  align-items: end;
  margin: 1.05rem 0 1.5rem;
}
.jump label { display: grid; gap: 0.3rem; color: var(--muted); font-size: 0.82rem; }
.jump input {
  width: 7rem;
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 12px;
  padding: 0.62rem 0.75rem;
  font-size: 16px;
}
.jump input:focus { outline: 2px solid #b7ddd8; border-color: transparent; }

.map-wrap { padding: 0; }
.map-head {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: baseline;
  margin-bottom: 0.15rem;
}
.map-head h2 { margin: 0; font-family: var(--serif); font-size: 1.35rem; font-weight: 600; }
.legend { color: var(--muted); font-size: 0.82rem; margin: 0; }
.swatch {
  display: inline-block;
  width: 0.65rem;
  height: 0.65rem;
  border-radius: 4px;
  margin: 0 0.25rem 0 0.7rem;
  vertical-align: -1px;
}
.swatch.open { background: #fff; border: 1px solid var(--line); }
.swatch.correct { background: var(--good); }
.swatch.partial { background: var(--part); }
.swatch.wrong { background: var(--bad); }
.group {
  margin-top: 0.85rem;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 1rem 1rem 1.05rem;
  box-shadow: var(--shadow);
}
.group-head { display: flex; justify-content: space-between; gap: 0.55rem 1rem; align-items: baseline; flex-wrap: wrap; }
.group-head h3 { margin: 0; font-family: var(--serif); font-size: 1.12rem; font-weight: 600; flex: 1 1 16rem; }
.group-head p { margin: 0; color: var(--muted); font-size: 0.82rem; white-space: nowrap; }
.flag-link { color: var(--bad); font-weight: 600; text-decoration: none; min-height: 44px; display: inline-flex; align-items: center; }
.button.is-flagged { background: var(--bad-bg); color: var(--bad); border-color: #f0c7c3; }
.flashcard .flag-form { margin-top: 0.8rem; }
.flashcard .flag-form .button,
.sheet > .flag-form .button { width: 100%; min-height: 48px; }
.sheet > .flag-form { margin-top: 0.8rem; }
.hit-links form { margin: 0; }
.section-name {
  color: var(--teal-deep);
  font-size: 0.82rem;
  letter-spacing: 0.01em;
  margin: 0 0 0.2rem;
}
.map {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(2.45rem, 1fr));
  gap: 0.32rem;
  margin-top: 0.7rem;
}
.cell {
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  border-radius: 9px;
  border: 1px solid var(--line);
  text-decoration: none;
  font-size: 0.74rem;
  font-variant-numeric: tabular-nums;
  background: #fff;
  transition: border-color 0.12s ease, transform 0.12s ease;
}
.cell.correct { background: var(--good-bg); color: var(--good); border-color: #c5e6d4; }
.cell.partial { background: var(--part-bg); color: var(--part); border-color: #ead7a4; }
.cell.wrong { background: var(--bad-bg); color: var(--bad); border-color: #f0c7c3; }
.cell:hover { border-color: var(--teal); transform: translateY(-1px); }

.sheet-meta { color: var(--muted); font-size: 0.92rem; }
.sheet-meta p { margin: 0 0 0.45rem; }
.bar { height: 5px; background: var(--paper-deep); border-radius: 99px; overflow: hidden; }
.bar span { display: block; height: 100%; background: linear-gradient(90deg, #149288, var(--teal)); }
.kind {
  display: inline-block;
  margin: 1.05rem 0 0.35rem;
  color: var(--teal-deep);
  background: var(--teal-soft);
  border-radius: 999px;
  padding: 0.22rem 0.7rem;
  font-size: 0.8rem;
  font-weight: 600;
}
.stem { font-size: clamp(1.4rem, 2.5vw, 1.9rem); margin-bottom: 0.85rem; }
.stem span { display: block; margin-top: 0.4rem; }
.stem span:first-child { margin-top: 0; }

.option {
  display: flex;
  gap: 0.8rem;
  align-items: flex-start;
  margin: 0.5rem 0;
  padding: 0.9rem 1rem;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  cursor: pointer;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.7) inset;
  transition: border-color 0.14s ease, background 0.14s ease;
}
.option:hover { border-color: #c5ddd8; }
.option input {
  position: absolute;
  width: 1.15rem;
  height: 1.15rem;
  margin: 0.2rem 0 0;
  opacity: 0;
}
.box {
  width: 1.15rem;
  height: 1.15rem;
  margin-top: 0.18rem;
  border: 2px solid #cfc4b4;
  border-radius: 4px;
  flex: none;
  background: white;
}
.option:has(input[type="radio"]) .box { border-radius: 50%; }
.option:has(input:checked) { border-color: #9dcec8; background: #f3faf8; }
.option:has(input:checked) .box {
  border-color: var(--teal);
  background: var(--teal);
  box-shadow: inset 0 0 0 3px #f3faf8;
}
.option:focus-within { outline: 2px solid #b7ddd8; outline-offset: 2px; }
.option-copy { display: grid; gap: 0.12rem; min-width: 0; }
.option-text { font-size: 1.05rem; }
.option em { font-style: normal; font-size: 0.78rem; font-weight: 600; color: var(--muted); }
.option em:empty { display: none; }
.option.good,
.option.good:has(input:checked) { border-color: #b7e0c8; background: var(--good-bg); }
.option.good .box,
.option.good:has(input:checked) .box { border-color: var(--good); background: var(--good); box-shadow: inset 0 0 0 3px var(--good-bg); }
.option.good em { color: var(--good); }
.option.bad,
.option.bad:has(input:checked) { border-color: #f0c7c3; background: var(--bad-bg); }
.option.bad .box,
.option.bad:has(input:checked) .box { border-color: var(--bad); background: var(--bad); box-shadow: inset 0 0 0 3px var(--bad-bg); }
.option.bad em { color: var(--bad); }
.option.missed,
.option.missed:has(input:checked) { border-style: dashed; border-color: #e0c27a; background: var(--part-bg); }
.option.missed .box,
.option.missed:has(input:checked) .box { border-color: var(--part); background: white; box-shadow: none; }
.option.missed em { color: var(--part); }
.option.idle { opacity: 0.72; }

.verdict {
  margin: 0.95rem 0 0.15rem;
  padding: 0.95rem 1.05rem;
  border-radius: 16px;
  display: grid;
  gap: 0.2rem;
}
.verdict strong { font-family: var(--serif); font-size: 1.35rem; font-weight: 600; }
.verdict span { white-space: pre-line; }
.text-button {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 0;
  cursor: pointer;
  font: inherit;
  font-size: 0.85rem;
}
.text-button:hover { color: var(--bad); }
.actions form { display: contents; }
.verdict-correct { background: var(--good-bg); color: var(--good); }
.verdict-partial { background: var(--part-bg); color: var(--part); }
.verdict-wrong { background: var(--bad-bg); color: var(--bad); }
.sheet-actions { margin-top: 1rem; }
.original { margin: 1.5rem 0 0; }
@media (min-width: 900px) {
  .original:not([hidden]) {
    width: min(1040px, calc(100vw - 3rem));
    margin-left: 50%;
    transform: translateX(-50%);
  }
}
.original[hidden] { display: none; }
.original figcaption {
  margin: 0 0 0.5rem;
  color: var(--muted);
  font-size: 0.78rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.original img {
  display: block;
  width: 100%;
  height: auto;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: white;
  box-shadow: var(--shadow);
}
@media (max-width: 900px) {
  .stats { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .map-head { flex-direction: column; align-items: flex-start; }
  h1 { font-size: 1.7rem; }
  .original:not([hidden]) {
    width: 100%;
    margin-left: 0;
    transform: none;
  }
}

@media (max-width: 480px) {
  main {
    padding:
      1rem
      calc(0.85rem + env(safe-area-inset-right))
      calc(2rem + env(safe-area-inset-bottom))
      calc(0.85rem + env(safe-area-inset-left));
  }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.5rem; }
  .stats dd { font-size: 1.4rem; }
  .card { min-height: 0; }
  .button { min-height: 44px; padding: 0.7rem 1rem; }
  .text-button { min-height: 44px; padding: 0.35rem 0.15rem; }
  .option { padding: 0.95rem 0.9rem; }
  .group-head p { white-space: normal; }
  .map { grid-template-columns: repeat(auto-fill, minmax(44px, 1fr)); gap: 0.4rem; }
  .cell { min-height: 44px; font-size: 0.8rem; border-radius: 10px; }
  .sheet-actions .button { flex: 1 1 calc(50% - 0.5rem); }
  .jump input { font-size: 16px; }
  .jump.search { flex-direction: column; align-items: stretch; }
  .chip { min-height: 44px; }
}

.tabs { display: flex; gap: 0.35rem; margin: 0 0 1.1rem; }
.tab {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0.45rem 0.95rem;
  border-radius: 999px;
  text-decoration: none;
  color: var(--muted);
  border: 1px solid transparent;
}
.tab.is-on { background: var(--card); color: var(--ink); border-color: var(--line); font-weight: 600; }
.card-links { display: flex; gap: 1rem; margin-top: auto; padding-top: 0.9rem; }
.card-links a { color: var(--teal-deep); font-weight: 600; text-decoration: none; }
.chips {
  display: flex;
  gap: 0.4rem;
  overflow-x: auto;
  margin: 0 0 0.9rem;
  padding-bottom: 0.2rem;
}
.chip {
  flex: none;
  display: inline-flex;
  align-items: center;
  min-height: 40px;
  padding: 0.35rem 0.8rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--card);
  text-decoration: none;
  color: var(--muted);
  font-size: 0.92rem;
}
.chip.is-on { color: var(--teal-deep); border-color: #9dcec8; background: var(--teal-soft); font-weight: 600; }
.study-bar { display: flex; justify-content: space-between; flex-wrap: wrap; gap: 0.4rem 1rem; align-items: baseline; color: var(--muted); font-size: 0.92rem; }
.study-bar p { margin: 0; }
.study-bar a { color: var(--teal-deep); font-weight: 600; text-decoration: none; }
.flashcard {
  margin-top: 0.8rem;
  padding: 1.15rem 1.15rem 1rem;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 22px;
  box-shadow: var(--shadow);
  cursor: pointer;
}
.face-options { list-style: none; margin: 0.8rem 0 0; padding: 0; display: grid; gap: 0.45rem; }
.face-options li {
  padding: 0.75rem 0.85rem;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #fff;
}
.face-options li.good { border-color: #b7e0c8; background: var(--good-bg); color: var(--good); }
.face-options li.idle { opacity: 0.55; }
.flashcard .button { margin-top: 1rem; }
.face-back[hidden] { display: none; }
.flashcard.is-flipped .face-front { display: none; }
.mark-actions { display: flex; gap: 0.5rem; margin-top: 1rem; }
.mark-actions form { flex: 1; }
.mark-actions .button { width: 100%; }
.study-links { display: flex; gap: 0.9rem; align-items: center; }
.study-links form { margin: 0; }
.study-links a { color: var(--teal-deep); font-weight: 600; text-decoration: none; }
.jump.search { flex-wrap: wrap; }
.jump.search label { flex: 1; min-width: min(100%, 16rem); }
.jump.search input { width: 100%; }
.search-hits { list-style: none; margin: 0.6rem 0 0; padding: 0; display: grid; gap: 0.6rem; }
.search-hit {
  padding: 0.95rem 1rem 0.85rem;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 18px;
}
.search-hit .kind { margin-top: 0; }
.stem-preview {
  margin: 0.35rem 0 0.7rem;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.hit-links { display: flex; gap: 1rem; }
.hit-links a { color: var(--teal-deep); font-weight: 600; text-decoration: none; min-height: 44px; display: inline-flex; align-items: center; }
.sheet-actions .original-toggle { flex: 1 1 100%; }
