:root {
  color-scheme: light dark;
  --bg: #fafaf8;
  --surface: #ffffff;
  --surface-subtle: #f3f2ee;
  --rule: rgba(20, 20, 19, 0.09);
  --rule-strong: rgba(20, 20, 19, 0.24);
  --tile-border: rgba(20, 20, 19, 0.13);
  --tile-border-hover: rgba(20, 20, 19, 0.28);
  --text: #141413;
  --text-muted: #5d5c58;
  --text-subtle: #8d8b85;
  --accent: #b83324;
  --accent-hover: #9f291c;
  --accent-fg: #ffffff;
  --accent-text: #b83324;
  --accent-bg: #b83324;
  --accent-soft: rgba(184, 51, 36, 0.08);
  --accent-soft-border: rgba(184, 51, 36, 0.32);
  --accent-border: #a12a1c;
  --err: #b83324;
  --shadow-tile: 0 1px 2px rgba(20, 20, 19, 0.06);
  --shadow-lift:
    0 5px 14px rgba(20, 20, 19, 0.09),
    0 1px 2px rgba(20, 20, 19, 0.06);
  --shadow-drag:
    0 14px 28px rgba(20, 20, 19, 0.16),
    0 2px 6px rgba(20, 20, 19, 0.08);
  --shadow-modal:
    0 22px 48px rgba(20, 20, 19, 0.18),
    0 4px 12px rgba(20, 20, 19, 0.08);
  --backdrop: rgba(20, 20, 19, 0.34);
  --rad: 4px;
  --rad-lg: 6px;
  --serif: "Iowan Old Style", "New York", "Palatino Linotype", "Book Antiqua", Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, monospace;
  --tile-w: 48px;
  --tile-h: 56px;
  --tile-gap: 7px;
  --tile-font: 25px;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131314;
    --surface: #1d1d1f;
    --surface-subtle: #18181a;
    --rule: rgba(243, 243, 240, 0.09);
    --rule-strong: rgba(243, 243, 240, 0.26);
    --tile-border: rgba(243, 243, 240, 0.13);
    --tile-border-hover: rgba(243, 243, 240, 0.3);
    --text: #f3f3f0;
    --text-muted: #9e9e9a;
    --text-subtle: #6e6e6a;
    --accent: #d45240;
    --accent-hover: #df6453;
    --accent-fg: #ffffff;
    --accent-text: #e87a6b;
    --accent-bg: #c94836;
    --accent-soft: rgba(212, 82, 64, 0.14);
    --accent-soft-border: rgba(212, 82, 64, 0.38);
    --accent-border: #df6655;
    --err: #e87a6b;
    --shadow-tile: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-lift:
      0 6px 16px rgba(0, 0, 0, 0.42),
      0 1px 3px rgba(0, 0, 0, 0.32);
    --shadow-drag:
      0 16px 32px rgba(0, 0, 0, 0.56),
      0 2px 6px rgba(0, 0, 0, 0.42);
    --shadow-modal:
      0 24px 56px rgba(0, 0, 0, 0.64),
      0 4px 14px rgba(0, 0, 0, 0.42);
    --backdrop: rgba(0, 0, 0, 0.62);
  }
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  padding: 0;
  user-select: none;
  -webkit-user-select: none;
}

body {
  min-height: 100svh;
  display: flex;
  flex-direction: column;
}

