/* Crossword / Mini styles. Shared tokens, header, buttons, modal and toast come from base.css.
   The grid is aida cloth: cream squares between running-stitch lines, dark woven patches for black squares. */

:root {
  --xw-cell: var(--card-bg);             /* cream square */
  --xw-black: var(--thread-ink);         /* dark woven patch */
  --xw-black-weave: rgba(255, 253, 248, .08);
  --xw-line: var(--stitch-color);        /* stitched grid lines */
  --xw-frame: var(--muted);
  --xw-letter: var(--fg);                /* 15.5:1 on the cell, 12.3:1 on the entry wash */
  --xw-letter-shade: rgba(42, 33, 24, .22);
  --xw-num: var(--muted);                /* 7.7:1 on the cell */
  --xw-word: #EFE2C9;                    /* current entry: sand wash */
  --xw-selected: #F8DFD2;                /* selected square: terra wash */
  --xw-sel-thread: var(--thread-terra);  /* selected square: terra thread */
  --xw-clue-active: var(--xw-word);
  --xw-clue-cross: var(--thread-gold);
  --xw-wrong: var(--thread-red);
  --xw-reveal: var(--thread-indigo);
  --xw-overlay: color-mix(in srgb, var(--bg) 97%, transparent);
}

/* Dark mode keeps the kit tokens above and tunes the squares, black patches and washes so every pair stays AA.
   --xw-wrong is lighter than --thread-red so the wrong letter passes on the washes. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --xw-cell: #222C43;
    --xw-black: #05070D;
    --xw-black-weave: rgba(244, 236, 221, .09);
    --xw-frame: var(--stitch-color);
    --xw-letter-shade: rgba(0, 0, 0, .45);
    --xw-word: #2E3D60;
    --xw-selected: #463A42;
    --xw-wrong: #F5A3A8;
  }
}
:root[data-theme="dark"] {
  --xw-cell: #222C43;
  --xw-black: #05070D;
  --xw-black-weave: rgba(244, 236, 221, .09);
  --xw-frame: var(--stitch-color);
  --xw-letter-shade: rgba(0, 0, 0, .45);
  --xw-word: #2E3D60;
  --xw-selected: #463A42;
  --xw-wrong: #F5A3A8;
}

.xw-main [hidden] {
  display: none !important;
}

.xw-main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 12px 16px 32px;
}

.xw-loading {
  color: var(--muted);
  text-align: center;
  padding: 32px 0;
}

.xw-loading a {
  color: var(--link);
  font-weight: 600;
}

/* ---- Toolbar ---- */

.xw-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  padding-bottom: 12px;
  margin-bottom: 12px;
  border-bottom: 1px dashed var(--border);
}

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

.xw-timer {
  font-family: var(--font-yarn);
  font-variant-numeric: tabular-nums;
  font-weight: 800;
  font-size: 1.1rem;
  letter-spacing: .03em;
  min-width: 3.2em;
  text-align: right;
}

.xw-icon-btn {
  min-width: 40px;
  padding: 8px 10px;
}

.xw-pause-icon {
  display: inline-block;
  width: 12px;
  height: 14px;
  border-left: 4px solid currentColor;
  border-right: 4px solid currentColor;
  border-radius: 1px;
}

.btn:disabled {
  opacity: 0.5;
  cursor: default;
}

.xw-menu {
  position: relative;
}

.xw-menu-list {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  z-index: 50;
  min-width: 150px;
  display: flex;
  flex-direction: column;
  padding: 7px;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  outline: 1.5px dashed var(--stitch-color);
  outline-offset: -4px;
  box-shadow: 0 8px 22px rgba(17, 12, 6, .16);
}

.xw-actions .xw-menu:last-of-type .xw-menu-list {
  left: auto;
  right: 0;
}

.xw-menu-list button {
  min-height: 40px;
  padding: 8px 12px;
  text-align: left;
  font: inherit;
  font-weight: 600;
  color: var(--fg);
  background: none;
  border: 0;
  border-radius: var(--radius-sm);
  cursor: pointer;
}

.xw-menu-list button:hover,
.xw-menu-list button:focus-visible {
  background: var(--sand);
}

