/* Google Fonts is loaded from a <link> in each page's <head>, not an
   @import here -- @import inside a stylesheet only starts fetching after
   styles.css itself has finished downloading and parsing, adding a full
   extra round-trip before fonts even begin loading. A <link> in <head>
   fetches in parallel with styles.css instead. */

:root {
  --blue: #1ea7e8;
  --blue-dark: #0f7fb8;
  --pink: #f7abc7;
  --pink-light: #fbeef4;
  --ink: #22252b;
  --ink-soft: #6b7078;
  --bg: #ffffff;
  --bg-muted: #f5f6f8;
  --card: #ffffff;
  --border: #e2e4e9;
  --success: #2fb380;
  --danger: #d6415a;
  --font-display: 'Lilita One', system-ui, sans-serif;
  --font-body: 'Inter', system-ui, -apple-system, sans-serif;
  --radius: 10px;
  --radius-sm: 6px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.5;
}

/* Sticky footer: every page's <body> is header + main + footer, so growing
   main to fill leftover height keeps the footer pinned to the bottom on
   short pages (like privacy.html) without affecting taller ones. */
body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

body > main { flex: 1 0 auto; }
body > .site-footer { flex-shrink: 0; }

img { max-width: 100%; display: block; }

a { color: var(--blue-dark); }

button, input, select, textarea { font-family: inherit; }

.container {
  width: 100%;
  max-width: 1140px;
  margin: 0 auto;
  padding: 0 20px;
}

.mascot { display: block; }

/* Header */
.site-header {
  position: sticky;
  top: 0;
  z-index: 30;
  background: #fff;
  border-bottom: 1px solid var(--border);
}

.site-header .container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: 12px;
  padding-bottom: 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  text-decoration: none;
  color: var(--ink);
}

.brand .mascot { width: 30px; height: 30px; }

.brand-name {
  font-family: var(--font-display);
  font-size: 18px;
  color: var(--blue);
  letter-spacing: 0.3px;
}

/* Hero -- big blue band, search bar overlaps its bottom edge on desktop */
.hero {
  background: var(--blue);
  padding: 40px 0 68px;
  text-align: center;
}

.hero h1 {
  font-family: var(--font-body);
  font-weight: 800;
  font-size: clamp(24px, 4vw, 34px);
  color: #fff;
  margin: 0 0 8px;
  line-height: 1.2;
  letter-spacing: -0.01em;
}

.hero h1 span { color: #fff; }

.hero p {
  color: rgba(255, 255, 255, 0.85);
  max-width: 540px;
  margin: 0 auto;
  font-size: 16px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 11px 20px;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

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

/* Bouncing "..." shown on buttons while they work. */
.dots span {
  display: inline-block;
  animation: dot-bounce 1s ease-in-out infinite;
}
.dots span:nth-child(2) { animation-delay: 0.15s; }
.dots span:nth-child(3) { animation-delay: 0.3s; }
@keyframes dot-bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-0.4em); }
}
@media (prefers-reduced-motion: reduce) {
  .dots span { animation: none; }
}

.btn-primary { background: var(--blue); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--blue-dark); }

.btn-outline { background: #fff; color: var(--ink); border-color: var(--border); }
.btn-outline:hover:not(:disabled) { border-color: var(--ink); }

.btn-ghost { background: transparent; color: var(--ink-soft); padding: 10px 14px; }
.btn-ghost:hover:not(:disabled) { color: var(--ink); }

.btn-sm { padding: 7px 14px; font-size: 13px; }
.btn-lg { padding: 13px 26px; font-size: 15px; width: 100%; }
.btn-danger { background: var(--danger); color: #fff; }

/* Filter bar */
.filter-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: flex-end;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin: -40px 0 16px;
  position: relative;
  z-index: 5;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1 1 140px;
  min-width: 120px;
}

.field-year-range { flex: 1 1 180px; }

.year-range-inputs { display: flex; align-items: center; gap: 6px; }
.year-range-inputs select { min-width: 0; flex: 1 1 0; }
.year-range-sep { color: var(--ink-soft); font-size: 13px; flex-shrink: 0; }

.field label {
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-soft);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.field input, .field select {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: #fff;
  font-size: 14px;
  color: var(--ink);
}

.field input:focus, .field select:focus {
  outline: none;
  border-color: var(--blue);
}

.field-error { color: var(--danger); font-size: 12px; min-height: 0; }
.field-error:empty { display: none; }
.field.has-error input, .field.has-error .custom-select-btn { border-color: var(--danger); }

/* Quick subject shortcuts -- the 12 most common subjects */
.quick-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 20px;
}