.app {
  width: min(100% - 2rem, 500px);
  margin: 0 auto auto;
  padding: clamp(2rem, 7svh, 4.25rem) 0 2rem;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* Header */
.page-header {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.title-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.wordmark {
  margin: 0;
  font-family: var(--serif);
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: -0.025em;
  line-height: 1;
  display: inline-flex;
  align-items: baseline;
}

.wordmark .wm-cap {
  color: var(--accent-text);
}

.subtitle {
  margin: 2px 0 0;
  font-family: var(--sans);
  font-size: 0.9rem;
  line-height: 1.5;
  color: var(--text-muted);
}

.subtitle .inline-cap {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-family: var(--serif);
  font-weight: 700;
  font-size: 0.85rem;
  line-height: 1;
  min-width: 20px;
  height: 21px;
  padding: 0 4px;
  border-radius: 3px;
  vertical-align: -1px;
  background: var(--accent-bg);
  color: var(--accent-fg);
  border: 1px solid var(--accent-border);
}

.puzzle-tabs {
  display: inline-flex;
  align-items: baseline;
  gap: 14px;
  background: transparent;
  padding: 0;
  border: none;
}

.puzzle-tab {
  appearance: none;
  border: none;
  border-bottom: 1.5px solid transparent;
  background: transparent;
  color: var(--text-subtle);
  font-family: var(--sans);
  font-size: 13px;
  font-weight: 500;
  letter-spacing: -0.005em;
  padding: 2px 1px 4px;
  border-radius: 0;
  cursor: pointer;
  transition: color 140ms ease, border-color 140ms ease;
}

.puzzle-tab:hover {
  color: var(--text);
}

.puzzle-tab:focus-visible {
  outline: 1.5px solid var(--text);
  outline-offset: 3px;
}

.puzzle-tab.active {
  color: var(--text);
  font-weight: 600;
  border-bottom-color: var(--text);
}

.puzzle-tab .done-dot {
  display: inline-block;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--accent);
  margin-left: 4px;
  vertical-align: 1px;
}

.puzzle-date {
  margin: 0;
  font-family: var(--sans);
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  font-variant-numeric: lining-nums tabular-nums;
}

/* Toolbar */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-top: 4px;
}

.controls {
  display: flex;
  align-items: center;
  gap: 6px;
}

.controls #hintBtn {
  min-width: 62px;
  justify-content: center;
}

