:root {
  --color-bg: #f5f5f5;
  --color-surface: #ffffff;
  --color-dark: #1a1a1a;
  --color-text: var(--color-dark);
  --color-text-on-accent: #ffffff;
  --color-text-muted: #6b7280;
  --color-text-subtle: #374151;
  --color-letter-value: #4b5563;
  --color-accent: #2051be;
  --color-success: #0e9a41;
  --color-danger: #fecaca;
  --color-disabled: #9ca3af;
  --color-button-bg: #e5e7eb;
  --color-backdrop: rgb(0 0 0 / 50%);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 1rem;
  font-family: system-ui, sans-serif;
  background-color: var(--color-bg);
  color: var(--color-text);
}

/* Sticky, not fixed: stays in normal flow (no compensating padding needed
   elsewhere) but pins to the top of the viewport once scrolled past. Spans
   the full width via align-self, overriding body's centering, so its
   background/border reach both edges - the content inside it is
   width-constrained separately by .toolbar-content below. */
.toolbar {
  position: sticky;
  top: 0;
  z-index: 10;
  align-self: stretch;
  padding: 0.5rem 1rem;
  background-color: var(--color-surface);
  border-bottom: 1px solid var(--color-button-bg);
}

/* Matches the 6x6 grid's own rendered width (6 cells + 5 gaps + 2 sides of
   padding, see .grid below) so the toolbar's icons line up with the game's
   content column instead of the full-bleed toolbar background. */
.toolbar-content {
  max-width: calc(6 * 3rem + 5 * 2px + 2 * 2px);
  margin: 0 auto;
  display: flex;
  justify-content: flex-end;
  gap: 0.25rem;
}

.icon-button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2.75rem;
  height: 2.75rem;
  padding: 0;
  background: none;
  color: var(--color-text);
}

.icon-button:hover {
  background-color: var(--color-button-bg);
}

header {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  /* Replaces body's old padding-top - moved here now that the toolbar sits
     flush at the very top of the page. */
  margin-top: 2rem;
}

header h1 {
  margin: 0;
  font-size: 1.75rem;
}

.status-display {
  display: flex;
  gap: 1rem;
}

/* `display: flex` above always wins over the browser's built-in
   `[hidden] { display: none }` rule (author styles beat UA styles
   regardless of specificity) - without this, setting .hidden in JS has no
   visible effect and both the status line and the violation message show
   at once. */
.status-display[hidden] {
  display: none;
}

#word-count-display,
#score-display,
#timer-display {
  font-size: 1.1rem;
  font-variant-numeric: tabular-nums;
}

#violation-display {
  font-size: 1.1rem;
  font-weight: bold;
}

footer {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.25rem;
  font-size: 0.875rem;
  font-variant-numeric: tabular-nums;
  color: var(--color-text-muted);
}

#copyright,
#footer-version {
  font-size: 0.75rem;
}

/* Replicates the 1rem spacing body's flex layout gives its direct children -
   wrapping the three sections in one container took them out of that
   direct-child gap. Scoped to this inner wrapper, not the <dialog> itself,
   so it never has to fight the dialog's own native open/closed display
   toggling (unlike .status-display[hidden] below, which does need that).
   The scrolling (this content is longer than the other dialogs') is scoped
   here too, rather than on the dialog as a whole, so the "Help" heading and
   the close button above stay put while only this part scrolls. */
.help-dialog-content {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  max-height: 65vh;
  overflow-y: auto;
}

#how-to-play,
#rule-violations,
#useful-info,
.fun-fact {
  max-width: 20rem;
  font-size: 0.875rem;
  color: var(--color-text-subtle);
}

.fun-fact {
  margin: 0;
  font-style: italic;
}

.help-version {
  margin: 0;
  font-size: 0.75rem;
  color: var(--color-text-muted);
}

#how-to-play h2,
#rule-violations h2,
#useful-info h2 {
  margin: 0 0 0.5rem;
  font-size: 1rem;
}

#how-to-play ul,
#rule-violations ul,
#useful-info ul {
  margin: 0;
  padding-left: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

button {
  font-size: 1rem;
  padding: 0.5rem 1.25rem;
  border: none;
  border-radius: 0.25rem;
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
}

.mode-buttons {
  display: flex;
  gap: 0.5rem;
}

.mode-buttons button {
  background-color: var(--color-button-bg);
  color: var(--color-text);
}

.mode-buttons button.active {
  background-color: var(--color-dark);
  color: var(--color-text-on-accent);
}

.mode-display {
  font-size: 0.875rem;
  color: var(--color-text-muted);
}

.action-buttons {
  display: flex;
  gap: 0.5rem;
}

.grid {
  display: grid;
  grid-template-columns: repeat(var(--size), 3rem);
  grid-template-rows: repeat(var(--size), 3rem);
  gap: 2px;
  background-color: var(--color-dark);
  padding: 2px;
}

.cell-wrapper {
  position: relative;
}

.cell {
  width: 100%;
  height: 100%;
  border: none;
  text-align: center;
  font-size: 1.5rem;
  font-weight: bold;
  text-transform: uppercase;
  background-color: var(--color-surface);
  color: var(--color-text);
}

.letter-value {
  position: absolute;
  bottom: 2px;
  right: 4px;
  font-size: 0.65rem;
  line-height: 1;
  color: var(--color-letter-value);
  pointer-events: none;
}

