:root {
  --bg: #f4f7fb;
  --panel: #ffffff;
  --ink: #1d2a35;
  --muted: #5b6975;
  --line: #cbd5df;
  --primary: #167c80;
  --primary-dark: #0e5f63;
  --accent: #f2b84b;
  --danger: #d95c4b;
  --answer: #dff4e8;
  --cell-size: 48px;
  --cols: 10;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background: var(--bg);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic", sans-serif;
}

button,
select {
  font: inherit;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  max-width: 1180px;
  margin: 0 auto;
  padding: 24px 20px 12px;
}

.eyebrow,
.sheet-label {
  margin: 0 0 4px;
  color: var(--primary-dark);
  font-size: 0.9rem;
  font-weight: 700;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: 2.1rem;
  letter-spacing: 0;
}

.target-badge {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 176px;
  padding: 10px 14px;
  border: 2px solid var(--primary);
  border-radius: 8px;
  background: var(--panel);
}

.target-badge span,
.find-box span {
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 700;
}

.target-badge strong,
.find-box strong {
  font-size: 2.2rem;
  line-height: 1;
  overflow-wrap: anywhere;
}

.app-shell {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  gap: 18px;
  max-width: 1180px;
  margin: 0 auto;
  padding: 12px 20px 28px;
}

.control-panel,
.worksheet {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.control-panel {
  align-self: start;
  display: grid;
  gap: 14px;
  padding: 16px;
}

.field {
  display: grid;
  gap: 7px;
}

.field label,
.field-label {
  color: var(--muted);
  font-weight: 700;
}

.symbol-picker {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.target-option {
  display: grid;
  place-items: center;
  min-height: 44px;
  padding: 0;
  font-size: 1.35rem;
  font-weight: 800;
}

.target-option .option-label {
  font-size: 0.82rem;
  font-weight: 700;
}

.target-option.is-selected {
  border-color: var(--primary);
  color: #ffffff;
  background: var(--primary);
}

.target-option.is-selected .color-swatch {
  border-color: #ffffff;
}

select {
  width: 100%;
  min-height: 46px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--ink);
  background: #ffffff;
}

.button-row {
  display: grid;
  gap: 10px;
  margin-top: 4px;
}

button {
  min-height: 48px;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--ink);
  background: #ffffff;
  cursor: pointer;
}

button:hover,
button:focus-visible {
  border-color: var(--primary);
  outline: 3px solid rgba(22, 124, 128, 0.2);
  outline-offset: 1px;
}

.primary-button {
  border-color: var(--primary);
  color: #ffffff;
  background: var(--primary);
  font-weight: 700;
}

.primary-button:hover,
.primary-button:focus-visible {
  background: var(--primary-dark);
}

.worksheet {
  padding: 20px;
}

.worksheet-heading {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  border-bottom: 2px solid var(--ink);
  padding-bottom: 12px;
}

.worksheet-heading h2 {
  margin-bottom: 0;
  font-size: 1.45rem;
}

.instruction-strip {
  display: grid;
  grid-template-columns: 180px minmax(0, 1fr);
  gap: 14px;
  align-items: center;
  margin: 16px 0;
}

.find-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 68px;
  padding: 10px 12px;
  border: 2px solid var(--accent);
  border-radius: 8px;
  background: #fff9e8;
}

.instruction-strip p {
  margin-bottom: 0;
  color: var(--muted);
  line-height: 1.7;
}

.stats {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 14px;
}

.stats span {
  min-width: 118px;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #f9fbfd;
  font-weight: 700;
  text-align: center;
}

.is-hidden {
  display: none !important;
}

.grid-frame {
  overflow: auto;
  padding: 10px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: #fbfcfe;
}

.symbol-grid {
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  gap: 6px;
  min-width: calc(var(--cols) * 34px);
}

.symbol-cell {
  position: relative;
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  min-width: 0;
  border: 1px solid #9eaab6;
  border-radius: 5px;
  color: #1f2933;
  background: #ffffff;
  font-size: 1.65rem;
  font-weight: 700;
  line-height: 1;
  user-select: none;
}

.symbol-cell.task-colors,
.target-option.task-colors {
  gap: 4px;
}

.symbol-cell.task-colors {
  font-size: 1rem;
}

.symbol-cell.task-colors.is-color-fill {
  border-color: rgba(31, 41, 51, 0.45);
  background: var(--item-color);
}

.symbol-cell.task-colors.is-color-fill.is-answer {
  border-color: #2f8f60;
  background: var(--item-color);
  box-shadow: inset 0 0 0 4px rgba(58, 160, 107, 0.7);
}

.symbol-cell.task-colors.is-color-fill.is-wrong {
  border-color: var(--danger);
  background: var(--item-color);
  box-shadow: inset 0 0 0 4px rgba(217, 92, 75, 0.72);
}

.color-swatch {
  display: inline-block;
  width: 1.55em;
  height: 1.55em;
  border: 2px solid #ffffff;
  border-radius: 999px;
  background: var(--item-color);
  box-shadow: 0 0 0 1px rgba(31, 41, 51, 0.32);
}

.target-token {
  display: inline-flex;
  align-items: center;
  gap: 0.25em;
  margin-right: 0.45em;
  font-size: 1.1rem;
  white-space: nowrap;
}