.move-pill {
  font-family: var(--sans);
  font-size: 13px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.move-pill strong {
  color: var(--text);
  font-weight: 650;
}

.move-pill .meta-sep {
  margin: 0 5px;
  color: var(--text-subtle);
}

.move-pill .par-inline {
  color: var(--text-subtle);
}

.btn {
  appearance: none;
  border: 1px solid var(--tile-border);
  background: transparent;
  color: var(--text);
  font-family: var(--sans);
  font-size: 12.5px;
  font-weight: 500;
  padding: 5px 11px;
  border-radius: var(--rad);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  transition:
    opacity 120ms ease,
    border-color 140ms ease,
    background-color 140ms ease,
    transform 100ms ease;
}

.btn:hover:not(:disabled) {
  border-color: var(--rule-strong);
  background: var(--surface);
}

.btn:focus-visible {
  outline: 1.5px solid var(--text);
  outline-offset: 2px;
}

.btn:active:not(:disabled) {
  transform: translateY(1px);
}

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

.btn-primary {
  background: var(--accent-bg);
  color: var(--accent-fg);
  border-color: var(--accent-border);
  font-weight: 600;
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

/* Board — open rows + clean goal shelf */
.board {
  display: flex;
  flex-direction: column;
  gap: 6px;
  position: relative;
  padding-top: 2px;
}

.row-wrap {
  display: flex;
  align-items: center;
  position: relative;
  border-radius: var(--rad-lg);
  transition: background-color 180ms ease, box-shadow 180ms ease;
}

.row-wrap.is-goal {
  background: var(--surface-subtle);
  box-shadow: inset 0 -2px 0 var(--accent);
  margin-top: 4px;
}

.row-wrap.is-goal.won {
  box-shadow:
    inset 0 0 0 1px var(--accent-soft-border),
    inset 0 -2px 0 var(--accent);
}

.row-wrap[data-len="8"] {
  --tile-w: 42px;
  --tile-h: 52px;
  --tile-gap: 5px;
  --tile-font: 22px;
}

.row-wrap[data-len="9"] {
  --tile-w: 38px;
  --tile-h: 48px;
  --tile-gap: 5px;
  --tile-font: 20px;
}

.row-track {
  flex: 1;
  min-height: 74px;
  padding: 9px 14px;
  background: transparent;
  border: none;
  border-radius: inherit;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.row-wrap.dimmed .word-row {
  opacity: 0.45;
}

.row-wrap.show-carets .row-track {
  cursor: pointer;
  touch-action: pinch-zoom;
}

.row-wrap.show-carets .tile {
  cursor: pointer;
}

.row-wrap.show-carets .tile:hover {
  transform: none;
  box-shadow: var(--shadow-tile);
  border-color: var(--tile-border);
}

.row-wrap.show-carets .tile.is-cap:hover {
  border-color: var(--accent-border);
}

.row-wrap.shake .word-row {
  animation: shake 320ms cubic-bezier(0.36, 0.07, 0.19, 0.97);
}

@keyframes shake {
  15%, 85% { transform: translateX(-4px); }
  30%, 70% { transform: translateX(5px); }
  45%, 60% { transform: translateX(-3px); }
}

.word-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--tile-gap);
  min-height: 56px;
  transition: opacity 150ms ease;
}

/* Serif letterpress tiles */
.tile,
.slot-ghost {
  font: 700 var(--tile-font)/1 var(--serif);
}

.tile {
  appearance: none;
  padding: 0;
  width: var(--tile-w);
  height: var(--tile-h);
  border-radius: var(--rad);
  background: var(--surface);
  border: 1px solid var(--tile-border);
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  position: relative;
  cursor: grab;
  touch-action: pinch-zoom;
  box-shadow: var(--shadow-tile);
  transition:
    transform 140ms cubic-bezier(0.2, 0.8, 0.2, 1),
    width 140ms cubic-bezier(0.2, 0.8, 0.2, 1),
    margin 140ms cubic-bezier(0.2, 0.8, 0.2, 1),
    box-shadow 140ms ease,
    opacity 140ms ease,
    border-color 140ms ease,
    background-color 140ms ease;
}

.tile:hover {
  transform: translateY(-1.5px);
  border-color: var(--tile-border-hover);
  box-shadow: var(--shadow-lift);
}

/* Illuminated Dropcap block — solid vermilion, zero ornament */
.tile.is-cap {
  background: var(--accent-bg);
  border-color: var(--accent-border);
  color: var(--accent-fg);
}

.tile.is-cap:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

/* Tap-to-select plucked state */
.tile.selected {
  transform: translateY(-5px);
  border-style: dashed;
  border-color: var(--rule-strong);
  box-shadow: none;
  opacity: 0.52;
  z-index: 2;
}

.tile.selected:hover {
  transform: translateY(-5px);
  box-shadow: none;
  opacity: 0.68;
}

.tile.is-cap.selected {
  background: var(--accent-soft);
  color: var(--accent-text);
  border-color: var(--accent);
  opacity: 0.85;
}

.tile.is-cap.selected:hover {
  background: var(--accent-soft);
  border-color: var(--accent);
  opacity: 1;
}

.tile.dragging-origin {
  width: 0 !important;
  min-width: 0 !important;
  padding: 0 !important;
  margin: 0 calc(var(--tile-gap) / -2) !important;
  border-width: 0 !important;
  opacity: 0 !important;
  overflow: hidden;
  pointer-events: none;
}

.tile.just-landed {
  animation: popIn 200ms cubic-bezier(0.2, 0.9, 0.3, 1.12);
}

@keyframes popIn {
  0% { transform: scale(0.92); }
  100% { transform: scale(1); }
}

/* Insertion gap slot */
.slot {
  width: 0;
  height: var(--tile-h);
  position: relative;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  overflow: visible;
  margin: 0 calc(var(--tile-gap) / -2);
  transition:
    width 160ms cubic-bezier(0.2, 0.8, 0.2, 1),
    margin 160ms cubic-bezier(0.2, 0.8, 0.2, 1);
}

.slot.open-drag,
.slot.open-preview {
  width: var(--tile-w);
  margin: 0;
}

.slot-ghost {
  position: absolute;
  inset: 0;
  border-radius: var(--rad);
  border: 1px dashed var(--rule-strong);
  background: transparent;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transform: scale(0.88);
  pointer-events: none;
  overflow: hidden;
  transition:
    opacity 130ms ease,
    transform 160ms cubic-bezier(0.2, 0.8, 0.2, 1);
}

.slot.open-drag .slot-ghost,
.slot.open-preview .slot-ghost {
  opacity: 1;
  transform: scale(1);
}

.slot.is-cap-slot .slot-ghost {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent-text);
}

