/* ==========================================================================
   Scriptify — stationery-inspired UI
   ========================================================================== */

@font-face {
  font-family: 'Hindi_Font';
  src: url(../fonts/Hindi_Type.ttf);
  font-display: swap;
}

:root {
  /* Palette: warm ivory + deep teal. Change these variables to re-theme the whole site. */
  --bg: #f6f3ec;
  --bg-glow: #fffdf8;
  --surface: #ffffff;
  --surface-2: #f2eee4;
  --desk: #e2ddcf;
  --ink: #1a2421;
  --muted: #58625d;
  --line: #e4ded0;
  --line-strong: #cdc5b2;
  --accent: #0f766e; /* fills: buttons, brand mark */
  --accent-hover: #0b5f58;
  --accent-ink: #ffffff; /* text on top of --accent */
  --accent-text: #0f766e; /* accent used as text / borders / focus rings */
  --accent-soft: #d8eee8;
  --knob-on: #ffffff;
  --gold: #8f5a12;
  --gold-soft: #f2e6c9;
  --danger: #c43d3d;
  --nib-hole: #ffffff;
  --shadow-sm: 0 1px 2px rgba(26, 36, 33, 0.07);
  --shadow: 0 1px 2px rgba(26, 36, 33, 0.05), 0 14px 34px -10px rgba(40, 70, 60, 0.22);
  --radius: 16px;
  --font-ui: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --font-display: 'Fraunces', Georgia, 'Times New Roman', serif;
  color-scheme: light;
}

:root[data-theme='dark'] {
  --bg: #0e1513;
  --bg-glow: #17332d;
  --surface: #141d1b;
  --surface-2: #1b2724;
  --desk: #0a100f;
  --ink: #ecf1ee;
  --muted: #9cada6;
  --line: #25332f;
  --line-strong: #364843;
  --accent: #2dd4bf;
  --accent-hover: #5eead4;
  --accent-ink: #04201c;
  --accent-text: #5eead4;
  --accent-soft: #14403a;
  --knob-on: #04201c;
  --gold: #e3b968;
  --gold-soft: #17332d;
  --danger: #ff8585;
  --nib-hole: #0e1513;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 14px 34px -10px rgba(0, 0, 0, 0.65);
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--font-ui);
  font-size: 15px;
  line-height: 1.5;
  color: var(--ink);
  background: radial-gradient(1200px 500px at 50% -120px, var(--bg-glow), transparent 70%), var(--bg);
  background-color: var(--bg);
  transition: background-color 0.25s ease, color 0.25s ease;
}

h1,
h2,
p {
  margin: 0;
}

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

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

[hidden] {
  display: none !important;
}

/* ---------- icons ---------- */
.i {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: none;
}
.i.fill {
  fill: currentColor;
  stroke: none;
}

/* ---------- top bar ---------- */
.topbar {
  max-width: 1280px;
  margin: 0 auto;
  padding: 18px 24px 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--ink);
  text-decoration: none;
}
.brand-mark {
  width: 36px;
  height: 36px;
  border-radius: 11px;
  display: grid;
  place-items: center;
  background: linear-gradient(145deg, color-mix(in srgb, var(--accent) 70%, #fff), var(--accent));
  color: var(--accent-ink);
  box-shadow: 0 6px 14px -4px color-mix(in srgb, var(--accent) 70%, transparent);
}
.brand-mark .i {
  width: 22px;
  height: 22px;
  --nib-hole: var(--accent);
}
.brand-name {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.45rem;
  letter-spacing: -0.01em;
}

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 11px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: transform 0.12s ease, border-color 0.12s ease, background 0.12s;
}
.icon-btn:hover:not(:disabled) {
  border-color: var(--line-strong);
  transform: translateY(-1px);
}
.icon-btn:disabled {
  opacity: 0.4;
  cursor: default;
}
.icon-btn.xs {
  width: 22px;
  height: 22px;
  border-radius: 7px;
  box-shadow: none;
}
.icon-btn.xs .i {
  width: 13px;
  height: 13px;
}
#theme-toggle .moon,
:root[data-theme='dark'] #theme-toggle .sun {
  display: none;
}
:root[data-theme='dark'] #theme-toggle .moon {
  display: block;
}

