/* English File Upper-intermediate — Mini App "Exercise book".
   A student's exercise book: blue-black ink, ruled lines, one red margin line, a highlighter, a red and a green pen.
   Numbers and answer letters stay in the margin. The handwriting font is only for the answer and the circled labels. */

/* Local fonts (latin subset, SIL OFL 1.1, see fonts/OFL-*.txt). No request to Google. */
@font-face {
  font-family: "Atkinson Hyperlegible Next";
  font-style: normal;
  font-weight: 400 700;
  font-display: swap;
  src: url("fonts/atkinson-hyperlegible-next.woff2") format("woff2");
}

@font-face {
  font-family: "Atkinson Hyperlegible Next";
  font-style: italic;
  font-weight: 400 700;
  font-display: swap;
  src: url("fonts/atkinson-hyperlegible-next-italic.woff2") format("woff2");
}

/* Playwrite England SemiJoined: the handwriting model of English schools. */
@font-face {
  font-family: "Playwrite GB S";
  font-style: normal;
  font-weight: 100 400;
  font-display: swap;
  src: url("fonts/playwrite-gb-s.woff2") format("woff2");
}

:root {
  --paper: #fbfcfe;
  --ink: #1c2b57;
  --muted: #54648c;
  --pen: #2446b8;
  --on-pen: #fbfcfe;
  --rule: #cbdcf3;
  --wash: #eaf1fb;
  --margin: #d2354a;
  --tick: #1d7a46;
  --marker: #ffe45c;
  --shadow: 0 12px 32px -18px rgba(28, 43, 87, 0.35);

  --font-body: "Atkinson Hyperlegible Next", "Atkinson Hyperlegible", system-ui, sans-serif;
  --font-hand: "Playwrite GB S", cursive;

  --line-x: 44px;  /* The red margin line, from the left edge of main. */
  --text-x: 56px;  /* The start of the text. */
  --row: 48px;     /* The distance of the ruled lines in the exercise. */
  --stroke: 4px 10px 5px 9px;  /* The uneven corners of a highlighter stroke. */

  --gutter: 16px;
  --tabs-height: 64px;
  --safe-top: max(var(--tg-content-safe-area-inset-top, 0px) + var(--tg-safe-area-inset-top, 0px), env(safe-area-inset-top, 0px));
  --safe-bottom: max(var(--tg-content-safe-area-inset-bottom, 0px) + var(--tg-safe-area-inset-bottom, 0px), env(safe-area-inset-bottom, 0px));

  color-scheme: light;
}

:root[data-theme="dark"] {
  --paper: #121a2d;
  --ink: #e6ecfa;
  --muted: #9eabcc;
  --pen: #93adff;
  --on-pen: #121a2d;
  --rule: #27345a;
  --wash: #1a2542;
  --margin: #f2707f;
  --tick: #6fd39a;
  --marker: #6b5a0c;
  --shadow: 0 12px 32px -18px rgba(0, 0, 0, 0.8);
  color-scheme: dark;
}

* { box-sizing: border-box; }

/* The fixed tab bar covers the bottom. scrollIntoView() must stop above it. */
html {
  -webkit-text-size-adjust: 100%;
  scrollbar-gutter: stable;
  scroll-padding-bottom: calc(var(--tabs-height) + var(--safe-bottom) + 12px);
}