.slot.open-preview .slot-ghost-char {
  opacity: 0.65;
}

.slot.open-preview.is-cap-slot .slot-ghost-char {
  opacity: 0.9;
}

/* Minimal hairline insertion ticks for tap-to-place (no arrow glyphs) */
.slot-caret {
  appearance: none;
  position: absolute;
  left: 50%;
  width: 28px;
  height: 20px;
  padding: 0;
  border: none;
  background: transparent;
  color: var(--text-subtle);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 3;
  opacity: 0;
  pointer-events: none;
  transform: translateX(-50%) scaleY(0.6);
  transition:
    opacity 140ms ease,
    color 140ms ease,
    transform 160ms cubic-bezier(0.2, 0.8, 0.2, 1);
}

.slot-caret:focus-visible {
  outline: 1.5px solid var(--text);
  outline-offset: -3px;
  border-radius: var(--rad);
}

.row-wrap.drop-from-above .slot-caret {
  top: -16px;
}

.row-wrap.drop-from-below .slot-caret {
  bottom: -16px;
}

.row-wrap.show-carets .slot-caret {
  opacity: 0.72;
  pointer-events: auto;
  transform: translateX(-50%) scaleY(1);
}

.row-wrap.show-carets.hover-drop .slot-caret {
  opacity: 0.22;
}

.row-wrap.show-carets .slot.open-preview .slot-caret {
  opacity: 1;
  color: var(--text);
  transform: translateX(-50%) scaleY(1.2);
}

.row-wrap.show-carets .slot.open-preview.is-cap-slot .slot-caret {
  color: var(--accent-text);
}

.caret-tick {
  display: block;
  width: 2px;
  height: 9px;
  border-radius: 999px;
  background: currentColor;
  flex-shrink: 0;
}

.drag-ghost {
  position: fixed;
  left: 0;
  top: 0;
  pointer-events: none;
  z-index: 1000;
  transform: translate(-50%, -50%) scale(1.05);
  box-shadow: var(--shadow-drag);
  cursor: grabbing;
}

/* Feedback note */
.feedback {
  min-height: 20px;
  text-align: center;
  font-family: var(--sans);
  font-size: 13px;
  color: var(--text-muted);
  transition: opacity 160ms ease;
}

.feedback.error {
  color: var(--err);
}

.tile:focus-visible {
  outline: 2px solid var(--text);
  outline-offset: 3px;
}

.tile.hint-source {
  box-shadow:
    0 0 0 2px var(--bg),
    0 0 0 4px var(--accent);
}

.slot.hint-gap::after {
  content: "";
  position: absolute;
  width: 3px;
  height: 60%;
  background: var(--accent);
  border-radius: 999px;
  pointer-events: none;
}

/* Move trail — reserved height so board never shifts */
.trail-bar {
  min-height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  flex-wrap: wrap;
}

#winHost:empty {
  display: none;
}

.trail-chip {
  font-family: var(--mono);
  font-size: 11.5px;
  font-weight: 500;
  color: var(--text-muted);
  display: inline-flex;
  align-items: center;
  letter-spacing: 0.01em;
}

.trail-chip + .trail-chip::before {
  content: "·";
  margin-right: 8px;
  color: var(--text-subtle);
  opacity: 0.65;
}

.trail-chip.is-cap {
  color: var(--accent-text);
  font-weight: 700;
}

