/* WorksheetForge offline app — screen shell + print-first paper */

:root {
  --ink: #0f172a;
  --muted: #5b6b82;
  --blue: #1d4ed8;
  --blue-dark: #1e3a8a;
  --line: #dbe2ee;
  --wash: #eef2f7;
  --green: #047857;
  --amber: #b45309;
}

* { box-sizing: border-box; }

html { -webkit-print-color-adjust: exact; print-color-adjust: exact; }

body {
  margin: 0;
  font-family: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  color: var(--ink);
  background: var(--wash);
}

button, input { font: inherit; }
button { cursor: pointer; }

/* ---------------------------------------------------------------- app shell */

.app-header {
  height: 62px;
  background: #0f172a;
  color: #fff;
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 0 24px;
}

.app-header .logo {
  font-size: 17px;
  font-weight: 800;
  letter-spacing: -0.3px;
}

.app-header .logo span { color: #93c5fd; }

.app-header .offline {
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.12em;
  color: #bbf7d0;
  border: 1px solid #166534;
  background: #052e16;
  padding: 3px 7px;
  border-radius: 4px;
}

.app-header .actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 10px;
}

.app-header button,
.app-header a {
  border: 0;
  border-radius: 7px;
  padding: 8px 12px;
  font-size: 12px;
  font-weight: 700;
  text-decoration: none;
  color: #fff;
  background: #334155;
}

.app-header button.licensed { background: #065f46; }
.app-header a.buy { background: #1d4ed8; }

.layout {
  display: grid;
  grid-template-columns: 340px minmax(0, 1fr);
  gap: 24px;
  max-width: 1500px;
  margin: 0 auto;
  padding: 24px;
  align-items: start;
}

.controls {
  position: sticky;
  top: 24px;
  max-height: calc(100vh - 48px);
  overflow: auto;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 22px;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.05);
}

.controls h1 {
  font-size: 21px;
  letter-spacing: -0.5px;
  margin: 0 0 4px;
}

.controls .intro {
  color: var(--muted);
  font-size: 12.5px;
  margin: 0 0 18px;
}

.field { display: block; margin-bottom: 12px; }

.field > span {
  display: block;
  font-size: 10.5px;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #64748b;
  margin-bottom: 5px;
}

.field input,
.field select {
  width: 100%;
  border: 1px solid #cbd5e1;
  border-radius: 7px;
  padding: 9px 10px;
  background: #fff;
  color: var(--ink);
  font-size: 13px;
}

.field input:focus,
.field select:focus {
  outline: 2px solid #bfdbfe;
  border-color: var(--blue);
}

.tier-head {
  margin: 12px 0 4px;
  font-size: 10.5px;
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--blue);
}

.tier-group.tier-hidden { display: none; }

.presets { margin: 0 0 14px; }

.presets-head {
  font-size: 10.5px;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #64748b;
  margin-bottom: 6px;
}

.preset-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.preset {
  border: 1px solid #cbd5e1;
  background: #f8fafc;
  color: #334155;
  border-radius: 6px;
  padding: 6px 9px;
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
}

.preset:hover {
  border-color: var(--blue);
  color: var(--blue);
  background: #eff6ff;
}

.check-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: #334155;
  margin: 10px 0 0;
  cursor: pointer;
}

.check-row input { accent-color: var(--blue); }

.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.seed-row { display: flex; gap: 6px; }
.seed-row input { min-width: 0; }
.seed-row button {
  flex: 0 0 auto;
  border: 1px solid #cbd5e1;
  background: #f8fafc;
  border-radius: 7px;
  padding: 0 9px;
  font-size: 11px;
  font-weight: 700;
  color: #334155;
}

.topics-head {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 16px 0 8px;
}

.topics-head span {
  font-size: 10.5px;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #64748b;
  margin-right: auto;
}

.topics-head button {
  border: 1px solid #dbe2ee;
  background: #f8fafc;
  color: #475569;
  border-radius: 5px;
  padding: 3px 7px;
  font-size: 10px;
  font-weight: 700;
}

.topics {
  display: grid;
  grid-template-columns: 1fr;
  gap: 4px;
  max-height: 310px;
  overflow: auto;
  padding-right: 4px;
  margin-bottom: 16px;
}

.topic {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 6px;
  font-size: 12px;
  color: #334155;
  cursor: pointer;
}