.nav {
  display: flex;
  gap: 4px;
  margin-left: auto;
  margin-right: 12px;
}
.nav a {
  padding: 8px 14px;
  border-radius: 10px;
  color: var(--muted);
  text-decoration: none;
  font-weight: 500;
  font-size: 0.93rem;
  transition: color 0.12s, background 0.12s;
}
.nav a:hover {
  color: var(--ink);
  background: var(--surface-2);
}
.nav a[aria-current='page'] {
  color: var(--accent-text);
  background: var(--accent-soft);
}

/* ---------- hero ---------- */
.hero {
  max-width: 1280px;
  margin: 0 auto;
  padding: 36px 24px 26px;
}
.hero h1 {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: clamp(2rem, 4.6vw, 3.2rem);
  line-height: 1.08;
  letter-spacing: -0.02em;
}
.hero h1 em {
  font-style: normal;
  color: var(--accent-text);
  background: linear-gradient(transparent 70%, var(--accent-soft) 70%);
}
.hero p {
  margin-top: 12px;
  max-width: 54ch;
  color: var(--muted);
  font-size: 1.05rem;
}

/* ---------- layout ---------- */
.app {
  max-width: 1280px;
  margin: 0 auto;
  padding: 0 24px 40px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 560px);
  grid-template-areas:
    'editor preview'
    'settings preview';
  grid-template-rows: auto 1fr;
  gap: 20px;
  align-items: start;
}
.editor {
  grid-area: editor;
}
.settings {
  grid-area: settings;
}
.preview {
  grid-area: preview;
  position: sticky;
  top: 16px;
}

@media (max-width: 999px) {
  .app {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas: 'editor' 'preview' 'settings';
    grid-template-rows: none;
  }
  .preview {
    position: static;
  }
  .topbar,
  .hero,
  .app {
    padding-left: 16px;
    padding-right: 16px;
  }
}

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  transition: background-color 0.25s ease, border-color 0.25s ease;
}
.card-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.card-head h2 {
  font-size: 1.05rem;
  font-weight: 600;
}
.meta {
  color: var(--muted);
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
}

/* ---------- buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 11px 16px;
  border-radius: 12px;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--ink);
  font-weight: 500;
  font-size: 0.93rem;
  cursor: pointer;
  text-decoration: none;
  box-shadow: var(--shadow-sm);
  transition: transform 0.12s ease, background 0.12s, border-color 0.12s, box-shadow 0.12s;
}
.btn:hover:not(:disabled) {
  transform: translateY(-1px);
  border-color: var(--accent-text);
}
.btn:active:not(:disabled) {
  transform: translateY(0);
}
.btn:disabled {
  opacity: 0.6;
  cursor: progress;
}
.btn.primary {
  background: var(--accent);
  border-color: var(--accent-hover);
  color: var(--accent-ink);
  font-weight: 600;
  padding: 14px 18px;
  font-size: 1rem;
  box-shadow: 0 8px 20px -8px color-mix(in srgb, var(--accent) 80%, transparent);
}
.btn.primary:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}
.btn.ghost {
  background: transparent;
  box-shadow: none;
  border-color: var(--line);
  color: var(--muted);
}
.btn.ghost:hover:not(:disabled) {
  color: var(--ink);
  border-color: var(--line-strong);
}
.btn.sm {
  padding: 7px 12px;
  font-size: 0.85rem;
  border-radius: 10px;
}
.btn.sm .i {
  width: 16px;
  height: 16px;
}

/* ---------- editor ---------- */
#text {
  display: block;
  width: 100%;
  min-height: 260px;
  max-height: 55vh;
  resize: vertical;
  padding: 16px 18px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface-2);
  line-height: 1.65;
  font-size: 1rem;
  transition: border-color 0.12s, box-shadow 0.12s;
}
#text:focus {
  outline: none;
  border-color: var(--accent-text);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.editor-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 12px;
}
.drawings {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 12px;
}
.chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 4px 6px 4px 4px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface-2);
  font-size: 0.8rem;
  color: var(--muted);
}
.chip img {
  width: 34px;
  height: 26px;
  object-fit: contain;
  background: #fff;
  border-radius: 6px;
  border: 1px solid var(--line);
}

.mathbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-top: 12px;
}
.mathbar-label {
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--gold);
  margin-right: 4px;
}
.mathbar button {
  min-width: 34px;
  height: 32px;
  padding: 0 9px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--surface-2);
  color: var(--ink);
  font-size: 0.95rem;
  cursor: pointer;
  transition: border-color 0.12s, background 0.12s, transform 0.12s;
}
.mathbar button:hover {
  border-color: var(--accent-text);
  background: var(--accent-soft);
  transform: translateY(-1px);
}
.mathbar button.wide {
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--accent-text);
}
.mathhint code {
  padding: 1px 5px;
  border-radius: 5px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  font-size: 0.78rem;
}

/* ---------- settings ---------- */
.settings {
  padding: 8px 20px 20px;
}
.tabs {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--line);
  margin: 0 -20px 18px;
  padding: 0 14px;
}
.tab {
  background: none;
  border: none;
  padding: 14px 12px 12px;
  font-weight: 500;
  color: var(--muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}
.tab:hover {
  color: var(--ink);
}
.tab[aria-selected='true'] {
  color: var(--accent-text);
  border-bottom-color: var(--accent-text);
}

.field {
  margin-bottom: 20px;
}
.field.two {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  align-items: end;
}
.label {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 8px;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--muted);
  letter-spacing: 0.01em;
}
.label output {
  font-weight: 500;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}
.hint {
  margin-top: 6px;
  font-size: 0.8rem;
  color: var(--muted);
}
.inline-row {
  display: flex;
  gap: 8px;
  margin-top: 12px;
  flex-wrap: wrap;
}
.settings-foot {
  display: flex;
  justify-content: flex-end;
  border-top: 1px solid var(--line);
  padding-top: 16px;
  margin-top: 4px;
}

input[type='text'],
select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: var(--surface-2);
  font-size: 0.92rem;
}
select {
  appearance: none;
  -webkit-appearance: none;
  padding-right: 34px;
  background-image: 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.5l5 5 5-5' fill='none' stroke='%23888' stroke-width='1.8' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
}
input[type='text']:focus,
select:focus {
  outline: none;
  border-color: var(--accent-text);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.input-with-btn {
  display: flex;
  gap: 6px;
}

/* range */
input[type='range'] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 22px;
  background: transparent;
  cursor: pointer;
  margin: 0;
}
input[type='range']::-webkit-slider-runnable-track {
  height: 5px;
  border-radius: 5px;
  background: var(--line-strong);
}
input[type='range']::-moz-range-track {
  height: 5px;
  border-radius: 5px;
  background: var(--line-strong);
}
input[type='range']::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  margin-top: -6.5px;
  border-radius: 50%;
  background: var(--accent-text);
  border: 3px solid var(--surface);
  box-shadow: 0 0 0 1px var(--accent-text), var(--shadow-sm);
}
input[type='range']::-moz-range-thumb {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--accent-text);
  border: 3px solid var(--surface);
  box-shadow: 0 0 0 1px var(--accent-text);
}

/* segmented */
.seg {
  display: flex;
  padding: 3px;
  gap: 2px;
  border-radius: 12px;
  background: var(--surface-2);
  border: 1px solid var(--line);
}
.seg button {
  flex: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 10px;
  border: none;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  font-weight: 500;
  font-size: 0.88rem;
  cursor: pointer;
  transition: background 0.12s, color 0.12s;
}
.seg button:hover {
  color: var(--ink);
}
.seg button[aria-pressed='true'] {
  background: var(--surface);
  color: var(--accent-text);
  box-shadow: var(--shadow-sm), 0 0 0 1px var(--line);
}
.seg.compact {
  display: inline-flex;
}
.seg .i {
  width: 16px;
  height: 16px;
}

