/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* === Bundled fonts === */
@font-face {
  font-family: 'PrintClearly';
  src: url('fonts/PrintClearly-Regular.ttf') format('truetype');
  font-weight: normal; font-style: normal; font-display: swap;
}
@font-face {
  font-family: 'PrintClearly-Bold';
  src: url('fonts/PrintClearly-Bold.ttf') format('truetype');
  font-weight: normal; font-style: normal; font-display: swap;
}
@font-face {
  font-family: 'PrintClearly-Dashed';
  src: url('fonts/PrintClearly-Dashed.ttf') format('truetype');
  font-weight: normal; font-style: normal; font-display: swap;
}
@font-face {
  font-family: 'PrintClearlyRuled';
  src: url('fonts/PrintClearlyRuled-Regular.ttf') format('truetype');
  font-weight: normal; font-style: normal; font-display: swap;
}
@font-face {
  font-family: 'PrintClearlyRuled-Bold';
  src: url('fonts/PrintClearlyRuled-Bold.ttf') format('truetype');
  font-weight: normal; font-style: normal; font-display: swap;
}
@font-face {
  font-family: 'PrintClearlyRuled-Dashed';
  src: url('fonts/PrintClearlyRuled-Dashed.ttf') format('truetype');
  font-weight: normal; font-style: normal; font-display: swap;
}
@font-face {
  font-family: 'Andika';
  src: url('fonts/Andika-Regular.ttf') format('truetype');
  font-weight: normal; font-style: normal; font-display: swap;
}
@font-face {
  font-family: 'Andika-Bold';
  src: url('fonts/Andika-Bold.ttf') format('truetype');
  font-weight: normal; font-style: normal; font-display: swap;
}
@font-face {
  font-family: 'LearningCurve';
  src: url('fonts/LearningCurve-Regular.ttf') format('truetype');
  font-weight: normal; font-style: normal; font-display: swap;
}
@font-face {
  font-family: 'LearningCurve-Bold';
  src: url('fonts/LearningCurve-Bold.ttf') format('truetype');
  font-weight: normal; font-style: normal; font-display: swap;
}
@font-face {
  font-family: 'LearningCurve-Dashed';
  src: url('fonts/LearningCurve-Dashed.ttf') format('truetype');
  font-weight: normal; font-style: normal; font-display: swap;
}
@font-face {
  font-family: 'LearningCurveStarts';
  src: url('fonts/LearningCurveStarts-Regular.ttf') format('truetype');
  font-weight: normal; font-style: normal; font-display: swap;
}
@font-face {
  font-family: 'LearningCurveStarts-Dashed';
  src: url('fonts/LearningCurveStarts-Dashed.ttf') format('truetype');
  font-weight: normal; font-style: normal; font-display: swap;
}
@font-face {
  font-family: 'LearningCurveRuled';
  src: url('fonts/LearningCurveRuled-Regular.ttf') format('truetype');
  font-weight: normal; font-style: normal; font-display: swap;
}
@font-face {
  font-family: 'LearningCurveRuled-Bold';
  src: url('fonts/LearningCurveRuled-Bold.ttf') format('truetype');
  font-weight: normal; font-style: normal; font-display: swap;
}
@font-face {
  font-family: 'LearningCurveRuled-Dashed';
  src: url('fonts/LearningCurveRuled-Dashed.ttf') format('truetype');
  font-weight: normal; font-style: normal; font-display: swap;
}

/* === Primary-color palette === */
:root {
  --bg: #fef3c7;          /* warm manila yellow */
  --bg-alt: #fffdf5;      /* near-white for panes */
  --text: #1e3a8a;        /* deep navy */
  --border: #2563eb;      /* primary blue */
  --accent: #dc2626;      /* red */
  --accent-2: #16a34a;    /* green */
  --accent-3: #f59e0b;    /* amber */
  --radius: 12px;
  --gap: 20px;

  /* Worksheet rules — adjusted by JS */
  --rule-color: #1e3a8a;
  --rule-color-mid: #93c5fd;
}

body {
  font-family: 'Andika', 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.4;
  min-height: 100vh;
}

/* === Editor Layout === */
.editor-wrap {
  max-width: 1300px;
  margin: 0 auto;
  padding: 24px 20px;
}

header {
  text-align: center;
  margin-bottom: var(--gap);
}

header h1 {
  font-size: 2.4em;
  letter-spacing: 4px;
  margin-bottom: 2px;
  color: var(--accent);
  text-shadow: 2px 2px 0 var(--border);
}

header .subtitle {
  color: var(--border);
  font-style: italic;
  font-size: 1em;
  font-weight: bold;
}

.header-note {
  margin-top: 4px;
  font-size: 0.78em;
  color: var(--accent);
}

.donate-link {
  color: var(--accent-2);
  font-weight: bold;
  text-decoration: underline;
}
.donate-link:hover {
  color: #15803d;
}

