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

:root {
  --primary: #1a56db;
  --primary-h: #1444b0;
  --primary-light: #dbeafe;
  --accent: #0284c7;
  --success: #15803d;
  --danger: #b91c1c;
  --bg: #f0f4fa;
  --surface: #ffffff;
  --border: #d1d9e6;
  --text: #1e293b;
  --muted: #475569;
  --radius: 16px;
  --shadow: 0 2px 12px rgba(0, 0, 0, .06), 0 1px 3px rgba(0, 0, 0, .04);
  --shadow-lg: 0 8px 32px rgba(0, 0, 0, .1);
}

html {
  scroll-behavior: smooth;
}

body {
  font-family: 'Source Sans 3', 'Segoe UI', Arial, sans-serif;
  font-size: 17px;
  line-height: 1.65;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

/* ── Header ───────────────────────────────────────────────── */
header {
  background: linear-gradient(135deg, #0f2a6e 0%, #1a56db 50%, #2563eb 100%);
  color: #fff;
  padding: 1.4rem 2rem;
  display: flex;
  align-items: center;
  gap: 1rem;
  box-shadow: 0 4px 24px rgba(15, 42, 110, .35);
  position: sticky;
  top: 0;
  z-index: 100;
}

header .logo {
  width: 44px;
  height: 44px;
  background: rgba(255, 255, 255, .18);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  font-weight: 800;
  flex-shrink: 0;
  font-family: 'Georgia', serif;
  letter-spacing: -1px;
}

header h1 {
  font-size: 1.45rem;
  font-weight: 800;
  letter-spacing: -.5px;
}

header p {
  font-size: .92rem;
  opacity: .85;
  font-weight: 400;
}

/* ── Layout ───────────────────────────────────────────────── */
main {
  max-width: 880px;
  margin: 1.75rem auto;
  padding: 0 1.25rem 5rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

/* ── Cards ────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.75rem 2rem;
  border: 1px solid rgba(0, 0, 0, .05);
  transition: box-shadow .25s ease;
}

.card:hover {
  box-shadow: 0 4px 20px rgba(0, 0, 0, .08);
}

.card-title {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--primary);
  margin-bottom: 1.2rem;
  display: flex;
  align-items: center;
  gap: .6rem;
}

.card-title svg {
  flex-shrink: 0;
}

/* ── Form Elements ────────────────────────────────────────── */
label {
  display: block;
  font-weight: 700;
  font-size: .95rem;
  margin-bottom: .4rem;
  color: var(--text);
}

label .hint {
  font-weight: 400;
  color: var(--muted);
  font-size: .82rem;
  margin-left: .35rem;
}

input[type="text"],
input[type="password"],
textarea,
select {
  width: 100%;
  padding: .8rem 1rem;
  border: 2px solid var(--border);
  border-radius: 10px;
  font-size: 1rem;
  font-family: inherit;
  color: var(--text);
  background: #fafbfc;
  transition: border-color .2s, box-shadow .2s, background .2s;
  outline: none;
  min-height: 50px;
}

input:focus,
textarea:focus,
select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 4px rgba(26, 86, 219, .12);
  background: #fff;
}

textarea {
  resize: vertical;
  min-height: 90px;
}

.form-group {
  display: flex;
  flex-direction: column;
  margin-bottom: .85rem;
}

/* ── API Key row ──────────────────────────────────────────── */
.api-row {
  display: flex;
  gap: .5rem;
  align-items: flex-end;
}

.api-row input {
  flex: 1;
}

.api-row button {
  white-space: nowrap;
  padding: .8rem 1.2rem;
  font-size: .9rem;
}

.api-saved {
  font-size: .85rem;
  color: var(--success);
  margin-top: .35rem;
  display: none;
  font-weight: 600;
}

.api-saved.visible {
  display: block;
}

/* ── Material Type Cards ──────────────────────────────────── */
.type-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(155px, 1fr));
  gap: .75rem;
  margin-top: .3rem;
}

.type-option {
  position: relative;
}