.seg.wrap {
  flex-wrap: wrap;
}
.seg.wrap button {
  flex: 1 1 30%;
  white-space: nowrap;
}

/* font cards */
.font-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 8px;
  margin-bottom: 12px;
  max-height: 340px;
  overflow-y: auto;
  padding: 2px 4px 2px 2px;
}
.font-card {
  display: flex;
  flex-direction: column;
  gap: 2px;
  text-align: left;
  padding: 10px 12px 8px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface-2);
  cursor: pointer;
  overflow: hidden;
  transition: border-color 0.12s, transform 0.12s, box-shadow 0.12s;
}
.font-card:hover {
  border-color: var(--line-strong);
  transform: translateY(-1px);
}
.font-card[aria-pressed='true'] {
  border-color: var(--accent-text);
  background: var(--accent-soft);
  box-shadow: 0 0 0 1px var(--accent-text);
}
.font-card .sample {
  font-size: 1.35rem;
  line-height: 1.4;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.font-card .name {
  font-size: 0.74rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* swatches */
.swatches {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
}
.swatch {
  position: relative;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 2px solid var(--surface);
  background: var(--c);
  box-shadow: 0 0 0 1px var(--line-strong);
  cursor: pointer;
  padding: 0;
  transition: transform 0.12s;
}
.swatch:hover {
  transform: scale(1.08);
}
.swatch[aria-pressed='true'],
.swatch.custom.active {
  box-shadow: 0 0 0 2px var(--accent-text);
}
.swatch.custom {
  display: block;
  background: conic-gradient(#e94f4f, #f0b43c, #5cc16a, #3ea6d8, #7d5be0, #e94f4f);
}
.swatch.custom.active {
  background: var(--c);
}
.swatch.custom input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
  border: 0;
  padding: 0;
}

/* switch */
.switch {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.92rem;
  cursor: pointer;
  user-select: none;
}
.switch input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}
.switch .track {
  position: relative;
  width: 40px;
  height: 24px;
  border-radius: 24px;
  background: var(--line-strong);
  transition: background 0.15s;
  flex: none;
}
.switch .track::after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  transition: transform 0.15s;
}
.switch input:checked + .track {
  background: var(--accent-text);
}
.switch input:checked + .track::after {
  background: var(--knob-on);
  transform: translateX(16px);
}
.switch input:focus-visible + .track {
  outline: 2px solid var(--accent-text);
  outline-offset: 2px;
}

/* ---------- preview ---------- */
.preview {
  padding: 14px;
}
.preview-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}
.preview-bar .btn.primary.sm {
  padding: 8px 14px;
  font-size: 0.88rem;
  font-weight: 600;
}
.pager {
  display: flex;
  align-items: center;
  gap: 10px;
}
.page-label {
  font-weight: 600;
  font-size: 0.92rem;
  font-variant-numeric: tabular-nums;
}
.stage {
  display: grid;
  place-items: center;
  padding: 16px;
  border-radius: 12px;
  background: var(--desk);
  background-image: radial-gradient(circle at 1px 1px, color-mix(in srgb, var(--ink) 7%, transparent) 1px, transparent 0);
  background-size: 18px 18px;
  min-height: 200px;
  overflow: hidden;
}
.scaler {
  position: relative;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 18px 40px -10px rgba(0, 0, 0, 0.4);
  border-radius: 2px;
}
.scaler .sheet {
  transform-origin: 0 0;
}
.export {
  display: grid;
  gap: 10px;
  margin-top: 14px;
}
.export-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.export-row .btn {
  padding: 10px 8px;
  font-size: 0.86rem;
}