/* ---- Body layout ---- */

.xw-body {
  --cell: 36px;
  position: relative;
  display: flex;
  align-items: flex-start;
  gap: 24px;
}

.xw-board-col {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 0;
  max-width: 100%;
}

.xw-grid-wrap {
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
}

/* ---- Grid: aida cloth ---- */

/* The 1px gaps between squares show the grid's own background: a running stitch drawn by two conic-gradient tiles,
   one per direction, sized to the cell pitch so the dashes land exactly in the gaps. */
.xw-grid {
  position: relative;
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell));
  grid-auto-rows: var(--cell);
  gap: 1px;
  width: max-content;
  background-color: var(--xw-cell);
  background-image:
    conic-gradient(from 180deg at 3px calc(100% - 1px), var(--xw-line) 90deg, transparent 0),
    conic-gradient(from 0deg at calc(100% - 1px) 3px, var(--xw-line) 90deg, transparent 0);
  background-size: 5px calc(var(--cell) + 1px), calc(var(--cell) + 1px) 5px;
  border: 2px solid var(--xw-frame);
  border-radius: 3px;
  outline: none;
  user-select: none;
  -webkit-user-select: none;
}

.xw-grid:focus-visible {
  box-shadow: 0 0 0 3px var(--focus);
}

.xw-cell {
  position: relative;
  background: var(--xw-cell);
  color: var(--xw-letter);
  cursor: pointer;
  overflow: hidden;
  touch-action: manipulation;
}

.xw-cell.xw-black {
  background-color: var(--xw-black);
  background-image:
    repeating-linear-gradient(0deg, var(--xw-black-weave) 0 1px, transparent 1px 3px),
    repeating-linear-gradient(90deg, var(--xw-black-weave) 0 1px, transparent 1px 3px);
  cursor: default;
}

.xw-cell.in-word {
  background: var(--xw-word);
}

/* Selected square: a terra thread just inside the edge on a terra wash. */
.xw-cell.selected {
  background: var(--xw-selected);
  outline: 2px solid var(--xw-sel-thread);
  outline-offset: -2px;
}

.xw-num {
  position: absolute;
  top: 1px;
  left: 2px;
  font-size: max(8px, calc(var(--cell) * 0.28));
  font-weight: 600;
  line-height: 1;
  color: var(--xw-num);
  pointer-events: none;
}

/* Embroidered letter: heavy rounded type with a 1px thread shade (no blur, so it stays cheap on a 15x15 grid). */
.xw-letter {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding-bottom: calc(var(--cell) * 0.07);
  font-family: var(--font-yarn);
  font-size: calc(var(--cell) * 0.58);
  font-weight: 700;
  letter-spacing: .02em;
  line-height: 1;
  text-shadow: 1px 1px 0 var(--xw-letter-shade);
  pointer-events: none;
}

/* A typed letter is sewn in (see stitch-in in base.css); the thread is drawn by the letter's own ::after,
   because the square's ::after is the wrong mark. */
.xw-letter.stitch-in {
  --stitch-in-color: var(--xw-sel-thread);
}