/* === Toolbar === */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 14px 18px;
  padding: 12px 16px;
  border: 3px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: var(--gap);
  background: var(--bg-alt);
  box-shadow: 4px 4px 0 var(--border);
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.toolbar-group label {
  font-size: 0.78em;
  font-weight: bold;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--accent);
}

.toolbar select, .toolbar button {
  font-family: inherit;
  font-size: 0.9em;
  padding: 5px 10px;
  border: 2px solid var(--border);
  border-radius: 6px;
  background: var(--bg-alt);
  color: var(--text);
  cursor: pointer;
  font-weight: 600;
}

.toolbar select:hover, .toolbar button:hover {
  background: var(--accent-3);
  color: var(--text);
}

.toolbar button:active {
  transform: translate(1px, 1px);
}

.toolbar input[type="range"] {
  width: 96px;
  accent-color: var(--border);
}

#blanks-value, #margin-value {
  font-size: 0.85em;
  min-width: 36px;
  color: var(--border);
  font-weight: bold;
}

#size-value {
  font-family: inherit;
  font-size: 0.9em;
  font-weight: bold;
  width: 50px;
  padding: 4px 6px;
  border: 2px solid var(--border);
  border-radius: 6px;
  background: var(--bg-alt);
  color: var(--border);
  text-align: center;
  -moz-appearance: textfield;  /* hide Firefox spinner */
}
#size-value::-webkit-outer-spin-button,
#size-value::-webkit-inner-spin-button {
  -webkit-appearance: none;  /* hide WebKit/Chrome spinner */
  margin: 0;
}
.unit-label {
  font-size: 0.85em;
  color: var(--border);
  font-weight: bold;
  margin-left: -2px;
}

.toolbar-actions {
  margin-left: auto;
  gap: 6px;
}

#print-btn {
  background: var(--accent-2);
  color: #fff;
  border-color: var(--accent-2);
}
#print-btn:hover {
  background: #15803d;
  color: #fff;
}

#help-btn {
  font-weight: bold;
  width: 36px;
  text-align: center;
}

/* === Workspace === */
.workspace {
  display: grid;
  grid-template-columns: 1fr 1.4fr;
  gap: var(--gap);
}

.workspace > * { min-width: 0; }

.pane {
  position: relative;
  border: 3px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-alt);
  box-shadow: 4px 4px 0 var(--border);
}

.pane::before {
  content: attr(data-title);
  position: absolute;
  top: -12px;
  left: 16px;
  background: var(--accent);
  color: #fff;
  padding: 2px 10px;
  font-size: 0.75em;
  font-weight: bold;
  text-transform: uppercase;
  letter-spacing: 1px;
  border-radius: 6px;
}

/* === Input Pane === */
.pane-input { display: flex; }

#text-input {
  width: 100%;
  min-height: 600px;
  padding: 22px;
  border: none;
  border-radius: var(--radius);
  background: transparent;
  font-family: 'Andika', system-ui, sans-serif;
  font-size: 1.05em;
  line-height: 1.6;
  color: var(--text);
  resize: vertical;
  outline: none;
}

#text-input::placeholder {
  color: var(--border);
  opacity: 0.5;
  font-style: italic;
}

/* === Preview Pane === */
.pane-preview {
  padding: 22px 12px 12px;
  text-align: center;
}

.preview-scaler {
  display: inline-block;
  /* zoom set by JS */
}

#preview-page {
  width: 8.5in;
  height: 11in;
  background: #fff;
  color: #000;
  padding: 0.5in;
  overflow: hidden;
  border: 1px solid #ccc;
  text-align: left;
}

#preview-content {
  display: flex;
  flex-direction: column;
}

/* === Writing rows === */
.row {
  position: relative;
  width: 100%;
  /* row height set via --row-h inline; descenders are allowed to spill
     into the row-gap below, so we don't clip */
  height: var(--row-h, 80px);
  margin-bottom: var(--row-gap, 20px);
  /* The rule lines are painted via background-image gradients (and
     ::before for the dashed midline). Browsers strip background
     graphics from print output by default — opt back in so worksheets
     actually have rules on paper. */
  -webkit-print-color-adjust: exact;
  print-color-adjust: exact;
}

.row::before { -webkit-print-color-adjust: exact; print-color-adjust: exact; }

/* Three-line ruling: top (solid), midline (dashed), baseline (solid) */
.row.lines-three {
  background-image:
    /* top line */
    linear-gradient(to bottom,
      var(--rule-color) 0, var(--rule-color) 1.5px,
      transparent 1.5px, transparent 100%),
    /* baseline */
    linear-gradient(to top,
      var(--rule-color) 0, var(--rule-color) 1.5px,
      transparent 1.5px, transparent 100%);
  background-repeat: no-repeat;
  background-size: 100% 100%, 100% 100%;
}

