*, *::before, *::after { box-sizing: border-box; }

:root {
  --bg: #f3f0e8;
  --card: #ffffff;
  --ink: #1f2a37;
  --muted: #647084;
  --line: #d9d4c7;
  --blue: #2f5bd7;
  --blue-dark: #2447ad;
  --green: #18794e;
  --green-bg: #e3f4ea;
  --red: #c0392b;
  --red-bg: #fbe9e6;
  --amber: #a15c00;
  --key: #ffffff;
}

html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 17px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
.hidden { display: none !important; }
h1, h2, h3, h4, p { margin: 0 0 .6rem; }
h2 { font-size: 1.45rem; }
h3 { font-size: 1.1rem; }
h4 { font-size: .85rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin-top: 1rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }
.error { color: var(--red); min-height: 1.2em; font-weight: 600; }
.warn { color: var(--amber); font-weight: 600; }
.loading { text-align: center; color: var(--muted); padding: 3rem 1rem; }
.notice { max-width: 760px; margin: 1rem auto 0; padding: .7rem 1rem; background: #fff6d6; border: 1px solid #e6cf7a; border-radius: 10px; width: calc(100% - 2rem); }

/* ── Buttons and fields ─────────────────────────────────────────────────── */
button, input { font: inherit; color: inherit; }
button {
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 10px;
  padding: .55rem 1rem;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}
button:hover { border-color: #b9b29f; }
button:disabled { opacity: .45; cursor: default; }
button.primary { background: var(--blue); border-color: var(--blue); color: #fff; }
button.primary:hover:not(:disabled) { background: var(--blue-dark); }
button.danger { background: var(--red); border-color: var(--red); color: #fff; }
button.quiet { border-color: transparent; background: transparent; color: var(--muted); }
button.quiet:hover { color: var(--ink); background: rgba(0, 0, 0, .05); }
button.wide { display: block; width: 100%; margin-top: .6rem; padding: .8rem 1rem; }
button:focus-visible, input:focus-visible, summary:focus-visible { outline: 3px solid #9db6f5; outline-offset: 2px; }

input:not([type=radio]):not([type=checkbox]) {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: .6rem .75rem;
  background: #fff;
}
.field { display: block; font-weight: 600; margin-bottom: .8rem; }
.field input { margin-top: .3rem; font-weight: 400; }
.check { display: flex; gap: .6rem; align-items: center; margin-bottom: .8rem; }
.check input { width: 1.2rem; height: 1.2rem; }
fieldset { border: 0; padding: 0; margin: 0 0 1rem; }
legend { font-weight: 600; margin-bottom: .4rem; padding: 0; }

.row { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.row.end { justify-content: flex-end; margin-top: .8rem; }
.grow { flex: 1; min-width: 0; }

/* ── Frame ──────────────────────────────────────────────────────────────── */
#topbar {
  display: flex; align-items: center; gap: .8rem;
  padding: .6rem 1rem; padding-top: calc(.6rem + env(safe-area-inset-top));
  background: var(--blue); color: #fff;
}
#topbar .who { margin-left: auto; font-size: .9rem; opacity: .9; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#topbar button.quiet { color: #fff; border-color: rgba(255, 255, 255, .5); padding: .35rem .7rem; }
.brand { font-size: 1.2rem; font-weight: 400; letter-spacing: .01em; white-space: nowrap; }
.brand b { font-weight: 800; }
.brand.big { font-size: 2.2rem; text-align: center; color: var(--blue); }

#app { max-width: 760px; margin: 0 auto; padding: 1rem; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 1rem 1.1rem; margin-bottom: .8rem; }
.auth-card { max-width: 420px; margin: 2rem auto; }
#auth { padding: 0 1rem; }
.empty { color: var(--muted); padding: 1.5rem 1rem; text-align: center; border: 2px dashed var(--line); border-radius: 14px; }

.roles { display: flex; gap: .6rem; flex-wrap: wrap; }
.roles legend { float: left; width: 100%; }
.role { flex: 1; }
.role input { position: absolute; opacity: 0; }
.role span { display: block; text-align: center; padding: .8rem .5rem; border: 2px solid var(--line); border-radius: 12px; font-weight: 700; cursor: pointer; }
.role input:checked + span { border-color: var(--blue); background: #e9effd; color: var(--blue-dark); }
.role input:focus-visible + span { outline: 3px solid #9db6f5; }

.tabs { display: flex; gap: .4rem; margin-bottom: 1rem; overflow-x: auto; }
.tab { border-radius: 999px; white-space: nowrap; }
.tab.on { background: var(--ink); border-color: var(--ink); color: #fff; }

.item { display: flex; gap: 1rem; align-items: center; }
.item h3 { margin-bottom: .2rem; overflow-wrap: anywhere; }
.item p { margin-bottom: .2rem; }
.item.done { background: #fbfaf6; }
.actions { display: flex; gap: .4rem; flex-wrap: wrap; justify-content: flex-end; align-items: center; }
.preview { color: var(--muted); font-size: .9rem; overflow-wrap: anywhere; }
.score { color: var(--green); font-weight: 700; }
.due { color: var(--amber); font-weight: 600; }
.due.late { color: var(--red); }
.back { margin-bottom: .5rem; padding-left: .3rem; }
.teacher { margin-bottom: 1.5rem; }
.teacher-title { font-size: 1.2rem; margin: 0; }
.teacher-name { font-size: 1.8rem; font-weight: 800; color: var(--blue); margin: 0; overflow-wrap: anywhere; }

/* ── Word list editor ───────────────────────────────────────────────────── */
.words { margin: .5rem 0 .8rem; }
.word-row {
  display: grid;
  grid-template-columns: 1.8rem minmax(0, 1fr) minmax(0, 1.4fr) minmax(0, 1fr) minmax(0, 1.4fr) 2.2rem;
  gap: .35rem; align-items: center; margin-bottom: .35rem;
}
.word-row .num { color: var(--muted); font-size: .85rem; text-align: right; }
.word-row input { padding: .45rem .55rem; }
.word-row .w-word { font-weight: 700; }
.word-row .x { padding: .3rem; }

.kinds { display: grid; gap: .5rem; }
.kind { display: flex; gap: .7rem; align-items: flex-start; padding: .7rem .8rem; border: 2px solid var(--line); border-radius: 12px; cursor: pointer; }
.kind:has(input:checked) { border-color: var(--blue); background: #e9effd; }
.kind input { margin-top: .3rem; width: 1.1rem; height: 1.1rem; }
.kind b, .kind small { display: block; }
.kind small { color: var(--muted); }
.kind.off { opacity: .55; cursor: default; }

/* ── Results ────────────────────────────────────────────────────────────── */
.stats { display: flex; gap: .8rem; margin-bottom: .8rem; }
.stat { flex: 1; background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: .8rem 1rem; color: var(--muted); }
.stat b { display: block; font-size: 1.6rem; color: var(--ink); }
.chips { display: flex; flex-wrap: wrap; gap: .4rem; margin: 0; }
.chip { background: var(--red-bg); border-radius: 999px; padding: .2rem .7rem; }
.student summary { display: flex; gap: .6rem; align-items: center; cursor: pointer; list-style: none; flex-wrap: wrap; }
.student summary::-webkit-details-marker { display: none; }
.student summary::before { content: '▸'; color: var(--muted); }
.student[open] summary::before { content: '▾'; }
.student[open] summary { margin-bottom: .6rem; }
.name { font-weight: 700; overflow-wrap: anywhere; }
.answers { list-style: none; margin: 0 0 .8rem; padding: 0; text-align: left; }
.answers li { display: flex; gap: .6rem; padding: .4rem .6rem; border-radius: 8px; margin-bottom: .25rem; overflow-wrap: anywhere; }
.answers li.right { background: var(--green-bg); }
.answers li.wrong { background: var(--red-bg); }
.answers .tick { font-weight: 800; }
.right .tick { color: var(--green); }
.wrong .tick { color: var(--red); }
.gave { color: var(--red); }

/* ── Taking a test ──────────────────────────────────────────────────────── */
.intro .bigicon { font-size: 3.5rem; margin: .5rem 0; }
.test { max-width: 620px; margin: 0 auto; }
.progress { display: flex; align-items: center; gap: .7rem; color: var(--muted); font-weight: 600; margin-bottom: 1rem; }
.bar { flex: 1; height: 10px; background: #e2ddd0; border-radius: 999px; overflow: hidden; }
.fill { height: 100%; background: var(--blue); border-radius: 999px; transition: width .2s; }
.listen { display: flex; gap: .5rem; flex-wrap: wrap; justify-content: center; margin-bottom: 1rem; }
.listen .hear { background: var(--blue); border-color: var(--blue); color: #fff; font-size: 1.15rem; padding: .8rem 1.4rem; flex-basis: 100%; }

.typed {
  min-height: 4rem; display: flex; align-items: center; justify-content: center;
  background: #fff; border: 2px solid var(--ink); border-radius: 14px;
  font-size: 2rem; font-weight: 700; letter-spacing: .08em; padding: .5rem .8rem; margin-bottom: 1rem;
  white-space: pre-wrap; overflow-wrap: anywhere; text-align: center;
  user-select: none; -webkit-user-select: none;
}
.caret { display: inline-block; width: 3px; height: 2.2rem; background: var(--blue); margin-left: 2px; animation: blink 1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }

.keyboard { margin-bottom: 1rem; user-select: none; -webkit-user-select: none; touch-action: manipulation; }
.keys { display: flex; justify-content: center; gap: 5px; margin-bottom: 5px; }
.key {
  flex: 1 1 0; max-width: 56px; min-width: 0; height: 52px; padding: 0;
  background: var(--key); border: 1px solid #c9c3b3; border-bottom-width: 3px; border-radius: 8px;
  font-size: 1.35rem; font-weight: 600;
}
.key:active { background: #dfe7fb; transform: translateY(1px); }
.key.space { flex: 4 1 0; max-width: 320px; font-size: 1rem; color: var(--muted); }
.key.back { flex: 1.6 1 0; max-width: 110px; }

.prompt { text-align: center; color: var(--muted); font-weight: 600; }
.vocab-word { display: flex; align-items: center; justify-content: center; gap: .4rem; font-size: 2.4rem; font-weight: 800; margin-bottom: 1rem; overflow-wrap: anywhere; text-align: center; }
.vocab-word .say { font-size: 1.4rem; }
.choices { display: grid; gap: .6rem; margin-bottom: 1rem; }
.choice { text-align: left; padding: .9rem 1rem; border-width: 2px; font-weight: 500; font-size: 1.05rem; overflow-wrap: anywhere; }
.choice.on { border-color: var(--blue); background: #e9effd; }

.bigscore { font-size: 3.4rem; font-weight: 800; color: var(--blue); margin: .3rem 0 0; }
.cheer { font-size: 1.2rem; font-weight: 600; }
.result h3 { text-align: left; margin-top: 1rem; }

/* ── Brightness: a dark sheet over everything, clicks go through it ─────── */
#dim { position: fixed; inset: 0; z-index: 40; background: #000; opacity: 0; pointer-events: none; }
.brightness { display: block; font-size: .9rem; color: var(--muted); }
.brightness b { color: var(--ink); }
.brightness input { display: block; width: 100%; margin-top: .4rem; padding: 0; accent-color: var(--blue); cursor: pointer; }
.auth-card .brightness { margin-top: 1rem; }
.bright-panel { position: fixed; top: calc(3.2rem + env(safe-area-inset-top)); right: .6rem; width: min(260px, calc(100% - 1.2rem)); z-index: 9; margin: 0; box-shadow: 0 8px 24px rgba(0, 0, 0, .18); }
#bright-btn { font-size: 1.1rem; line-height: 1; }

/* ── Pop-up ─────────────────────────────────────────────────────────────── */
.overlay { position: fixed; inset: 0; background: rgba(20, 26, 36, .55); display: flex; align-items: center; justify-content: center; padding: 1rem; z-index: 10; }
.ask-card { width: 100%; max-width: 420px; margin: 0; }

@media (max-width: 720px) {
  body { font-size: 16px; }
  .item { flex-direction: column; align-items: stretch; }
  .actions { justify-content: flex-start; }
  .word-row { grid-template-columns: 1.6rem minmax(0, 1fr) 2.2rem; padding-bottom: .5rem; margin-bottom: .6rem; border-bottom: 1px solid var(--line); }
  .word-row .w-meaning, .word-row .w-opposite, .word-row .w-sentence { grid-column: 2 / 3; }
  .typed { font-size: 1.6rem; }
  .keys { gap: 3px; }
  .key { height: 48px; font-size: 1.2rem; }
  .vocab-word { font-size: 1.9rem; }
}
