@font-face {
  font-family: "Marck Script";
  src:
    local("Marck Script"),
    local("MarckScript-Regular"),
    url("./assets/fonts/MarckScript-Regular.ttf") format("truetype");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: "Lobster";
  src:
    local("Lobster"),
    local("Lobster-Regular"),
    url("./assets/fonts/Lobster-Regular.ttf") format("truetype");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: "Caveat";
  src:
    local("Caveat"),
    local("Caveat Regular"),
    url("./assets/fonts/Caveat-Regular.ttf") format("truetype");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

:root {
  --viewport-height: 100vh;
  --paper-bg: #f6f1e8;
  --paper: #fffdf9;
  --panel: rgba(255, 253, 249, 0.94);
  --ink: #1f1714;
  --muted: #7a6f68;
  --line: rgba(31, 23, 20, 0.1);
  --accent: #9e432f;
  --accent-strong: #7f3221;
  --accent-soft: rgba(158, 67, 47, 0.08);
  --blue-line: rgba(72, 129, 202, 0.3);
  --blue-line-strong: rgba(72, 129, 202, 0.72);
  --shadow: 0 18px 48px rgba(43, 30, 21, 0.08);
  --lesson-bg: #f3f4f6;
  --lesson-surface: #f9fafb;
  --lesson-border: #d6dae1;
  --russian-handwriting-font: "Propisi Runtime", "Marck Script", "Caveat", "Brush Script MT", "Segoe Script", cursive;
  --latin-handwriting-font: "Marck Script", "Caveat", "Brush Script MT", "Segoe Script", cursive;
  --handwriting-font: var(--russian-handwriting-font);
  --handwriting-guide-ink: rgba(107, 114, 128, 0.64);
  --handwriting-guide-stroke: 1.35px;
}

@supports (height: 100dvh) {
  :root {
    --viewport-height: 100dvh;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  font-family: "Georgia", "Times New Roman", serif;
  color: var(--ink);
  font-synthesis: none;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.45), rgba(255, 255, 255, 0)),
    var(--paper-bg);
}

button,
input,
select {
  font: inherit;
}

