/* ------------------------------------------------------------------ tokens */
:root {
  --paper: #fbfbfc;
  --surface: #ffffff;
  --ink: #14171f;
  --ink-soft: #3a4050;
  --muted: #596173;
  --rule: #e1e4ea;
  --rule-strong: #c7ccd6;

  /* One accent: the navy of the AzbekDev logo. */
  --accent: #0d1f43;
  --accent-hover: #1a3270;
  --accent-tint: #eef1f8;

  /* Functional colour, used only to mark mistakes. */
  --mark: #b42318;
  --mark-bg: #fdeceb;

  --on-accent: #ffffff;
  --editor-focus: #fdfdfe;
  --toast-bg: #14171f;
  --toast-fg: #ffffff;
  color-scheme: light;

  --serif: "Literata", "Source Serif 4", Georgia, "Times New Roman", serif;
  --sans: "Source Sans 3", system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

  --gutter: 24px;
  --radius: 4px;
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; scroll-padding-top: 88px; }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 1rem;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

img { max-width: 100%; height: auto; }
a { color: var(--accent); text-underline-offset: 3px; }
a:hover { color: var(--accent-hover); }
button, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

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

.wrap { width: 100%; max-width: 1120px; margin-inline: auto; padding-inline: var(--gutter); }

.skip-link {
  position: absolute; left: 12px; top: -60px; z-index: 200;
  background: var(--surface); color: var(--accent);
  padding: 10px 14px; border: 1px solid var(--accent); border-radius: var(--radius);
}
.skip-link:focus { top: 12px; }