/* Win modal overlay — never disturbs underlying typeset layout */
.win-backdrop {
  position: fixed;
  inset: 0;
  z-index: 900;
  background: var(--backdrop);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.win-backdrop.animate-in {
  animation: fadeIn 160ms ease;
}

.win-card {
  position: relative;
  width: min(100%, 420px);
  max-height: calc(100svh - 2rem);
  overflow-y: auto;
  padding: 20px 22px;
  border-radius: var(--rad-lg);
  background: var(--surface);
  border: 1px solid var(--tile-border);
  box-shadow: var(--shadow-modal);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.win-backdrop.animate-in .win-card {
  animation: modalIn 180ms cubic-bezier(0.2, 0.8, 0.2, 1);
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes modalIn {
  from { opacity: 0; transform: translateY(6px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.win-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.win-heading-group {
  display: flex;
  align-items: baseline;
  gap: 10px;
  flex-wrap: wrap;
}

.win-close {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--text-subtle);
  width: 28px;
  height: 28px;
  margin: -4px -6px -4px 0;
  border-radius: var(--rad);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
  transition: color 120ms ease, background-color 120ms ease;
}

.win-close:hover {
  color: var(--text);
  background: var(--surface-subtle);
}

.win-close:focus-visible {
  outline: 1.5px solid var(--text);
  outline-offset: 2px;
}

.win-title {
  font-family: var(--serif);
  font-size: 19px;
  font-weight: 700;
  color: var(--text);
  margin: 0;
}

.win-title:focus {
  outline: none;
}

.par-badge {
  font-family: var(--sans);
  font-size: 12px;
  font-weight: 500;
  padding: 2px 8px;
  border-radius: var(--rad);
  background: var(--surface-subtle);
  border: 1px solid var(--rule);
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.par-badge.optimal {
  background: var(--accent-soft);
  border-color: var(--accent-soft-border);
  color: var(--accent-text);
  font-weight: 600;
}

.share-preview {
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.55;
  padding: 11px 13px;
  border-radius: var(--rad);
  background: var(--surface-subtle);
  border: 1px solid var(--rule);
  color: var(--text);
  white-space: pre-wrap;
  user-select: text;
  -webkit-user-select: text;
}

.win-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.win-note {
  margin: 0;
  font-size: 13px;
  color: var(--text-muted);
}

.solution-box {
  margin-top: 2px;
  padding: 11px 13px;
  border-radius: var(--rad);
  background: var(--surface-subtle);
  border: 1px solid var(--rule);
  font-size: 12px;
  line-height: 1.7;
  color: var(--text-muted);
  font-family: var(--mono);
}

.solution-box .sol-cap {
  color: var(--accent-text);
  font-weight: 700;
}

/* Footer */
.site-footer {
  width: min(100% - 2rem, 500px);
  margin: 0 auto;
  padding: 1.25rem 0 1.75rem;
  border-top: 1px solid var(--rule);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  color: var(--text-subtle);
  font-family: var(--sans);
  font-size: 0.82rem;
}

.site-footer a {
  color: inherit;
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: color 140ms ease, border-color 140ms ease;
}

.site-footer a:hover {
  color: var(--text);
  border-bottom-color: var(--rule-strong);
}

@media (max-width: 440px) {
  :root {
    --tile-w: 40px;
    --tile-h: 48px;
    --tile-gap: 5px;
    --tile-font: 22px;
  }

  .app {
    padding-top: 1.65rem;
  }

  .wordmark {
    font-size: 1.75rem;
  }

  .row-wrap[data-len="7"] {
    --tile-w: 36px;
    --tile-h: 46px;
    --tile-gap: 4px;
    --tile-font: 20px;
  }

  .row-wrap[data-len="8"] {
    --tile-w: 32px;
    --tile-h: 44px;
    --tile-gap: 3px;
    --tile-font: 18px;
  }

  .row-wrap[data-len="9"] {
    --tile-w: 29px;
    --tile-h: 42px;
    --tile-gap: 3px;
    --tile-font: 17px;
  }

  .row-wrap.drop-from-above .slot-caret {
    top: -15px;
  }

  .row-wrap.drop-from-below .slot-caret {
    bottom: -15px;
  }

  .row-track {
    min-height: 66px;
    padding: 9px 8px;
  }

  .word-row {
    min-height: 48px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .tile,
  .slot,
  .slot-ghost,
  .slot-caret,
  .btn,
  .puzzle-tab,
  .win-backdrop,
  .win-card,
  .win-close,
  .row-wrap,
  .word-row,
  .row-wrap.shake .word-row,
  .tile.just-landed {
    transition: none;
    animation: none;
  }
}