body {
  margin: 0;
  min-height: var(--tg-viewport-stable-height, 100vh);
  background: var(--paper);
  color: var(--ink);
  font: 400 17px/1.5 var(--font-body);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

h1, h2, h3, h4 {
  font-weight: 700;
  line-height: 1.2;
  margin: 0;
}

button, input { font: inherit; color: inherit; }

:focus-visible { outline: 2px solid var(--pen); outline-offset: 3px; }

[hidden] { display: none !important; }

/* ---------- Frame ---------- */

/* The red margin line goes down each screen. */
main {
  position: relative;
  max-width: 560px;
  min-height: var(--tg-viewport-stable-height, 100vh);
  margin: 0 auto;
  padding: calc(20px + var(--safe-top)) var(--gutter) calc(var(--tabs-height) + var(--safe-bottom) + 32px) var(--text-x);
  outline: none;
}

main::before {
  content: "";
  position: absolute;
  inset: 0 auto 0 var(--line-x);
  width: 1.5px;
  background: var(--margin);
  pointer-events: none;
}

.tabs {
  position: fixed;
  inset: auto 0 0 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tabs-height) + var(--safe-bottom));
  padding: 0 max(0px, calc((100% - 560px) / 2)) var(--safe-bottom);
  background: color-mix(in srgb, var(--paper) 94%, transparent);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--rule);
  z-index: 10;
}

.tabs a {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  color: var(--muted);
  text-decoration: none;
  font-size: 12px;
  font-weight: 600;
}

.tabs svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linejoin: round;
  stroke-linecap: round;
}

.tabs a[aria-current="page"] { color: var(--ink); }
.tabs a[aria-current="page"] svg { stroke: var(--pen); fill: color-mix(in srgb, var(--pen) 16%, transparent); }
/* A highlighter stroke below the label of the current tab. */
.tabs a[aria-current="page"] span {
  padding: 0 6px;
  background: linear-gradient(transparent 38%, var(--marker) 38% 96%, transparent 96%);
}

/* ---------- Shared parts ---------- */

.lede { color: var(--muted); margin: 8px 0 0; max-width: 34em; }

.section-heading {
  font-size: 21px;
  margin: 36px 0 4px;
}

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 48px;
  padding: 0 22px;
  border: 0;
  border-radius: 8px;
  background: var(--pen);
  color: var(--on-pen);
  font-weight: 700;
  cursor: pointer;
  transition: transform 120ms ease, opacity 120ms ease;
}

.button:active,
.chip:active,
.answer:not(:disabled):active,
.unit:active { transform: scale(0.98); }
.button:disabled { opacity: 0.5; cursor: default; }

a.button { text-decoration: none; }

.button-quiet {
  background: transparent;
  color: var(--pen);
  box-shadow: inset 0 0 0 1.5px var(--rule);
}

.button-row { display: flex; gap: 10px; margin-top: 20px; }
.button-row .button { flex: 1; padding: 0 8px; }
.button-row + .button-row { margin-top: 10px; }

.chip {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0 14px;
  border-radius: 8px;
  border: 1.5px solid var(--rule);
  background: var(--paper);
  color: var(--ink);
  font-weight: 600;
  cursor: pointer;
  transition: transform 120ms ease, border-color 160ms ease;
}

.chip[aria-pressed="true"] {
  background: var(--marker);
  border-color: var(--ink);
}

.notice {
  margin: 0 0 16px;
  padding: 10px 12px;
  background: var(--marker);
  border-radius: var(--stroke);
}

.notice-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.notice-row .chip { flex-shrink: 0; }
.history-nav { margin: 0 0 16px; }  /* "← Back" to an earlier question. */

.state {
  position: relative;
  padding: 48px 0;
  color: var(--muted);
}
.state h1 { color: var(--ink); font-size: 26px; margin-bottom: 8px; }
.state .button { margin-top: 20px; }
/* The circled "Aa" is in the margin, beside the heading. */
.state > .unit-mark { position: absolute; top: 43px; left: calc(2px - var(--text-x)); }

/* The circled number or letters of a teacher, in handwriting. */
.unit-mark {
  position: relative;
  display: grid;
  place-items: center;
  flex: none;
  width: 40px;
  height: 40px;
  color: var(--ink);
  font: 400 15px/1 var(--font-hand);
}
.unit-mark svg {
  position: absolute;
  inset: 0;
  fill: none;
  stroke: var(--margin);
  stroke-width: 1.6;
  stroke-linecap: round;
}

