:root {
  color-scheme: light;
  --bg: #f3eee4;
  --ink: #1c1612;
  --muted: #6d6156;
  --line: #ddd3c4;
  --paper: #fffdf8;
  --clay: #8d4b2f;
  --ok: #0e7a3c;
  --ok-bg: #e6f6ec;
  --ok-line: #b7e0c6;
  --bad: #b42318;
  --bad-bg: #fdeceb;
  --bad-line: #f3c0bb;
  --focus: #c46a3a;
  --field: #fff;
  --field-line: #cfc3b2;
  --action: #2a211b;
  --action-ink: #fffdf8;
  --action-hover: #3d3228;
  --hover-line: #c9baa8;
  --shadow: 0 1px 0 rgba(36, 24, 16, 0.04);
  --tab-bg: rgba(36, 24, 16, 0.08);
  --tab-active: #fffdf8;
  --tab-active-ink: #1c1612;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #141210;
  --ink: #f6f1ea;
  --muted: #cbbbaa;
  --line: #7a6b5d;
  --paper: #221e1a;
  --clay: #f0b08a;
  --ok: #5dce86;
  --ok-bg: #13281c;
  --ok-line: #3d8a5c;
  --bad: #ff8d84;
  --bad-bg: #2e1614;
  --bad-line: #c46a62;
  --focus: #e39a66;
  --field: #1a1714;
  --field-line: #8a7768;
  --action: #f3eee4;
  --action-ink: #1c1612;
  --action-hover: #fffdf8;
  --hover-line: #a8927e;
  --shadow: 0 1px 0 rgba(0, 0, 0, 0.45);
  --tab-bg: rgba(246, 241, 234, 0.12);
  --tab-active: #f3eee4;
  --tab-active-ink: #1c1612;
}

* {
  box-sizing: border-box;
}

html {
  overflow-x: clip;
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100dvh;
  max-width: 100%;
  overflow-x: clip;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  line-height: 1.45;
}

.page {
  width: min(36rem, 100%);
  margin: 0 auto;
  padding:
    max(1.75rem, env(safe-area-inset-top))
    max(1.15rem, env(safe-area-inset-right))
    max(2.75rem, env(safe-area-inset-bottom))
    max(1.15rem, env(safe-area-inset-left));
  animation: page-in 220ms ease both;
}

.chrome {
  margin: 0 0 0.85rem;
}

.chrome-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin: 0 0 0.55rem;
}

.chrome-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.55rem 0.75rem;
}

h1 {
  margin: 0;
  font-size: 1.7rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.2;
}

.lede {
  margin: 0 0 0.35rem;
  color: var(--muted);
  font-size: 0.95rem;
}

.tabs {
  display: inline-flex;
  align-items: stretch;
  gap: 0.15rem;
  margin: 0;
  padding: 0.18rem;
  min-width: 0;
  border-radius: 999px;
  background: var(--tab-bg);
}

.tab {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 2rem;
  padding: 0.25rem 0.75rem;
  border-radius: 999px;
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 600;
  line-height: 1.2;
  text-decoration: none;
  white-space: nowrap;
  touch-action: manipulation;
}

.tab:hover {
  color: var(--ink);
}

.tab.is-active {
  background: var(--tab-active);
  color: var(--tab-active-ink);
  box-shadow: var(--shadow);
}

button.theme-toggle {
  flex: 0 0 auto;
  margin: 0;
  min-height: 2rem;
  padding: 0.28rem 0.7rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--paper);
  color: var(--ink);
  font-size: 0.85rem;
  font-weight: 600;
  line-height: 1.2;
}

button.theme-toggle:hover {
  border-color: var(--hover-line);
}

button.theme-toggle:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

html[data-theme="dark"] .theme-label-light,
html:not([data-theme="dark"]) .theme-label-dark {
  display: none;
}

.score {
  margin: 0;
  font-size: 0.95rem;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  white-space: nowrap;
}

#quiz {
  min-width: 0;
}

.card {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 1.15rem 1.1rem 1rem;
  box-shadow: var(--shadow);
  animation: card-in 250ms ease both;
}