.target-token .color-swatch {
  width: 1.2em;
  height: 1.2em;
}

.symbol-cell.is-marked::before,
.symbol-cell.is-marked::after {
  content: "";
  position: absolute;
  width: 78%;
  height: 4px;
  border-radius: 999px;
  background: var(--primary);
}

.symbol-cell.is-marked::before {
  transform: rotate(35deg);
}

.symbol-cell.is-marked::after {
  transform: rotate(-35deg);
}

.symbol-cell.is-wrong {
  border-color: var(--danger);
  background: #fff1ef;
}

.symbol-cell.is-wrong::before,
.symbol-cell.is-wrong::after {
  background: var(--danger);
}

.symbol-cell.is-answer {
  border-color: #3aa06b;
  background: var(--answer);
}

@media (max-width: 900px) {
  .app-header {
    align-items: stretch;
    flex-direction: column;
  }

  .target-badge {
    width: 100%;
  }

  .app-shell {
    grid-template-columns: 1fr;
  }

  .control-panel {
    grid-template-columns: minmax(260px, 1.4fr) repeat(2, minmax(0, 1fr));
  }

  .button-row {
    grid-column: 1 / -1;
    grid-template-columns: repeat(5, minmax(0, 1fr));
  }
}

@media (max-width: 640px) {
  h1 {
    font-size: 1.75rem;
  }

  .control-panel,
  .button-row,
  .symbol-picker,
  .instruction-strip,
  .worksheet-heading {
    grid-template-columns: 1fr;
  }

  .worksheet-heading {
    display: grid;
  }

  .symbol-cell {
    font-size: 1.35rem;
  }
}

@media print {
  @page {
    size: A4 portrait;
    margin: 10mm;
  }

  body {
    background: #ffffff;
  }

  .app-header,
  .control-panel,
  .stats {
    display: none !important;
  }

  .app-shell {
    display: block;
    max-width: none;
    padding: 0;
  }

  .worksheet {
    border: 0;
    border-radius: 0;
    padding: 0;
  }

  .instruction-strip {
    margin: 10mm 0 6mm;
  }

  .find-box {
    border-color: #222222;
    background: #ffffff;
  }

  .grid-frame {
    overflow: visible;
    padding: 0;
    border: 0;
    background: #ffffff;
  }

  .symbol-grid {
    gap: 1.5mm;
    min-width: 0;
  }

  .symbol-cell {
    border-color: #555555;
    border-radius: 0;
    color: #111111;
    background: #ffffff !important;
    font-size: 14pt;
  }

  .symbol-cell.task-colors.is-color-fill {
    background: var(--item-color) !important;
    print-color-adjust: exact;
    -webkit-print-color-adjust: exact;
  }

  .color-swatch {
    print-color-adjust: exact;
    -webkit-print-color-adjust: exact;
  }

  .symbol-cell::before,
  .symbol-cell::after {
    display: none !important;
  }
}

/* LiNest v3: 色抹消課題の出題色と正誤色は保ち、外枠のみ共通化する。 */
@media screen {
  body.linest-themed .app-header,
  body.linest-themed :where(.control-panel, .worksheet) {
    min-width: 0;
    border-color: var(--linest-border);
    background: rgba(255, 255, 255, 0.96);
    box-shadow: var(--linest-shadow);
  }

  body.linest-themed .app-header {
    max-width: min(1180px, calc(100% - 40px));
    margin-top: 24px;
    padding: 18px;
    border: 1px solid var(--linest-border);
    border-radius: 8px;
  }

  body.linest-themed .app-shell {
    min-width: 0;
    padding-top: 18px;
    padding-bottom: 36px;
  }

  body.linest-themed .target-badge {
    max-width: 100%;
    border-color: #b9d9b8;
    background: var(--linest-green-soft);
  }

  body.linest-themed :where(.target-option, button, select) {
    min-width: 0;
    border-color: var(--linest-border);
  }

  body.linest-themed .target-option.is-selected {
    border-color: var(--linest-green);
    background: var(--linest-green);
    color: #ffffff;
  }

  body.linest-themed .instruction-strip {
    padding: 12px;
    border-radius: 8px;
    background: #fdfbf7;
  }

  body.linest-themed .grid-frame {
    border-color: var(--linest-border);
    background: #fdfcf9;
  }

  body.linest-themed :where(.app-header, .app-shell, .control-panel, .button-row, .instruction-strip, .worksheet-heading) > * {
    min-width: 0;
  }
}

@media screen and (max-width: 640px) {
  body.linest-themed .app-header,
  body.linest-themed .app-shell {
    max-width: calc(100% - 20px);
    padding-inline: 0;
  }

  body.linest-themed .app-header {
    padding: 14px;
  }

  body.linest-themed :where(.control-panel, .worksheet) {
    padding: 14px;
  }

  body.linest-themed .symbol-picker {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  body.linest-themed .grid-frame {
    overflow-x: hidden;
    padding: 6px;
  }

  body.linest-themed .symbol-grid {
    min-width: 0;
    gap: 4px;
  }

  body.linest-themed .symbol-cell {
    font-size: clamp(1rem, 5.4vw, 1.35rem);
  }

  body.linest-themed :where(button, select) {
    white-space: normal;
  }
}
