/* ============================================================
   Practice-sheet design system
   The world of this app is the kana drill sheet: washi paper,
   pale print-blue guides, sumi ink, and the teacher's vermilion
   pen. Red appears ONLY where a teacher's pen would touch the
   page: marks, corrections, the one primary action, the hanko.
   Dark mode is the same sheet at night, ink-side out.
   ============================================================ */
:root {
  --paper: #f6f2e9;
  --paper-raise: #fcf9f2;
  --paper-sunk: #efe9dc;
  --ink: #262a33;
  --ink-dim: #6e6a61;
  --shu: #c93a2b;            /* teacher's vermilion */
  --shu-soft: #c93a2b14;
  --ai: #39516b;             /* aizome indigo, quiet secondary */
  --guide: #a9bdd4;          /* pale print-blue practice guides */
  --guide-soft: #a9bdd455;
  --line: #262a3318;
  --radius: 10px;

  /* JP text in a clean gothic — mincho/textbook faces got spindly and hard
     to read, especially small kanji on Windows */
  --jp-font: "Hiragino Kaku Gothic ProN", "Hiragino Sans", "Yu Gothic Medium",
             "Yu Gothic", "Meiryo", "Noto Sans JP", sans-serif;
  --display: "Iowan Old Style", "Constantia", "Palatino Linotype", Georgia, serif;
  --ui: "Avenir Next", "Candara", "Seravek", "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #171a21;
    --paper-raise: #1e222b;
    --paper-sunk: #12141a;
    --ink: #ece7da;
    --ink-dim: #8d8a80;
    --shu: #e05a44;
    --shu-soft: #e05a4420;
    --ai: #8fa8c8;
    --guide: #39424f;
    --guide-soft: #39424fcc;
    --line: #ece7da1c;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--paper);
  color: var(--ink);
  font: 16px/1.55 var(--display);
  -webkit-font-smoothing: antialiased;
}

/* washi grain, light mode only — at night the paper is just ink */
@media (prefers-color-scheme: light), (prefers-color-scheme: no-preference) {
  body::before {
    content: "";
    position: fixed;
    inset: 0;
    pointer-events: none;
    opacity: 0.035;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='240' height='240'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.8' numOctaves='2'/%3E%3C/filter%3E%3Crect width='240' height='240' filter='url(%23n)'/%3E%3C/svg%3E");
  }
}

.app {
  max-width: 560px;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) 18px max(18px, env(safe-area-inset-bottom));
  position: relative;
}

/* ---- shared controls ---- */
button {
  font: 600 15px/1.3 var(--ui);
  color: var(--ink);
  border: 1.5px solid var(--ink);
  background: transparent;
  border-radius: var(--radius);
  padding: 10px 16px;
  cursor: pointer;
  transition: transform 0.06s ease, background-color 0.15s ease;
}
button:active { transform: translateY(1px); }
button:focus-visible, input:focus-visible, textarea:focus-visible, summary:focus-visible {
  outline: 2px solid var(--ai);
  outline-offset: 2px;
}

button.primary {
  background: var(--shu);
  border-color: var(--shu);
  color: #fdf9f0;
}
button.big { width: 100%; padding: 15px; font-size: 17px; letter-spacing: 0.01em; }
button.ghost { border-color: var(--line); color: var(--ink-dim); width: 100%; margin-top: 10px; }
button:disabled { opacity: 0.45; cursor: default; }
.muted { color: var(--ink-dim); }

input[type="text"], input[type="password"], input[type="number"] {
  font: 18px var(--ui);
  color: var(--ink);
  background: var(--paper-raise);
  border: 1.5px solid var(--guide);
  border-radius: var(--radius);
  padding: 10px 14px;
  width: 100%;
}
input:focus { outline: 2px solid var(--ai); outline-offset: -1px; }

/* eyebrow: the tiny bilingual label that organizes every section */
.kind-tag, .stat span, .set-pct, .review-count, .key-hint {
  font-family: var(--ui);
}