/* -------------------------------------------------------------- dark theme */
:root[data-theme="dark"] {
  --paper: #0e1117;
  --surface: #151a23;
  --ink: #e9ecf3;
  --ink-soft: #c4cad7;
  --muted: #98a1b4;
  --rule: #262d3a;
  --rule-strong: #3a4354;

  /* Same hue as the logo navy, lightened so it reads on a dark page. */
  --accent: #9db4f2;
  --accent-hover: #bccaf7;
  --accent-tint: #1c2640;
  --on-accent: #0d1f43;

  --mark: #ff8d83;
  --mark-bg: #3b1d20;

  --editor-focus: #181e29;
  --toast-bg: #eef1f8;
  --toast-fg: #14171f;
  color-scheme: dark;
}
:root[data-theme="dark"] select option { background: var(--surface); color: var(--ink); }
:root[data-theme="dark"] .editor::placeholder { color: #6f788c; }
:root[data-theme="dark"] .site-header,
:root[data-theme="dark"] .site-footer { background: #0e2044; }
:root[data-theme="dark"] .skip-link { color: var(--accent); }
:root[data-theme="dark"] .dialog::backdrop { background: rgba(0, 0, 0, 0.65); }
:root[data-theme="dark"] .toast { box-shadow: 0 2px 12px rgba(0, 0, 0, 0.5); }

/* ------------------------------------------------------------ theme toggle */
.theme-toggle {
  flex: none; display: inline-flex; align-items: center; justify-content: center;
  width: 44px; height: 44px; margin-left: 12px; padding: 0;
  background: transparent; color: #fff; border: 1px solid rgba(255, 255, 255, 0.28); border-radius: var(--radius);
  cursor: pointer;
}
.theme-toggle:hover { background: rgba(255, 255, 255, 0.12); }
.theme-toggle .icon-sun { display: none; }
:root[data-theme="dark"] .theme-toggle .icon-sun { display: block; }
:root[data-theme="dark"] .theme-toggle .icon-moon { display: none; }

/* ------------------------------------------------------------------ header */
.site-header, .site-footer { background: #0e2044; color: #fff; }
.site-header { position: sticky; top: 0; z-index: 50; }
.site-header :focus-visible, .site-footer :focus-visible { outline-color: #fff; }

.header-inner {
  display: flex; align-items: center;
  gap: 16px 0; min-height: 68px; padding-block: 6px;
}

.brand { display: block; flex: none; margin-right: auto; line-height: 0; border-radius: 2px; }
.brand img { display: block; height: 54px; width: auto; }

.main-nav { display: flex; align-items: center; gap: 28px; }
.main-nav a, .site-footer a {
  color: #fff; text-decoration: none; font-size: 0.9375rem; font-weight: 500;
}
.main-nav a:not(.tg-link):hover { text-decoration: underline; text-underline-offset: 6px; }

.tg-link { display: inline-flex; align-items: center; gap: 8px; }
.tg-link:hover span { text-decoration: underline; text-underline-offset: 4px; }
.tg-icon { flex: none; }

/* -------------------------------------------------------------------- hero */
.hero { padding-top: 64px; padding-bottom: 36px; }

h1, h2, h3 { font-family: var(--serif); font-weight: 500; color: var(--ink); margin: 0; }

.hero h1 {
  font-size: clamp(2rem, 1.2rem + 3.4vw, 3.5rem);
  line-height: 1.12;
  letter-spacing: -0.015em;
  max-width: 17ch;
}
.lead {
  margin: 20px 0 0; max-width: 54ch;
  font-size: 1.125rem; line-height: 1.6; color: var(--ink-soft);
}

/* ---------------------------------------------------------------- controls */
.workspace { padding-bottom: 64px; }

.controls {
  display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between;
  gap: 18px 28px; margin-bottom: 18px;
}

.direction { display: flex; align-items: flex-end; gap: 14px; }
.dir-arrow { color: var(--muted); margin-bottom: 16px; flex: none; }

.field { display: flex; flex-direction: column; gap: 4px; }
.field-label { font-size: 0.8125rem; color: var(--muted); }

select {
  -webkit-appearance: none; appearance: none;
  height: 44px; min-width: 150px; padding: 0 38px 0 14px;
  background: var(--surface) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1.5 6 6.5 11 1.5' fill='none' stroke='%23596173' stroke-width='1.5'/%3E%3C/svg%3E") no-repeat right 14px center;
  border: 1px solid var(--rule-strong); border-radius: var(--radius);
  cursor: pointer;
}
select:hover { border-color: var(--accent); }
select option:disabled { color: #9aa1b0; }


.btn {
  position: relative;
  display: inline-flex; align-items: center; justify-content: center; gap: 10px;
  min-height: 44px; padding: 0 20px;
  border: 1px solid transparent; border-radius: var(--radius);
  font-size: 0.9375rem; font-weight: 600; line-height: 1.2;
  cursor: pointer; white-space: nowrap;
}
.btn-primary { background: var(--accent); color: var(--on-accent); }
.btn-primary:hover:not([disabled]) { background: var(--accent-hover); }
.btn-secondary { background: var(--surface); color: var(--accent); border-color: var(--accent); }
.btn-secondary:hover:not([disabled]) { background: var(--accent-tint); }
.btn-quiet { background: transparent; color: var(--ink); border-color: var(--rule-strong); }
.btn-quiet:hover:not([disabled]) { border-color: var(--accent); color: var(--accent); }
.btn-small { min-height: 38px; padding: 0 14px; font-size: 0.875rem; }
.btn[disabled] { opacity: 0.55; cursor: not-allowed; }

.btn[aria-busy="true"] { cursor: progress; }
.btn[aria-busy="true"]::after {
  content: ""; width: 14px; height: 14px; flex: none;
  border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ------------------------------------------------------------------ spread */
.spread {
  display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: auto 1fr auto;
  background: var(--surface);
  border: 1px solid var(--rule-strong); border-radius: 6px; overflow: hidden;
}
.pane { display: flex; flex-direction: column; min-width: 0; }
@supports (grid-template-rows: subgrid) {
  /* Heads, editors and counters line up across both panes even when tools wrap. */
  .pane { display: grid; grid-row: span 3; grid-template-rows: subgrid; }
}
.pane + .pane { border-left: 1px solid var(--rule-strong); }

.pane-head {
  display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap;
  gap: 4px 12px; padding: 8px 12px 8px 20px;
  border-bottom: 1px solid var(--rule);
}
.pane-head h2 { font-family: var(--sans); font-size: 0.9375rem; font-weight: 600; color: var(--ink); }
.tools { display: flex; flex-wrap: wrap; gap: 2px; }

.tool {
  min-height: 34px; padding: 4px 9px;
  background: none; border: 0; border-radius: 3px;
  color: var(--accent); font-size: 0.875rem; font-weight: 500; cursor: pointer;
}
.tool:hover { background: var(--accent-tint); }

.editor {
  display: block; flex: 1; width: 100%;
  min-height: 380px; padding: 20px;
  border: 0; border-radius: 0; resize: vertical; background: transparent;
  font-family: var(--serif); font-size: 1.0625rem; line-height: 1.7;
  color: var(--ink);
}
.editor::placeholder { color: #8a92a3; }
.editor:focus { outline: none; background: var(--editor-focus); box-shadow: inset 0 -2px 0 var(--accent); }

.pane-foot {
  display: flex; align-items: center; gap: 18px; padding: 9px 12px 9px 20px; min-height: 46px;
  border-top: 1px solid var(--rule);
  font-size: 0.8125rem; color: var(--muted); font-variant-numeric: tabular-nums;
}

.foot-tool { margin-left: auto; }

/* ------------------------------------------------------------- info blocks */
.info { padding-top: 56px; padding-bottom: 56px; border-top: 1px solid var(--rule); }
.info > h2 { font-size: clamp(1.5rem, 1.1rem + 1.4vw, 2rem); letter-spacing: -0.01em; }

.steps {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 0 40px;
  margin: 28px 0 0; padding: 0; list-style: none; counter-reset: step;
}
.steps li { counter-increment: step; padding-top: 18px; border-top: 2px solid var(--accent); }
.steps li::before {
  content: counter(step);
  display: block; margin-bottom: 8px;
  font-family: var(--serif); font-size: 1.75rem; line-height: 1; color: var(--accent);
}
.steps h3 { font-size: 1.1875rem; margin-bottom: 6px; }
.steps p { margin: 0; color: var(--ink-soft); max-width: 40ch; }

.privacy {
  margin: 32px 0 0; padding-left: 16px; max-width: 70ch;
  border-left: 2px solid var(--rule-strong); color: var(--ink-soft);
}

.rules-intro { margin: 14px 0 0; max-width: 62ch; color: var(--ink-soft); font-size: 1.0625rem; }

.rules { margin: 24px 0 0; max-width: 760px; }
.rules > div { padding: 14px 0; border-bottom: 1px solid var(--rule); }
.rules > div:first-child { border-top: 1px solid var(--rule); }
.rules dt { font-weight: 600; }
.rules dd { margin: 4px 0 0; color: var(--ink-soft); max-width: 66ch; }

.alphabet { display: grid; grid-template-columns: 1fr 1fr; gap: 24px 40px; margin-top: 36px; }
.table-scroll { overflow-x: auto; }
.alphabet table { width: 100%; border-collapse: collapse; font-size: 0.9375rem; }
.alphabet th, .alphabet td { padding: 7px 10px; text-align: left; border-bottom: 1px solid var(--rule); white-space: nowrap; }
.alphabet th { font-weight: 600; color: var(--ink); border-bottom-color: var(--rule-strong); }
.alphabet td:first-child { font-family: var(--serif); }

/* ------------------------------------------------------------------ footer */
.site-footer { margin-top: 0; }
.footer-inner {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between;
  gap: 14px 32px; padding-block: 18px;
}
.brand-footer img { height: 46px; }
.footer-text { margin: 0; flex: 1 1 260px; color: #dbe1ef; font-size: 0.9375rem; }
.footer-meta { margin: 0; display: flex; align-items: center; gap: 22px; font-size: 0.9375rem; }
.copyright { color: #b9c2d8; }

/* ----------------------------------------------------------------- dialog */
.dialog {
  width: min(640px, 94vw); max-height: 82vh; padding: 0;
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--rule-strong); border-radius: 6px;
}
.dialog[open] { display: flex; flex-direction: column; }
.dialog::backdrop { background: rgba(20, 23, 31, 0.5); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 12px 14px 20px; border-bottom: 1px solid var(--rule); }
.dialog-head h2 { font-size: 1.25rem; }
.dialog-foot { padding: 12px 20px; border-top: 1px solid var(--rule); }

.history-list { margin: 0; padding: 0; list-style: none; overflow: auto; }
.history-item { padding: 14px 20px; border-bottom: 1px solid var(--rule); }
.history-meta { font-size: 0.8125rem; color: var(--muted); }
.history-preview {
  margin: 4px 0 10px; font-family: var(--serif); overflow-wrap: anywhere;
  display: -webkit-box; -webkit-line-clamp: 2; line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.history-actions { display: flex; gap: 8px; }
.history-empty { margin: 0; padding: 28px 20px; color: var(--muted); }
.history-list:not(:empty) + .history-empty { display: none; }

/* ------------------------------------------------------------------ toasts */
.toasts {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%);
  z-index: 100; display: flex; flex-direction: column; align-items: center; gap: 8px;
  width: min(92vw, 480px); pointer-events: none;
}
.toast {
  pointer-events: auto; width: 100%;
  padding: 12px 16px; background: var(--toast-bg); color: var(--toast-fg);
  border-left: 4px solid #5d77c4; border-radius: var(--radius);
  font-size: 0.9375rem; line-height: 1.4;
  box-shadow: 0 2px 10px rgba(20, 23, 31, 0.22);
  animation: toast-in 0.16s ease-out;
}
.toast[data-kind="success"] { border-left-color: #2f9e62; }
.toast[data-kind="error"] { border-left-color: #d9473b; }
@keyframes toast-in { from { opacity: 0; } to { opacity: 1; } }

/* -------------------------------------------------------------- responsive */
@media (max-width: 960px) {
  .steps { grid-template-columns: 1fr; gap: 28px; }
  .steps p { max-width: 60ch; }
}

@media (max-width: 860px) {
  .spread { grid-template-columns: 1fr; grid-template-rows: none; }
  @supports (grid-template-rows: subgrid) {
    .pane { display: flex; grid-row: auto; grid-template-rows: none; }
  }
  .pane + .pane { border-left: 0; border-top: 1px solid var(--rule-strong); }
  .editor { min-height: 280px; }
  .alphabet { grid-template-columns: 1fr; }
}

@media (max-width: 640px) {
  :root { --gutter: 16px; }
  html { scroll-padding-top: 120px; }

  .site-header { position: static; }
  .header-inner { flex-wrap: wrap; min-height: 0; padding-block: 8px 6px; gap: 0; }
  .theme-toggle { order: 2; margin-left: 0; }
  .brand img { height: 46px; }
  .main-nav { order: 3; width: 100%; justify-content: flex-start; gap: 28px; padding-top: 2px; border-top: 1px solid rgba(255, 255, 255, 0.16); }
  .main-nav a { display: inline-flex; align-items: center; min-height: 44px; }
  .tg-link { min-height: 44px; }
  .footer-meta { flex-wrap: wrap; gap: 4px 22px; }

  .hero { padding-top: 36px; padding-bottom: 24px; }
  .lead { font-size: 1.0625rem; }

  .controls { flex-direction: column; align-items: stretch; }
  .direction { display: grid; grid-template-columns: 1fr 22px 1fr; align-items: end; gap: 8px; }
  .direction select { min-width: 0; width: 100%; }

  .tool { min-height: 40px; padding-inline: 10px; }
  .pane-head { padding-left: 16px; }
  .editor { padding: 16px; min-height: 260px; }
  .pane-foot { padding-left: 16px; padding-right: 8px; }


  .info { padding-block: 40px; }
  .footer-inner { flex-direction: column; align-items: flex-start; }
  .footer-text { flex-basis: auto; }
  .toasts { bottom: 12px; }
  .history-actions .btn { min-height: 44px; }
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  .toast { animation: none; }
  .btn[aria-busy="true"]::after { animation-duration: 2.4s; }
}

.history-meta { display: flex; flex-wrap: wrap; gap: 4px 16px; }
