/* Crossword: one-screen play area, themed to match rtwl.fun (body.fx). */
html:has(body.cw-page), body.cw-page {
  height: 100%;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
  color-scheme: dark;
}
body.cw-page {
  min-height: 0;
  touch-action: manipulation;
}

body.cw-page .game-wrap {
  width: min(1180px, calc(100% - 16px));
  height: 100%;
  height: 100dvh;
  max-height: 100dvh;
  margin: 0 auto;
  padding: max(6px, env(safe-area-inset-top)) 0 max(8px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow: hidden;
  box-sizing: border-box;
}

/* ---- header ---- */
.cw-top {
  display: flex;
  align-items: center;
  gap: 10px 14px;
  flex-shrink: 0;
  min-height: 0;
}
.cw-top .back {
  margin: 0;
  flex-shrink: 0;
  color: var(--fx-neon, #3dff8a);
  font-size: 0.92rem;
}
body.cw-page .cw-top .gg-head {
  flex: 1 1 auto;
  min-width: 0;
  align-items: center;
}
body.cw-page .cw-top h1 {
  margin: 0;
  font-size: clamp(1.15rem, 2.2vw + 0.4rem, 1.75rem);
  line-height: 1.1;
  filter: drop-shadow(0 0 12px rgba(25, 193, 85, 0.4));
}
body.cw-page .gg-links a,
body.cw-page .cw-clues-btn,
body.cw-page .cw-boards-btn,
body.cw-page .cw-clues-close,
body.cw-page .cw-boards-close {
  font-weight: 600;
  color: var(--fx-text, #e6fff0);
  text-decoration: none;
  padding: 4px 12px;
  min-height: 32px;
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--fx-edge, rgba(61, 255, 138, 0.24));
  border-radius: 999px;
  background: rgba(25, 193, 85, 0.08);
  cursor: pointer;
  font: inherit;
  font-weight: 600;
}
body.cw-page .gg-links a:hover,
body.cw-page .gg-links a:focus-visible,
body.cw-page .cw-clues-btn:hover,
body.cw-page .cw-boards-btn:hover,
body.cw-page .cw-clues-close:hover,
body.cw-page .cw-boards-close:hover {
  background: rgba(25, 193, 85, 0.16);
  border-color: var(--fx-neon, #3dff8a);
  outline: none;
}
body.cw-page .cw-clues-btn,
body.cw-page .cw-boards-btn,
body.cw-page .cw-clues-close,
body.cw-page .cw-boards-close { display: none; }
body.cw-page .cw-clues-close,
body.cw-page .cw-boards-close { font-size: 0.9rem; }
.cw-check {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
  padding: 0;
  margin: -1px;
}
.cw-clues-mask, .cw-boards-mask { display: none; }

/* ---- setup / controls ---- */
body.cw-page .setup {
  flex-shrink: 0;
  margin: 0;
  padding: 8px 12px;
}
body.cw-page .setup .row {
  flex-wrap: nowrap;
  gap: 8px 10px;
  align-items: flex-end;
}
body.cw-page .setup .row > * { flex: 1 1 0; min-width: 0; }
body.cw-page .setup .row > .btn { flex: 0 0 auto; }
body.cw-page .setup .field { margin-bottom: 0; color: var(--fx-neon, #3dff8a); font-size: 0.72rem; letter-spacing: 0.1em; }
body.cw-page input[type=text],
body.cw-page select {
  margin-top: 3px;
  padding: 6px 10px;
  color: var(--fx-text, #e6fff0);
  background: rgba(3, 15, 9, 0.72);
  border: 1px solid rgba(61, 255, 138, 0.3);
  border-radius: 12px;
  font-size: 16px;
}
body.cw-page input::placeholder { color: #7fae96; }
body.cw-page input:focus,
body.cw-page select:focus {
  outline: 2px solid var(--fx-neon, #3dff8a);
  outline-offset: 1px;
  border-color: var(--fx-neon, #3dff8a);
}
body.cw-page .btn {
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  border: 0;
  padding: 8px 16px;
  border-radius: 999px;
  background: linear-gradient(180deg, #3dff8a, #19c155);
  color: #03140b;
  box-shadow: 0 0 18px rgba(25, 193, 85, 0.45);
}
body.cw-page .btn:active { transform: translateY(1px); box-shadow: 0 0 10px rgba(25, 193, 85, 0.35); }
body.cw-page .btn.ghost {
  background: rgba(25, 193, 85, 0.08);
  color: var(--fx-text, #e6fff0);
  box-shadow: none;
  border: 1px solid var(--fx-edge, rgba(61, 255, 138, 0.24));
}
body.cw-page .btn:disabled { opacity: .5; cursor: default; }

/* ---- play + side layout ---- */
.cw-layout {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(240px, 320px);
  gap: 10px;
  align-items: stretch;
}
.cw-play {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  padding: 8px 12px 10px;
}
.cw-side {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow: hidden;
}
.cw-clues-dock, .cw-scores-dock {
  min-height: 0;
  display: flex;
  flex-direction: column;
}
.cw-clues-dock { flex: 1 1 60%; }
.cw-scores-dock { flex: 0 1 40%; }
body.cw-page .cw-side .panel {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  padding: 12px 14px;
  overscroll-behavior: contain;
  display: flex;
  flex-direction: column;
}
.cw-side-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
body.cw-page .cw-side h3,
body.cw-page .cw-clue-h {
  color: #fff;
  text-shadow: 0 0 14px rgba(25, 193, 85, 0.35);
  margin: 0 0 8px;
  font-size: 1.15rem;
}
body.cw-page .cw-clue-h {
  font-family: Chango, Georgia, serif;
  font-weight: 400;
  font-size: 1rem;
  margin: 10px 0 6px;
  color: var(--fx-neon, #3dff8a);
  text-shadow: none;
}
body.cw-page .cw-clue-h:first-of-type { margin-top: 0; }
.cw-board-table { font-size: 0.9rem; overflow: auto; min-height: 0; }
body.cw-page .cw-rules {
  font-size: 0.8rem;
  line-height: 1.35;
  margin: 10px 0 0;
  color: var(--fx-dim, #a6d8bb);
}
body.cw-page .tab {
  background: rgba(3, 15, 9, 0.55);
  color: var(--fx-dim, #a6d8bb);
  border-color: var(--fx-edge, rgba(61, 255, 138, 0.24));
  padding: 4px 10px;
}
body.cw-page .tab.on {
  background: rgba(25, 193, 85, 0.16);
  color: #7dffb0;
  border-color: var(--fx-neon, #3dff8a);
}
body.cw-page .muted { color: var(--fx-dim, #a6d8bb); }
body.cw-page .msg { color: #7dffb0; margin: 4px 0 0; min-height: 1.15em; font-size: 0.95rem; }
body.cw-page .msg.bad { color: #ffb4ab; }

/* ---- hud, active clue, grid ---- */
.hud {
  display: flex;
  justify-content: space-around;
  text-align: center;
  margin-bottom: 6px;
  flex-shrink: 0;
  padding: 0;
  background: none;
  border: 0;
  box-shadow: none;
}
.hud .k {
  display: block;
  font-size: 0.68rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--fx-neon, #3dff8a);
  font-weight: 600;
}
.hud .v {
  font-family: Chango, Georgia, serif;
  font-size: clamp(1.05rem, 2.2vh, 1.5rem);
  font-variant-numeric: tabular-nums;
  color: #fff;
}

.activeclue {
  flex-shrink: 0;
  min-height: 2.2em;
  margin: 0 0 8px;
  padding: 6px 10px;
  font-weight: 600;
  color: var(--fx-text, #e6fff0);
  background: rgba(3, 15, 9, 0.45);
  border: 1px solid var(--fx-edge, rgba(61, 255, 138, 0.24));
  border-radius: 12px;
  line-height: 1.3;
}
.activeclue b {
  font-family: Chango, Georgia, serif;
  font-weight: 400;
  color: var(--fx-neon, #3dff8a);
  margin-right: 8px;
}

.cw-grid-wrap {
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
  width: 100%;
  display: grid;
  place-items: center;
  container-type: size;
}
.cwgrid {
  --cw-cols: 8;
  --cw-rows: 8;
  --cw-gap: clamp(1px, 0.4cqi, 3px);
  --cw-s: min(
    calc((100cqw - (var(--cw-cols) - 1) * var(--cw-gap)) / var(--cw-cols)),
    calc((100cqh - (var(--cw-rows) - 1) * var(--cw-gap)) / var(--cw-rows))
  );
  width: calc(var(--cw-cols) * var(--cw-s) + (var(--cw-cols) - 1) * var(--cw-gap));
  max-width: 100cqw;
  max-height: 100cqh;
  display: grid;
  gap: var(--cw-gap);
  grid-template-rows: repeat(var(--cw-rows), var(--cw-s));
}
@supports not (container-type: size) {
  .cwgrid { width: min(100%, 70vw, calc((100dvh - 16rem) * var(--cw-cols) / var(--cw-rows))); }
}

.cwc { position: relative; aspect-ratio: auto; width: 100%; height: 100%; border-radius: 4px; min-width: 0; min-height: 0; }
.cwc.block { background: transparent; }
.cwc.cell {
  background: #04150d;
  border: 1px solid rgba(61, 255, 138, 0.28);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
}
.cwc .n {
  position: absolute;
  top: 1px;
  left: 3px;
  font-size: max(8px, 0.28em);
  font-weight: 600;
  color: var(--fx-neon, #3dff8a);
  pointer-events: none;
  z-index: 1;
  line-height: 1;
}
.cwc input {
  width: 100%;
  height: 100%;
  border: 0;
  background: transparent;
  text-align: center;
  font: inherit;
  font-weight: 700;
  font-size: max(16px, calc(100cqw / var(--cw-cols) * 0.5));
  text-transform: uppercase;
  color: #e6fff0;
  padding: 10% 0 0;
  caret-color: transparent;
  cursor: pointer;
  border-radius: 4px;
  -webkit-user-select: text;
  user-select: text;
}
.cwc input:focus { outline: none; }
.cwc.word { background: rgba(25, 193, 85, 0.22); border-color: rgba(61, 255, 138, 0.45); }
.cwc.cur {
  background: var(--fx-neon, #3dff8a);
  border-color: #b8ffd2;
  box-shadow: 0 0 12px rgba(61, 255, 138, 0.55);
  z-index: 2;
}
.cwc.cur input { color: #03140b; }
.cwc.cur .n { color: #0e7a36; }
.cwc.wrong { background: #4a1a16; border-color: #ff8a80; }
.cwc.wrong input { color: #ffb4ab; }
.cwc.rev input { color: #7dffb0; font-style: italic; }
.cwc.cur.wrong { background: #e57a72; }
.cwc.cur.wrong input { color: #2a0a08; }

.cw-foot { flex-shrink: 0; }
.tools { margin-top: 8px; justify-content: center; flex-wrap: wrap; }
.tools .btn { flex: 0 1 auto; }

/* clues list */
.clues ol { list-style: none; margin: 0; padding: 0; }
.clues li {
  padding: 5px 8px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.95rem;
  color: var(--fx-text, #e6fff0);
}
.clues li b {
  display: inline-block;
  min-width: 1.6em;
  color: var(--fx-neon, #3dff8a);
}
.clues li:hover { background: rgba(25, 193, 85, 0.1); }
.clues li.on {
  background: rgba(25, 193, 85, 0.22);
  border: 1px solid rgba(61, 255, 138, 0.35);
}
.clues li.done { text-decoration: line-through; opacity: .55; }

@media (min-width: 800px) {
  body.cw-page .ggc-fab { right: 20px; }
}

/* ---- compact / phone ---- */
@media (max-width: 799px), (orientation: portrait) and (max-width: 899px) {
  .cw-layout { grid-template-columns: 1fr; }
  body.cw-page .cw-clues-btn,
  body.cw-page .cw-boards-btn { display: inline-flex; }
  .cw-side {
    display: contents;
  }
  body.cw-page .cw-clues-dock,
  body.cw-page .cw-scores-dock {
    display: none;
    position: fixed;
    inset: max(10px, env(safe-area-inset-top)) 12px max(10px, env(safe-area-inset-bottom));
    z-index: 90;
    max-width: 420px;
    margin: 0 auto;
    overflow: hidden;
  }
  body.cw-page .cw-clues-dock .panel,
  body.cw-page .cw-scores-dock .panel {
    background: linear-gradient(160deg, rgba(10, 46, 28, 0.98), rgba(3, 15, 9, 0.98) 55%);
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.7), 0 0 30px rgba(25, 193, 85, 0.25);
  }
  body.cw-page .cw-clues-close,
  body.cw-page .cw-boards-close { display: inline-flex; }
  #cw-show-clues:checked ~ .cw-layout .cw-clues-dock { display: flex; }
  #cw-show-boards:checked ~ .cw-layout .cw-scores-dock { display: flex; }
  #cw-show-clues:checked ~ .cw-top .cw-clues-btn,
  #cw-show-boards:checked ~ .cw-top .cw-boards-btn {
    border-color: var(--fx-neon, #3dff8a);
    background: rgba(25, 193, 85, 0.2);
  }
  #cw-show-clues:checked ~ .cw-clues-mask,
  #cw-show-boards:checked ~ .cw-boards-mask {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 85;
    background: rgba(0, 0, 0, 0.62);
    cursor: pointer;
  }
}
@media (max-width: 640px) {
  .back-full { display: none; }
  body.cw-page .setup .row { flex-wrap: wrap; }
  body.cw-page .setup .row > * { flex: 1 1 calc(50% - 10px); }
  body.cw-page .setup .row > .btn { flex: 1 1 100%; }
  body.cw-page .cw-top { flex-wrap: wrap; gap: 4px 8px; }
  body.cw-page .cw-top .gg-head { display: contents; }
  body.cw-page .cw-top h1 { font-size: 1.12rem; flex: 1 1 auto; min-width: 0; }
  body.cw-page .cw-top .gg-links { width: 100%; justify-content: center; flex-wrap: wrap; gap: 6px; }
  body.cw-page .gg-links a,
  body.cw-page .cw-clues-btn,
  body.cw-page .cw-boards-btn { min-height: 30px; padding: 3px 10px; font-size: 0.88rem; }
  body.cw-page .game-wrap { width: calc(100% - 12px); gap: 6px; }
  .activeclue { font-size: 0.92rem; min-height: 2em; padding: 5px 8px; }
  .tools .btn { padding: 7px 12px; }
}

/* On small or short screens, collapse the setup form once a puzzle is on the
   board, and hide it entirely while a grid cell is focused so the on-screen
   keyboard leaves room for the grid and current clue. */
@media (max-width: 799px), (max-height: 520px) {
  body.cw-page:has(#grid .cwc) .setup .field { display: none; }
  body.cw-page:has(#grid .cwc) .setup { padding: 4px 10px; }
  body.cw-page:has(#grid .cwc) .setup .row { justify-content: flex-end; }
  body.cw-page:has(#grid input:focus) .setup { display: none; }
}
@media (max-height: 520px) {
  body.cw-page .game-wrap { padding-top: 4px; padding-bottom: 4px; gap: 4px; }
  body.cw-page .setup { padding: 4px 10px; }
  body.cw-page .setup .field { font-size: 0; letter-spacing: 0; }
  body.cw-page .setup input, body.cw-page .setup select { padding: 4px 8px; font-size: 16px; letter-spacing: 0; text-transform: none; }
  body.cw-page:has(#grid .cwc) .setup { display: none; }
  .hud { margin-bottom: 2px; }
  .hud .v { font-size: 1rem; }
  .cw-rules { display: none; }
  .activeclue { margin-bottom: 4px; min-height: 1.6em; padding: 4px 8px; font-size: 0.88rem; }
  body.cw-page .cw-top h1 { font-size: 1.05rem; }
  body.cw-page .cw-play { padding: 4px 8px 6px; }
  body.cw-page .btn { padding: 6px 10px; font-size: 0.88rem; }
  .tools { margin-top: 4px; flex-wrap: nowrap; gap: 6px; }
  body.cw-page .msg { font-size: 0.85rem; min-height: 0; }
  body.cw-page .msg:not(.bad) { display: none; }
  .cw-play {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-rows: auto auto minmax(0, 1fr);
    grid-template-areas:
      "hud tools"
      "clue clue"
      "grid grid";
    gap: 4px 8px;
    align-items: stretch;
  }
  .hud { grid-area: hud; margin: 0; justify-content: flex-start; gap: 16px; align-self: center; }
  .cw-grid-wrap { grid-area: grid; }
  .activeclue { grid-area: clue; margin: 0; }
  .cw-foot { display: contents; }
  .tools { grid-area: tools; margin: 0; align-self: center; }
  .msg {
    grid-area: grid;
    align-self: end;
    justify-self: center;
    z-index: 3;
    margin: 0 0 2px;
    padding: 2px 8px;
    border-radius: 8px;
    background: rgba(3, 15, 9, 0.88);
    pointer-events: none;
    max-width: 95%;
  }
  /* While typing, keep the current clue and grid above the keyboard. */
  body.cw-page:has(#grid input:focus) .cw-top,
  body.cw-page:has(#grid input:focus) .tools,
  body.cw-page:has(#grid input:focus) .hud { display: none; }
}

@media (orientation: landscape) and (max-height: 500px) {
  body.cw-page .setup .row { flex-wrap: nowrap; }
  body.cw-page .setup .row > * { flex: 1 1 0; }
  body.cw-page .setup .row > .btn { flex: 0 0 auto; }
  .cw-layout { grid-template-columns: minmax(0, 1fr) minmax(200px, 260px); }
  .cw-side {
    display: flex;
    position: static;
  }
  body.cw-page .cw-clues-dock,
  body.cw-page .cw-scores-dock {
    display: flex;
    position: static;
    inset: auto;
    max-width: none;
    margin: 0;
  }
  body.cw-page .cw-clues-dock { flex: 1 1 70%; }
  body.cw-page .cw-scores-dock { display: none; }
  body.cw-page .cw-clues-btn,
  body.cw-page .cw-boards-btn,
  body.cw-page .cw-clues-close,
  body.cw-page .cw-boards-close { display: none; }
  /* Scores still reachable via the Scores button if we show it — keep a compact Scores control */
  body.cw-page .cw-boards-btn { display: inline-flex; }
  body.cw-page .cw-boards-close { display: inline-flex; }
  #cw-show-boards:checked ~ .cw-layout .cw-scores-dock {
    display: flex;
    position: fixed;
    inset: max(8px, env(safe-area-inset-top)) 12px max(8px, env(safe-area-inset-bottom));
    z-index: 90;
    max-width: 420px;
    margin: 0 auto;
  }
}

@media (prefers-reduced-motion: reduce) {
  body.cw-page .btn:active { transform: none; }
}