.type-option input[type="checkbox"],
.type-option input[type="radio"] {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.type-option label {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: .4rem;
  padding: 1rem .7rem;
  border: 2.5px solid var(--border);
  border-radius: 12px;
  cursor: pointer;
  font-weight: 700;
  font-size: .88rem;
  transition: all .2s ease;
  line-height: 1.3;
  background: #fafbfc;
  min-height: 110px;
  justify-content: center;
  position: relative;
}

.type-option label .icon {
  font-size: 1.8rem;
}

.type-option input:checked+label {
  border-color: var(--primary);
  background: var(--primary-light);
  color: var(--primary);
  box-shadow: 0 0 0 3px rgba(26, 86, 219, .12);
}

.type-option input:checked+label::after {
  content: '\2713';
  position: absolute;
  top: 6px;
  right: 8px;
  width: 22px;
  height: 22px;
  background: var(--primary);
  color: #fff;
  border-radius: 50%;
  font-size: .7rem;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: pop-in .2s ease;
}

@keyframes pop-in {
  0% {
    transform: scale(0);
  }

  80% {
    transform: scale(1.15);
  }

  100% {
    transform: scale(1);
  }
}

.type-option label:hover {
  border-color: var(--accent);
  background: #f0f9ff;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, .06);
}

.type-desc {
  font-size: .72rem;
  font-weight: 400;
  color: var(--muted);
  line-height: 1.3;
  margin-top: .15rem;
}

.type-option input:checked+label .type-desc {
  color: var(--primary);
}

/* ── Goal Chips (multi-select) ────────────────────────────── */
.goal-chips {
  display: flex;
  flex-wrap: wrap;
  gap: .55rem;
  margin-top: .3rem;
}

.goal-chip {
  position: relative;
}

.goal-chip input[type="checkbox"] {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.goal-chip label {
  display: inline-flex;
  align-items: center;
  gap: .35rem;
  padding: .5rem 1rem;
  border: 2px solid var(--border);
  border-radius: 24px;
  cursor: pointer;
  font-size: .88rem;
  font-weight: 500;
  background: #fafbfc;
  color: var(--text);
  transition: all .2s ease;
  white-space: nowrap;
  margin-bottom: 0;
  min-height: 42px;
}

.goal-chip input[type="checkbox"]:checked+label {
  border-color: var(--primary);
  background: var(--primary-light);
  color: var(--primary);
  font-weight: 700;
}

.goal-chip label:hover {
  border-color: var(--accent);
  background: #f0f9ff;
}

/* ── Section divider ──────────────────────────────────────── */
.section-divider {
  border: none;
  border-top: 2px dashed #e2e8f0;
  margin: 1.5rem 0 1.2rem;
}

.section-label {
  font-size: .82rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--muted);
  margin-bottom: .9rem;
}

/* ── Form Steps ───────────────────────────────────────────── */
.form-step {
  display: flex;
  align-items: center;
  gap: .75rem;
  font-size: 1.1rem;
  font-weight: 800;
  color: var(--text);
  margin-bottom: 1.1rem;
}

.step-num {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  font-size: .95rem;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.step-divider {
  border: none;
  border-top: 2px solid #e2e8f0;
  margin: 1.75rem 0 1.5rem;
}

/* ── Group preset chips ───────────────────────────────────── */
.preset-chips {
  display: flex;
  flex-wrap: wrap;
  gap: .45rem;
  margin-bottom: .6rem;
}

.preset-chip {
  padding: .4rem .9rem;
  border: 2px solid var(--border);
  border-radius: 16px;
  background: #f1f5f9;
  font-size: .85rem;
  cursor: pointer;
  color: var(--muted);
  font-weight: 600;
  font-family: inherit;
  transition: all .15s ease;
  min-height: 40px;
  display: inline-flex;
  align-items: center;
}

.preset-chip:hover,
.preset-chip.active {
  background: var(--primary-light);
  border-color: var(--primary);
  color: var(--primary);
  font-weight: 700;
}

/* ── Level chips ──────────────────────────────────────────── */
.level-chips {
  display: flex;
  flex-wrap: wrap;
  gap: .55rem;
  margin-top: .3rem;
}

.level-chip {
  position: relative;
}

.level-chip input[type="radio"] {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.level-chip label {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  padding: .7rem 1.2rem;
  border: 2.5px solid var(--border);
  border-radius: 12px;
  cursor: pointer;
  font-size: 1.05rem;
  font-weight: 800;
  background: #fafbfc;
  color: var(--text);
  transition: all .2s ease;
  min-width: 72px;
  text-align: center;
  margin-bottom: 0;
  min-height: 60px;
  justify-content: center;
}

.level-chip input[type="radio"]:checked+label {
  border-color: var(--primary);
  background: var(--primary-light);
  color: var(--primary);
  box-shadow: 0 0 0 3px rgba(26, 86, 219, .1);
}

.level-chip label:hover {
  border-color: var(--accent);
  background: #f0f9ff;
  transform: translateY(-1px);
}

.sub-lbl {
  font-size: .7rem;
  font-weight: 500;
  color: var(--muted);
  margin-top: .15rem;
}

.level-chip input[type="radio"]:checked+label .sub-lbl,
.dur-chip input[type="radio"]:checked+label .sub-lbl {
  color: #60a5fa;
}

/* ── Duration chips ───────────────────────────────────────── */
.dur-chips {
  display: flex;
  flex-wrap: wrap;
  gap: .55rem;
  margin-top: .3rem;
}

.dur-chip {
  position: relative;
}

.dur-chip input[type="radio"] {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.dur-chip label {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  padding: .7rem 1.1rem;
  border: 2.5px solid var(--border);
  border-radius: 12px;
  cursor: pointer;
  font-size: .95rem;
  font-weight: 700;
  background: #fafbfc;
  color: var(--text);
  transition: all .2s ease;
  min-width: 95px;
  text-align: center;
  margin-bottom: 0;
  min-height: 60px;
  justify-content: center;
}

.dur-chip input[type="radio"]:checked+label {
  border-color: var(--primary);
  background: var(--primary-light);
  color: var(--primary);
  box-shadow: 0 0 0 3px rgba(26, 86, 219, .1);
}

.dur-chip label:hover {
  border-color: var(--accent);
  background: #f0f9ff;
  transform: translateY(-1px);
}

/* ── Writing lines in output ──────────────────────────────── */
#material-output .write-line {
  border-bottom: 1.5px solid var(--border);
  min-height: 28px;
  margin: 5px 0 8px;
  display: block;
}

/* ── Buttons ──────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .5rem;
  padding: .85rem 1.5rem;
  border: none;
  border-radius: 10px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  transition: all .2s ease;
  font-family: inherit;
  text-decoration: none;
  min-height: 50px;
}

.btn:active {
  transform: scale(.97);
}

.btn-primary {
  background: linear-gradient(135deg, var(--primary) 0%, #2563eb 100%);
  color: #fff;
  box-shadow: 0 2px 8px rgba(26, 86, 219, .2);
}

.btn-primary:hover:not(:disabled) {
  background: linear-gradient(135deg, var(--primary-h) 0%, #1a56db 100%);
  box-shadow: 0 4px 18px rgba(26, 86, 219, .4);
  transform: translateY(-1px);
}

/* ── Section break divider between material types ──────── */
hr.section-break {
  border: none;
  border-top: 3px solid #c7d2fe;
  margin: 2.5rem 0;
}

.btn-secondary {
  background: #e2e8f0;
  color: var(--text);
}

.btn-secondary:hover:not(:disabled) {
  background: #cbd5e1;
}

.btn-success {
  background: var(--success);
  color: #fff;
}

.btn-success:hover:not(:disabled) {
  background: #166534;
}

.btn:disabled {
  opacity: .5;
  cursor: not-allowed;
}

.btn-lg {
  padding: 1rem 2.5rem;
  font-size: 1.15rem;
  min-height: 58px;
  border-radius: 12px;
}

.generate-row {
  display: flex;
  justify-content: center;
  margin-top: .75rem;
}

/* ── Spinner & Progress ───────────────────────────────────── */
.spinner {
  width: 22px;
  height: 22px;
  border: 3px solid rgba(255, 255, 255, .35);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin .7s linear infinite;
  display: none;
}

.loading .spinner {
  display: inline-block;
}

.loading .btn-text {
  display: none;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.progress-bar-container {
  display: none;
  margin-top: 1rem;
  background: #e2e8f0;
  border-radius: 8px;
  height: 8px;
  overflow: hidden;
}

.progress-bar-container.visible {
  display: block;
}

.progress-bar {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), var(--accent));
  border-radius: 8px;
  width: 0%;
  transition: width .4s ease;
  animation: progress-pulse 2s ease-in-out infinite;
}

@keyframes progress-pulse {

  0%,
  100% {
    opacity: 1;
  }

  50% {
    opacity: .7;
  }
}

.loading-message {
  display: none;
  text-align: center;
  font-size: .9rem;
  color: var(--muted);
  margin-top: .6rem;
  font-weight: 500;
}

.loading-message.visible {
  display: block;
}

/* ── Result Section ───────────────────────────────────────── */
#result-card {
  display: none;
}

.result-toolbar {
  display: flex;
  gap: .65rem;
  flex-wrap: wrap;
  margin-bottom: 1.2rem;
}

#material-output {
  border: 2px solid var(--border);
  border-radius: 10px;
  padding: 1.75rem;
  font-size: 1rem;
  line-height: 1.8;
  min-height: 200px;
  background: #fdfdfd;
  white-space: pre-wrap;
  font-family: 'Source Sans 3', 'Segoe UI', Arial, sans-serif;
}

