:root {
  color-scheme: light;
  --bg: #f7f8f4;
  --paper: #ffffff;
  --ink: #202426;
  --muted: #5d676b;
  --line: #d8dedb;
  --line-strong: #bac7c0;
  --teal: #147c72;
  --teal-dark: #0d5f58;
  --coral: #c7533e;
  --amber: #d99b2b;
  --blue: #466ca8;
  --good: #167a4a;
  --bad: #b93834;
  --shadow: 0 18px 55px rgba(27, 38, 35, 0.1);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background:
    linear-gradient(180deg, rgba(20, 124, 114, 0.08), transparent 28rem),
    var(--bg);
  color: var(--ink);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

button,
input {
  font: inherit;
}

.app-shell {
  width: min(1180px, calc(100vw - 28px));
  margin: 0 auto;
  padding: 26px 0 44px;
}

.topbar,
.puzzle-intro,
.toolbar,
.content-grid,
.solution-panel {
  width: 100%;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 24px;
}

.eyebrow {
  margin: 0 0 5px;
  color: var(--teal-dark);
  font-size: 0.8rem;
  font-weight: 750;
  letter-spacing: 0;
  text-transform: uppercase;
}

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

h1 {
  margin-bottom: 0;
  font-size: clamp(2rem, 3vw, 3.2rem);
  line-height: 1;
  letter-spacing: 0;
}

h2 {
  margin-bottom: 12px;
  font-size: 1.15rem;
  line-height: 1.25;
  letter-spacing: 0;
}

.actions,
.toolbar-actions,
.mode-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.button,
.mode-button {
  min-height: 40px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  padding: 0 14px;
  background: var(--paper);
  color: var(--ink);
  cursor: pointer;
  font-weight: 700;
  transition:
    transform 140ms ease,
    border-color 140ms ease,
    background 140ms ease;
}

.button:hover,
.mode-button:hover {
  transform: translateY(-1px);
}

.button:disabled {
  cursor: wait;
  opacity: 0.65;
  transform: none;
}

.primary {
  border-color: var(--teal);
  background: var(--teal);
  color: white;
}

.secondary {
  border-color: var(--teal);
  color: var(--teal-dark);
}

.ghost {
  border-color: transparent;
  background: transparent;
  color: var(--muted);
}

.puzzle-intro {
  display: flex;
  align-items: stretch;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.78);
  box-shadow: var(--shadow);
}

.puzzle-intro p {
  margin-bottom: 0;
  color: var(--muted);
}

.story-copy {
  max-width: 68ch;
  margin-top: 12px;
}

.story-copy p {
  margin: 0 0 7px;
  color: #303638;
  line-height: 1.45;
}

.status-panel {
  display: grid;
  min-width: 176px;
  place-items: center;
  border-left: 1px solid var(--line);
  padding-left: 18px;
  color: var(--muted);
  font-weight: 700;
  text-align: center;
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 16px;
  margin-bottom: 18px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
}

.mode-group {
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f0f3ef;
}

.mode-button {
  min-width: 48px;
  border-color: transparent;
  background: transparent;
}

.mode-button.active {
  background: var(--ink);
  color: white;
}

.toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-weight: 700;
}

.toggle input {
  width: 18px;
  height: 18px;
  accent-color: var(--teal);
}

.content-grid {
  display: grid;
  grid-template-columns: minmax(270px, 0.7fr) minmax(0, 1.9fr);
  gap: 18px;
  align-items: start;
}

.clue-panel,
.grid-panel,
.solution-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  box-shadow: var(--shadow);
}

.clue-panel,
.grid-panel {
  padding: 18px;
}

.clue-panel {
  position: sticky;
  top: 16px;
}

#clueList {
  margin: 0;
  padding-left: 22px;
}

#clueList li {
  margin: 0 0 12px;
  color: #303638;
  line-height: 1.45;
}

.grid-wrap {
  overflow-x: auto;
  padding: 2px 0 10px;
}

.logic-table {
  --cell-size: 44px;
  --row-label-size: 140px;
  --side-label-size: 38px;
  --header-height: 38px;
  --column-label-height: 126px;
  --grid-line: #303837;
  border-collapse: collapse;
  color: var(--ink);
  table-layout: fixed;
  width: calc(var(--side-label-size) + var(--row-label-size) + (var(--item-column-count) * var(--cell-size)));
}

.side-label-column {
  width: var(--side-label-size);
}

.row-label-column {
  width: var(--row-label-size);
}

.item-column {
  width: var(--cell-size);
}

.logic-table th,
.logic-table td {
  padding: 0;
}

.table-spacer {
  border: 0;
  width: calc(var(--side-label-size) + var(--row-label-size));
}

