:root {
  --bg: #eef1f5;
  --surface: #ffffff;
  --paper: #fcfdfb;
  --text: #1b2230;
  --muted: #546072;
  --ink: #1e3a8a;      /* ballpoint blue: actions */
  --rule: #d3def0;     /* exercise-book ruling */
  --margin: #c83737;   /* exercise-book margin; also errors */
  --line: 1.75rem;
  --radius: 10px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
  line-height: 1.5;
}
* { box-sizing: border-box; }
body { margin: 0; }
h1, h2 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.25rem; font-weight: 700; color: var(--ink); }
h2 { font-size: 1.125rem; font-weight: 650; }
:focus-visible { outline: 3px solid #f2b705; outline-offset: 2px; }
.skip { position: absolute; left: -999px; }
.skip:focus { left: 1rem; top: 1rem; background: var(--surface); padding: .5rem 1rem; z-index: 10; min-height: 44px; display: inline-flex; align-items: center; }

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; min-height: 44px; padding: .5rem 1rem; border-radius: 8px; border: 1px solid #b9c3d3; background: var(--surface); }
button:hover { border-color: var(--ink); }
button:disabled { opacity: .6; cursor: progress; }
button.primary { background: var(--ink); color: #fff; border-color: var(--ink); font-weight: 600; width: 100%; }
button.primary:hover { background: #172e70; }
button.link { border: 0; background: none; color: var(--ink); text-decoration: underline; padding: .25rem 0; min-height: 44px; text-align: left; }

label { display: block; font-weight: 600; font-size: .95rem; margin-bottom: 1rem; }
input, select, textarea {
  display: block; width: 100%; margin-top: .35rem; padding: .6rem .7rem;
  border: 1px solid #b9c3d3; border-radius: 8px; background: #fff; font-weight: 400; min-height: 44px;
}
textarea { resize: vertical; line-height: 1.5; }
[aria-invalid="true"] { border-color: var(--margin); box-shadow: 0 0 0 1px var(--margin); }
.field-error { display: block; color: var(--margin); font-weight: 600; font-size: .875rem; margin-top: .25rem; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 0 .75rem; }
.hint { color: var(--muted); font-size: .9rem; margin: 0 0 1rem; }
.note { font-size: .875rem; background: #fff6e0; border-left: 4px solid #e0a100; padding: .5rem .75rem; margin: 0 0 1rem; }
.alert { color: #8f1f1f; background: #fdecec; border-left: 4px solid var(--margin); padding: .75rem 1rem; margin: 0 0 1rem; }
.muted { color: var(--muted); }

/* Header + tool switcher */
.top { display: flex; justify-content: space-between; align-items: center; padding: .5rem 1.25rem; background: var(--surface); border-bottom: 1px solid #dde3ec; }
.tools { display: flex; gap: .5rem; overflow-x: auto; padding: .75rem 1.25rem; scrollbar-width: thin; }
.tools button { white-space: nowrap; border-radius: 999px; background: transparent; }
.tools button[aria-pressed="true"] { background: var(--ink); color: #fff; border-color: var(--ink); }

/* Layout */
.layout { display: grid; gap: 1rem; padding: 0 1.25rem 2rem; max-width: 1400px; margin: 0 auto; }
@media (min-width: 900px) { .layout { grid-template-columns: 380px minmax(0, 1fr); align-items: start; } }
.panel { background: var(--surface); border-radius: var(--radius); padding: 1.25rem; border: 1px solid #dde3ec; }
.panel > h2 { margin-bottom: .5rem; }

.extras { border: 1px solid #dde3ec; border-radius: 8px; padding: 0 .75rem; margin-bottom: 1rem; }
.extras[open] { padding-bottom: .25rem; }
.extras summary { cursor: pointer; font-weight: 600; min-height: 44px; display: flex; align-items: center; gap: .35rem; }
.extras fieldset { border: 0; padding: 0; margin: 0 0 .5rem; }
.extras legend { font-weight: 600; font-size: .95rem; margin-bottom: .25rem; padding: 0; }
label.check { display: flex; align-items: center; gap: .6rem; font-weight: 400; margin-bottom: .25rem; min-height: 44px; }
/* Box is 44px tall (touch target); the visible square stays 20px, drawn as a background so it doesn't stretch. */
label.check input {
  appearance: none; width: 1.25rem; height: 44px; margin: 0; padding: 0; border: 0; border-radius: 0; flex: none; cursor: pointer;
  background: no-repeat center / 1.25rem url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'%3E%3Crect x='1' y='1' width='18' height='18' rx='3' fill='white' stroke='%23546072' stroke-width='2'/%3E%3C/svg%3E");
}
label.check input:checked { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'%3E%3Crect x='1' y='1' width='18' height='18' rx='3' fill='%231e3a8a' stroke='%231e3a8a' stroke-width='2'/%3E%3Cpath d='M5 10.5l3.2 3.2L15 7' fill='none' stroke='white' stroke-width='2.4'/%3E%3C/svg%3E"); }
label.check input:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }
@media (forced-colors: active) { label.check input { appearance: auto; height: 1.25rem; } }
.files { list-style: none; padding: 0; margin: 0 0 1rem; }
.files li { display: flex; justify-content: space-between; align-items: center; gap: .75rem; border-bottom: 1px solid #eef1f5; }
.files span { overflow-wrap: anywhere; font-size: .9rem; }
.files button { flex: none; }

.history { margin-top: 1.5rem; border-top: 1px solid #dde3ec; padding-top: 1rem; }
.history summary { cursor: pointer; font-weight: 600; min-height: 44px; display: flex; align-items: center; }
.history ul { list-style: none; padding: 0; margin: .5rem 0; }
.history li { display: flex; flex-direction: column; border-bottom: 1px solid #eef1f5; }
.history time { font-size: .8rem; color: var(--muted); padding-bottom: .4rem; }

/* Result: a page of a school exercise book */
.result { min-height: 60vh; outline: none; }
.result-head { display: flex; flex-wrap: wrap; gap: .75rem; justify-content: space-between; align-items: center; margin-bottom: .75rem; }
.actions { display: flex; flex-wrap: wrap; gap: .5rem; }
.actions[hidden], [hidden] { display: none !important; }
#status:empty { display: none; }
#status { color: var(--ink); font-weight: 600; }
.empty { color: var(--muted); padding: 2rem 0; max-width: 46ch; }
.paper {
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.0625rem;
  line-height: var(--line);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  background-color: var(--paper);
  background-image:
    linear-gradient(to right, transparent 3.25rem, var(--margin) 3.25rem, var(--margin) calc(3.25rem + 2px), transparent calc(3.25rem + 2px)),
    repeating-linear-gradient(to bottom, transparent 0, transparent calc(var(--line) - 1px), var(--rule) calc(var(--line) - 1px), var(--rule) var(--line));
  background-position: 0 0, 0 .4rem;
  padding: .4rem 1.25rem var(--line) 4.25rem;
  border: 1px solid #dde3ec;
  border-radius: 4px;
  max-height: 75vh;
  overflow-y: auto;
}
#game { width: 100%; height: 70vh; min-height: 420px; border: 2px solid var(--ink); border-radius: 8px; background: #fff; }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.login-box { width: min(400px, 100%); background: var(--surface); padding: 2rem; border-radius: var(--radius); border: 1px solid #dde3ec; }
.login-box h1 { font-size: 1.5rem; margin-bottom: .5rem; }

@media (max-width: 599px) {
  .top, .tools { padding-left: 1rem; padding-right: 1rem; }
  .layout { padding: 0 .75rem 1.5rem; }
  .panel { padding: 1rem; }
  .paper { padding-left: 2.75rem; background-image:
    linear-gradient(to right, transparent 2rem, var(--margin) 2rem, var(--margin) calc(2rem + 2px), transparent calc(2rem + 2px)),
    repeating-linear-gradient(to bottom, transparent 0, transparent calc(var(--line) - 1px), var(--rule) calc(var(--line) - 1px), var(--rule) var(--line)); }
  .actions button { flex: 1 1 auto; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; scroll-behavior: auto !important; } }