#material-output h2 {
  font-size: 1.15rem;
  color: var(--primary);
  margin: 1.1rem 0 .45rem;
  padding-bottom: .25rem;
  border-bottom: 2.5px solid #dbeafe;
}

#material-output h3 {
  font-size: 1.05rem;
  color: #374151;
  margin: .85rem 0 .3rem;
}

#material-output p {
  margin: .45rem 0;
}

#material-output ul,
#material-output ol {
  padding-left: 1.5rem;
  margin: .45rem 0;
}

#material-output li {
  margin: .3rem 0;
}

#material-output strong {
  color: var(--primary-h);
}

#material-output .word-bank {
  background: #f0f9ff;
  border: 1.5px solid #bae6fd;
  border-radius: 8px;
  padding: .7rem 1.1rem;
  margin: .8rem 0;
}

#material-output table {
  border-collapse: collapse;
  width: 100%;
  margin: .8rem 0;
}

#material-output th,
#material-output td {
  border: 1.5px solid var(--border);
  padding: .55rem .85rem;
  text-align: left;
}

#material-output th {
  background: #eff6ff;
  font-weight: 700;
}

#material-output hr {
  border: none;
  border-top: 2px solid #e2e8f0;
  margin: 1.1rem 0;
}

/* ── Error ────────────────────────────────────────────────── */
.error-box {
  background: #fef2f2;
  border: 2px solid #fca5a5;
  border-radius: 10px;
  padding: 1.1rem 1.4rem;
  color: var(--danger);
  font-size: .95rem;
  font-weight: 600;
  display: none;
}