.chip {
  border: 1px solid var(--border);
  background: #fff;
  border-radius: var(--radius-sm);
  padding: 7px 14px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  color: var(--ink-soft);
}

.chip:hover { border-color: var(--blue); color: var(--blue-dark); }
.chip.active { background: var(--blue); border-color: var(--blue); color: #fff; }

/* Results toolbar */
.results-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.results-count { color: var(--ink-soft); font-size: 14px; }

.select-controls {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

/* Paper grid -- product-card style, image-led like a shop catalogue */
.paper-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 14px;
  margin-bottom: 24px;
}

.paper-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  position: relative;
  transition: border-color 0.15s ease;
}

.paper-card:hover { border-color: var(--ink); }

.paper-card-media {
  display: block;
  position: relative;
  aspect-ratio: 4 / 3;
  background: var(--pink-light);
}

.paper-card-media img { width: 100%; height: 100%; object-fit: cover; display: block; }

/* The mascot placeholder stays underneath; the preview fades in over it. */
.paper-card-media img.paper-thumb {
  position: absolute;
  inset: 0;
  opacity: 0;
  transition: opacity 0.25s ease;
}
.paper-card-media img.paper-thumb.loaded { opacity: 1; }

.paper-card-media-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.paper-card-media-placeholder .mascot { width: 42%; height: 42%; opacity: 0.5; }

.paper-year-tag {
  position: absolute;
  top: 8px;
  left: 8px;
  background: rgba(34, 37, 43, 0.75);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  border-radius: var(--radius-sm);
  padding: 3px 7px;
}

.paper-card .paper-check {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 18px;
  height: 18px;
  accent-color: var(--blue);
  z-index: 2;
}

.paper-card-body { padding: 10px 12px 12px; }

.paper-card-title-link { display: block; text-decoration: none; color: inherit; }

.paper-card h3 {
  margin: 0 0 10px;
  font-size: 14px;
  font-weight: 700;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  min-height: 2.6em;
}

.paper-sub {
  font-size: 13px;
  color: var(--ink-soft);
  margin: 0;
}

.paper-card-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.paper-curriculum {
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-soft);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.paper-download-btn {
  flex-shrink: 0;
  width: 34px;
  height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: var(--blue);
  color: #fff;
  text-decoration: none;
  transition: background-color 0.15s ease;
}

.paper-download-btn:hover { background: var(--blue-dark); }

.load-more-row {
  display: flex;
  justify-content: center;
  margin: 0 0 40px;
}

.empty-state {
  grid-column: 1 / -1;
  text-align: center;
  padding: 60px 20px;
  color: var(--ink-soft);
}

/* Gate modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(20, 20, 25, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px 20px;
  z-index: 100;
}

.modal {
  background: #fff;
  border-radius: var(--radius);
  padding: 20px;
  width: 100%;
  max-width: 360px;
  border: 1px solid var(--border);
  /* Tied to the backdrop's own padding so the card can never grow tall
     enough to touch the top/bottom of the viewport. */
  max-height: calc(100vh - 64px);
  overflow-y: auto;
}

