:root {
  --ink: #172336;
  --ink-soft: #657287;
  --navy: #132a46;
  --navy-2: #1d416b;
  --blue: #2a65a9;
  --red: #d8493f;
  --paper: #ffffff;
  --canvas: #f3f1eb;
  --sand: #ded8cc;
  --line: #d8d2c7;
  --mist: #edf1f5;
  --green: #248663;
  --amber: #b86b0e;
  --shadow: 0 18px 46px rgba(19, 42, 70, .09);
  --radius: 20px;
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  color: var(--ink);
  background:
    linear-gradient(90deg, transparent 0 23px, rgba(42, 101, 169, .035) 24px, transparent 25px) 0 0 / 48px 48px,
    var(--canvas);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
}

button, input, select { font: inherit; }
button, select { cursor: pointer; }

button:focus-visible, input:focus-visible, select:focus-visible, a:focus-visible {
  outline: 3px solid rgba(42, 101, 169, .3);
  outline-offset: 2px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
}

.app-header {
  min-height: 96px;
  padding: 18px 30px;
  color: white;
  background: var(--navy);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  border-bottom: 4px solid var(--red);
}

.brand-lockup, .header-actions, .course-bar, .result-toolbar, .basket-heading,
.viewer-header, .site-footer, .footer-contact { display: flex; align-items: center; }

.brand-lockup { gap: 14px; }
.brand-mark { width: 60px; height: 60px; flex: 0 0 auto; }
.brand-mark svg { display: block; width: 100%; height: 100%; }
.brand-eyebrow, .basket-kicker, .viewer-tag {
  font-family: "Avenir Next Condensed", "Arial Narrow", sans-serif;
  font-weight: 700;
  letter-spacing: .14em;
  font-size: 11px;
  color: #a9c7e8;
}
.app-header h1 {
  margin: 1px 0 0;
  font-family: "Avenir Next", "PingFang SC", sans-serif;
  font-size: 31px;
  line-height: 1;
  letter-spacing: .02em;
}