/* ---- login ---- */
.login {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding-top: 20vh;
  text-align: center;
}
.login-title {
  font-family: var(--jp-font);
  font-size: 60px;
  font-weight: 500;
  margin: 0;
  letter-spacing: 0.08em;
}
.login-sub {
  color: var(--ink-dim);
  margin: 0 0 26px;
  font-style: italic;
}
.login-sub::after {
  content: "";
  display: block;
  width: 42px;
  height: 2px;
  background: var(--shu);
  margin: 14px auto 0;
}
.login form { display: flex; flex-direction: column; gap: 12px; width: min(280px, 80vw); }
.login-error { color: var(--shu); margin: 0; font-family: var(--ui); font-size: 14px; }

/* ---- home ---- */
.home { display: flex; flex-direction: column; gap: 16px; padding-top: 6px; }
.home > * { animation: rise 0.4s ease both; }
.home > *:nth-child(2) { animation-delay: 0.05s; }
.home > *:nth-child(3) { animation-delay: 0.1s; }
.home > *:nth-child(4) { animation-delay: 0.15s; }
.home > *:nth-child(5) { animation-delay: 0.2s; }
.home > *:nth-child(n+6) { animation-delay: 0.25s; }
@keyframes rise {
  from { opacity: 0; transform: translateY(7px); }
  to { opacity: 1; transform: none; }
}

.home-head { display: flex; align-items: center; justify-content: space-between; }
.page-title {
  font-family: var(--jp-font);
  font-size: 32px;
  font-weight: 500;
  margin: 0;
  letter-spacing: 0.06em;
}

/* streak as a hanko stamp: red seal square, count inside */
.hanko {
  font-family: var(--jp-font);
  color: var(--shu);
  border: 2px solid var(--shu);
  border-radius: 6px;
  min-width: 44px;
  padding: 3px 7px 4px;
  text-align: center;
  line-height: 1.1;
  transform: rotate(2.5deg);
  display: inline-flex;
  flex-direction: column;
  box-shadow: 0 0 0 1px var(--shu-soft);
}
.hanko b { font-size: 19px; font-weight: 600; }
.hanko i { font-style: normal; font-size: 10px; letter-spacing: 0.2em; }

.offline-note {
  margin: 0;
  padding: 10px 14px;
  border-radius: var(--radius);
  border: 1px dashed var(--guide);
  color: var(--ink-dim);
  font-size: 14px;
  font-family: var(--ui);
}

.stat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.summary .stat-grid { grid-template-columns: repeat(3, 1fr); }
.stat {
  background: var(--paper-raise);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 4px 10px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.stat b {
  font-size: 24px;
  font-weight: 600;
  font-variant-numeric: lining-nums tabular-nums;
}
.stat span {
  color: var(--ink-dim);
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
}
.stat span i {
  font-style: normal;
  font-family: var(--jp-font);
  display: block;
  font-size: 10px;
  letter-spacing: 0.25em;
  margin-bottom: 1px;
}

/* ---- coach: red-pen margin notes ---- */
.coach { display: flex; flex-direction: column; gap: 8px; }
.coach-card {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--paper-raise);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 11px 14px;
  position: relative;
}
.coach-card::before {
  content: "";
  position: absolute;
  left: 0;
  top: 10px;
  bottom: 10px;
  width: 3px;
  border-radius: 2px;
  background: var(--ai);
}
.coach-leech::before, .coach-confusion::before, .coach-lapse::before { background: var(--shu); }
.coach-mastery::before, .coach-streak::before { background: var(--guide); }
.coach-icon { font-size: 16px; flex: 0 0 22px; text-align: center; color: var(--ink-dim); }
.coach-text { flex: 1; display: flex; flex-direction: column; gap: 2px; min-width: 0; text-align: left; }
.coach-text b { font-size: 15px; font-weight: 600; line-height: 1.3; }
.coach-text span { color: var(--ink-dim); font-size: 13px; font-family: var(--ui); }
.coach-cta {
  flex-shrink: 0;
  font-size: 12.5px;
  padding: 7px 11px;
  border-color: var(--shu);
  color: var(--shu);
}