/* Check: a red running stitch through a wrong letter (persists until the letter changes). */
.xw-cell.wrong::after {
  content: "";
  position: absolute;
  inset: 12%;
  pointer-events: none;
  background: linear-gradient(
    to top right,
    transparent calc(50% - 1.25px),
    var(--xw-wrong) calc(50% - 1.25px),
    var(--xw-wrong) calc(50% + 1.25px),
    transparent calc(50% + 1.25px)
  );
  -webkit-mask-image: repeating-linear-gradient(45deg, #000 0 5px, transparent 5px 8px);
  mask-image: repeating-linear-gradient(45deg, #000 0 5px, transparent 5px 8px);
}

.xw-cell.wrong .xw-letter {
  color: var(--xw-wrong);
}

/* Reveal: a small indigo cross-stitch in the top-right corner; the letter is locked. */
.xw-cell.revealed::before {
  content: "";
  position: absolute;
  top: max(2px, calc(var(--cell) * 0.07));
  right: max(2px, calc(var(--cell) * 0.07));
  width: max(6px, calc(var(--cell) * 0.24));
  height: max(6px, calc(var(--cell) * 0.24));
  background:
    linear-gradient(45deg, transparent calc(50% - 1px), var(--xw-reveal) calc(50% - 1px) calc(50% + 1px), transparent calc(50% + 1px)),
    linear-gradient(-45deg, transparent calc(50% - 1px), var(--xw-reveal) calc(50% - 1px) calc(50% + 1px), transparent calc(50% + 1px));
  pointer-events: none;
}

/* Solved: the frame becomes a gold running stitch on cloth. It runs in once when the puzzle is solved (xw-sewn),
   not on reload. */
.xw-body.is-done .xw-grid {
  border-color: var(--xw-cell);
}

.xw-body.is-done .xw-grid::after {
  content: "";
  position: absolute;
  inset: -2px;
  border: 2px dashed var(--thread-gold);
  border-radius: 3px;
  pointer-events: none;
}

.xw-body.is-done .xw-grid.xw-sewn::after {
  animation: stitch-run 300ms var(--ease-thread) both;
}

/* ---- Clue bar ---- */

.xw-clue-bar {
  display: flex;
  align-items: stretch;
  gap: 6px;
  min-height: 52px;
  background: var(--xw-word);
  border-radius: var(--radius);
  outline: 1.5px dashed var(--stitch-color);
  outline-offset: -4px;
  padding: 5px;
  order: -1;
}

.xw-clue-nav {
  font-size: 1.4rem;
  line-height: 1;
  background: transparent;
  border-color: transparent;
}

.xw-clue-nav:not(:focus-visible) {
  outline-color: transparent;
}

.xw-clue-nav:hover {
  background: color-mix(in srgb, var(--card-bg) 60%, transparent);
}

.xw-clue-bar-text {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  padding: 4px 6px;
  font-size: 1rem;
}

.xw-clue-bar-label {
  font-family: var(--font-yarn);
  font-weight: 800;
  letter-spacing: .03em;
  color: var(--terra-text);
  flex: 0 0 auto;
}

.xw-clue-bar-clue {
  overflow-wrap: anywhere;
}

/* ---- Clue lists ---- */

.xw-clues {
  flex: 1 1 300px;
  min-width: 0;
}

.xw-clues-toggle {
  display: none;
  width: 100%;
}

.xw-clue-lists {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.xw-clue-list-wrap h3 {
  margin: 0 0 6px;
  padding-bottom: 8px;
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--terra-text);
  background: repeating-linear-gradient(90deg, var(--stitch-color) 0 6px, transparent 6px 10px) left bottom / 100% var(--stitch-w) no-repeat;
}

.xw-clue-list {
  position: relative;
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: var(--list-h, 480px);
  overflow-y: auto;
}

.xw-clue {
  display: flex;
  gap: 8px;
  padding: 6px 8px;
  border-left: 4px solid transparent;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  cursor: pointer;
  font-size: 0.95rem;
  line-height: 1.3;
}

.xw-clue:hover {
  background: var(--card-bg);
}

.xw-clue-num {
  flex: 0 0 1.8em;
  font-weight: 700;
  text-align: right;
}

.xw-clue.filled .xw-clue-text {
  color: var(--muted);
}

.xw-clue.cross {
  border-left-color: var(--xw-clue-cross);
}

.xw-clue.active {
  background: var(--xw-clue-active);
  border-left-color: var(--xw-sel-thread);
}

.xw-clue.active .xw-clue-text {
  color: inherit;
}

/* ---- Pause overlay ---- */

.xw-pause-overlay {
  position: absolute;
  inset: -4px;
  z-index: 40;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 80px;
  background-color: var(--xw-overlay);
  background-image:
    repeating-linear-gradient(0deg, var(--fabric-weave) 0 1px, transparent 1px 4px),
    repeating-linear-gradient(90deg, var(--fabric-weave) 0 1px, transparent 1px 4px);
  border-radius: var(--radius);
}

.xw-pause-card {
  text-align: center;
  padding: 24px 28px;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  outline: var(--stitch-w) dashed var(--stitch-color);
  outline-offset: calc(-1 * (var(--stitch-w) + var(--stitch-inset) + 2px));
  box-shadow: 0 1px 0 var(--border), 0 3px 10px rgba(42, 33, 24, .06);
}

.xw-pause-title {
  font-family: var(--font-display);
  font-size: 1.3rem;
  font-weight: 700;
  margin: 0 0 16px;
}

/* ---- Solved state ---- */

.xw-solved-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  flex-wrap: wrap;
  width: fit-content;
  max-width: 100%;
  margin: 16px auto 0;
  padding: 10px 12px 10px 18px;
  font-weight: 600;
  background: color-mix(in srgb, var(--thread-gold) 14%, var(--card-bg));
  border-radius: var(--radius);
  outline: 1.5px dashed var(--thread-gold);
  outline-offset: -4px;
}

#xw-solved-text {
  font-family: var(--font-yarn);
  font-weight: 800;
  letter-spacing: .02em;
}