/* ---------- the paper sheet (also used for export — no theme variables!) ---------- */
.sheet {
  position: relative;
  overflow: hidden;
  box-sizing: border-box;
  text-align: left;
  font-weight: 400;
  letter-spacing: normal;
}
.sheet *,
.measure {
  box-sizing: border-box;
}
.sheet.preview-scanner {
  filter: contrast(1.22) brightness(1.03) saturate(0.85);
}
.layer {
  position: absolute;
  margin: 0;
}
.layer.content,
.measure {
  white-space: normal;
  word-break: normal;
  overflow-wrap: normal;
}
.layer.content .w,
.measure .w {
  display: inline-block;
}
.layer.content .ind {
  display: inline-block;
  height: 1px;
}
.layer.content .img-block {
  display: block;
}
.layer.content .img-block img {
  display: block;
}
.layer.header {
  display: flex;
  justify-content: space-between;
  white-space: nowrap;
}
.layer.fx {
  pointer-events: none;
}

/* ---- handwritten maths (no theme variables: used for export too) ---- */
.mi {
  display: inline-block;
  height: 0;
  white-space: nowrap;
  vertical-align: baseline;
}
.mi,
.mi *,
.mbk,
.mbk * {
  line-height: 1.15;
}
.mbk {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
}
.mbi {
  display: block;
  white-space: nowrap;
  text-align: center;
  font-size: 1.15em;
}
.mt,
.ms,
.mf,
.mo,
.mr,
.mss2,
.msup,
.msub,
.msp,
.mov,
.mvec {
  display: inline-block;
}
.mt {
  vertical-align: baseline;
}
.mop {
  margin: 0 0.22em;
}
.mfn2 {
  margin-right: 0.14em;
}
.mtx {
  white-space: pre;
}
.ms {
  font-family: 'Alegreya', 'Times New Roman', serif;
  font-style: italic;
  font-size: 0.95em;
  padding: 0 0.05em;
}
.mf {
  vertical-align: middle;
  text-align: center;
  padding: 0 0.12em;
}
.mfn,
.mfd {
  display: block;
  padding: 0 0.1em;
}
.mfb {
  display: block;
  height: 2px;
  margin: 0.08em 0;
  border-radius: 2px;
  background: currentColor;
}
.mr {
  display: inline-flex;
  align-items: stretch;
  vertical-align: middle;
}
.mrk {
  display: block;
  width: 0.7em;
  background-repeat: no-repeat;
  background-size: 100% 100%;
}
.mri {
  align-self: flex-start;
  font-size: 0.55em;
  margin-right: -0.5em;
}
.mrb {
  display: block;
  padding: 0.12em 0.14em 0 0.08em;
  border-top: 1.8px solid currentColor;
  border-radius: 1px;
}
.msup,
.msub {
  position: relative;
  font-size: 0.65em;
}
.msup {
  top: -0.72em;
}
.msub {
  top: 0.38em;
}
.mss2 {
  vertical-align: middle;
  font-size: 0.65em;
}
.mss2 > span {
  display: block;
}
.mo {
  display: inline-flex;
  align-items: center;
  vertical-align: middle;
  padding: 0 0.1em;
}
.mo.mostack {
  flex-direction: column;
}
.mbs {
  display: block;
  background-repeat: no-repeat;
  background-size: 100% 100%;
}
.mos {
  font-size: 0.62em;
  white-space: nowrap;
}
.mss {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-self: stretch;
  font-size: 0.62em;
  margin-left: 0.15em;
}
.mov {
  border-top: 1.6px solid currentColor;
  padding-top: 0.05em;
}
.mvec {
  position: relative;
}
.mvarr {
  position: absolute;
  left: 0.05em;
  top: -0.6em;
  font-size: 0.6em;
}
.measure {
  left: -99999px;
  top: 0;
  visibility: hidden;
  pointer-events: none;
}
.measure .w {
  display: inline-block;
}
.measure .ind {
  display: inline-block;
  height: 1px;
}
.measure .img-block,
.measure .img-block img {
  display: block;
}
.measure .mi {
  display: inline-block;
}

