.category-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 10px;
}
.category-scroll-hint { display: none; }
.cat-card {
  min-width: 0;
  min-height: 132px;
  padding: 16px;
  color: inherit;
  text-align: left;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(251, 250, 246, .9);
  box-shadow: none;
  cursor: pointer;
  transition: border-color .16s ease, background-color .16s ease, transform .16s ease, box-shadow .16s ease;
}
.cat-card:hover {
  border-color: var(--ink);
  box-shadow: 0 8px 20px rgba(23, 23, 23, .05);
  transform: translateY(-1px);
}
.cat-card.active {
  border-color: var(--ink);
  background: var(--accent-soft);
  box-shadow: inset 4px 0 0 var(--accent);
}
.cat-icon {
  display: grid;
  width: 34px;
  height: 34px;
  place-items: center;
  margin-bottom: 9px;
  color: var(--accent);
  border: 1px solid #d5a29b;
  border-radius: 2px;
  background: transparent;
  font-family: var(--font-display);
  font-weight: 600;
}
.cat-card b { display: block; font-size: 14px; }
.cat-card span:not(.cat-icon) {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.5;
}
.cat-card .cat-count { margin-top: 7px; color: var(--accent-dark); font-weight: 700; }

.setup {
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(251, 250, 246, .94);
  box-shadow: var(--shadow);
}
.setup-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px;
}
.setup-field label { display: block; margin-bottom: 6px; font-size: 13px; font-weight: 700; }
.setup-field select {
  width: 100%;
  min-height: 48px;
  padding: 10px 12px;
  color: var(--ink);
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  background: var(--panel-strong);
}
.start-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-top: 18px; }
.hint-text { color: var(--muted); font-size: 12px; }