/* ---- set list ---- */
.card-panel {
  background: var(--paper-raise);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 4px 14px;
}
.set-row { display: flex; align-items: center; gap: 10px; padding: 11px 0; font-family: var(--ui); }
.set-row + .set-row { border-top: 1px solid var(--line); }
.set-row input[type="checkbox"] { accent-color: var(--shu); width: 17px; height: 17px; }
.set-name { flex: 0 0 auto; min-width: 6.2em; font-weight: 600; font-size: 14.5px; white-space: nowrap; }
.set-name i {
  font-style: normal;
  font-family: var(--jp-font);
  font-weight: 400;
  color: var(--ink-dim);
  font-size: 12px;
  margin-left: 5px;
}
.set-bar { flex: 1; height: 5px; background: var(--paper-sunk); border-radius: 3px; overflow: hidden; }
.set-bar-fill { display: block; height: 100%; background: var(--ai); border-radius: 3px; }
.set-pct { color: var(--ink-dim); font-size: 12.5px; min-width: 3em; text-align: right; font-variant-numeric: tabular-nums; }

.settings { color: var(--ink-dim); font-family: var(--ui); font-size: 14.5px; }
.settings summary { cursor: pointer; padding: 6px 0; }
.settings label { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 10px 0; }
.settings input { width: 90px; text-align: center; }
/* Checkbox rows read left-to-right, unlike the numeric rows above which push
   their input to the right edge. */
.settings-check { justify-content: flex-start !important; gap: 10px !important; cursor: pointer; }
.settings-check input { width: auto; accent-color: var(--shu); flex: none; }
.settings-row { display: flex; gap: 10px; margin-top: 12px; }
.settings-row button { flex: 1; }

/* ---- review ---- */
.review-top { display: flex; align-items: center; gap: 12px; padding: 4px 0 16px; }
.icon-btn {
  border: none;
  background: none;
  font-size: 21px;
  color: var(--ink-dim);
  padding: 4px 8px;
}
.icon-btn:disabled { opacity: 0.3; }
.review-progress { flex: 1; height: 4px; background: var(--paper-sunk); border-radius: 2px; overflow: hidden; }
.review-progress-fill { display: block; height: 100%; background: var(--shu); transition: width 0.25s; }
.review-count { color: var(--ink-dim); font-size: 12.5px; font-variant-numeric: tabular-nums; }

.card-face {
  background: var(--paper-raise);
  border: 1px solid var(--line);
  border-radius: 14px;
  min-height: 400px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 26px 20px;
  text-align: center;
}

.kind-tag {
  color: var(--ink-dim);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.16em;
}

/* THE practice square: dashed crosshair guides, pale print blue,
   exactly like a kana drill sheet cell */
.practice-cell {
  position: relative;
  width: min(230px, 58vw);
  aspect-ratio: 1;
  border: 1.5px solid var(--guide);
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    repeating-linear-gradient(to right, transparent 0 6px, var(--guide-soft) 6px 11px)
      center / 100% 1px no-repeat,
    repeating-linear-gradient(to bottom, transparent 0 6px, var(--guide-soft) 6px 11px)
      center / 1px 100% no-repeat;
}
.card-glyph {
  font-family: var(--jp-font);
  font-size: clamp(88px, 26vw, 150px);
  font-weight: 500;
  line-height: 1;
  animation: settle 0.28s ease both;
}
@keyframes settle {
  from { opacity: 0; filter: blur(3px); transform: scale(1.04); }
  to { opacity: 1; filter: none; transform: none; }
}

.card-prompt { font-size: 52px; font-weight: 600; font-family: var(--display); }
.card-prompt-small { font-size: 26px; }
.card-word {
  font-family: var(--jp-font);
  font-size: clamp(44px, 13vw, 72px);
  font-weight: 600;
  line-height: 1.2;
  animation: settle 0.28s ease both;
}
.card-sub { color: var(--ink-dim); font-size: 19px; font-family: var(--ui); }
.card-sub.jp { font-family: var(--jp-font); }

.answer-form { width: min(280px, 80%); }
.answer-input { text-align: center; }
.answer-input.answer-kana { font-family: var(--jp-font); font-size: 24px; }