/* Crossword-style clue number - see getNumberedStarts in runs.js. Empty
   (no textContent) on any cell that doesn't start a word, so it never
   shows for most cells. */
.cell-number {
  position: absolute;
  top: 1px;
  left: 3px;
  font-size: 0.6rem;
  line-height: 1;
  color: var(--color-letter-value);
  pointer-events: none;
}

.cell:focus {
  outline: 2px solid var(--color-accent);
  outline-offset: -2px;
}

.cell--starter {
  background-color: var(--color-accent);
  color: var(--color-text-on-accent);
  /* Double-tap triggers the definition lookup - stop iOS's native
     text-selection callout/magnifier from appearing over it instead. */
  -webkit-touch-callout: none;
  user-select: none;
}

.cell--valid {
  background-color: var(--color-success);
  color: var(--color-text-on-accent);
}

/* Explicit color, not just background: a starter cell can simultaneously
   be part of a problem crossing word, and this must win over the starter
   rule's white text for readability against the lighter danger background.
   One colour covers every rule violation (invalid, duplicate, overused,
   floating) - the specific reason is spelled out in the "Score" display
   instead of a separate colour per rule, see getViolationMessage. */
.cell--problem {
  background-color: var(--color-danger);
  color: var(--color-text);
}

.cell--blocked {
  background-color: var(--color-dark);
}

/* A permanently unfillable cell (like a black square in a real crossword) -
   visually identical to .cell--blocked today, but a deliberately separate
   rule, since the two mean different things (structural vs "left blank at
   submit") and may want to diverge visually later. */
.cell--unusable {
  background-color: var(--color-dark);
  cursor: not-allowed;
}

.cell--starter + .letter-value,
.cell--valid + .letter-value,
.cell--starter ~ .cell-number,
.cell--valid ~ .cell-number {
  color: var(--color-text-on-accent);
}

#submit-button {
  background-color: var(--color-accent);
  color: var(--color-text-on-accent);
}

#submit-button:disabled {
  background-color: var(--color-disabled);
}

#clear-button {
  background-color: var(--color-button-bg);
  color: var(--color-text);
}

#clear-button:disabled {
  background-color: var(--color-disabled);
  color: var(--color-text-on-accent);
}

#share-button {
  background-color: var(--color-accent);
  color: var(--color-text-on-accent);
}

#share-button:disabled {
  background-color: var(--color-disabled);
}

dialog#definition-dialog,
dialog#share-dialog,
dialog#settings-dialog,
dialog#help-dialog {
  position: relative;
  width: min(24rem, 90vw);
  border: none;
  border-radius: 0.5rem;
  padding: 1.5rem;
}

/* Fixed in the corner via .dialog-close-form below, a sibling of whatever
   scrolls (see .help-dialog-content) - not a descendant of it, so it can
   never scroll out of view itself. */
.dialog-close-form {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
}

.dialog-close-button {
  width: 2.25rem;
  height: 2.25rem;
}

dialog#definition-dialog::backdrop,
dialog#share-dialog::backdrop,
dialog#settings-dialog::backdrop,
dialog#help-dialog::backdrop {
  background-color: var(--color-backdrop);
}

#definition-content h2 {
  margin: 0 0 0.25rem;
  /* Leaves room for the top-right close button, see .dialog-close-form. */
  padding-right: 2rem;
  text-transform: capitalize;
}

#definition-content .definition-phonetic {
  margin: 0 0 1rem;
  color: var(--color-text-muted);
  font-style: italic;
}

#definition-content .definition-part-of-speech {
  margin: 1rem 0 0.25rem;
  font-weight: bold;
  text-transform: capitalize;
}

#definition-content ol {
  margin: 0;
  padding-left: 1.25rem;
}

.definition-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-top: 1.25rem;
}

#definition-search-link {
  font-size: 0.9rem;
  color: var(--color-accent);
  text-decoration: none;
}

#definition-search-link:hover {
  text-decoration: underline;
}

dialog#definition-dialog button,
dialog#share-dialog button,
dialog#settings-dialog button,
dialog#help-dialog button {
  background-color: var(--color-button-bg);
  color: var(--color-text);
}

#share-dialog h2,
#settings-dialog h2,
#help-dialog h2 {
  margin: 0 0 1rem;
  /* Leaves room for the top-right close button, see .dialog-close-form. */
  padding-right: 2rem;
}

.settings-field {
  margin: 0 0 1.25rem;
  padding: 0;
  border: none;
}

.settings-field legend {
  padding: 0;
  margin: 0 0 0.5rem;
  font-weight: bold;
}

.settings-option {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  font-size: 0.9rem;
  margin-bottom: 0.75rem;
}

.settings-option input {
  margin-top: 0.2rem;
}

#share-preview {
  margin: 0;
  padding: 0.75rem;
  border-radius: 0.25rem;
  background-color: var(--color-bg);
  font-family: inherit;
  font-size: 0.9rem;
  white-space: pre-wrap;
  overflow-wrap: break-word;
}

.share-toggles {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-top: 1rem;
}

.share-toggle {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.9rem;
}

.share-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-top: 1.25rem;
}

#share-copy-button {
  background-color: var(--color-accent);
  color: var(--color-text-on-accent);
}