/* A highlighter stroke shows the answered part. */
.progress {
  display: block;
  height: 10px;
  background: var(--wash);
  border-radius: var(--stroke);
  overflow: hidden;
}
.progress::after {
  content: "";
  display: block;
  width: calc(var(--done) * 100%);
  height: 100%;
  background: var(--marker);
  border-radius: inherit;
}

/* Rows that start in the margin. The first column holds the number or the letter. */
.unit,
.answer,
.section-link,
.quiz-head {
  display: grid;
  grid-template-columns: var(--line-x) minmax(0, 1fr);
  column-gap: calc(var(--text-x) - var(--line-x));
  width: calc(100% + var(--text-x));
  margin-left: calc(-1 * var(--text-x));
}

.unit:focus-visible,
.answer:focus-visible,
.section-link:focus-visible { outline-offset: -3px; }

/* ---------- Home ---------- */

.hello h1 {
  font-size: clamp(30px, 9vw, 40px);
  letter-spacing: -0.01em;
}

.hello .button { margin-top: 20px; width: 100%; }

.units { list-style: none; margin: 8px 0 0; padding: 0; }

.unit {
  align-items: start;
  padding: 16px 0;
  border: 0;
  border-bottom: 1px solid var(--rule);
  background: none;
  text-align: left;
  cursor: pointer;
  transition: transform 120ms ease, background 160ms ease;
}

.units li:first-child .unit { border-top: 1px solid var(--rule); }
.unit > .unit-mark { justify-self: center; }
.unit-body { display: block; }

.unit-name {
  display: flex;
  align-items: baseline;
  gap: 10px;
  font-size: 20px;
  font-weight: 700;
}

.unit-title {
  display: block;
  font-style: italic;
  color: var(--muted);
  margin: 2px 0 8px;
}

.unit .progress { margin-bottom: 8px; }
.unit-stats { display: block; font-size: 14px; color: var(--muted); }
.unit-stats b { color: var(--ink); font-weight: 700; }

.badge {
  font-size: 12px;
  font-weight: 700;
  color: var(--ink);
  background: var(--marker);
  border-radius: var(--stroke);
  padding: 1px 8px;
}

/* ---------- Quiz ---------- */

.quiz-head {
  grid-template-columns: var(--line-x) minmax(0, 1fr) auto;
  align-items: center;
  margin-bottom: 8px;
}
.quiz-head > .unit-mark { justify-self: center; }
.quiz-head h1 { font-size: 20px; }
.quiz-topic { display: block; font-style: italic; color: var(--muted); font-size: 15px; }
.quiz-head + .progress { margin-bottom: 20px; }

.icon-button {
  display: inline-grid;
  place-items: center;
  width: 44px;
  height: 44px;
  flex: none;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--pen);
  cursor: pointer;
}
.icon-button svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linejoin: round;
  stroke-linecap: round;
}
.icon-button[aria-pressed="true"] { color: var(--muted); }

/* The exercise: the ruled lines start at the margin line and go to the edge of the screen. */
.exercise {
  margin: 0 calc(-1 * var(--gutter)) 0 calc(var(--line-x) - var(--text-x));
  padding: 0 var(--gutter) 0 calc(var(--text-x) - var(--line-x));
}

.instruction { margin: 0 0 8px; color: var(--muted); font-size: 15px; }
.instruction.gloss { margin: 8px 0 0; }

.prompt {
  margin: 0;
  font-size: clamp(21px, 6vw, 25px);
  line-height: var(--row);
  font-weight: 500;
  /* A ruled line below each line of text. The offset puts the line near the letters. */
  background: repeating-linear-gradient(to bottom,
    transparent 0 calc(var(--row) - 1px), var(--rule) calc(var(--row) - 1px) var(--row));
  background-position: 0 -8px;
}

.blank {
  display: inline-block;
  min-width: 3.2em;
  padding: 0 4px;
  border-bottom: 2px solid var(--pen);
  line-height: 1.2;
  text-align: center;
}