.study-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 270px;
  gap: 18px;
}
.question-panel, .side-panel, .result-panel, .history-panel {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(251, 250, 246, .96);
  box-shadow: var(--shadow);
}
.question-panel { min-width: 0; padding: clamp(20px, 3vw, 32px); }
.side-panel { position: sticky; top: 98px; height: max-content; padding: 18px; }
.progress-line {
  height: 7px;
  overflow: hidden;
  border-radius: 0;
  background: #ded9d0;
}
.progress-fill {
  width: 0;
  height: 100%;
  background: var(--accent);
  transition: width .2s;
}
.q-meta {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  margin: 11px 0 20px;
  color: var(--muted);
  font-size: 12px;
}
.badge {
  display: inline-flex;
  align-items: center;
  padding: 4px 9px;
  color: var(--ink-soft);
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #efede7;
  font-size: 12px;
  font-weight: 700;
}
.passage {
  max-width: var(--reading-measure);
  margin-bottom: 21px;
  padding: 20px 22px;
  color: var(--ink);
  white-space: pre-wrap;
  border: 0;
  border-left: 3px solid var(--accent);
  border-radius: 0;
  background: #f6f3ed;
  font-family: var(--font-display);
  font-size: 1.04rem;
  font-weight: 500;
  line-height: 2;
}
.structured-stimulus {
  max-width: none;
  white-space: normal;
  font-family: var(--font-ui);
  font-size: 1rem;
  font-weight: 400;
  line-height: 1.78;
}
.stimulus-title {
  margin: 0 0 12px;
  color: var(--accent-dark);
  font-family: var(--font-display);
  font-size: 17px;
  line-height: 1.55;
}
.stimulus-instruction { margin: -4px 0 12px; color: var(--muted); font-size: 13px; }
.stimulus-block {
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: rgba(255, 254, 251, .75);
}
.stimulus-block + .stimulus-block { margin-top: 12px; }
.stimulus-block-title { margin: 0 0 8px; font-family: var(--font-display); font-size: 14px; line-height: 1.5; }
.stimulus-text { margin: 0; white-space: pre-wrap; line-height: 1.82; }
.legacy-stimulus .stimulus-text { font-family: var(--font-display); font-weight: 500; }
.stimulus-list { margin: 0; padding-left: 1.4em; }
.stimulus-list li + li { margin-top: 5px; }
.stimulus-table-scroll { max-width: 100%; overflow-x: auto; }
.stimulus-table { width: 100%; min-width: 520px; border-collapse: collapse; font-family: var(--font-ui); font-size: 13px; }
.stimulus-table th, .stimulus-table td {
  padding: 8px 9px;
  text-align: left;
  vertical-align: top;
  border: 1px solid var(--line);
}
.stimulus-table thead th { color: var(--ink); background: #e9e5dd; }
.stimulus-table tbody th { background: #f3f0e9; }
.stimulus-note { margin: 8px 0 0; color: var(--muted); font-size: 12px; }
.relation {
  display: inline-block;
  margin-bottom: 10px;
  padding: 3px 9px;
  color: var(--ink-soft);
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #efede7;
  font-size: 12px;
  font-weight: 700;
}
.question-text {
  max-width: var(--reading-measure);
  margin: 0 0 18px;
  font-family: var(--font-display);
  font-size: 20px;
  font-weight: 600;
  line-height: 1.75;
}
.choices { display: grid; gap: 10px; margin: 0; padding: 0; border: 0; }
.choice {
  display: flex;
  width: 100%;
  min-height: 56px;
  align-items: flex-start;
  gap: 12px;
  padding: 13px 14px;
  color: var(--ink);
  text-align: left;
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  background: var(--panel-strong);
  cursor: pointer;
  transition: border-color .15s ease, background-color .15s ease, box-shadow .15s ease;
}
.choice:hover { border-color: var(--ink); background: #f7f4ee; }
.choice[aria-checked="true"], .choice.selected {
  border-color: var(--ink);
  background: var(--accent-soft);
  box-shadow: inset 4px 0 0 var(--accent);
}
.choice.correct {
  border: 2px solid var(--ok);
  background: var(--ok-soft);
  box-shadow: inset 4px 0 0 var(--ok);
}
.choice.wrong {
  border: 2px solid var(--ng);
  background: var(--ng-soft);
  box-shadow: inset 4px 0 0 var(--ng);
}
.choice-key {
  display: grid;
  width: 28px;
  height: 28px;
  min-width: 28px;
  place-items: center;
  border-radius: 2px;
  background: #e5e1d9;
  font-weight: 800;
}
.choice[aria-checked="true"] .choice-key, .choice.selected .choice-key { color: #fff; background: var(--accent-dark); }
.choice.correct .choice-key { color: #fff; background: var(--ok); }
.choice.wrong .choice-key { color: #fff; background: var(--ng); }
.q-actions { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-top: 20px; }
.feedback { margin-top: 18px; padding: 15px 17px; border-radius: 4px; }
.feedback.ok { border: 1px solid #8caf9c; background: var(--ok-soft); }
.feedback.ng { border: 1px solid #c89096; background: var(--ng-soft); }
.feedback b { display: block; margin-bottom: 4px; }
.side-score { padding: 8px 0 14px; text-align: center; border-bottom: 1px solid var(--line); }
.side-score b { color: var(--ink); font-family: var(--font-latin); font-size: 38px; font-weight: 400; }
.mini-list { margin: 14px 0 0; padding: 0; list-style: none; }
.mini-list li { display: flex; justify-content: space-between; gap: 10px; padding: 7px 0; border-bottom: 1px dashed var(--line); font-size: 13px; }
.mini-list span { color: var(--muted); }

.result-panel { padding: clamp(22px, 4vw, 34px); }
.result-hero { padding: 10px; text-align: center; }
.result-hero h1, .result-hero h2 { font-size: clamp(27px, 4vw, 38px); }
.score-ring {
  display: grid;
  width: 140px;
  height: 140px;
  margin: 0 auto 14px;
  place-items: center;
  border-radius: 50%;
  background: conic-gradient(var(--accent) 0deg, #ded9d0 0);
}
.score-inner {
  display: grid;
  width: 108px;
  height: 108px;
  place-items: center;
  border-radius: 50%;
  background: var(--panel);
}
.score-inner b { color: var(--ink); font-family: var(--font-latin); font-size: 34px; font-weight: 400; }
.score-inner small { display: block; color: var(--muted); }
.breakdown {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  margin: 22px 0;
}
.break-card { min-width: 0; padding: 13px; border: 1px solid var(--line); border-radius: 4px; background: rgba(255, 254, 251, .7); }
.break-card b { display: block; }
.break-card span { color: var(--muted); font-size: 12px; }
.missed-list { display: grid; gap: 10px; }
.missed { padding: 14px; border: 1px solid #c89096; border-radius: 4px; background: var(--ng-soft); }
.missed b { font-size: 13px; }
.missed p { margin: 4px 0; font-size: 13px; }
.missed small { color: var(--muted); }
.missed-stimulus { margin: 8px 0; }
.missed-stimulus summary { display: flex; min-height: 44px; align-items: center; padding-block: 6px; color: var(--accent-dark); font-size: 13px; font-weight: 700; cursor: pointer; }
.missed-stimulus-body { margin: 8px 0 10px; font-size: 13px; }
.missed .stimulus-text { margin: 0; }
.history-panel { padding: 22px; overflow: hidden; }
.history-content { max-width: 100%; overflow-x: auto; overscroll-behavior-inline: contain; }
.history-table { width: 100%; min-width: 560px; border-collapse: collapse; font-size: 13px; }
.history-table th, .history-table td { padding: 10px; text-align: left; border-bottom: 1px solid var(--line); }
.history-table th { color: var(--muted); font-size: 12px; }
.history-table tbody tr:hover { background: rgba(245, 233, 229, .45); }
.history-data-tools {
  margin-top: 22px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255, 254, 251, .72);
}
.history-data-tools h2 { margin: 0; font-size: 18px; }
.history-data-tools p { margin: 5px 0 0; color: var(--muted); font-size: 12px; }
.history-data-tools .start-row { margin-top: 12px; }
.empty { padding: 28px; color: var(--muted); text-align: center; }

#printArea { display: none; }

@media (max-width: 960px) {
  .category-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .setup-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .study-layout { grid-template-columns: 1fr; }
  .side-panel { position: static; }
  .breakdown { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 600px) {
  .category-grid {
    display: flex;
    width: 100%;
    min-width: 0;
    max-width: 100%;
    gap: 10px;
    padding: 4px 4px 12px;
    overflow-x: auto;
    overflow-y: hidden;
    overscroll-behavior-inline: contain;
    scroll-padding-inline: 4px;
    scroll-snap-type: inline proximity;
    scrollbar-color: var(--line-strong) transparent;
    scrollbar-width: thin;
    -webkit-overflow-scrolling: touch;
  }
  .category-grid .cat-card {
    display: grid;
    min-height: 112px;
    flex: 0 0 clamp(232px, 78vw, 300px);
    grid-template-columns: 34px minmax(0, 1fr);
    grid-template-rows: repeat(3, auto);
    align-content: center;
    column-gap: 12px;
    padding: 14px;
    scroll-snap-align: start;
  }
  .category-grid .cat-icon {
    grid-column: 1;
    grid-row: 1 / span 3;
    align-self: start;
    margin-bottom: 0;
  }
  .category-grid .cat-card b,
  .category-grid .cat-card span:not(.cat-icon) { grid-column: 2; }
  .category-scroll-hint {
    display: flex;
    align-items: center;
    gap: 7px;
    margin: 3px 0 0;
    color: var(--muted);
    font-size: 12px;
    line-height: 1.5;
  }
  .category-scroll-icon {
    flex: 0 0 auto;
    color: var(--accent-dark);
    font-size: 17px;
    line-height: 1;
  }
  .setup-grid, .breakdown { grid-template-columns: 1fr; }
  .question-panel { padding: 17px; }
  .passage { padding: 16px; font-size: 1rem; line-height: 1.9; }
  .question-text { font-size: 18px; }
  .choice { min-height: 58px; padding: 13px 12px; }
  .q-actions { align-items: stretch; flex-direction: column-reverse; }
  .q-actions > *, .q-actions .btn { width: 100%; }
  .history-table { min-width: 520px; font-size: 12px; }
  .history-table th, .history-table td { padding: 8px 7px; }
  .history-data-tools .btn { width: 100%; }
}

@media (max-width: 420px) {
  .cat-card { min-height: 0; }
}

@page { size: A4 portrait; margin: 12mm; }
@media print {
  body { color: #000; background: #fff !important; font-family: "Yu Mincho", "Hiragino Mincho ProN", serif; font-size: 10.5pt; line-height: 1.5; }
  body:not(.diagnostic-print) .site-header,
  body:not(.diagnostic-print) main,
  body:not(.diagnostic-print) .site-footer,
  body:not(.diagnostic-print) .toast { display: none !important; }
  body:not(.diagnostic-print) #printArea { display: block !important; color: #000; }
  .print-page { position: relative; min-height: 260mm; page-break-after: always; }
  .print-page:last-child { page-break-after: auto; }
  .print-head { display: flex; align-items: end; justify-content: space-between; margin-bottom: 12px; padding-bottom: 6px; border-bottom: 2px solid #000; }
  .print-head h1 { margin: 0; font-size: 17pt; }
  .print-head div { font-size: 9pt; }
  .print-info { display: flex; gap: 20px; margin: 8px 0 13px; }
  .print-info span { min-width: 120px; border-bottom: 1px solid #333; }
  .print-q { margin: 0 0 12px; break-inside: avoid; }
  .print-q .num { font-weight: 700; }
  .print-passage { margin: 5px 0; padding: 7px 9px; white-space: pre-wrap; border: 1px solid #999; background: #fafafa; }
  .print-passage .stimulus-title { margin-bottom: 5px; color: #000; font-size: 10pt; }
  .print-passage .stimulus-instruction { margin: 0 0 5px; color: #333; font-size: 8.5pt; }
  .print-passage .stimulus-block { padding: 5px 7px; border-color: #aaa; border-radius: 0; }
  .print-passage .stimulus-block + .stimulus-block { margin-top: 5px; }
  .print-passage .stimulus-block-title { margin-bottom: 3px; font-size: 9pt; }
  .print-passage .stimulus-text, .print-passage .stimulus-list { font-size: 8.5pt; line-height: 1.45; }
  .print-passage .stimulus-table { min-width: 0; font-size: 7.5pt; }
  .print-passage .stimulus-table th, .print-passage .stimulus-table td { padding: 3px 4px; border-color: #777; }
  .print-choices { display: grid; grid-template-columns: 1fr 1fr; gap: 3px 16px; margin: 5px 0; }
  .print-answer-line { height: 18px; border-bottom: 1px solid #777; }
  .print-footer { margin-top: 10px; color: #777; text-align: center; font-size: 8pt; }
  .answer-box { margin: 0 0 10px; padding: 0 0 8px; border-bottom: 1px solid #aaa; break-inside: avoid; }
  .answer-box b { font-size: 10pt; }
  .answer-box p { margin: 3px 0; }
  .answer-box .ans { color: #000; font-weight: 700; }
}