.error-box.visible {
  display: block;
}

/* ── Help Tooltip ─────────────────────────────────────────── */
.help-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #e2e8f0;
  color: var(--muted);
  font-size: .75rem;
  font-weight: 800;
  cursor: help;
  margin-left: .4rem;
  position: relative;
  vertical-align: middle;
  transition: background .15s;
  flex-shrink: 0;
}

.help-icon:hover {
  background: var(--primary-light);
  color: var(--primary);
}

.help-icon .tooltip {
  display: none;
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  background: #1e293b;
  color: #fff;
  font-size: .82rem;
  font-weight: 400;
  padding: .65rem .9rem;
  border-radius: 8px;
  white-space: normal;
  width: 240px;
  line-height: 1.4;
  z-index: 50;
  box-shadow: 0 4px 16px rgba(0, 0, 0, .2);
}

.help-icon .tooltip::after {
  content: '';
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  border: 6px solid transparent;
  border-top-color: #1e293b;
}

.help-icon:hover .tooltip,
.help-icon:focus .tooltip,
.help-icon.active .tooltip {
  display: block;
}

/* ── Back to top ──────────────────────────────────────────── */
.back-to-top {
  position: fixed;
  bottom: 2rem;
  right: 2rem;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  border: none;
  cursor: pointer;
  font-size: 1.3rem;
  display: none;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 16px rgba(26, 86, 219, .35);
  z-index: 90;
  transition: opacity .2s, transform .2s;
}