.modal h2 {
  font-family: var(--font-body);
  font-weight: 800;
  color: var(--ink);
  font-size: 19px;
  margin: 0 0 4px;
}

.modal p.modal-sub { color: var(--ink-soft); margin: 0 0 12px; font-size: 13px; }

.modal .field { margin-bottom: 6px; }
.modal .field label { font-size: 11px; }
.modal .field input, .modal .custom-select-btn { padding: 8px 10px; font-size: 13px; }
/* Smaller than its siblings -- full school names run long and this is the
   field most likely to overflow or wrap awkwardly. */
#gate-school { font-size: 12px; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.form-grid .field.full { grid-column: 1 / -1; }
.gate-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 6px; }
.gate-row .field { margin-bottom: 0; }

.form-error {
  color: var(--danger);
  font-size: 13px;
  margin-top: 0;
  min-height: 0;
}
.form-error:empty { display: none; }

.modal-actions { margin-top: 8px; }

/* Custom select */
.custom-select { position: relative; }
.custom-select-btn {
  width: 100%;
  text-align: left;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%236b7078' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E") no-repeat right 10px center;
  font-size: 14px;
  color: var(--ink);
  cursor: pointer;
}
.custom-select-btn.placeholder { color: #9297a1; }
.custom-select-list {
  /* Fixed (not absolute) so it renders relative to the viewport and is
     never clipped by the modal's own overflow-y: auto -- position/width are
     set from JS (initCustomSelect) to match the button each time it opens.
     max-height + overflow-y here is what scrolls if the list is long, not
     the modal. */
  position: fixed;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  z-index: 120;
  max-height: 240px;
  overflow-y: auto;
  padding: 4px;
  display: none;
}
.custom-select-list.open { display: block; }
.custom-select-option {
  padding: 8px 10px;
  font-size: 14px;
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.custom-select-option:hover, .custom-select-option.active { background: var(--bg-muted); }

/* Footer */
.site-footer {
  border-top: 1px solid var(--border);
  background: var(--bg-muted);
  margin-top: 40px;
}

.footer-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 24px;
  justify-content: space-between;
  padding: 32px 0 16px;
}

.footer-brand { display: flex; align-items: center; gap: 10px; max-width: 320px; }
.footer-brand .mascot { width: 34px; height: 34px; flex-shrink: 0; }
.footer-brand-name { font-family: var(--font-display); color: var(--blue); font-size: 16px; }
.footer-tagline { color: var(--ink-soft); font-size: 13px; margin: 2px 0 0; }

.footer-links { display: flex; gap: 20px; }
.footer-links a { color: var(--ink-soft); font-size: 14px; text-decoration: none; }
.footer-links a:hover { color: var(--ink); text-decoration: underline; }

.footer-bottom {
  border-top: 1px solid var(--border);
  padding: 14px 0;
  text-align: center;
  color: var(--ink-soft);
  font-size: 12px;
}

.footer-bottom .hidden-trigger {
  display: inline-block;
  width: 12px;
  height: 12px;
  background: transparent;
  cursor: default;
  margin-left: 6px;
  vertical-align: middle;
}

/* Admin page */
.admin-shell { max-width: 760px; margin: 0 auto; padding: 40px 20px; }

.admin-lock {
  max-width: 360px;
  margin: 80px auto;
  text-align: center;
}

.admin-lock .field { text-align: left; margin-bottom: 16px; }

.dropzone {
  border: 1.5px dashed var(--border);
  border-radius: var(--radius);
  padding: 40px 20px;
  text-align: center;
  color: var(--ink-soft);
  cursor: pointer;
  background: #fff;
}

.dropzone.dragover { border-color: var(--blue); background: var(--pink-light); }

.review-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-top: 20px;
}

.ai-note {
  font-size: 13px;
  color: var(--ink-soft);
  background: var(--bg-muted);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  margin-bottom: 14px;
}