/* The answer in handwriting. vertical-align: top keeps each of its lines on the ruled lines. */
.blank.filled {
  max-width: 100%;
  border-bottom: 0;
  color: var(--pen);
  font: 400 0.78em/var(--row) var(--font-hand);
  text-align: left;
  overflow-wrap: anywhere;
  vertical-align: top;
}

/* The answer writes itself into the gap. The negative insets keep the long script strokes. */
.blank.filled.write { animation: write 700ms cubic-bezier(.45, .05, .35, 1) both; }

@keyframes write {
  from { clip-path: inset(-20px 100% -20px -8px); }
  to { clip-path: inset(-20px -8px -20px -8px); }
}

.answers { margin-top: 16px; }

.answer {
  align-items: center;
  min-height: 56px;
  padding: 10px 0;
  border: 0;
  border-bottom: 1px solid var(--rule);
  background: none;
  font-size: 18px;
  font-weight: 600;
  text-align: left;
  cursor: pointer;
  transition: transform 120ms ease, background 160ms ease;
}

.answers .answer:first-child { border-top: 1px solid var(--rule); }
.answer:disabled { cursor: default; }

.letter {
  grid-column: 1;
  grid-row: 1 / span 2;
  justify-self: center;
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border: 2px solid transparent;
  border-radius: 50%;
  color: var(--muted);
  font-size: 16px;
  font-weight: 700;
}

.answer-text { grid-column: 2; }
.answer .mark { grid-column: 2; font-size: 14px; font-weight: 700; }

/* The green pen circles the correct letter. The red pen crosses out a wrong selection. */
.answer.is-correct .letter { border-color: var(--tick); border-radius: 48% 52% 50% 46%; color: var(--tick); }
.answer.is-correct .mark { color: var(--tick); }
.answer.is-wrong .letter,
.answer.is-wrong .mark { color: var(--margin); }
.answer.is-wrong .answer-text { text-decoration: line-through 2px var(--margin); }
.answer.is-dim { opacity: 0.55; }

.result { margin: 16px 0 0; font-weight: 600; outline: none; }
.result.good { color: var(--tick); }
.result.bad { color: var(--margin); }

/* ---------- Study ---------- */

.unit-picker { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }

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

.section-link {
  align-items: baseline;
  padding: 14px 0;
  border-bottom: 1px solid var(--rule);
  color: var(--ink);
  text-decoration: none;
  transition: background 160ms ease;
}

.section-list li:first-child .section-link { border-top: 1px solid var(--rule); }

.section-link .num {
  justify-self: center;
  color: var(--pen);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.reader-kind { margin: 0 0 12px; }

.reader-nav {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 28px;
  padding-top: 16px;
  border-top: 1px solid var(--rule);
}

.reader-nav .button { padding: 0 14px; text-align: center; }
.reader-nav .next { grid-column: 2; }
.reader-back { margin-top: 10px; width: 100%; }

/* Section text from the server (md_to_web_html). */
.prose { line-height: 1.6; }
.prose h3 { font-size: 26px; margin: 0 0 12px; }
.prose h4 { font-size: 18px; margin: 24px 0 8px; color: var(--pen); }
.prose p { margin: 0 0 12px; max-width: 36em; }
.prose ul, .prose ol { margin: 0 0 12px; padding-left: 1.3em; }
.prose li { margin: 4px 0; }
.prose li::marker { color: var(--pen); }
.prose li.sub { margin-left: 1.2em; list-style-type: circle; }
.prose i { color: var(--pen); }
.prose b { font-weight: 700; }
.prose s { color: var(--margin); }
.prose blockquote {
  margin: 0 0 12px;
  padding: 8px 12px;
  background: var(--marker);
  border-radius: var(--stroke);
}
.prose blockquote i { color: inherit; }

.prose table {
  display: block;
  overflow-x: auto;
  max-width: 100%;
  margin: 8px 0 16px;
  border-collapse: collapse;
  font-size: 15px;
  line-height: 1.4;
  -webkit-overflow-scrolling: touch;
}

.prose th, .prose td {
  padding: 8px 12px;
  border: 1px solid var(--rule);
  text-align: left;
  vertical-align: top;
  min-width: 7em;
}

.prose th { background: var(--wash); font-weight: 700; }
.prose tbody tr:nth-child(even) td { background: color-mix(in srgb, var(--wash) 35%, transparent); }

/* ---------- Schedule ---------- */

.field { margin-top: 28px; }
.field-label { display: block; font-weight: 700; margin-bottom: 10px; }

.switch-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-top: 24px;
  padding: 16px;
  background: var(--paper);
  border: 1.5px solid var(--rule);
  border-radius: 8px;
}