.meta {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.35rem 0.75rem;
  margin: 0 0 0.7rem;
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.topic {
  color: var(--clay);
}

.lang {
  color: var(--muted);
  font-weight: 600;
}

.prompt {
  margin: 0 0 1.15rem;
  font-size: clamp(2rem, 1.35rem + 2.6vw, 2.45rem);
  font-weight: 700;
  line-height: 1.15;
  letter-spacing: -0.03em;
  overflow-wrap: anywhere;
}

form {
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
  min-width: 0;
}

label {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  min-width: 0;
  color: var(--muted);
  font-size: 0.92rem;
}

input[type="text"] {
  width: 100%;
  min-width: 0;
  max-width: 100%;
  min-height: 3rem;
  padding: 0.75rem 0.85rem;
  border: 1px solid var(--field-line);
  border-radius: 12px;
  background: var(--field);
  color: var(--ink);
  font: inherit;
  font-size: max(16px, 1.125rem);
}

input[type="text"]:focus {
  outline: 2px solid var(--focus);
  outline-offset: 1px;
  border-color: var(--focus);
}

button {
  min-height: 3rem;
  padding: 0.7rem 1rem;
  border-radius: 12px;
  font: inherit;
  font-size: 1rem;
  cursor: pointer;
  touch-action: manipulation;
}

button[type="submit"],
button.continue {
  border: 0;
  background: var(--action);
  color: var(--action-ink);
  font-weight: 600;
}

button[type="submit"]:hover,
button.continue:hover {
  background: var(--action-hover);
}

button.skip {
  display: block;
  align-self: flex-start;
  width: auto;
  min-height: 2.25rem;
  margin: 0.15rem 0 0;
  padding: 0.15rem 0.1rem;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 0.95rem;
  font-weight: 600;
  text-decoration: underline;
  text-underline-offset: 0.18em;
}

button.skip:hover {
  color: var(--ink);
}

.dock {
  margin-top: 0.75rem;
  animation: page-in 220ms ease both;
}

.dock button {
  width: 100%;
}

form.htmx-request button[type="submit"],
button.skip.htmx-request,
button.continue.htmx-request {
  opacity: 0.6;
}

.result {
  margin: 0 0 1rem;
  padding: 0.85rem 1rem;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--paper);
  animation: verdict-in 220ms ease both;
}

.result:has(.ok) {
  background: var(--ok-bg);
  border-color: var(--ok-line);
}

.result:has(.bad) {
  background: var(--bad-bg);
  border-color: var(--bad-line);
}

.verdict {
  margin: 0 0 0.15rem;
  font-size: 1.05rem;
  font-weight: 700;
}

.verdict.ok {
  color: var(--ok);
}

.verdict.bad {
  color: var(--bad);
}

.sticker {
  margin: 0.15rem 0 0.45rem;
}

.sticker img {
  display: block;
  width: 5.5rem;
  height: 5.5rem;
  max-width: 100%;
  object-fit: contain;
}

.asked {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 700;
  overflow-wrap: anywhere;
}

.given,
.correction {
  margin: 0.3rem 0 0;
  font-size: 0.95rem;
  overflow-wrap: anywhere;
}

.correction {
  font-weight: 600;
}

.sheet {
  list-style: none;
  margin: 0;
  padding: 0;
}

.sheet li {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  column-gap: 0.4rem;
  row-gap: 0.15rem;
  margin: 0;
  padding: 0.4rem 0;
  min-width: 0;
}

.sheet li > span {
  min-width: 0;
  max-width: 100%;
  overflow-wrap: anywhere;
}

@keyframes page-in {
  from { opacity: 0.45; }
  to { opacity: 1; }
}