/* ---------- footer ---------- */
.foot {
  max-width: 1280px;
  margin: 24px auto 0;
  padding: 26px 24px 36px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 0.82rem;
  text-align: center;
}
.foot-nav {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 4px 6px;
}
.foot-nav a {
  padding: 8px 12px;
  border-radius: 9px;
  color: var(--muted);
  font-size: 0.92rem;
  font-weight: 500;
  text-decoration: none;
  transition: color 0.12s, background 0.12s;
}
.foot-nav a:hover {
  color: var(--ink);
  background: var(--accent-soft);
}
.foot-note {
  max-width: 60ch;
  line-height: 1.5;
}
@media (max-width: 999px) {
  .foot {
    padding-left: 16px;
    padding-right: 16px;
  }
}

/* ---------- drawing dialog ---------- */
dialog {
  width: min(860px, calc(100vw - 24px));
  max-height: calc(100vh - 24px);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 30px 80px -10px rgba(0, 0, 0, 0.5);
  overflow: auto;
}
dialog::backdrop {
  background: rgba(10, 12, 25, 0.55);
  backdrop-filter: blur(3px);
}
.dlg-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px 4px;
}
.dlg-head h2 {
  font-size: 1.1rem;
}
.dlg-tools {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  padding: 10px 20px;
}
.dlg-tools .size {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.82rem;
  color: var(--muted);
  min-width: 130px;
}
.canvas-wrap {
  margin: 4px 20px;
  border: 1px solid var(--line-strong);
  border-radius: 12px;
  overflow: hidden;
  background-color: #fff;
  background-image: linear-gradient(to bottom, rgba(80, 100, 160, 0.2) 1px, transparent 1px);
  background-size: 100% 8.33%;
}
#draw-canvas {
  display: block;
  width: 100%;
  aspect-ratio: 2 / 1;
  touch-action: none;
  cursor: crosshair;
}
.dlg-foot {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 14px 20px 18px;
}
.dlg-foot .btn.primary {
  padding: 11px 18px;
  font-size: 0.95rem;
}

/* ---------- toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 20px);
  padding: 11px 18px;
  border-radius: 12px;
  background: var(--ink);
  color: var(--bg);
  font-size: 0.9rem;
  font-weight: 500;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 100;
  max-width: calc(100vw - 32px);
}
.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

@media (max-width: 520px) {
  .card {
    padding: 16px;
  }
  .settings {
    padding: 4px 16px 16px;
  }
  .tabs {
    margin-left: -16px;
    margin-right: -16px;
    padding: 0 8px;
  }
  .field.two {
    grid-template-columns: 1fr;
  }
  .font-grid {
    grid-template-columns: 1fr 1fr;
  }
}

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

/* ==========================================================================
   Additions
   ========================================================================== */

/* The off-screen box used to measure text for pagination. It lives inside a
   0x0 clipped wrapper so it can never widen the page (this was the phone bug). */
.measure-box {
  position: fixed;
  left: 0;
  top: 0;
  width: 0;
  height: 0;
  overflow: hidden;
  visibility: hidden;
  pointer-events: none;
}

/* ---------- preview bar: file name + pager ---------- */
.preview-bar {
  flex-wrap: wrap;
}
.namebox {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 1 1 200px;
  min-width: 0;
}
.namefield {
  display: flex;
  align-items: center;
  flex: 1;
  min-width: 0;
  padding: 0 12px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: var(--surface-2);
  cursor: text;
  transition: border-color 0.12s, box-shadow 0.12s;
}
.namefield:focus-within {
  border-color: var(--accent-text);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.namefield input[type='text'] {
  flex: 1;
  min-width: 0;
  padding: 9px 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
  font-size: 0.92rem;
  font-weight: 500;
}
.namefield input[type='text']:focus {
  box-shadow: none;
}
.namefield .ext {
  flex: none;
  padding-left: 4px;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 500;
}
.icon-only {
  flex: none;
  width: 40px;
  height: 40px;
  padding: 0 !important;
}
.icon-only .i {
  width: 18px;
  height: 18px;
}
.pager {
  flex: none;
}

/* ---------- question / answer ink ---------- */
.qabar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-top: 12px;
}
.qabar button {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 32px;
  padding: 0 11px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--surface-2);
  color: var(--ink);
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  transition: border-color 0.12s, background 0.12s, transform 0.12s;
}
.qabar button:hover {
  border-color: var(--accent-text);
  background: var(--accent-soft);
  transform: translateY(-1px);
}
.qabar button::before {
  content: '';
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--dot, currentColor);
  box-shadow: 0 0 0 1.5px color-mix(in srgb, var(--ink) 50%, transparent);
}
.qa-hint code,
.hint code {
  padding: 1px 5px;
  border-radius: 5px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  font-size: 0.78rem;
}
.qa-ink {
  margin-top: 14px;
}