.topic:hover { background: #f1f5f9; }
.topic input { margin-top: 2px; accent-color: var(--blue); }

.primary {
  width: 100%;
  border: 0;
  border-radius: 8px;
  background: var(--blue);
  color: #fff;
  font-weight: 800;
  font-size: 14px;
  padding: 11px 14px;
  box-shadow: 0 5px 14px rgba(29, 78, 216, 0.2);
}

.primary:hover { background: #1e40af; }

.hint {
  color: #7b8aa0;
  font-size: 11px;
  text-align: center;
  margin: 10px 0 0;
}

/* ---------------------------------------------------------------- preview */

.preview-wrap { min-width: 0; }

.preview-toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
  padding: 10px 12px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
}

.preview-toolbar .summary {
  font-size: 12px;
  font-weight: 700;
  color: #475569;
}

.preview-toolbar .tools {
  margin-left: auto;
  display: flex;
  gap: 8px;
}

.preview-toolbar button {
  border: 1px solid #cbd5e1;
  background: #f8fafc;
  color: #1e293b;
  border-radius: 6px;
  padding: 7px 10px;
  font-size: 12px;
  font-weight: 700;
}

.preview-toolbar button.primary-small {
  background: var(--blue);
  border-color: var(--blue);
  color: #fff;
}

/* ---------------------------------------------------------------- the paper */

.paper {
  width: 210mm;
  min-height: 297mm;
  margin: 0 auto 40px;
  padding: 15mm 14mm 14mm;
  background: #fff;
  border: 1px solid #d8dfeb;
  box-shadow: 0 18px 50px rgba(15, 23, 42, 0.16);
  font-family: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  font-size: 11pt;
  line-height: 1.45;
  color: #111;
}

.paper .head {
  border-bottom: 2.5px solid #111;
  padding-bottom: 4mm;
  margin-bottom: 5mm;
}

.paper .brand {
  font-size: 9pt;
  font-weight: 700;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--blue);
}

.paper .brand .tag {
  display: inline-block;
  background: #fde68a;
  color: #78350f;
  padding: 0.4mm 1.6mm;
  border-radius: 1mm;
  margin-left: 2mm;
  letter-spacing: 0.08em;
  font-size: 8pt;
}