.switch {
  appearance: none;
  position: relative;
  flex: none;
  width: 52px;
  height: 32px;
  margin: 0;
  border-radius: 999px;
  background: var(--muted);
  cursor: pointer;
  transition: background 160ms ease;
}

.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--paper);
  transition: transform 160ms ease;
}

.switch:checked { background: var(--pen); }
.switch:checked::after { transform: translateX(20px); }

.days { display: flex; flex-wrap: wrap; gap: 8px; }
.days .chip { min-width: 56px; justify-content: center; }

.time-input {
  min-height: 52px;
  padding: 0 16px;
  border: 1.5px solid var(--rule);
  border-radius: 8px;
  background: var(--paper);
  font-size: 24px;
  font-variant-numeric: tabular-nums;
}

.hint { margin: 10px 0 0; color: var(--muted); font-size: 14px; }
.switch-row .hint { margin: 0; }

fieldset { border: 0; margin: 0; padding: 0; min-width: 0; }
fieldset:disabled { opacity: 0.45; }

/* ---------- Sheet and toast ---------- */

.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(9, 14, 30, 0.45);
  z-index: 20;
}

.sheet {
  position: fixed;
  inset: auto 0 0 0;
  max-height: calc(0.86 * var(--tg-viewport-stable-height, 100vh));
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 10px var(--gutter) calc(20px + var(--safe-bottom));
  background: var(--paper);
  border-radius: 16px 16px 0 0;
  border-top: 3px solid var(--pen);
  z-index: 21;
  animation: rise 260ms cubic-bezier(.2, .8, .2, 1);
}

.sheet > * { max-width: 560px; margin-left: auto; margin-right: auto; }
.sheet-handle { width: 40px; height: 4px; border-radius: 2px; background: var(--rule); margin-bottom: 14px; }
.sheet-title { font-size: 15px; color: var(--muted); font-weight: 600; margin-bottom: 8px; }
.sheet-close { display: flex; width: 100%; margin-top: 8px; }
/* The script moves the focus to these containers. They are not controls, so they show no ring. */
main:focus,
.result:focus,
.sheet:focus { outline: none; }
body.sheet-open { overflow: hidden; }

@keyframes rise {
  from { transform: translateY(40px); opacity: 0; }
  to { transform: none; opacity: 1; }
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabs-height) + var(--safe-bottom) + 16px);
  transform: translate(-50%, 20px);
  padding: 10px 18px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--paper);
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 200ms ease, transform 200ms ease;
  z-index: 30;
}

.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Desktop Telegram: hover states for a mouse. Touch screens do not get them. */
@media (hover: hover) {
  .button:not(:disabled):hover { filter: brightness(1.08); }
  .button-quiet:not(:disabled):hover { box-shadow: inset 0 0 0 1.5px var(--pen); filter: none; }
  .chip:hover { border-color: var(--pen); }
  .answer:not(:disabled):hover .letter { border-color: var(--pen); }
  .unit:hover,
  .answer:not(:disabled):hover,
  .section-link:hover { background: color-mix(in srgb, var(--wash) 70%, transparent); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