.grid-top-title {
  height: var(--header-height);
  border: 1px solid var(--grid-line);
  background: #090b0b;
  color: white;
  font-size: 1rem;
  font-weight: 850;
  letter-spacing: 0;
  line-height: 1;
  text-align: center;
}

.grid-column-label {
  height: var(--column-label-height);
  border: 1px solid var(--grid-line);
  background: white;
  color: var(--ink);
  font-size: 0.82rem;
  font-weight: 800;
  text-align: center;
  vertical-align: middle;
}

.grid-column-label span {
  align-items: center;
  display: inline-flex;
  height: calc(var(--column-label-height) - 12px);
  justify-content: center;
  line-height: 1;
  max-height: calc(var(--column-label-height) - 12px);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  writing-mode: vertical-rl;
  transform: rotate(180deg);
}

.grid-side-title {
  border: 1px solid var(--grid-line);
  background: #090b0b;
  color: white;
  overflow: hidden;
  text-align: center;
  vertical-align: middle;
}

.grid-side-title span {
  display: inline-block;
  font-size: 1rem;
  font-weight: 850;
  letter-spacing: 0;
  line-height: 1;
  max-height: calc((var(--cell-size) * 4) - 16px);
  white-space: nowrap;
  writing-mode: vertical-rl;
  transform: rotate(180deg);
}

.grid-row-label {
  height: var(--cell-size);
  border: 1px solid var(--grid-line);
  background: white;
  color: var(--ink);
  font-size: 0.95rem;
  font-weight: 800;
  line-height: 1.1;
  overflow: hidden;
  padding: 0 14px 0 8px;
  text-align: right;
  text-overflow: ellipsis;
  vertical-align: middle;
  white-space: nowrap;
}

.logic-table .grid-row-label {
  padding: 0 14px 0 8px;
}

.grid-mark-slot {
  height: var(--cell-size);
  border: 1px solid var(--grid-line);
  background: white;
}

.grid-empty-slot {
  border: 0;
}

.mark-cell {
  display: grid;
  place-items: center;
  width: var(--cell-size);
  height: var(--cell-size);
  border: 0;
  border-radius: 0;
  padding: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 1.14rem;
  font-weight: 900;
  line-height: 1;
  touch-action: manipulation;
  user-select: none;
}

.mark-cell:hover {
  background: #fff8e8;
}

.mark-cell.mark-x {
  color: var(--coral);
}