.paper .brand .licensed-tag { background: #d1fae5; color: #065f46; }
.paper .brand .demo-tag { background: #fde68a; color: #78350f; }
.paper .brand .answer-tag { background: #dbeafe; color: #1e3a8a; }

.paper h1 {
  font-size: 19pt;
  line-height: 1.15;
  margin: 2mm 0 1mm;
  letter-spacing: -0.2pt;
}

.paper .sub {
  margin: 0;
  color: #444;
  font-size: 10.5pt;
}

.paper .meta {
  display: flex;
  gap: 8mm;
  margin-top: 4mm;
  font-size: 10pt;
  color: #333;
}

.paper .meta .line {
  display: inline-block;
  width: 38mm;
  border-bottom: 1px solid #777;
  height: 4mm;
}

.paper .meta .line.short { width: 22mm; }

.paper .instructions {
  background: #f4f6fb;
  border-left: 3px solid var(--blue);
  padding: 3mm 4mm;
  margin-bottom: 6mm;
  font-size: 9.5pt;
}

.paper .instructions ul { margin: 1.5mm 0 0; padding-left: 5mm; }

.paper .questions { list-style: none; margin: 0; padding: 0; }

.paper .q {
  page-break-inside: avoid;
  break-inside: avoid;
  margin-bottom: 8mm;
  padding-bottom: 1mm;
}

.paper .q-head {
  display: flex;
  align-items: baseline;
  gap: 3mm;
  font-size: 9pt;
  color: #555;
  margin-bottom: 1.5mm;
}

.paper .q-num {
  background: #111;
  color: #fff;
  font-weight: 700;
  min-width: 7mm;
  height: 7mm;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 9.5pt;
}

.paper .q-topic { font-weight: 600; color: var(--blue); }
.paper .q-marks { margin-left: auto; font-weight: 600; color: #111; }
.paper .q-body { font-size: 11.5pt; }

.paper .q-diagram {
  display: flex;
  justify-content: center;
  margin: 2mm 0 0;
  page-break-inside: avoid;
  break-inside: avoid;
}

.paper .q-diagram svg {
  width: 52mm;
  max-width: 100%;
  height: auto;
  overflow: visible;
  -webkit-print-color-adjust: exact;
  print-color-adjust: exact;
}

.paper .q-diagram .dg-line {
  fill: none;
  stroke: #0f172a;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.paper .q-diagram .dg-fill { fill: #f8fafc; }

.paper .q-diagram .dg-circle {
  fill: #f8fafc;
  stroke: #0f172a;
  stroke-width: 1.7;
}

.paper .q-diagram .dg-right {
  fill: none;
  stroke: #0f172a;
  stroke-width: 1.2;
}

.paper .q-diagram .dg-angle {
  fill: none;
  stroke: #1d4ed8;
  stroke-width: 1.5;
}

.paper .q-diagram .dg-sector {
  fill: #eff6ff;
  stroke: #0f172a;
  stroke-width: 1.7;
  stroke-linejoin: round;
}

.paper .q-diagram .dg-dot { fill: #0f172a; }

.paper .q-diagram .dg-cone-base {
  fill: #f8fafc;
  stroke: #0f172a;
  stroke-width: 1.4;
}

.paper .q-diagram .dg-dash {
  fill: none;
  stroke: #64748b;
  stroke-width: 1;
  stroke-dasharray: 3 3;
}

.paper .q-diagram .dg-vertex {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 9px;
  font-weight: 700;
  fill: #1d4ed8;
}

.paper .q-diagram .dg-small {
  font-size: 9.5px;
}

.paper .q-diagram .dg-label {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 11px;
  font-weight: 600;
  fill: #0f172a;
}

.paper .q-diagram .dg-question {
  font-size: 13px;
  font-weight: 700;  fill: #1d4ed8;
}

.paper .q-diagram .dg-grid {
  fill: none;
  stroke: #dbe4f0;
  stroke-width: 0.8;
}

.paper .q-diagram .dg-axis {
  fill: none;
  stroke: #0f172a;
  stroke-width: 1.6;
}

.paper .q-diagram .dg-shade {
  fill: #c7d7f0;
  fill-opacity: 0.85;
  stroke: none;
}

.paper .q-diagram .dg-tick {
  fill: none;
  stroke: #0f172a;
  stroke-width: 1.4;
}

.paper .q-diagram .dg-parallel {
  fill: none;
  stroke: #1d4ed8;
  stroke-width: 1.3;
}

.paper .q-diagram .dg-arrowhead {
  fill: #0f172a;
  stroke: none;
}

.paper .q-diagram .dg-match {
  fill: none;
  stroke: #b45309;
  stroke-width: 2.4;
  stroke-linecap: round;
}

.paper .q-diagram .dg-matchhead {
  fill: #7c2d12;
  stroke: none;
}

.paper .answers .q-diagram svg { width: 50mm; }

.paper .q.has-diagram {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 56mm;
  grid-template-areas:
    "head head"
    "body diagram"
    "work work";
  column-gap: 5mm;
  margin-bottom: 6mm;
}

.paper .q.has-diagram .q-head { grid-area: head; }
.paper .q.has-diagram .q-body { grid-area: body; align-self: start; }
.paper .q.has-diagram .q-diagram { grid-area: diagram; align-self: center; margin: 0; }
.paper .q.has-diagram .workspace { grid-area: work; }

.paper .recur {
  display: inline-block;
  position: relative;
}
.paper .recur::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 0.48em;
  border-top: 1.2px solid #111;
}

.paper table.data {
  border-collapse: collapse;
  margin: 2mm 0;
  font-size: 10pt;
}

.paper table.data th,
.paper table.data td {
  border: 1px solid #555;
  padding: 1mm 4mm;
  text-align: center;
}

.paper table.data th { background: #eef2ff; }

.paper .workspace {
  margin-top: 2mm;
  background-image: repeating-linear-gradient(
    to bottom,
    transparent 0,
    transparent 7.7mm,
    #c3ccda 7.7mm,
    #c3ccda 8mm
  );
}

.paper .frac {
  display: inline-flex;
  flex-direction: column;
  vertical-align: middle;
  text-align: center;
  font-size: 0.88em;
  line-height: 1.05;
  margin: 0 0.6mm;
}

.paper .frac .top { border-bottom: 0.8px solid #111; padding: 0 1.2mm; }

.paper footer {
  margin-top: 6mm;
  padding-top: 2mm;
  border-top: 0.5px solid #999;
  font-size: 8pt;
  color: #666;
  text-align: center;
}

/* answer key */

.paper .answers { margin: 0; padding-left: 6mm; }

.paper .answers > li {
  page-break-inside: avoid;
  break-inside: avoid;
  margin-bottom: 5mm;
  padding-bottom: 3mm;
  border-bottom: 0.5px solid #ccc;
}

.paper .a-head {
  display: flex;
  justify-content: space-between;
  font-size: 9.5pt;
  font-weight: 700;
  color: var(--blue);
  margin-bottom: 1mm;
}

.paper .a-body { font-size: 10pt; color: #333; margin-bottom: 1.5mm; }

.paper .answer {
  font-size: 11pt;
  background: #ecfdf5;
  border-left: 3px solid #059669;
  padding: 1.5mm 3mm;
  margin: 1.5mm 0;
}

.paper .scheme {
  border-collapse: collapse;
  width: 100%;
  font-size: 9.5pt;
}

.paper .scheme td {
  border-bottom: 0.5px solid #ddd;
  padding: 0.8mm 2mm;
  vertical-align: top;
}

.paper .scheme td.code {
  width: 12mm;
  font-weight: 700;
  color: #7c2d12;
}

/* ---------------------------------------------------------------- modal */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 50;
}

.modal.hidden { display: none; }

.modal-card {
  width: 100%;
  max-width: 440px;
  background: #fff;
  border-radius: 14px;
  padding: 26px;
  box-shadow: 0 30px 80px rgba(15, 23, 42, 0.35);
}

.modal-card h2 {
  margin: 0 0 6px;
  font-size: 22px;
  letter-spacing: -0.5px;
}

.modal-card p {
  color: var(--muted);
  font-size: 13px;
  margin: 0 0 16px;
}

.modal-card label {
  display: block;
  font-size: 10.5px;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #64748b;
  margin-bottom: 6px;
}

.modal-card input {
  width: 100%;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  padding: 10px;
  font-family: Consolas, "Courier New", monospace;
  font-size: 12px;
}

.modal-actions {
  display: flex;
  gap: 8px;
  margin-top: 14px;
}

.modal-actions button {
  border: 1px solid #cbd5e1;
  background: #f8fafc;
  color: #1e293b;
  border-radius: 7px;
  padding: 9px 13px;
  font-weight: 700;
  font-size: 12px;
}

.modal-actions button.primary {
  width: auto;
  flex: 1;
  border-color: var(--blue);
  background: var(--blue);
  color: #fff;
  box-shadow: none;
}

.modal-msg {
  color: #b91c1c !important;
  font-size: 12px !important;
  margin: 10px 0 0 !important;
  min-height: 16px;
}

.modal-card .small {
  font-size: 11.5px;
  margin: 14px 0 0;
  color: #7b8aa0;
}

.modal-card .small a { color: var(--blue); font-weight: 700; }

/* ---------------------------------------------------------------- responsive */

@media (max-width: 1120px) {
  .layout { grid-template-columns: 1fr; }
  .controls { position: static; max-height: none; }
  .topics { grid-template-columns: 1fr 1fr; max-height: 230px; }
  .paper { width: 100%; min-height: 0; padding: 10mm 8mm; }
}

@media (max-width: 620px) {
  .app-header { padding: 0 12px; }
  .app-header .offline { display: none; }
  .layout { padding: 12px; gap: 12px; }
  .topics { grid-template-columns: 1fr; }
  .preview-toolbar { flex-wrap: wrap; }
  .preview-toolbar .tools { margin-left: 0; width: 100%; }
  .paper { padding: 7mm 6mm; font-size: 10pt; }
  .paper h1 { font-size: 16pt; }
}

/* ---------------------------------------------------------------- print */

@page {
  size: A4;
  margin: 15mm 14mm 14mm;
}

@media print {
  html, body { background: #fff !important; }

  .app-header,
  .controls,
  .preview-toolbar,
  .modal {
    display: none !important;
  }

  .layout {
    display: block;
    max-width: none;
    margin: 0;
    padding: 0;
  }

  .preview-wrap { padding: 0; margin: 0; }

  .paper {
    width: auto;
    min-height: 0;
    margin: 0;
    padding: 0;
    border: 0;
    box-shadow: none;
  }

  .paper .q,
  .paper .answers > li {
    page-break-inside: avoid;
    break-inside: avoid;
  }
}