/* Midline (dashed) drawn via inline SVG below */
.row.lines-three::before {
  content: "";
  position: absolute;
  left: 0; right: 0;
  top: 50%;
  height: 1.5px;
  background-image: linear-gradient(to right,
    var(--rule-color-mid) 0, var(--rule-color-mid) 8px,
    transparent 8px, transparent 14px);
  background-size: 14px 100%;
  background-repeat: repeat-x;
  pointer-events: none;
}

.row.lines-baseline {
  background-image: linear-gradient(to top,
    var(--rule-color) 0, var(--rule-color) 1.5px,
    transparent 1.5px, transparent 100%);
  background-repeat: no-repeat;
}

.row.lines-none {
  /* no background */
}

.row .text {
  position: absolute;
  /* Per-font baseline nudge: pushes the text box's bottom edge below
     the row to align the visible glyph baseline with the bottom rule.
     Each font's em-square has different sTypoDescender padding, so
     the offset is font-specific. */
  bottom: var(--baseline-nudge, -0.18em);
  left: 0;
  /* Per-font visual-size multiplier so the same "Letter size" slider
     produces equivalent visual letter heights across fonts with
     different em-square fill ratios. */
  font-size: calc(var(--letter-size, 56pt) * var(--font-scale, 1));
  line-height: 1;
  white-space: pre;
  font-family: var(--worksheet-font, 'PrintClearly-Dashed');
  color: #000;
}

.row.font-print  .text { --baseline-nudge: -0.18em; --font-scale: 1.64; }
.row.font-ruled  .text { --baseline-nudge: -0.12em; --font-scale: 1.64; }
.row.font-andika .text,
.row.font-andika-bold .text { --baseline-nudge: -0.09em; --font-scale: 1; }
/* Learning Curve cursive — same Blue Vinyl em-square as Print Clearly
   but the cursive descenders (g, j, p, q, y) loop deeper, so the
   baseline sits visibly lower within the em-box. Push the box down
   more than print fonts to land the visible baseline on the rule. */
.row.font-cursive .text         { --baseline-nudge: -0.30em; --font-scale: 1.64; }
.row.font-cursive-ruled .text   { --baseline-nudge: -0.20em; --font-scale: 1.64; }

/* Overflow warning */
#overflow-warning {
  margin-top: 8px;
  padding: 5px 14px;
  font-size: 0.9em;
  font-weight: bold;
  color: #fff;
  background: var(--accent);
  border-radius: 6px;
  text-align: center;
  display: inline-block;
}

.hidden { display: none !important; }

/* === Help Modal === */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(30, 58, 138, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal-inner {
  background: var(--bg-alt);
  color: var(--text);
  border: 3px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 6px 6px 0 var(--accent);
  padding: 30px;
  max-width: 620px;
  width: 92%;
  max-height: 86vh;
  overflow-y: auto;
  position: relative;
}

.modal-close {
  position: absolute;
  top: 10px;
  right: 14px;
  background: none;
  border: none;
  font-size: 1.8em;
  cursor: pointer;
  color: var(--accent);
  line-height: 1;
  font-weight: bold;
}

.modal-inner h2 {
  font-size: 1.5em;
  margin-bottom: 14px;
  color: var(--accent);
  letter-spacing: 1px;
}

.modal-inner h3 {
  font-size: 1.1em;
  margin: 18px 0 8px;
  color: var(--border);
}

.modal-inner table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 10px;
}

.modal-inner td {
  padding: 5px 8px;
  border-bottom: 1px solid #fde68a;
  vertical-align: top;
  font-size: 0.9em;
}

.modal-inner td:first-child {
  white-space: nowrap;
  width: 42%;
}

.modal-inner ul {
  margin: 0 0 10px 1.4em;
}

.modal-inner li, .modal-inner p {
  font-size: 0.9em;
  margin-bottom: 5px;
}

.modal-inner a {
  color: var(--accent);
  font-weight: bold;
}

/* === Print Styles === */
@media print {
  body {
    background: #fff;
    margin: 0;
    padding: 0;
  }

  body, html {
    width: 8.5in;
    height: 11in;
    overflow: hidden;
  }

  header, .toolbar, .pane-input, #overflow-warning, .modal,
  .pane::before, .pane-preview::before { display: none !important; }

  .editor-wrap, .workspace, .pane-preview, .preview-scaler, .pane {
    display: block;
    margin: 0;
    padding: 0;
    border: none;
    box-shadow: none;
    width: auto;
    height: auto;
    overflow: visible;
    text-align: left;
    background: #fff;
  }

  .preview-scaler { zoom: 1 !important; }

  #preview-page {
    width: 8.5in;
    height: 11in;
    border: none;
    margin: 0;
    overflow: hidden;
  }

  @page {
    size: letter;
    margin: 0;
  }
}

/* === Responsive === */
@media (max-width: 900px) {
  .workspace { grid-template-columns: 1fr; }
  header h1 { font-size: 1.9em; }
  #text-input { min-height: 280px; }
  .toolbar { gap: 10px 14px; }
  .toolbar-actions { margin-left: 0; }
}