.mark-cell.mark-yes {
  background:
    radial-gradient(circle at 45% 38%, rgba(255, 255, 255, 0.5), transparent 29%),
    radial-gradient(circle, #74d36e 0 45%, #39a94f 47% 62%, transparent 64%);
  color: white;
  font-size: 0.98rem;
  text-shadow: 0 1px 2px rgba(20, 62, 29, 0.4);
}

.mark-cell.is-wrong {
  outline: 2px solid var(--bad);
  outline-offset: -2px;
  background: #fff0ef;
}

.solution-panel {
  margin-top: 18px;
  padding: 18px;
}

.solution-panel.hidden {
  display: none;
}

.solution-rows {
  display: grid;
  grid-template-columns: repeat(4, minmax(170px, 1fr));
  gap: 10px;
}

.solution-row {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: #fbfcfa;
}

.solution-row strong {
  display: block;
  margin-bottom: 7px;
  color: var(--teal-dark);
}

.solution-row dl {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 10px;
  margin: 0;
  color: var(--muted);
}

.solution-row dd {
  margin: 0;
  color: var(--ink);
  font-weight: 650;
}

@media (max-width: 900px) {
  .content-grid,
  .solution-rows {
    grid-template-columns: 1fr;
  }

  .clue-panel {
    position: static;
  }
}

@media (max-width: 680px) {
  .app-shell {
    width: min(100vw - 18px, 1180px);
    padding-top: 16px;
  }

  .topbar,
  .puzzle-intro,
  .toolbar {
    align-items: stretch;
    flex-direction: column;
  }

  .actions,
  .toolbar-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .button {
    width: 100%;
  }

  .status-panel {
    min-width: 0;
    border-left: 0;
    border-top: 1px solid var(--line);
    padding: 12px 0 0;
  }

  .classic-grid {
    --cell-size: 34px;
    --row-label-size: 108px;
  }
}

.hidden { display: none !important; }
.puzzle-types { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 18px; }
.puzzle-types button { min-height: 64px; font-size: 1.1rem; }
.library { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-bottom: 18px; }
.library select { max-width: 100%; padding: 8px; font: inherit; }
#saveNotice { font-size: .85rem; color: var(--muted); }
.toolbar-actions { flex-wrap: wrap; }
.picture-mode .clue-panel { display: none; }
.picture-mode .content-grid { grid-template-columns: 1fr; }
.picture-mode .grid-panel { width: fit-content; max-width: 100%; margin: auto; }
.picture-mode .solution-rows { display: block; overflow: auto; }
#pictureTools { margin-bottom: 20px; }
[data-tool][aria-pressed="true"] { background: var(--ink); color: white; }
.picture-table { border-collapse: collapse; border: 2px solid #202426; margin: auto; }
.picture-table th { font-size: 15px; padding: 4px 8px; color: #202426; }
.picture-table tr:first-child th { vertical-align: bottom; padding: 8px 0; }
.picture-table th span { display: block; line-height: 20px; }
.picture-table th[scope="row"] { white-space: pre; text-align: right; min-width: 90px; }
.picture-table td { padding: 0; border: 1px solid #8c9390; }
.picture-table td.block-top { border-top: 2px solid #202426; }
.picture-table td.block-left { border-left: 2px solid #202426; }
.picture-cell { display: grid; place-items: center; width: 36px; height: 36px; padding: 0; border: 0; border-radius: 0; background: white; color: #555; font-size: 25px; touch-action: none; user-select: none; }
button.picture-cell { cursor: pointer; }
.picture-cell.filled { background: #202426; }
.picture-cell.is-wrong { box-shadow: inset 0 0 0 3px #d63f38; }
.picture-cell:focus-visible { outline: 3px solid #147c72; outline-offset: -3px; }
@page { size: A4 portrait; margin: 12mm; }
@media print {
  body { background: white; color: black; print-color-adjust: exact; -webkit-print-color-adjust: exact; }
  .app-shell { width: 100%; padding: 0; margin: 0; }
  .topbar, .puzzle-types, .library, .toolbar, #pictureTools, .status-panel, .solution-panel { display: none !important; }
  .puzzle-intro, .clue-panel, .grid-panel { box-shadow: none; border: 0; padding: 0; margin: 0 0 4mm; }
  .puzzle-intro h2 { font-size: 16pt; }
  .puzzle-intro p, .story-copy { font-size: 9pt; }
  .story-copy p { margin-bottom: 2mm; }
  .content-grid { display: block; }
  .clue-panel { position: static; font-size: 9pt; }
  #clueList li { margin-bottom: 2mm; line-height: 1.25; }
  .grid-panel h2 { display: none; }
  .grid-wrap { overflow: visible; padding: 0; }
  .logic-table { --cell-size: 9mm; --row-label-size: 40mm; --side-label-size: 8mm; --header-height: 7mm; --column-label-height: 27mm; margin: auto; }
  .logic-table .grid-row-label { font-size: 9pt; }
  .logic-table .grid-column-label { font-size: 8pt; }
  .grid-panel, .picture-table, .logic-table { break-inside: avoid; }
  .picture-cell { width: 12mm; height: 12mm; }
  .picture-table th { font-size: 11pt; }
  .picture-table th span { line-height: 6mm; }
  .print-blank .mark-cell, .print-blank .picture-cell { background: white !important; color: transparent !important; text-shadow: none; outline: 0; box-shadow: none; }
}

.setup-panel { padding: 24px; margin-bottom: 20px; background: white; border: 1px solid var(--line); border-radius: 8px; }
.difficulty-options { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; padding: 0; border: 0; margin: 20px 0; }
.difficulty-options legend { margin-bottom: 12px; font-weight: 700; }
.difficulty-options label { position: relative; display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 16px 5px; border: 2px solid var(--line); border-radius: 8px; cursor: pointer; }
.difficulty-options label:has(input:checked) { background: #e2f1eb; border-color: var(--teal); }
.difficulty-options strong { font-size: 1.7rem; }
.difficulty-options span { font-size: .85rem; text-align: center; }
#setupStatus { margin: 12px 0 0; color: var(--teal-dark); }
.picture-cell { width: var(--picture-cell, 24px); height: var(--picture-cell, 24px); font-size: calc(var(--picture-cell, 24px) * .75); }
.zoom-control { display: flex; align-items: center; gap: 12px; margin: 12px 0; }
.picture-mode .grid-panel { width: 100%; }
.grid-side-title span { max-height: calc(var(--cell-size) * var(--items-per-category, 4) - 16px); }
@media (max-width: 500px) { .difficulty-options { gap: 4px; } .difficulty-options span { font-size: .7rem; } .setup-panel { padding: 14px; } }
@media print {
  .setup-panel { display: none !important; }
  .logic-table { --cell-size: var(--print-cell, 9mm); }
  .picture-cell { width: var(--print-picture-cell); height: var(--print-picture-cell); font-size: var(--print-clue-font); }
  .picture-table th, .picture-table tr:first-child th { padding: 0; font-size: var(--print-clue-font); }
  .picture-table th[scope="row"] { min-width: 0; width: var(--print-clue-width); padding-right: 1mm; }
  .picture-table th span { line-height: var(--print-picture-cell); }
}