/* ---------- plain text pages (privacy, license) ---------- */
.doc {
  max-width: 760px;
  margin: 0 auto;
  padding: 40px 24px 16px;
}
.doc h1 {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: clamp(2rem, 5vw, 2.8rem);
  line-height: 1.1;
  letter-spacing: -0.02em;
  margin-bottom: 6px;
}
.doc .updated {
  color: var(--muted);
  font-size: 0.9rem;
  margin-bottom: 28px;
}
.doc h2 {
  margin: 28px 0 8px;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.25rem;
}
.doc p,
.doc li {
  color: var(--muted);
  line-height: 1.7;
}
.doc p + p {
  margin-top: 10px;
}
.doc ul {
  margin: 8px 0 0;
  padding-left: 1.2em;
}
.doc a {
  color: var(--accent-text);
}
.doc pre {
  margin: 14px 0 0;
  padding: 18px 20px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--ink);
  font: 0.82rem/1.65 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* ---------- small screens & touch ---------- */
html {
  overflow-x: clip;
}
body {
  min-height: 100vh;
  min-height: 100dvh;
}
img,
svg,
canvas {
  max-width: 100%;
}

/* iOS Safari zooms the whole page when a field smaller than 16px gets focus.
   That made the layout look cut off / mis-scaled on phones. */
@media (max-width: 999px) {
  input[type='text'],
  select,
  textarea,
  .namefield input[type='text'] {
    font-size: 16px;
  }
}

@media (max-width: 999px) {
  .topbar {
    padding-top: 14px;
  }
  .preview {
    padding: 12px;
  }
}

@media (max-width: 520px) {
  body {
    font-size: 14.5px;
  }
  .hero {
    padding-top: 22px;
    padding-bottom: 18px;
  }
  .hero p {
    font-size: 1rem;
  }
  .pager {
    width: 100%;
    justify-content: space-between;
  }
  .stage {
    padding: 10px;
  }
  .export-row {
    grid-template-columns: 1fr 1fr 1fr;
    gap: 6px;
  }
  .export-row .btn {
    flex-direction: column;
    gap: 4px;
    padding: 10px 4px;
    font-size: 0.8rem;
    white-space: nowrap;
  }
  .tabs {
    overflow-x: auto;
    scrollbar-width: none;
  }
  .tab {
    flex: none;
  }
  .mathbar button {
    min-width: 40px;
    height: 38px;
  }
  .qabar button,
  .editor-actions .btn {
    height: auto;
    min-height: 38px;
  }
  .dlg-tools .size {
    flex: 1 1 100%;
  }
  .dlg-head,
  .dlg-tools {
    padding-left: 14px;
    padding-right: 14px;
  }
  .canvas-wrap {
    margin-left: 14px;
    margin-right: 14px;
  }
  .dlg-foot {
    padding: 12px 14px 16px;
  }
  .dlg-foot .btn {
    flex: 1;
  }
  .toast {
    bottom: 16px;
  }
}

@media (max-width: 360px) {
  .topbar,
  .hero,
  .app {
    padding-left: 12px;
    padding-right: 12px;
  }
  .brand-name {
    font-size: 1.25rem;
  }
}