.header-actions { gap: 10px; }
.header-meta { display: grid; justify-items: end; gap: 9px; margin-left: auto; }
.beta-badge, .quiet-button {
  border: 1px solid rgba(255, 255, 255, .22);
  border-radius: 999px;
  padding: 9px 14px;
  color: white;
  background: rgba(255, 255, 255, .08);
  font-size: 13px;
}
.quiet-button:hover { background: rgba(255, 255, 255, .16); }
.header-contact { display: flex; align-items: center; justify-content: flex-end; gap: 16px; }
.header-contact a, .header-contact span { color: #b8c9d9; font-size: 12px; text-decoration: none; white-space: nowrap; }
.header-contact a:hover { color: white; }

.course-bar {
  height: 58px;
  padding: 0 30px;
  gap: 8px;
  background: #fff;
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 30;
}
.course-label { margin-right: 8px; color: var(--ink-soft); font-weight: 650; }
.course-tab {
  min-width: 172px;
  padding: 10px 22px;
  border: 0;
  border-radius: 11px;
  background: var(--mist);
  color: var(--ink);
  font-weight: 700;
}
.course-tab.active { color: white; background: var(--blue); box-shadow: 0 8px 18px rgba(42, 101, 169, .2); }
.offline-status { margin-left: auto; color: var(--green); font-size: 13px; }

.filter-shell {
  display: grid;
  grid-template-columns: minmax(280px, 1.5fr) repeat(5, minmax(140px, .7fr)) auto;
  gap: 10px;
  padding: 16px 30px;
  background: rgba(255, 255, 255, .87);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 58px;
  z-index: 25;
}

.search-box {
  height: 46px;
  border: 1px solid #cbc4b8;
  border-radius: 13px;
  background: white;
  display: flex;
  align-items: center;
  padding: 0 13px;
  gap: 9px;
}
.search-box:focus-within { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(42, 101, 169, .1); }
.search-box svg { width: 18px; fill: none; stroke: var(--ink-soft); stroke-width: 2; }
.search-box input { min-width: 0; flex: 1; border: 0; outline: 0; color: var(--ink); background: transparent; }
.search-box kbd { color: #8a94a3; background: #f3f5f7; border: 1px solid #dfe3e8; border-radius: 6px; padding: 3px 7px; font-size: 11px; }

.filter-shell select, .sort-control select {
  min-width: 0;
  height: 46px;
  padding: 0 34px 0 13px;
  border: 1px solid #cbc4b8;
  border-radius: 13px;
  color: var(--ink);
  background: white;
}
.reset-button {
  padding: 0 14px;
  border: 0;
  color: var(--blue);
  background: transparent;
  font-weight: 700;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 390px;
  gap: 22px;
  align-items: start;
  padding: 22px 30px 42px;
  max-width: 1880px;
  margin: 0 auto;
}

.result-toolbar { min-height: 46px; justify-content: space-between; margin-bottom: 12px; }
.result-toolbar p { margin: 0; color: var(--ink-soft); font-weight: 650; }
.sort-control { display: flex; align-items: center; gap: 8px; color: var(--ink-soft); font-size: 13px; }
.sort-control select { height: 38px; }

.question-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 20px; }
.question-card {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper);
  box-shadow: 0 2px 0 rgba(19, 42, 70, .025);
  display: flex;
  flex-direction: column;
  min-width: 0;
  transition: transform .16s ease, box-shadow .16s ease, border-color .16s ease;
}
.question-card:hover { transform: translateY(-2px); border-color: #b9c9da; box-shadow: var(--shadow); }
.card-head { padding: 20px 20px 15px; border-bottom: 1px solid #e4dfd5; }
.card-title-row { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; }
.card-title-row h2 { margin: 0 auto 0 0; font-size: 20px; letter-spacing: -.02em; }
.pill { border-radius: 999px; padding: 6px 10px; font-size: 12px; font-weight: 750; }
.pill.marks { color: #334052; background: #edf1f5; }
.pill.easy { color: #187153; background: #dff2e9; }
.pill.medium { color: #97590e; background: #f9ead4; }
.pill.hard { color: #a53a3a; background: #f8dfdf; }
.topic-line { margin: 14px 0 4px; font-size: 17px; font-weight: 800; }
.subtopic-line { margin: 0; color: #34628f; font-weight: 700; line-height: 1.45; min-height: 42px; }
.source-line { margin: 5px 0 0; color: var(--ink-soft); font-size: 13px; }
.question-image-wrap {
  position: relative;
  width: 100%;
  padding: 0;
  border: 0;
  background: #f7f6f1;
  border-bottom: 1px solid #e4dfd5;
  min-height: 190px;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  overflow: visible;
  color: inherit;
  text-align: left;
}
.question-image-wrap:hover .question-image { filter: brightness(.985); }
.question-image-wrap:focus-visible { outline: 4px solid rgba(42, 101, 169, .28); outline-offset: -4px; }
.question-image { display: block; width: 100%; height: auto; background: white; }
.question-image-wrap.image-load-failed { background: #fff5f2; }
.question-image.image-load-failed { min-height: 180px; object-fit: contain; color: #a23d36; }
.structured-card .question-image-wrap {
  display: block;
  height: 470px;
  min-height: 470px;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
}
.structured-card .question-image { width: 100%; max-width: none; }
.structured-card .question-image-wrap::-webkit-scrollbar { width: 10px; }
.structured-card .question-image-wrap::-webkit-scrollbar-track { background: #ece9e1; }
.structured-card .question-image-wrap::-webkit-scrollbar-thumb { border: 2px solid #ece9e1; border-radius: 999px; background: #9aa9b8; }
.question-image-hint {
  position: absolute;
  top: 12px;
  right: 12px;
  padding: 7px 10px;
  border-radius: 999px;
  color: white;
  background: rgba(19, 42, 70, .88);
  box-shadow: 0 5px 16px rgba(19, 42, 70, .18);
  font-size: 12px;
  font-weight: 750;
  opacity: 0;
  transform: translateY(-3px);
  transition: opacity .16s ease, transform .16s ease;
}
.question-image-wrap:hover .question-image-hint,
.question-image-wrap:focus-visible .question-image-hint { opacity: 1; transform: translateY(0); }
.card-actions { display: grid; grid-template-columns: 1.2fr 1fr; gap: 10px; padding: 14px; margin-top: auto; }
.card-actions button, .basket-actions button, .empty-state button {
  min-height: 44px;
  border: 1px solid #cfc8bc;
  border-radius: 12px;
  background: white;
  color: var(--ink);
  font-weight: 750;
}
.card-actions .add-button { border-color: #8ebbe9; color: #174f88; background: #eaf4ff; }
.card-actions .add-button.added { border-color: #b7d9ca; color: var(--green); background: #e9f6f0; }

.basket-panel {
  position: sticky;
  top: 146px;
  max-height: calc(100vh - 166px);
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 24px;
  background: white;
  box-shadow: var(--shadow);
  padding: 22px;
}
.basket-heading { justify-content: space-between; gap: 16px; }
.basket-heading h2 { margin: 2px 0 0; font-size: 25px; }
.basket-kicker { color: var(--red); }
.count-badge { padding: 8px 12px; border-radius: 999px; color: white; background: var(--navy); font-weight: 800; }
.basket-metrics { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 18px 0 8px; }
.basket-metrics div { padding: 15px; border-radius: 15px; background: var(--mist); }
.basket-metrics strong { display: block; font-family: "Avenir Next", sans-serif; font-size: 28px; }
.basket-metrics span { color: var(--ink-soft); font-size: 12px; }
.difficulty-summary { margin: 0 0 14px; color: var(--ink-soft); font-size: 13px; }
.basket-list { max-height: 280px; overflow: auto; border-block: 1px solid #e4dfd5; }
.basket-empty { padding: 30px 12px; color: #939cab; text-align: center; }
.basket-item { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px; padding: 12px 4px; border-bottom: 1px solid #ece8df; }
.basket-item:last-child { border-bottom: 0; }
.basket-item strong { display: block; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-size: 13px; }
.basket-item span { color: var(--ink-soft); font-size: 11px; }
.item-controls { display: flex; gap: 4px; }
.item-controls button, .icon-button { width: 34px; height: 34px; border: 1px solid #d8d1c5; border-radius: 10px; color: var(--ink); background: white; }

.assignment-form { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; padding: 16px 0 14px; }
.assignment-form label { display: grid; gap: 6px; color: var(--ink-soft); font-size: 12px; font-weight: 700; }
.assignment-form .wide-field { grid-column: 1 / -1; }
.assignment-form input { width: 100%; height: 42px; border: 1px solid #ccc5b9; border-radius: 11px; padding: 0 12px; color: var(--ink); background: white; }
.assignment-form input:focus { border-color: var(--blue); }
.basket-actions { display: grid; grid-template-columns: .8fr 1.2fr; gap: 9px; }
.basket-actions .full { grid-column: 1 / -1; }
.basket-actions .primary-button { color: white; border-color: var(--blue); background: var(--blue); }
.basket-actions .secondary-button { color: var(--blue); border-color: #a7c8e8; background: #f3f8fd; }
.basket-actions .danger-button { color: #b33333; border-color: #efbcbc; background: #fff8f8; }
.basket-actions button:disabled { cursor: not-allowed; opacity: .45; }
.print-note { margin: 10px 0 0; color: #8d96a4; font-size: 11px; text-align: center; }
.export-status { min-height: 18px; margin: 10px 0 0; color: var(--green); font-size: 12px; font-weight: 700; text-align: center; }

.empty-state { padding: 90px 20px; text-align: center; color: var(--ink-soft); }
.empty-state h2 { color: var(--ink); }
.empty-wave { color: var(--red); font-size: 68px; line-height: 1; }
.empty-state button { padding: 0 20px; }

.load-more-wrap { padding: 24px 0 10px; text-align: center; }
.load-more-button {
  min-width: 260px;
  min-height: 48px;
  padding: 0 24px;
  border: 1px solid #9bbfe3;
  border-radius: 14px;
  color: #174f88;
  background: #f5f9fd;
  font-weight: 750;
}
.load-more-button:hover { background: #e9f3fc; }

.site-footer { justify-content: space-between; gap: 28px; padding: 26px 34px; color: #dfeaf5; background: var(--navy); }
.site-footer strong { display: block; color: white; }
.site-footer span, .site-footer a { color: #b6c7d8; font-size: 12px; text-decoration: none; }
.footer-wave { width: 120px; }
.footer-wave svg { display: block; fill: none; stroke: #6da0d3; stroke-width: 3; }
.footer-wave .red { stroke: #e3675e; }
.footer-contact { gap: 18px; margin-left: auto; flex-wrap: wrap; justify-content: flex-end; }
.footer-contact a:hover { color: white; }

dialog {
  padding: 0;
  border: 0;
  border-radius: 22px;
  color: var(--ink);
  background: white;
  box-shadow: 0 30px 90px rgba(10, 24, 42, .34);
}
dialog::backdrop { background: rgba(10, 24, 42, .62); backdrop-filter: blur(5px); }
.viewer-dialog { width: min(1360px, 96vw); max-height: 94vh; overflow: hidden; }
.viewer-header { position: sticky; top: 0; z-index: 2; justify-content: space-between; gap: 20px; padding: 17px 20px; border-bottom: 1px solid var(--line); background: rgba(255, 255, 255, .95); backdrop-filter: blur(10px); }
.viewer-header h2 { margin: 2px 0 0; font-size: 20px; }
.viewer-tag { color: var(--blue); }
.viewer-actions { display: flex; align-items: center; gap: 8px; flex: 0 0 auto; }
.viewer-action-button { min-height: 36px; padding: 0 14px; border: 1px solid #b9cde0; border-radius: 10px; color: #174f88; background: #f3f8fd; font-weight: 750; }
.viewer-action-button:hover { background: #e7f2fd; }
.viewer-action-button:disabled { opacity: .5; cursor: wait; }
.viewer-canvas { max-height: calc(94vh - 78px); overflow: auto; padding: 18px; background: #eceae4; }
.viewer-dialog img { display: block; width: auto; max-width: none; height: auto; margin: 0 auto; background: white; box-shadow: 0 10px 34px rgba(19, 42, 70, .12); }
.viewer-dialog img.image-load-failed { min-width: min(720px, 80vw); min-height: 300px; object-fit: contain; background: #fff5f2; }
.icon-button { font-size: 24px; line-height: 1; }
.history-dialog { width: min(900px, 88vw); max-height: 82vh; }
.history-note { margin: 16px 20px 0; color: var(--ink-soft); }
.history-list { padding: 12px 20px 22px; }
.history-empty { padding: 48px 10px; color: var(--ink-soft); text-align: center; }
.history-item { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 16px; padding: 16px 0; border-bottom: 1px solid #e8e3da; }
.history-item h3 { margin: 0 0 6px; font-size: 16px; }
.history-item p { margin: 0; color: var(--ink-soft); font-size: 12px; }
.history-item .item-controls { max-width: 330px; flex-wrap: wrap; justify-content: flex-end; align-content: center; }
.history-item button { width: auto; align-self: center; padding: 8px 13px; border: 1px solid #c5d7ea; border-radius: 10px; color: #174f88; background: #f4f8fd; }
.history-item button:hover { background: #e8f2fc; }
.history-item button[data-action="delete-history"] { border-color: #efbcbc; color: #b33333; background: #fff8f8; }
.history-item .history-pdf-status { margin-top: 7px; color: #28745b; font-weight: 700; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  z-index: 80;
  transform: translate(-50%, 20px);
  padding: 12px 18px;
  border-radius: 12px;
  color: white;
  background: #172336;
  box-shadow: 0 12px 34px rgba(0, 0, 0, .22);
  opacity: 0;
  pointer-events: none;
  transition: .2s ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 1420px) {
  .filter-shell { grid-template-columns: minmax(260px, 1.4fr) repeat(3, minmax(145px, .7fr)) auto; }
  #typeFilter, #yearFilter { grid-row: 2; }
  .filter-shell { position: relative; top: auto; }
  .basket-panel { top: 78px; }
}

@media (max-width: 980px) {
  .app-header { align-items: flex-start; flex-direction: column; }
  .header-actions { flex-wrap: wrap; }
  .header-meta { width: 100%; justify-items: start; margin-left: 0; }
  .course-bar { position: relative; overflow-x: auto; }
  .filter-shell { grid-template-columns: 1fr 1fr; }
  .search-box { grid-column: 1 / -1; }
  .workspace { grid-template-columns: 1fr; }
  .question-grid { grid-template-columns: 1fr; }
  .basket-panel { position: relative; top: auto; max-height: none; }
  .site-footer { align-items: flex-start; flex-direction: column; }
  .footer-contact { margin-left: 0; justify-content: flex-start; }
  .viewer-header { align-items: flex-start; }
  .viewer-actions { flex-wrap: wrap; justify-content: flex-end; }
  .header-contact { width: 100%; justify-content: flex-start; flex-wrap: wrap; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
}