.admin-table { width: 100%; border-collapse: collapse; margin-top: 24px; font-size: 13px; }
.admin-table th, .admin-table td { text-align: left; padding: 8px; border-bottom: 1px solid var(--border); }
.admin-table th { color: var(--ink-soft); font-weight: 600; }

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  padding: 12px 20px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  z-index: 200;
}

/* Paper detail page */
.paper-detail {
  max-width: 900px;
  margin: 0 auto;
  padding: 32px 20px 60px;
}

.paper-detail-back { font-size: 13px; color: var(--ink-soft); text-decoration: none; display: inline-block; margin-bottom: 16px; }
.paper-detail-back:hover { color: var(--ink); }

.paper-detail-header { margin-bottom: 20px; display: flex; gap: 16px; align-items: flex-start; }
.paper-detail-thumb { width: 92px; height: 122px; object-fit: cover; border-radius: var(--radius-sm); border: 1px solid var(--border); flex-shrink: 0; }
.paper-detail-header h1 { font-size: clamp(20px, 3vw, 28px); margin: 0 0 6px; font-weight: 800; }
.paper-detail-header .paper-sub { font-size: 14px; }

.doc-viewer {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-muted);
  overflow-y: auto;
  overflow-x: hidden;
  margin-bottom: 20px;
  max-height: 80vh;
  padding: 14px;
}

.doc-page {
  display: block;
  width: 100%;
  height: auto;
  margin: 0 auto 14px;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15);
}

.doc-viewer-loading { position: relative; }
.doc-viewer-loading-img {
  display: block;
  width: 100%;
  height: auto;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15);
  opacity: 0.55;
  filter: blur(1px);
}
.doc-viewer-loading-blank { height: 260px; }
.doc-viewer-loading .doc-viewer-status {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  font-weight: 600;
  color: var(--ink);
  text-shadow: 0 0 8px #fff, 0 0 8px #fff;
}

.doc-viewer-status {
  text-align: center;
  padding: 50px 16px;
  color: var(--ink-soft);
  font-size: 14px;
}

.paper-detail-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 16px;
}

.paper-detail-actions .btn { flex: 1 1 140px; }

.download-all-box {
  border-top: 1px solid var(--border);
  padding-top: 16px;
}

@media (max-width: 640px) {
  .form-grid { grid-template-columns: 1fr; }

  /* Phone stays side-by-side with the grade dropdown even on small screens.
     16px input font-size (not the desktop modal's 13px) is required here --
     anything smaller makes iOS Safari auto-zoom the whole page on focus. */
  .modal .field input, .modal .custom-select-btn { font-size: 16px; padding: 10px 8px; }
  #gate-phone { letter-spacing: -0.02em; }
  /* #gate-school's own 12px (an ID selector) would otherwise outrank the
     16px rule above despite coming first -- restore it here so iOS Safari
     still doesn't auto-zoom the page when this field gets focus. */
  #gate-school { font-size: 16px; }

  /* Blue band shrinks and the search bar drops below it (no overlap) --
     a positive top margin here, not 0, so the white filter card doesn't
     sit flush against the blue hero. */
  .hero { padding: 22px 0 18px; text-align: left; }
  .hero p { margin: 0; }
  .filter-bar { flex-direction: column; align-items: stretch; margin: 14px 0 16px; }
  .field { min-width: 0; flex: 1 1 auto; }

  .results-toolbar { flex-direction: column; align-items: stretch; }

  /* Two-column shop grid -- this is where most of the traffic is */
  .paper-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .paper-card-body { padding: 8px 10px 10px; }
  .paper-card h3 { font-size: 13px; min-height: 2.4em; }
  .paper-curriculum { font-size: 11px; }
  .paper-download-btn { width: 30px; height: 30px; }

  .paper-detail-thumb { width: 72px; height: 96px; }

  .footer-grid { flex-direction: column; }
  .doc-viewer { max-height: 60vh; padding: 10px; }
}