.app-shell {
  width: min(1040px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 24px 0 36px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 16px;
  margin-bottom: 16px;
}

.back-link {
  color: var(--muted);
  text-decoration: none;
  border-bottom: 1px solid transparent;
}

.back-link:hover {
  border-color: currentColor;
}

.topbar h1,
.section-heading h2,
.lesson-context h2,
.dialog-card h2 {
  margin: 0;
  font-weight: 600;
  letter-spacing: 0.01em;
}

.topbar h1 {
  font-size: clamp(1.8rem, 3.4vw, 3rem);
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 22px;
  box-shadow: var(--shadow);
  padding: 20px;
}

.section-heading,
.stage-header {
  display: flex;
  justify-content: space-between;
  align-items: start;
  gap: 16px;
}

.section-heading.tight {
  margin-bottom: 12px;
}

.stage-actions,
.lesson-actions,
.dialog-actions,
.rows-control {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

.button,
.mini-button,
.icon-button,
.lesson-chip {
  border: 1px solid transparent;
  cursor: pointer;
  transition:
    transform 120ms ease,
    border-color 120ms ease,
    background 120ms ease,
    color 120ms ease;
}

.button:hover,
.mini-button:hover,
.icon-button:hover,
.lesson-chip:hover {
  transform: translateY(-1px);
}

.button:disabled,
.mini-button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

.button {
  padding: 10px 15px;
  border-radius: 999px;
}

.button.primary {
  background: var(--accent);
  color: white;
}

.button.primary:hover {
  background: var(--accent-strong);
}

.button.secondary {
  background: var(--accent-soft);
  color: var(--accent);
}

.button.ghost,
.mini-button {
  background: transparent;
  border-color: rgba(31, 23, 20, 0.16);
  color: var(--ink);
}

.mini-button {
  width: 34px;
  height: 34px;
  border-radius: 50%;
}

.profiles-grid,
.stages-grid {
  display: grid;
  gap: 16px;
}

.profiles-grid {
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
}

.stages-grid {
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
}

.profile-card,
.stage-card {
  background: rgba(255, 255, 255, 0.7);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 18px;
}

.profile-card-head,
.stage-card-top {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: start;
}

.avatar {
  width: 58px;
  height: 58px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: rgba(158, 67, 47, 0.1);
  color: var(--accent);
  font-size: 2rem;
}

.icon-button {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
}

.profile-name,
.stage-name {
  margin-bottom: 8px;
}

.profile-progress,
.stage-progress,
.stage-sample,
.lesson-chip-meta {
  color: var(--muted);
}

.stage-sample,
.lesson-chip-text {
  font-family: var(--russian-handwriting-font);
  font-weight: 400;
  letter-spacing: -0.01em;
  font-kerning: normal;
  font-variant-ligatures: common-ligatures contextual discretionary-ligatures;
  font-feature-settings: "liga" 1, "clig" 1, "calt" 1, "kern" 1;
  text-rendering: optimizeLegibility;
}

.stage-sample {
  font-size: 1.4rem;
  line-height: 1.1;
}

.progress-bar {
  width: 100%;
  height: 10px;
  border-radius: 999px;
  background: rgba(103, 91, 84, 0.12);
  overflow: hidden;
  margin: 14px 0 16px;
}

.progress-bar span {
  display: block;
  height: 100%;
  background: linear-gradient(90deg, var(--accent), #cf8a72);
}

.lesson-list {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 14px;
}

.stage-preview-list .lesson-chip {
  min-width: 72px;
}

.stage-preview-list .lesson-chip-text {
  margin-bottom: 0;
  max-width: none;
}

.stage-preview-list .lesson-chip-meta {
  display: none;
}

.stage-lesson-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(112px, 1fr));
  margin-top: 0;
}

.stage-lesson-list .lesson-chip {
  min-width: 0;
}

.lesson-chip {
  text-align: left;
  padding: 10px 12px;
  border-radius: 18px;
  background: rgba(158, 67, 47, 0.08);
  min-width: 96px;
}

.lesson-chip.completed {
  background: rgba(31, 23, 20, 0.08);
  color: var(--ink);
}

.lesson-chip-text {
  display: block;
  font-size: 1.3rem;
  line-height: 1.2;
  margin-bottom: 4px;
  max-width: 14rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

body.lesson-mode {
  background: white;
  overflow: hidden;
}

body.dialog-open {
  overflow: hidden;
}

body.lesson-mode .app-shell {
  width: 100vw;
  margin: 0;
  padding: 0;
}

body.lesson-mode .topbar {
  display: none;
}

.lesson-practice-view {
  height: var(--viewport-height);
  min-height: var(--viewport-height);
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  background: var(--lesson-bg);
}

.lesson-control-strip {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.lesson-context {
  min-width: 0;
}

.lesson-context h2 {
  margin-top: 2px;
  font-size: clamp(1.9rem, 3.6vw, 2.7rem);
  line-height: 0.94;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.lesson-actions {
  justify-content: flex-end;
}

.rows-control {
  border-radius: 10px;
  background: rgba(209, 213, 219, 0.42);
  padding: 5px 8px;
  gap: 8px;
  flex-wrap: nowrap;
}

.rows-control span {
  min-width: 4.2rem;
  text-align: center;
  font-size: 0.92rem;
  color: #374151;
}

.close-lesson-button {
  width: 42px;
  height: 42px;
  border-radius: 10px;
  padding: 0;
  font-size: 1.12rem;
  line-height: 1;
}

.practice-area-shell {
  flex: 1;
  min-height: 0;
  display: flex;
}

.trace-sheet {
  position: relative;
  flex: 1;
  min-height: 0;
  border-radius: 14px;
  overflow: hidden;
  border: 4px solid var(--lesson-border);
  background: var(--lesson-surface);
  --trace-pad-top: 24px;
  --trace-pad-right: 30px;
  --trace-pad-bottom: 18px;
  --trace-pad-left: 30px;
}

.trace-sheet-surface {
  position: absolute;
  inset: var(--trace-pad-top) var(--trace-pad-right) var(--trace-pad-bottom) var(--trace-pad-left);
}

.reference-text {
  --row-count: 3;
  --pattern-height: calc((var(--viewport-height) - 12rem) / var(--row-count));
  --x-height: calc(var(--pattern-height) / 6);
  --font-size: calc(var(--pattern-height) * 0.78);
  --guide-gap: clamp(1rem, calc(var(--font-size) * 0.28), 2.75rem);
  --baseline-offset-ratio: 0.33;
  --manual-offset: calc(var(--pattern-height) * 0.1);
  --bg-offset: calc(var(--font-size) * var(--baseline-offset-ratio) + var(--manual-offset));
  --glyph-offset: 0px;
  position: absolute;
  inset: 0;
  font-family: var(--russian-handwriting-font);
  font-size: var(--font-size);
  font-weight: 400;
  letter-spacing: -0.01em;
  font-kerning: normal;
  font-variant-ligatures: common-ligatures contextual discretionary-ligatures;
  font-feature-settings: "liga" 1, "clig" 1, "calt" 1, "kern" 1;
  line-height: var(--pattern-height);
  text-align: left;
  text-rendering: optimizeLegibility;
  pointer-events: none;
  user-select: none;
}

.reference-text.show-guides {
  background-image:
    repeating-linear-gradient(
      to bottom,
      rgba(147, 197, 253, 0.4) 0,
      rgba(147, 197, 253, 0.4) calc(var(--x-height) * 0.071),
      transparent calc(var(--x-height) * 0.071),
      transparent var(--x-height),
      rgba(147, 197, 253, 0.4) var(--x-height),
      rgba(147, 197, 253, 0.4) calc(var(--x-height) * 1.071),
      transparent calc(var(--x-height) * 1.071),
      transparent calc(var(--x-height) * 2),
      rgba(147, 197, 253, 0.4) calc(var(--x-height) * 2),
      rgba(147, 197, 253, 0.4) calc(var(--x-height) * 2.071),
      transparent calc(var(--x-height) * 2.071),
      transparent calc(var(--x-height) * 3),
      rgba(147, 197, 253, 0.7) calc(var(--x-height) * 3),
      rgba(147, 197, 253, 0.7) calc(var(--x-height) * 3.143),
      transparent calc(var(--x-height) * 3.143),
      transparent calc(var(--x-height) * 4),
      rgba(147, 197, 253, 0.4) calc(var(--x-height) * 4),
      rgba(147, 197, 253, 0.4) calc(var(--x-height) * 4.071),
      transparent calc(var(--x-height) * 4.071),
      transparent calc(var(--x-height) * 5),
      rgba(147, 197, 253, 0.4) calc(var(--x-height) * 5),
      rgba(147, 197, 253, 0.4) calc(var(--x-height) * 5.071),
      transparent calc(var(--x-height) * 5.071),
      transparent var(--pattern-height)
    );
  background-position: 0 var(--bg-offset);
}

.practice-line {
  display: block;
  height: var(--pattern-height);
  white-space: nowrap;
}

.reference-text[data-stage-id="common-words"] .practice-line {
  display: flex;
  align-items: flex-start;
  gap: min(52vw, 32rem);
}

.letter-guide {
  display: inline-block;
  color: var(--handwriting-guide-ink);
  transform: translateY(var(--glyph-offset, 0px));
  -webkit-text-stroke: 0;
  text-stroke: 0;
}

.letter-guide:not(:last-child) {
  margin-inline-end: var(--guide-gap);
}

.reference-text[data-stage-id="common-words"] .letter-guide:not(:last-child) {
  margin-inline-end: 0;
}

@supports (-webkit-text-stroke: 1px black) or (text-stroke: 1px black) {
  .letter-guide {
    color: transparent;
    -webkit-text-fill-color: transparent;
    -webkit-text-stroke: var(--handwriting-guide-stroke) var(--handwriting-guide-ink);
    text-stroke: var(--handwriting-guide-stroke) var(--handwriting-guide-ink);
  }
}

#practice-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: crosshair;
}

.profile-dialog {
  border: 0;
  padding: 0;
  background: transparent;
}

.profile-dialog::backdrop {
  background: rgba(32, 22, 18, 0.4);
}

.dialog-card {
  width: min(460px, calc(100vw - 32px));
  background: var(--paper);
  padding: 24px;
  border-radius: 22px;
  box-shadow: var(--shadow);
}

.field {
  display: grid;
  gap: 8px;
  margin-bottom: 16px;
  color: var(--muted);
}

.field-note {
  margin: -4px 0 16px;
  color: var(--muted);
  font-size: 0.92rem;
}

.field input,
.field select {
  border: 1px solid rgba(31, 23, 20, 0.16);
  border-radius: 16px;
  padding: 12px 14px;
  background: white;
  color: var(--ink);
}

.empty-state {
  padding: 24px;
  border: 1px dashed rgba(31, 23, 20, 0.2);
  border-radius: 18px;
  color: var(--muted);
}

.section-note {
  margin: 0;
  color: var(--muted);
  font-size: 0.92rem;
}

@media (max-width: 1080px) {
  body.lesson-mode .lesson-control-strip {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }

  body.lesson-mode .lesson-actions {
    width: 100%;
    justify-content: flex-start;
    flex-wrap: nowrap;
    overflow-x: auto;
    padding-bottom: 2px;
  }

  body.lesson-mode .lesson-actions > * {
    flex: 0 0 auto;
  }
}

@media (max-width: 720px) {
  .app-shell {
    width: min(100vw - 20px, 1200px);
    padding-top: 16px;
  }

  .topbar,
  .section-heading,
  .stage-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .panel {
    padding: 18px;
    border-radius: 18px;
  }

  body.lesson-mode .lesson-practice-view {
    padding: 10px;
    gap: 10px;
  }

  body.lesson-mode .lesson-control-strip {
    gap: 6px;
  }

  body.lesson-mode .trace-sheet {
    --trace-pad-top: 18px;
    --trace-pad-right: 12px;
    --trace-pad-bottom: 14px;
    --trace-pad-left: 12px;
  }

  body.lesson-mode .reference-text {
    --pattern-height: calc((var(--viewport-height) - 16rem) / var(--row-count));
    --manual-offset: calc(var(--pattern-height) * -0.085);
  }

  body.lesson-mode .letter-guide {
    color: var(--handwriting-guide-ink);
    -webkit-text-fill-color: currentColor;
    -webkit-text-stroke: 0;
    text-stroke: 0;
  }
}