.back-to-top:hover {
  transform: translateY(-2px);
}

.back-to-top.visible {
  display: flex;
}

/* ── Footer ───────────────────────────────────────────────── */
footer {
  text-align: center;
  color: var(--muted);
  font-size: .82rem;
  padding: 1.2rem;
}

/* ── Mobile ───────────────────────────────────────────────── */
@media (max-width: 600px) {
  header {
    padding: 1.1rem 1rem;
  }

  header h1 {
    font-size: 1.2rem;
  }

  main {
    padding: 0 .75rem 4rem;
  }

  .card {
    padding: 1.25rem 1.1rem;
  }

  .type-grid {
    grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
  }

  .btn-lg {
    width: 100%;
  }
}

/* ══ PRINT STYLES – A4 ═════════════════════════════════════ */
@media print {

  /*
   * margin: 0 is the ONLY reliable way to suppress the browser's
   * built-in URL, date, and title headers/footers.
   * Content margins come from #material-output padding instead.
   */
  @page {
    size: A4 portrait;
    margin: 0;
  }

  body {
    background: #fff;
    font-size: 11.5pt;
    color: #000;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
    margin: 0;
    padding: 0;
  }

  header,
  footer,
  .card:not(#result-card),
  .result-toolbar,
  #error-box,
  .api-saved,
  .back-to-top,
  .progress-bar-container,
  .loading-message {
    display: none !important;
  }

  main {
    margin: 0;
    padding: 0;
    max-width: none;
  }

  #result-card {
    box-shadow: none;
    padding: 0;
    border: none;
    display: block !important;
  }

  .card-title {
    display: none !important;
  }

  #material-output {
    border: none;
    /* These paddings act as page margins since @page margin is 0 */
    padding: 1.8cm 2.2cm;
    font-size: 11.5pt;
    line-height: 2;
    white-space: normal;
  }

  /*
   * .print-keep wraps each heading + its first content sibling
   * so they physically can't be split across pages.
   */
  .print-keep {
    break-inside: avoid;
    page-break-inside: avoid;
  }

  /* ── Headings ── */
  #material-output h2 {
    font-size: 15pt;
    font-weight: 800;
    color: #000;
    border-bottom: 1.5pt solid #333;
    padding-top: 16pt;
    padding-bottom: 6pt;
    margin: 22pt 0 12pt;
    /* Every h2 section starts on a new page */
    break-before: page;
    page-break-before: always;
    break-after: avoid;
    page-break-after: avoid;
  }

  /* First h2 must NOT force a page break */
  #material-output h2:first-child,
  .print-keep:first-child>h2 {
    break-before: auto;
    page-break-before: auto;
    margin-top: 0;
    padding-top: 0;
  }

  #material-output h3 {
    font-size: 12.5pt;
    font-weight: 700;
    color: #111;
    margin: 20pt 0 8pt;
    padding-top: 6pt;
    break-after: avoid;
    page-break-after: avoid;
  }

  /* ── Paragraphs ── */
  #material-output p {
    margin: 6pt 0;
    orphans: 3;
    widows: 3;
  }

  #material-output strong {
    color: #000;
  }

  #material-output em {
    font-style: italic;
  }

  /* ── Horizontal rules ── */
  #material-output hr {
    border: none;
    border-top: .75pt solid #bbb;
    margin: 16pt 0;
  }

  /* ── Lists ── */
  #material-output ul,
  #material-output ol {
    padding-left: 1.2cm;
    margin: 8pt 0;
    page-break-inside: avoid;
    break-inside: avoid;
  }

  #material-output li {
    margin: 5pt 0;
    line-height: 2;
  }

  /* ── Tables ── */
  #material-output table {
    width: 100%;
    border-collapse: collapse;
    font-size: 10.5pt;
    margin: 12pt 0;
    page-break-inside: avoid;
    break-inside: avoid;
  }

  #material-output th,
  #material-output td {
    border: .75pt solid #aaa;
    padding: 6pt 8pt;
    vertical-align: top;
    line-height: 1.7;
  }

  #material-output th {
    background: #f0f0f0 !important;
    font-weight: 700;
  }

  /* ── Write lines ── */
  #material-output .write-line {
    border-bottom: 1pt solid #888;
    min-height: 1cm;
    margin: 6pt 0;
    display: block;
  }

  /* ── Word bank ── */
  #material-output .word-bank {
    margin: 10pt 0;
    padding: 10pt 14pt;
    page-break-inside: avoid;
    break-inside: avoid;
  }

  /* ── Image placeholder ── */
  #material-output .img-placeholder {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 72px;
    height: 56px;
    border: 2px dashed #bbb;
    border-radius: 6px;
    font-size: 1.4rem;
    color: #bbb;
    background: #f8f8f8;
  }

  a {
    text-decoration: none;
    color: #000;
  }

  /* ── Multi-type section break ── */
  hr.section-break {
    border: none;
    border-top: 2pt solid #333;
    margin: 0;
    padding: 0;
    page-break-before: always;
    break-before: page;
  }
}