.xw-body.is-done .xw-cell {
  cursor: default;
}

.xw-body.is-done .xw-keyboard {
  display: none;
}

/* ---- Results modal ---- */

.xw-result-time {
  font-size: 1.15rem;
  margin: 0 0 12px;
}

.xw-result-time strong {
  font-family: var(--font-yarn);
  font-weight: 800;
}

.xw-result-assist {
  color: var(--muted);
  font-size: 0.95rem;
}

.xw-stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  text-align: center;
  margin: 8px 0 16px;
}

.xw-stats > div {
  padding: 10px 4px 8px;
  border-radius: var(--radius);
  background: var(--sand);
  outline: 1.5px dashed var(--stitch-color);
  outline-offset: -5px;
}

.xw-stats strong {
  display: block;
  font-family: var(--font-yarn);
  font-size: 1.3rem;
  font-weight: 800;
  line-height: 1.25;
}

.xw-stats span {
  font-size: 0.8rem;
  color: var(--muted);
}

.xw-share-row {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin: 0;
}

.xw-share-preview {
  color: var(--muted);
  font-size: 0.9rem;
}

/* ---- On-screen keyboard (phones / touch only): small cloth labels ---- */

.xw-keyboard {
  display: none;
  flex-direction: column;
  gap: 6px;
  margin-top: 4px;
}

.xw-kb-row {
  display: flex;
  justify-content: center;
  gap: 4px;
}

.xw-key {
  flex: 1 1 0;
  max-width: 44px;
  min-height: 46px;
  padding: 0;
  font: inherit;
  font-family: var(--font-yarn);
  font-weight: 800;
  font-size: 1rem;
  color: var(--fg);
  background: var(--muted-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: 0 1px 0 var(--control-border);
  cursor: pointer;
  touch-action: manipulation;
  transition: translate .08s ease;
}

.xw-key:active {
  translate: 0 1px;
}

.xw-key-wide {
  flex-grow: 1.6;
  max-width: 68px;
}

@media (pointer: coarse) {
  .xw-keyboard {
    display: flex;
  }
}

/* ---- Mobile ---- */

@media (max-width: 719px) {
  .xw-main {
    padding: 8px 8px 24px;
  }

  .xw-toolbar {
    padding-bottom: 8px;
    margin-bottom: 8px;
  }

  .xw-actions {
    gap: 6px;
  }

  .xw-actions .btn {
    padding: 8px 10px;
  }

  .xw-body {
    flex-direction: column;
    align-items: center;
    gap: 12px;
  }

  .xw-board-col {
    align-items: stretch;
  }

  .xw-clue-bar {
    order: 0;
  }

  .xw-keyboard {
    display: flex;
  }

  .xw-clues {
    flex: none;
    width: 100%;
  }

  .xw-clues-toggle {
    display: flex;
  }

  .xw-clue-lists {
    display: none;
    grid-template-columns: 1fr;
    margin-top: 12px;
  }

  .xw-clues.open .xw-clue-lists {
    display: grid;
  }

  .xw-clue-list {
    max-height: none;
    overflow: visible;
  }
}
