/* Plain CSS, no frameworks. Colours live in the variables below; the dark
   theme only swaps their values. */

:root {
  color-scheme: light dark;

  --paper: #f5f0e6;
  --sheet: #fffdf8;
  --ink: #24211c;
  --muted: #736b5e;
  --line: #ddd3c1;
  --hover: #efe6d4;
  --in-play: rgba(76, 175, 80, 0.16); /* numbers not crossed out */
  --in-play-hover: rgba(76, 175, 80, 0.3);
  --strike: #b3261e;
  --button: #24211c;
  --button-ink: #fffdf8;
  --good: #2f6b3a;
  --bad: #b3261e;
  --warn: #8a5300;

  --serif: Georgia, "Iowan Old Style", "Palatino Linotype", Palatino, serif;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #171613;
    --sheet: #201e1a;
    --ink: #ebe5d9;
    --muted: #a0978a;
    --line: #3a362f;
    --hover: #2d2a24;
    --in-play: rgba(132, 208, 148, 0.14);
    --in-play-hover: rgba(132, 208, 148, 0.26);
    --strike: #ff7b6f;
    --button: #ebe5d9;
    --button-ink: #171613;
    --good: #84d094;
    --bad: #ff7b6f;
    --warn: #f2b44c;
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--serif);
  line-height: 1.5;
}

.page-header,
.layout,
.action-bar-inner {
  max-width: 1040px;
  margin: 0 auto;
  padding-left: 16px;
  padding-right: 16px;
}

/* ---- Header ---- */

.page-header {
  padding-top: 32px;
}

h1 {
  margin: 0 0 8px;
  font-size: clamp(1.75rem, 5vw, 2.5rem);
  font-weight: normal;
  line-height: 1.15;
}

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

.noscript {
  color: var(--bad);
}

/* ---- The book: a card under the puzzle, and a smaller one after a correct answer ---- */

/* The whole card is one link, cover included */
.book-card {
  display: flex;
  align-items: center;
  gap: 14px;
  max-width: 62ch;
  padding: 10px 14px 10px 10px;
  background: var(--sheet);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--ink);
  text-decoration: none;
}

.book-card[hidden] {
  display: none;
}

@media (hover: hover) {
  .book-card:hover {
    border-color: var(--muted);
  }
}

.book-card:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}

.book-cover {
  flex: none;
  width: 64px;
  height: auto;
  border-radius: 2px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
}

.book-card-action {
  font-weight: bold;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.book-card-compact {
  flex-basis: 100%;
  max-width: none;
  padding: 6px 10px 6px 6px;
  gap: 10px;
  font-size: 0.95rem;
}

.book-card-compact .book-cover {
  width: 36px;
}

h2 {
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: normal;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

/* ---- Layout: clues above the register on phones, beside it on wide screens ---- */

.layout {
  display: grid;
  /* minmax(0, ...) lets the register scroll sideways inside its sheet on very
     narrow phones, instead of stretching the whole page */
  grid-template-columns: minmax(0, 1fr);
  gap: 32px;
  padding-top: 28px;
  padding-bottom: 28px;
}

/* "Wide" means wide AND tall enough for the clue column to stay in view;
   phones held sideways keep the phone layout. Same breakpoint further down. */
@media (min-width: 860px) and (min-height: 600px) {
  .layout {
    grid-template-columns: minmax(240px, 340px) minmax(0, 1fr);
    align-items: start;
  }

  .clues {
    position: sticky;
    top: 16px;
  }

  .book {
    grid-column: 1 / -1; /* under both columns, until there's room for a third */
  }

  .book .book-cover {
    width: 96px;
  }
}

/* Very wide screens: the book gets its own column right of the register and,
   like the clues, stays in view while scrolling. */
@media (min-width: 1200px) and (min-height: 600px) {
  .page-header,
  .layout,
  .action-bar-inner {
    max-width: 1280px;
  }

  .layout {
    grid-template-columns: minmax(240px, 320px) minmax(0, 1fr) 260px;
  }

  .book {
    grid-column: auto;
    position: sticky;
    top: 16px;
  }

  .book .book-card {
    flex-direction: column;
    align-items: stretch;
    padding: 12px;
  }

  .book .book-cover {
    width: 100%;
  }
}

/* ---- Clues ---- */

.clue-list {
  display: grid;
  gap: 12px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.clues-hint {
  margin: -4px 0 12px;
  color: var(--muted);
  font-size: 0.9rem;
}

@media (min-width: 860px) and (min-height: 600px) {
  .clues-hint {
    display: none; /* the clues stay beside the register here */
  }
}

/* Each clue card is a button that pins the clue */
.clue {
  display: block;
  width: 100%;
  padding: 14px 16px;
  background: var(--sheet);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
  touch-action: manipulation;
}

@media (hover: hover) {
  .clue:hover {
    border-color: var(--muted);
  }
}

.clue:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}

/* aria-pressed="true" means the clue is pinned */
.clue[aria-pressed="true"] {
  border-color: var(--ink);
  box-shadow: inset 0 0 0 1px var(--ink);
}

.clue-title,
.clue-quote,
.clue-rule {
  display: block;
}

.clue-title {
  margin-bottom: 4px;
  font-weight: bold;
}

.clue-quote {
  margin-bottom: 8px;
  color: var(--muted);
  font-style: italic;
}

/* ---- Register ---- */

/* "Pi Register" on the left, the page's range of digits on the right */
.register-header {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 0 12px;
  margin-bottom: 12px;
}

.register-header h2 {
  margin: 0;
}

.register-digits {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.sheet {
  padding: 8px;
  overflow-x: auto;
  background: var(--sheet);
  border: 1px solid var(--line);
  border-radius: 6px;
}

.register-table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--mono);
}

.register-table th {
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: normal;
}

.register-table thead th {
  padding: 4px 0 8px;
  letter-spacing: 0.1em;
}

.register-table tbody th {
  width: 2em;
  padding-right: 6px;
  text-align: right;
}

.register-table td {
  padding: 1px;
  text-align: center;
}

/* A faint rule every five rows helps the eye keep its place. */
.register-table tbody tr:nth-child(5n):not(:last-child) > * {
  border-bottom: 1px solid var(--line);
}

.number {
  width: 100%;
  padding: 6px 2px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: inherit;
  font: inherit;
  font-size: clamp(0.75rem, 3.6vw, 1.05rem);
  letter-spacing: 0.02em;
  cursor: pointer;
  touch-action: manipulation; /* no double-tap zoom when tapping quickly */
}

/* Numbers still in play get a pale green background */
.number[aria-pressed="false"] {
  background: var(--in-play);
}

/* Hover only where there's a real pointer; on phones it would stick after a tap */
@media (hover: hover) {
  .number:hover {
    background: var(--hover);
  }

  .number[aria-pressed="false"]:hover {
    background: var(--in-play-hover);
  }
}

.number:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: -2px;
}