/* ── Template bar ────────────────────────────────────────── */
.template-bar {
  display: flex;
  align-items: center;
  gap: .55rem;
  flex-wrap: wrap;
  margin-bottom: 1.3rem;
  padding-bottom: 1.1rem;
  border-bottom: 2px dashed #e2e8f0;
}

.template-bar .template-select {
  flex: 1;
  min-width: 180px;
  padding: .6rem .9rem;
  border: 2px solid var(--border);
  border-radius: 10px;
  font-size: .92rem;
  font-family: inherit;
  color: var(--text);
  background: #fafbfc;
  cursor: pointer;
  min-height: 44px;
}

.template-bar .template-select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 4px rgba(26, 86, 219, .12);
  background: #fff;
  outline: none;
}

.template-bar .btn {
  padding: .55rem 1rem;
  font-size: .85rem;
  min-height: 44px;
  white-space: nowrap;
}

.template-bar .template-save-btn {
  background: #dbeafe;
  color: var(--primary);
  font-weight: 700;
  border: 2px solid var(--primary-light);
}

.template-bar .template-save-btn:hover {
  background: var(--primary-light);
  border-color: var(--primary);
}

@media (max-width: 600px) {
  .template-bar {
    flex-direction: column;
    align-items: stretch;
  }

  .template-bar .template-select {
    width: 100%;
  }

  .template-bar .btn {
    width: 100%;
  }
}

@media print {
  .template-bar {
    display: none !important;
  }
}

/* ── History panel ───────────────────────────────────────── */
#history-card {
  display: none;
}

#history-list {
  display: flex;
  flex-direction: column;
  gap: .6rem;
  margin-top: .4rem;
}

.history-entry {
  display: flex;
  align-items: center;
  gap: .75rem;
  padding: .65rem .9rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fafbfc;
  transition: background .15s;
}

.history-entry:hover {
  background: var(--primary-light);
  border-color: var(--primary);
  box-shadow: 0 2px 8px rgba(26, 86, 219, .08);
}

.history-entry-info {
  flex: 1;
  min-width: 0;
}

.history-entry-info strong {
  display: block;
  font-size: .9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-entry-info span {
  font-size: .78rem;
  color: var(--muted);
}

.history-entry .btn {
  padding: .35rem .75rem;
  font-size: .82rem;
}

.history-empty {
  color: var(--muted);
  font-size: .9rem;
  text-align: center;
  padding: 1rem 0;
}