@keyframes card-in {
  from {
    opacity: 0;
    transform: translateY(0.55rem);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

@keyframes verdict-in {
  from {
    opacity: 0;
    transform: translateY(-0.4rem);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

@media (max-width: 40rem) {
  .page {
    display: flex;
    flex-direction: column;
    min-height: 100dvh;
    padding:
      max(0.35rem, env(safe-area-inset-top))
      max(0.7rem, env(safe-area-inset-right))
      max(0.55rem, env(safe-area-inset-bottom))
      max(0.7rem, env(safe-area-inset-left));
  }

  .page:has(#quiz) {
    padding-bottom: calc(4.55rem + env(safe-area-inset-bottom));
  }

  .chrome {
    position: sticky;
    top: 0;
    z-index: 6;
    flex: 0 0 auto;
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    gap: 0.35rem;
    margin: 0 0 0.35rem;
    background: var(--bg);
  }

  .chrome-top {
    flex: 0 0 auto;
    order: 2;
    margin: 0;
    gap: 0;
  }

  .chrome-top h1 {
    display: none;
  }

  .chrome-row {
    flex: 1 1 auto;
    order: 1;
    min-width: 0;
    flex-wrap: nowrap;
    gap: 0.3rem 0.35rem;
  }

  .quip,
  .hint {
    display: none;
  }

  .tabs {
    flex: 1 1 auto;
    min-width: 0;
    padding: 0.12rem;
    gap: 0.1rem;
  }

  .tab {
    flex: 1 1 0;
    min-height: 1.85rem;
    padding: 0.2rem 0.28rem;
    font-size: 0.74rem;
  }

  button.theme-toggle {
    min-height: 1.85rem;
    min-width: auto;
    padding: 0.2rem 0.45rem;
    font-size: 0.74rem;
  }

  .score {
    flex: 0 0 auto;
    font-size: 0.72rem;
  }

  #quiz {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    min-height: 0;
  }

  #quiz > form,
  #quiz > .result {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    min-height: 0;
  }

  .card {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    min-height: 0;
    padding: 0.85rem 0.95rem 0.75rem;
    border-radius: 18px;
  }

  .meta {
    flex: 0 0 auto;
    margin-bottom: 0.35rem;
    font-size: 0.72rem;
  }

  .prompt {
    flex: 1 1 auto;
    display: flex;
    align-items: center;
    font-size: clamp(1.95rem, 1rem + 5.5vw, 2.5rem);
    margin: 0 0 0.55rem;
    min-height: 3.5rem;
  }

  .card label,
  .card .skip {
    flex: 0 0 auto;
  }

  form {
    gap: 0;
  }

  .card label {
    gap: 0.3rem;
    font-size: 0.88rem;
  }

  input[type="text"] {
    font-size: 16px;
    min-height: 2.85rem;
    padding: 0.6rem 0.75rem;
    scroll-margin-top: 3.5rem;
    scroll-margin-bottom: 5.5rem;
  }

  button[type="submit"],
  button.continue {
    min-height: 2.75rem;
  }

  button.skip {
    min-height: 2.1rem;
    margin-top: 0.35rem;
  }

  .dock {
    position: fixed;
    z-index: 5;
    left: 50%;
    bottom: 0;
    width: min(36rem, 100%);
    margin: 0;
    transform: translateX(-50%);
    padding:
      0.4rem
      max(0.7rem, env(safe-area-inset-right))
      calc(0.4rem + env(safe-area-inset-bottom))
      max(0.7rem, env(safe-area-inset-left));
    background: var(--bg);
    box-shadow: 0 -1px 0 var(--line);
  }

  .result {
    margin: 0;
    padding: 1.1rem 1rem 1.2rem;
    border-radius: 18px;
    justify-content: center;
    text-align: center;
    align-items: center;
  }

  .verdict {
    font-size: 1.35rem;
  }

  .result .lede {
    font-size: 0.95rem;
  }

  .given,
  .correction {
    font-size: 0.9rem;
  }

  .asked {
    font-size: 1.35rem;
  }

  .sticker {
    flex: 0 0 auto;
    margin: 0.45rem 0 0.55rem;
  }

  .sticker img {
    width: 7.25rem;
    height: 7.25rem;
    margin-inline: auto;
    object-fit: contain;
  }

  .sheet {
    margin-top: 0.25rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .page,
  .card,
  .result,
  .dock {
    animation: none;
  }
}