/* aria-pressed="true" means the number is crossed out */
.number[aria-pressed="true"] {
  color: var(--muted);
  text-decoration-line: line-through;
  text-decoration-color: var(--strike);
  text-decoration-thickness: 2px;
}

/* ---- Action bar: stays at the bottom of the screen while scrolling ---- */

.action-bar {
  position: sticky;
  bottom: 0;
  background: var(--paper);
  border-top: 1px solid var(--line);
}

.action-bar-inner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 16px;
  padding-top: 12px;
  padding-bottom: 12px;
}

.remaining {
  flex: 1 1 auto;
  margin: 0;
}

.actions {
  display: flex;
  gap: 8px;
  margin-left: auto; /* stays on the right, also when it wraps below the count */
}

.button {
  padding: 10px 18px;
  border: 1px solid var(--button);
  border-radius: 6px;
  font: inherit;
  cursor: pointer;
}

.button:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.button:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}

.button-primary {
  background: var(--button);
  color: var(--button-ink);
  font-weight: bold;
}

.button-secondary {
  background: transparent;
  color: var(--ink);
}

@media (hover: hover) {
  .button-secondary:not(:disabled):hover {
    background: var(--hover);
  }
}

/* Shown when a crossed-out number actually fits the pinned clue */
.warning {
  flex-basis: 100%;
  margin: 0;
  color: var(--warn);
}

.warning:empty {
  display: none;
}

.result {
  flex-basis: 100%;
  margin: 0;
}

.result:empty {
  display: none;
}

.result[data-kind="correct"] {
  color: var(--good);
  font-weight: bold;
}

.result[data-kind="incorrect"],
.result[data-kind="error"] {
  color: var(--bad);
}

/* The clue dock sits at the top of the action bar on phones, so it stays in
   view while scrolling the register: a numbered button per clue, and the
   pinned clue's rule under them. */
.clue-dock {
  flex-basis: 100%;
  padding: 6px 6px 6px 12px;
  background: var(--sheet);
  border: 1px solid var(--line);
  border-radius: 6px;
}

@media (min-width: 860px) and (min-height: 600px) {
  .clue-dock {
    display: none; /* the clues stay beside the register here */
  }
}

.clue-dock-bar {
  display: flex;
  align-items: center;
  gap: 8px;
}

.clue-dock-label {
  color: var(--muted);
  font-size: 0.85rem;
}

@media (max-width: 359px) {
  .clue-dock-label {
    display: none; /* room for five numbers and × on one line */
  }
}

.quick-pins {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

/* aria-pressed="true" marks the pinned clue's number */
.quick-pin {
  min-width: 36px;
  height: 36px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-family: var(--mono); /* level digits, unlike the serif's old-style ones */
  font-weight: bold;
  cursor: pointer;
  touch-action: manipulation;
}

.quick-pin[aria-pressed="true"] {
  background: var(--button);
  border-color: var(--button);
  color: var(--button-ink);
}

@media (hover: hover) {
  .quick-pin:not([aria-pressed="true"]):hover {
    background: var(--hover);
  }
}

.quick-pin:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}

.pinned-clue {
  margin: 6px 0 2px;
  font-size: 0.95rem;
  line-height: 1.4;
}

.pinned-clue-title {
  display: block;
  color: var(--muted);
  font-size: 0.8rem;
}

.pinned-clue-title:empty {
  display: none;
}

.unpin-button {
  flex: none;
  margin-left: auto;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
}

@media (hover: hover) {
  .unpin-button:hover {
    background: var(--hover);
    color: var(--ink);
  }
}

.unpin-button:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: -2px;
}