/* the teacher's marks. Both are red — that's how real pages come back. */
.answer-echo {
  position: relative;
  font-size: 20px;
  font-family: var(--ui);
  padding: 6px 20px;
}
.answer-wrong { color: var(--shu); }
.answer-wrong::after {
  content: "";
  position: absolute;
  left: 8%;
  right: 8%;
  top: 55%;
  height: 2px;
  background: var(--shu);
  transform: rotate(-4deg);
}
.answer-right { color: var(--ink); }
.maru {
  position: absolute;
  inset: -12px -6px;
  width: calc(100% + 12px);
  height: calc(100% + 24px);
  pointer-events: none;
}
.maru ellipse {
  fill: none;
  stroke: var(--shu);
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-dasharray: 300;
  stroke-dashoffset: 300;
  animation: draw-maru 0.45s ease-out 0.05s forwards;
}
@keyframes draw-maru { to { stroke-dashoffset: 0; } }

.mnemonic {
  margin: 0;
  color: var(--ai);
  font-size: 15px;
  font-style: italic;
  max-width: 90%;
}

.reveal-btn { width: min(280px, 80%); }

.grade-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; width: 100%; margin-top: 4px; }
.grade-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  padding: 9px 4px 10px;
  font-size: 14px;
  border-color: var(--line);
  color: var(--ink);
  background: var(--paper-raise);
}
.grade-jp {
  font-family: var(--jp-font);
  font-weight: 400;
  font-size: 10.5px;
  color: var(--ink-dim);
  letter-spacing: 0.1em;
}
.key-hint { font-size: 10px; color: var(--ink-dim); }
@media (pointer: coarse) { .key-hint { display: none; } }
.grade-again { color: var(--shu); }
.grade-default { border-color: var(--ink); border-width: 2px; }
.grade-btn.grade-default.grade-again { border-color: var(--shu); }

.summary { display: flex; flex-direction: column; gap: 20px; padding-top: 16vh; text-align: center; }
.summary .page-title { font-size: 44px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: max(24px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--paper);
  border-radius: 10px;
  padding: 10px 18px;
  max-width: 90vw;
  font-family: var(--ui);
  font-size: 14px;
  z-index: 10;
}

/* ---- tutor chat ---- */
.chat {
  display: flex;
  flex-direction: column;
  height: calc(100dvh - 34px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
}
.chat-top { display: flex; align-items: center; justify-content: space-between; padding: 4px 0 10px; }
.chat-title { font-family: var(--jp-font); font-size: 20px; font-weight: 600; }
.chat-title i { font-style: normal; font-family: var(--ui); font-size: 12px; color: var(--ink-dim); margin-left: 6px; letter-spacing: 0.1em; }
.chat-log {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 6px 2px 12px;
}
.chat-empty { color: var(--ink-dim); font-size: 14px; font-family: var(--ui); text-align: center; margin: 12px 18px; }
.msg {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 12px;
  font-family: var(--ui);
  font-size: 15px;
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  text-align: left;
}
.msg-me {
  align-self: flex-end;
  background: var(--ink);
  color: var(--paper);
  border-bottom-right-radius: 4px;
}
.msg-tutor {
  align-self: flex-start;
  background: var(--paper-raise);
  border: 1px solid var(--line);
  border-left: 3px solid var(--shu);
  border-bottom-left-radius: 4px;
}
.msg-error { border-left-color: var(--guide); color: var(--ink-dim); font-style: italic; }
.msg-busy { color: var(--ink-dim); animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.45; } }
.chat-input { display: flex; gap: 8px; padding-top: 8px; border-top: 1px solid var(--line); }
.chat-input input { flex: 1; }
.chat-input button { flex-shrink: 0; }

/* ---- modal ---- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: #1a1611aa;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
  padding: 20px;
}
.modal {
  background: var(--paper-raise);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 24px;
  width: min(400px, 100%);
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.modal-title { font-family: var(--jp-font); font-size: 46px; font-weight: 500; margin: 0; text-align: center; }
.modal textarea {
  font: 15px/1.5 var(--ui);
  color: var(--ink);
  background: var(--paper);
  border: 1.5px solid var(--guide);
  border-radius: var(--radius);
  padding: 10px 14px;
  resize: vertical;
}
.modal .muted { margin: 0; font-size: 14px; font-family: var(--ui); }

.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  border: 0; padding: 0; margin: -1px;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
  .maru ellipse { stroke-dashoffset: 0; }
}
