/* ========== Design System ========== */
:root {
  --color-bg: #ffffff;
  --color-bg-subtle: #fafafa;
  --color-bg-hover: #f4f4f5;
  --color-border: #e4e4e7;
  --color-border-strong: #d4d4d8;
  --color-text: #18181b;
  --color-text-secondary: #383838;
  --color-text-muted: #a1a1aa;
  --color-primary: #1c1c1c;
  --color-primary-hover: #27272a;
  --color-accent: #3b82f6;
  --color-accent-hover: #2563eb;
  --color-input-bg: #fafafa;
  --radius-sm: 6px;
  --radius-md: 8px;
  --radius-lg: 10px;
  --space-xs: 4px;
  --space-sm: 8px;
  --space-md: 16px;
  --space-lg: 24px;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.07);
  --font-ui: "Geist", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: "Geist Mono", ui-monospace, monospace;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: var(--font-ui);
  font-size: 14px;
  line-height: 1.5;
  background: var(--color-bg);
  color: var(--color-text);
  -webkit-font-smoothing: antialiased;
}

.app {
  display: grid;
  grid-template-columns: minmax(200px, 250px) minmax(350px, 350px) minmax(400px, 400px) minmax(350px, 1fr);
  height: 100vh;
  overflow: hidden;
}

/* ========== Main content ========== */
.app-main {
  grid-column: 2 / -1;
  grid-row: 1;
  display: grid;
  grid-template-columns: subgrid;
  min-width: 0;
  min-height: 0;
}

/* ========== Sidebar ========== */
.nav-panel {
  grid-column: 1;
  grid-row: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--color-bg-subtle);
  border-right: 1px solid var(--color-border);
  overflow: hidden;
}

.back-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  margin-right: var(--space-sm);
  border: none;
  background: transparent;
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--color-text-secondary);
}

.back-btn:hover:not(:disabled) {
  background: var(--color-bg-hover);
  color: var(--color-text);
}

.back-btn:disabled {
  cursor: default;
  opacity: 0.7;
}

.nav-header {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  padding: var(--space-md);
  min-height: 52px;
  border-bottom: 1px solid var(--color-border);
  background: var(--color-bg);
}

.nav-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--color-text);
}

.nav-content {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: var(--space-sm) 0 var(--space-sm) var(--space-sm);
  scrollbar-gutter: stable;
  scrollbar-width: thin;
  scrollbar-color: var(--color-border) transparent;
}

.nav-content::-webkit-scrollbar {
  width: 6px;
}

.nav-content::-webkit-scrollbar-track {
  background: transparent;
}

.nav-content::-webkit-scrollbar-thumb {
  background: var(--color-border);
  border-radius: 3px;
}

.nav-content::-webkit-scrollbar-thumb:hover {
  background: var(--color-border-strong);
}

.nav-loading {
  padding: var(--space-md);
  font-size: 13px;
  color: var(--color-text-secondary);
}

.nav-error {
  padding: var(--space-md);
  font-size: 13px;
  color: #dc2626;
}

.nav-empty {
  padding: var(--space-md);
  font-size: 13px;
  color: var(--color-text-secondary);
}

.nav-footer {
  flex-shrink: 0;
  padding: var(--space-sm);
  border-top: 1px solid var(--color-border);
  background: var(--color-bg);
}

.new-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-sm);
  width: 100%;
  padding: var(--space-sm) var(--space-sm);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-bg);
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  color: var(--color-text);
  cursor: pointer;
}

.new-btn:hover {
  background: var(--color-bg-hover);
  border-color: var(--color-border-strong);
}

.nav-auth {
  flex-shrink: 0;
  padding: var(--space-sm);
  border-top: 1px solid var(--color-border);
  background: var(--color-bg);
}

.auth-btn {
  width: 100%;
  padding: var(--space-sm) var(--space-sm);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-bg);
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  color: var(--color-text);
  cursor: pointer;
}

.auth-btn:hover {
  background: var(--color-bg-hover);
  border-color: var(--color-border-strong);
}

.auth-status {
  display: block;
  text-align: center;
  font-size: 13px;
  color: var(--color-text-muted);
}

.auth-status[hidden] {
  display: none;
}

/* Login modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal-overlay[hidden] {
  display: none;
}

.modal {
  background: var(--color-bg);
  border-radius: var(--radius-md);
  padding: var(--space-lg);
  min-width: 280px;
  box-shadow: var(--shadow-md);
}

.modal-title {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: var(--space-md);
}

.modal-form {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}

.modal-input {
  padding: var(--space-sm) var(--space-sm);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 14px;
}

.modal-input:focus {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

.modal-error {
  font-size: 12px;
  color: #dc2626;
}

.modal-error[hidden] {
  display: none;
}

.modal-actions {
  display: flex;
  gap: var(--space-sm);
  justify-content: flex-end;
  margin-top: var(--space-sm);
}

.modal-btn {
  padding: var(--space-sm) var(--space-md);
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
}

.modal-btn-secondary {
  border: 1px solid var(--color-border);
  background: var(--color-bg);
  color: var(--color-text);
}

.modal-btn-secondary:hover {
  background: var(--color-bg-hover);
}

.modal-btn-primary {
  border: none;
  background: var(--color-primary);
  color: white;
}

.modal-btn-primary:hover {
  background: var(--color-primary-hover);
}

/* Nav items */
.nav-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: var(--space-sm) var(--space-md);
  margin-bottom: var(--space-sm);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-bg);
  text-align: left;
  cursor: pointer;
  transition: all 0.15s ease;
}

.nav-item:hover {
  border-color: var(--color-border-strong);
  background: var(--color-bg-subtle);
}

.nav-item.selected {
  border-color: var(--color-primary);
  background: var(--color-bg);
  box-shadow: 0 0 0 1px var(--color-primary);
}

.nav-item-diff {
  padding: var(--space-sm) var(--space-md);
  margin-bottom: var(--space-sm);
  border: 1px solid;
  border-radius: var(--radius-sm);
  text-align: left;
  cursor: pointer;
  transition: all 0.15s ease;
}

.nav-item-diff.easy { border-color: #86efac; background: #dcfce7; }
.nav-item-diff.easy:hover { border-color: #4ade80; background: #bbf7d0; }
.nav-item-diff.medium { border-color: #fde047; background: #fef9c3; }
.nav-item-diff.medium:hover { border-color: #facc15; background: #fef08a; }
.nav-item-diff.hard { border-color: #fdba74; background: #ffedd5; }
.nav-item-diff.hard:hover { border-color: #fb923c; background: #fed7aa; }
.nav-item-diff.bonus { border-color: #e9d5ff; background: #f3e8ff; }
.nav-item-diff.bonus:hover { border-color: #c084fc; background: #e9d5ff; }
.nav-item-diff.selected {
  border-color: var(--color-primary);
  background: var(--color-bg);
  box-shadow: 0 0 0 1px var(--color-primary);
}

.nav-item-title {
  font-weight: 500;
  font-size: 14px;
}

.nav-item-sub {
  font-size: 12px;
  color: var(--color-text-secondary);
  margin-top: 2px;
}

.nav-item-meta {
  font-size: 12px;
  color: var(--color-text-secondary);
  margin-top: 2px;
}

.nav-item-pack {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

.nav-item-pack-text {
  flex: 1;
  min-width: 0;
}

.nav-item-pack-num {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: var(--color-bg-hover);
  font-size: 12px;
  font-weight: 500;
  color: var(--color-text-secondary);
}

.nav-item-badges {
  display: flex;
  gap: var(--space-xs);
  margin-top: var(--space-sm);
}

.badge {
  padding: 2px 6px;
  font-size: 10px;
  font-weight: 500;
  border-radius: 4px;
}

.badge.e { background: #dcfce7; color: #166534; }
.badge.m { background: #fef9c3; color: #854d0e; }
.badge.h { background: #ffedd5; color: #9a3412; }
.badge.b { background: #f3e8ff; color: #6b21a8; }

.chevron {
  width: 16px;
  height: 16px;
  color: var(--color-text-muted);
  flex-shrink: 0;
}

.nav-item:hover .chevron,
.nav-item-diff.selected .chevron {
  color: var(--color-text);
}

/* ========== Puzzle Panel ========== */
.puzzle-panel {
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--color-border);
  background: var(--color-bg);
  overflow: hidden;
}

.puzzle-panel.hidden {
  display: none;
}

.level-name-section {
  margin-bottom: var(--space-lg);
}

.level-name-section .puzzle-name {
  width: 100%;
  padding: var(--space-sm) var(--space-sm);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 14px;
  background: var(--color-input-bg);
  color: var(--color-text);
}

.level-name-section .puzzle-name:focus {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

.level-name-section .puzzle-name::placeholder {
  color: var(--color-text-muted);
}

.puzzle-content {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-md);
}

.section-label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--color-text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: var(--space-sm);
}

/* Grid Letters */
.grid-letters-section {
  margin-bottom: var(--space-lg);
}

.word-group {
  display: flex;
  gap: var(--space-sm);
  margin-bottom: var(--space-md);
}

.word-inputs {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.word-input {
  width: 100%;
  padding: var(--space-sm) var(--space-sm);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: 24px;
  letter-spacing: var(--space-md);
  line-height: 1.3;
  background: var(--color-input-bg);
  transition: border-color 0.15s, box-shadow 0.15s;
}

.word-input:focus {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

.word-value-wrap {
  position: relative;
}

.word-valid-count {
  position: absolute;
  right: var(--space-sm);
  top: 50%;
  transform: translateY(-50%);
  pointer-events: none;
  user-select: none;
  font-size: 12px;
  color: #c2410c;
}

.word-valid-count:empty {
  visibility: hidden;
}

.word-input.word-value {
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  color: var(--color-accent);
  padding-right: 2.5rem;
  text-transform: uppercase;
}

.word-input.revealed-value {
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}

.word-input.revealed-value {
  color: var(--color-text);
}

.word-input.revealed-value em {
  font-style: italic;
}

.word-actions {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  align-self: center;
  flex-shrink: 0;
}

.word-actions .icon-btn {
  width: 36px;
  height: 36px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-bg);
}

/* Control buttons */
.control-buttons {
  display: flex;
  justify-content: center;
  gap: var(--space-sm);
}

.control-buttons-arrows {
  margin-bottom: var(--space-lg);
}

.control-buttons-below {
  margin-top: var(--space-lg);
}

/* Warning module */
.warning-module {
  margin-top: var(--space-lg);
  padding: var(--space-md);
  background: #fef3c7;
  border: 1px solid #f59e0b;
  border-radius: var(--radius-md);
}

.warning-module-title {
  font-size: 12px;
  font-weight: 600;
  color: #92400e;
  margin-bottom: var(--space-sm);
}

.warning-list {
  margin: 0;
  padding-left: var(--space-md);
  font-size: 12px;
  color: #92400e;
  line-height: 1.5;
}

.control-buttons .icon-btn {
  width: 36px;
  height: 36px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-bg);
}

.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  background: var(--color-bg);
  color: var(--color-text-secondary);
  cursor: pointer;
  transition: all 0.15s ease;
}

.icon-btn:hover {
  background: var(--color-bg-hover);
  color: var(--color-text);
  border-color: var(--color-border-strong);
}

.icon-btn img,
.icon-btn svg {
  flex-shrink: 0;
  display: block;
  object-fit: contain;
}

/* Tray Letters */
.tray-letters-section {
  margin-bottom: var(--space-md);
}

.tray-letters-row {
  display: flex;
  align-items: flex-start;
  gap: var(--space-sm);
}

.tray-inputs-wrap {
  flex: 1;
  min-width: 0;
}

.tray-letters-row .control-buttons {
  flex-shrink: 0;
}

.tray-input {
  width: 100%;
  padding: var(--space-sm) var(--space-sm);
  margin-bottom: var(--space-sm);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: 24px;
  letter-spacing: var(--space-md);
  line-height: 1.3;
  background: var(--color-input-bg);
  color: var(--color-text);
  transition: border-color 0.15s, box-shadow 0.15s;
}

.tray-input:focus {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

/* ========== Settings Panel ========== */
.settings-panel {
  display: flex;
  flex-direction: column;
  background: var(--color-bg);
  overflow: hidden;
}

.settings-panel.hidden {
  display: none;
}

/* ========== Panel 4 ========== */
.panel-4 {
  display: flex;
  flex-direction: column;
  border-left: 1px solid var(--color-border);
  background: var(--color-bg);
  overflow: hidden;
}

.panel-4.hidden {
  display: none;
}

.panel-4-content {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-md);
}

/* ========== Puzzle Preview (Panel 4) ========== */
.pp-root {
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
  user-select: none;
}

.pp-title {
  font-family: "Lora", serif;
  font-weight: 600;
  font-size: 24px;
  letter-spacing: 0.01em;
  color: var(--color-text);
  text-align: center;
}

.pp-stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0; /* grid and tray sections touch */
}

.pp-section {
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  overflow: hidden;
  min-height: 0;
  display: flex;
}

.pp-grid-section {
  min-height: 280px;
  background: #FFF5E5;
  border-bottom: none;
  border-radius: var(--radius-md) var(--radius-md) 0 0;
}

.pp-tray-section {
  min-height: 200px;
  background: #E8C07D;
  border-top: none;
  border-radius: 0 0 var(--radius-md) var(--radius-md);
}

.pp-section-inner {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center; /* vertical centering of lines group */
  justify-content: center; /* horizontal centering of each line wrapper */
  padding: var(--space-md);
}

.pp-lines {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.pp-lines.pp-grid-lines {
  gap: 10px; /* medium-small gap between word lines */
}

.pp-lines.pp-tray-lines {
  gap: 12px; /* medium gap between tray lines */
}

.pp-line {
  display: flex;
  justify-content: center;
  align-items: flex-end; /* bottom-align tiles within the row */
}

.pp-line.pp-grid-line {
  gap: 6px; /* small gap between grid tiles */
}

.pp-line.pp-tray-line {
  gap: 12px; /* medium gap between tray tiles */
}

.pp-slot {
  width: 40px;
  height: 70px;
  position: relative;
  flex: 0 0 auto;
}

.pp-placemat {
  position: absolute;
  inset: 0;
  background: center bottom / contain no-repeat url("assets/game/placemat.svg");
}

.pp-letter {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: 40px;
  text-align: center;
  font-family: "Lexend", var(--font-ui);
  font-weight: 600;
  font-size: 30px;
  line-height: 1;
  color: #1F425C;
  pointer-events: none;
}

.pp-letter-top-8 { top: 12px; }   /* free_up, fixed_up, sticker_up */
.pp-letter-top-18 { top: 20px; } /* free_down, fixed_down, sticker_down */
.pp-letter-top-28 { top: 26px; } /* sticker_free, placemat */

.pp-placemat-letter {
  z-index: 1;
  color: #E8C07D;
}

.pp-tile {
  position: absolute;
  inset: 0;
  background-position: center bottom;
  background-size: contain;
  background-repeat: no-repeat;
  cursor: grab;
  z-index: 2;
}

.pp-tile:active {
  cursor: grabbing;
}

/* Clone used as setDragImage() source: size and position off-screen so image renders correctly */
.pp-drag-ghost {
  position: fixed;
  left: -9999px;
  top: 0;
  width: 40px;
  height: 70px;
  pointer-events: none;
  box-sizing: border-box;
}

.pp-tile[data-draggable="false"] {
  cursor: default;
}

.pp-tile.pp-fixed-up {
  background-image: url("assets/game/fixed_up.svg");
}
.pp-tile.pp-fixed-down {
  background-image: url("assets/game/fixed_down.svg");
}
.pp-tile.pp-free-up {
  background-image: url("assets/game/free_up.svg");
}
.pp-tile.pp-free-down {
  background-image: url("assets/game/free_down.svg");
}
.pp-tile.pp-sticker-free {
  background-image: url("assets/game/sticker_free.svg");
}
.pp-tile.pp-sticker-up {
  background-image: url("assets/game/sticker_up.svg");
}
.pp-tile.pp-sticker-down {
  background-image: url("assets/game/sticker_down.svg");
}
.pp-tile.pp-empty {
  background-image: url("assets/game/empty_grid.svg");
  cursor: default;
}
.pp-tile.pp-highlight {
  background-image: url("assets/game/highlighted_grid.svg");
}

.pp-status {
  font-size: 12px;
  color: var(--color-text-secondary);
  margin-top: -6px;
}

.pp-status.pp-error {
  color: #dc2626;
}

.tabs-header {
  display: flex;
  gap: var(--space-xs);
  padding: 0 var(--space-md);
  border-bottom: 1px solid var(--color-border);
  background: var(--color-bg);
}

.tab-btn {
  padding: var(--space-md) var(--space-sm);
  border: none;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  background: transparent;
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  color: var(--color-text-secondary);
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}

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

.tab-btn.active {
  color: var(--color-text);
  border-bottom-color: var(--color-primary);
}

.tab-panel {
  display: none;
  flex: 1;
  overflow-y: auto;
}

.tab-panel.active {
  display: flex;
  flex-direction: column;
}

.scratchpad-text {
  flex: 1;
  min-height: 0;
  width: 100%;
  padding: var(--space-md);
  border: none;
  outline: none;
  resize: none;
  font-family: inherit;
  font-size: 14px;
  line-height: 1.5;
  color: var(--color-text);
  background: var(--color-bg);
  box-sizing: border-box;
}

.scratchpad-text::placeholder {
  color: var(--color-text-muted);
}

/* Debug tab */
.debug-panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.debug-options {
  flex-shrink: 0;
  padding: var(--space-md);
  border-bottom: 1px solid var(--color-border);
  min-height: 48px;
}

.debug-log-wrap {
  flex: 1;
  min-height: 0;
  padding: var(--space-md);
}

.debug-log {
  width: 100%;
  height: 100%;
  min-height: 120px;
  padding: var(--space-sm);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.4;
  color: var(--color-text);
  background: var(--color-bg-subtle);
  resize: none;
  outline: none;
  box-sizing: border-box;
}

.debug-log::placeholder {
  color: var(--color-text-muted);
}

.settings-content {
  flex: 1;
  min-width: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: var(--space-md);
}

.puzzle-content .generation-section {
  margin-top: var(--space-lg);
  padding-top: var(--space-md);
  border-top: 1px solid var(--color-border);
}

.constraints-section {
  margin-bottom: var(--space-md);
  min-width: 0;
}

.dict-error {
  padding: var(--space-sm) var(--space-sm);
  margin-bottom: var(--space-sm);
  font-size: 12px;
  color: #dc2626;
  background: #fef2f2;
  border-radius: var(--radius-sm);
}

.dict-error[hidden] {
  display: none;
}

.constraint-template-row {
  display: flex;
  align-items: stretch;
  gap: var(--space-sm);
  margin-bottom: var(--space-sm);
}

.constraint-template-label {
  font-size: 12px;
  font-weight: 500;
  color: var(--color-text-secondary);
  align-self: center;
}

.constraint-template-select {
  padding: var(--space-xs) var(--space-sm);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 13px;
  background: var(--color-bg);
  color: var(--color-text);
  min-width: 120px;
}

.action-btn-template {
  margin-left: auto;
  margin-bottom: 0;
  width: auto;
  aspect-ratio: 1;
  padding: 0;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.action-btn-template:hover,
.action-btn-template:active,
.action-btn-template:focus {
  background: transparent;
  border: none;
}

.constraint-template-select:focus {
  outline: none;
  border-color: var(--color-accent);
}

.constraints-box {
  padding: var(--space-md);
  margin-bottom: var(--space-md);
  background: var(--color-bg-subtle);
  border-radius: var(--radius-md);
  border: 1px solid var(--color-border);
  min-width: 0;
}

.constraints-box > .action-btn {
  margin-top: var(--space-md);
  margin-bottom: 0;
}

/* Overall constraints: Words, Tray */
.constraint-overall {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  padding-bottom: var(--space-sm);
  margin-bottom: var(--space-sm);
  border-bottom: 1px solid var(--color-border);
  font-size: 12px;
}

.constraint-overall-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-sm) var(--space-sm);
}

.constraint-item {
  display: inline-flex;
  align-items: center;
  gap: var(--space-xs);
}

.constraint-name {
  color: var(--color-text-muted);
  min-width: 2.8em;
}

.constraint-sep {
  color: var(--color-text-muted);
  font-weight: 300;
}

/* Per-word table */
.constraint-table {
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
  font-size: 12px;
}

.constraint-table-header,
.constraint-table-row {
  display: grid;
  grid-template-columns: 2.2em repeat(3, minmax(0, 1fr));
  gap: var(--space-sm);
  align-items: center;
}

.constraint-th {
  color: var(--color-text-muted);
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.constraint-row-label {
  color: var(--color-text-secondary);
  font-weight: 500;
}

.constraint-range {
  display: flex;
  align-items: center;
  gap: 2px;
  min-width: 0;
}

.constraint-input {
  width: 2.25em;
  min-width: 1.75em;
  max-width: 3em;
  padding: 2px 4px;
  border: 1px solid var(--color-border);
  border-radius: 4px;
  font-family: inherit;
  font-size: 12px;
  text-align: center;
  -moz-appearance: textfield;
  appearance: textfield;
}

.constraint-input::-webkit-outer-spin-button,
.constraint-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.constraint-input:focus {
  outline: none;
  border-color: var(--color-accent);
}

.constraint-dash {
  color: var(--color-text-muted);
  font-size: 11px;
  flex-shrink: 0;
}

.generation-row {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  margin-bottom: var(--space-sm);
}

.generation-label {
  font-size: 13px;
  font-weight: 500;
  color: var(--color-text-secondary);
  min-width: 5em;
}

.generation-select {
  padding: var(--space-xs) var(--space-sm);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 13px;
  background: var(--color-bg);
  min-width: 100px;
}

.action-btn {
  width: 100%;
  padding: var(--space-sm) var(--space-md);
  margin-bottom: var(--space-sm);
  border: none;
  border-radius: var(--radius-sm);
  background: var(--color-primary);
  color: white;
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s ease;
}

.action-btn:hover {
  background: var(--color-primary-hover);
}

#uploadBtn:disabled {
  opacity: 0.25;
  cursor: not-allowed;
}

.action-btn-sm {
  width: auto;
  margin-bottom: 0;
  padding: var(--space-xs) var(--space-sm);
  font-size: 12px;
}

.save-template-saved {
  opacity: 0;
  transition: opacity 0.4s ease;
  pointer-events: none;
}

.settings-content #previewContent {
  margin-top: 0;
  margin-left: 0;
  margin-right: 0;
  margin-bottom: var(--space-md);
  padding: 0;
}

.output-area {
  width: 100%;
  padding: var(--space-sm);
  margin-bottom: var(--space-sm);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: 12px;
  background: var(--color-input-bg);
  resize: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.output-area:focus {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

.output-area::placeholder {
  color: var(--color-text-muted);
}

.output-area.output-stale {
  color: var(--color-text-muted);
}

.data-box {
  display: grid;
  grid-template-columns: 1fr 1fr;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  margin-bottom: var(--space-sm);
  overflow: hidden;
}

.data-col {
  padding: var(--space-sm);
  border-right: 1px solid var(--color-border);
}

.data-col:last-child {
  border-right: none;
}

.data-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-text-secondary);
  margin-bottom: var(--space-sm);
}

.data-text {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--color-text);
  line-height: 1.5;
  white-space: pre-line;
}

.data-text.muted {
  color: var(--color-text-muted);
}

/* ========== Placeholder ========== */
.placeholder {
  grid-column: 1 / -1;
  grid-row: 1;
  z-index: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-start;
  background: var(--color-bg);
  color: var(--color-text);
  overflow: scroll;
}

.placeholder.hidden {
  display: none;
}

.placeholder-title {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: var(--space-sm);
  color: var(--color-text);
}

.placeholder-subtitle {
  font-size: 13px;
  color: var(--color-text-muted);
}

/* ========== Instructions (markdown-style) ========== */
.instructions {
  padding: var(--space-lg);
}

.instructions-content {
  max-width: 60rem;
  margin: 0;
}

.instructions-title {
  font-size: 1.5rem;
  font-weight: 600;
  margin: 0 0 var(--space-sm);
  color: var(--color-text);
}

.instructions-lead {
  font-size: 0.9375rem;
  color: var(--color-text-secondary);
  margin: 0 0 var(--space-lg);
  line-height: 1.5;
}

.instructions-toc {
  margin-bottom: var(--space-xl);
  padding: var(--space-md);
  background: var(--color-bg-elevated, rgba(0, 0, 0, 0.03));
  border-radius: 6px;
  border: 1px solid var(--color-border);
}

.instructions-toc-title {
  font-size: 0.8125rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--color-text-secondary);
  margin: 0 0 var(--space-sm);
}

.instructions-toc-list {
  margin: 0;
  padding-left: 1.25rem;
  font-size: 0.9375rem;
}

.instructions-toc-list li {
  margin-bottom: 0.35rem;
}

.instructions-toc-list a {
  color: var(--color-accent, #2563eb);
  text-decoration: none;
}

.instructions-toc-list a:hover {
  text-decoration: underline;
}

.instructions-section {
  margin-bottom: var(--space-xl);
  scroll-margin-top: var(--space-md);
}

.instructions-section h2 {
  font-size: 1.125rem;
  font-weight: 600;
  margin: var(--space-lg) 0 var(--space-sm);
  padding-bottom: 0.25rem;
  border-bottom: 1px solid var(--color-border);
  color: var(--color-text);
}

.instructions-section p {
  font-size: 0.9375rem;
  line-height: 1.55;
  margin: 0 0 var(--space-sm);
  color: var(--color-text-secondary);
}

.instructions-section ul {
  margin: 0 0 var(--space-sm);
  padding-left: 1.5rem;
  font-size: 0.9375rem;
  line-height: 1.55;
  color: var(--color-text-secondary);
}

.instructions-section li {
  margin-bottom: 0.35rem;
}

.instructions-section code {
  font-size: 0.875em;
  padding: 0.15em 0.4em;
  background: var(--color-bg-elevated, rgba(0, 0, 0, 0.06));
  border-radius: 4px;
  font-family: ui-monospace, monospace;
}

/* ========== Wordfinder (fourth column) ========== */
#wordfinderPanel {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  height: 100%;
  padding: var(--space-md);
  min-height: 0;
  position: relative;
}

#wordfinderPanel .wordfinder-dict-status {
  padding: var(--space-sm);
  margin-bottom: var(--space-sm);
  background: var(--color-warning-bg, #fff3cd);
  color: var(--color-warning-text, #856404);
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-family: var(--font-ui);
}

#wordfinderPanel .wordfinder-input-row {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  margin-bottom: var(--space-sm);
}

#wordfinderPanel .wordfinder-input {
  flex: 1;
  min-width: 0;
  max-width: 330px;
  padding: var(--space-sm) var(--space-sm);
  font-size: 14px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-bg);
  color: var(--color-text);
  text-transform: uppercase;
  font-family: var(--font-mono);
}

#wordfinderPanel .wordfinder-input::placeholder {
  text-transform: none;
  opacity: 0.6;
}

#wordfinderPanel .wordfinder-letter-count {
  font-size: 12px;
  color: var(--color-text-muted);
  font-variant-numeric: tabular-nums;
}

#wordfinderPanel .wordfinder-letter-count.warning {
  color: var(--color-error, #c00);
}

#wordfinderPanel .wordfinder-controls {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  margin-bottom: var(--space-sm);
  flex-wrap: wrap;
}

#wordfinderPanel .wordfinder-btn {
  padding: var(--space-sm) var(--space-sm);
  font-size: 13px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-border);
  background: var(--color-bg);
  color: var(--color-text);
  cursor: pointer;
  font-family: var(--font-ui);
}

#wordfinderPanel .wordfinder-btn:hover {
  background: var(--color-bg-hover);
}

#wordfinderPanel .wordfinder-btn-primary {
  padding: var(--space-sm) var(--space-md);
  border: none;
  border-radius: var(--radius-sm);
  background: var(--color-primary);
  color: white;
  font-weight: 500;
  transition: background 0.15s ease;
}

#wordfinderPanel .wordfinder-btn-primary:hover {
  background: var(--color-primary-hover);
}

#wordfinderPanel .wordfinder-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--color-border);
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: wordfinder-spin 0.8s linear infinite;
  margin: var(--space-sm) auto;
}

@keyframes wordfinder-spin {
  to { transform: rotate(360deg); }
}

#wordfinderPanel .wordfinder-loading-message {
  text-align: center;
  font-size: 13px;
  color: var(--color-text-muted);
  margin-bottom: var(--space-sm);
}

#wordfinderPanel .wordfinder-loading-message.wordfinder-loading-message-error {
  color: var(--color-error, #b91c1c);
}

#wordfinderPanel .wordfinder-result {
  flex: 1;
  min-height: 120px;
  max-height: 800px;
  overflow: auto;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: var(--space-sm);
  background: var(--color-bg);
  scrollbar-width: thin;
  scrollbar-color: var(--color-border) transparent;
  font-family: var(--font-mono);
}

#wordfinderPanel .wordfinder-result::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

#wordfinderPanel .wordfinder-result::-webkit-scrollbar-track {
  background: transparent;
}

#wordfinderPanel .wordfinder-result::-webkit-scrollbar-thumb {
  background: var(--color-border);
  border-radius: 3px;
}

#wordfinderPanel .wordfinder-result::-webkit-scrollbar-thumb:hover {
  background: var(--color-text-muted);
}

#wordfinderPanel .wordfinder-nav {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-md);
  flex-wrap: wrap;
  padding: var(--space-sm) 0;
  margin-top: var(--space-sm);
}

#wordfinderPanel .wordfinder-nav-group {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

#wordfinderPanel .wordfinder-nav-btn {
  padding: var(--space-sm) var(--space-sm);
  font-size: 13px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--color-bg-secondary, rgba(0, 0, 0, 0.06));
  color: var(--color-text);
  cursor: pointer;
  font-family: var(--font-ui);
  transition: background 0.15s ease;
}

#wordfinderPanel .wordfinder-nav-btn:hover:not(:disabled) {
  background: var(--color-bg-hover);
}

#wordfinderPanel .wordfinder-nav-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

#wordfinderPanel .wordfinder-nav-danger {
  color: var(--color-error, #b91c1c);
  background: transparent;
}

#wordfinderPanel .wordfinder-nav-danger:hover:not(:disabled) {
  background: var(--color-error, #b91c1c);
  color: #fff;
}

#wordfinderPanel .wordfinder-page-info {
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  color: var(--color-text-muted);
  min-width: 4em;
  text-align: center;
}

#wordfinderPanel .wordfinder-combination {
  display: none;
}

#wordfinderPanel .wordfinder-combination.wordfinder-active {
  display: block;
}

#wordfinderPanel .wordfinder-toast {
  position: relative;
  padding: var(--space-sm) var(--space-sm);
  font-size: 13px;
  background: var(--color-bg-secondary);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  pointer-events: auto;
  min-width: 220px;
  max-width: min(320px, calc(100vw - 32px));
}

#wordfinderPanel .wordfinder-toast.error {
  background: var(--color-error-bg, #fee);
  border-color: var(--color-error, #c00);
  color: var(--color-error, #c00);
}

#wordfinderPanel .wordfinder-toast-host {
  position: fixed;
  top: 4px;
  right: 4px;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: var(--space-sm);
  pointer-events: none;
}

/* Wordfinder columns layout (2/3 line puzzle + Tray Words) */
#wordfinderPanel .wordfinder-columns {
  display: flex;
  gap: var(--space-sm);
  overflow-x: auto;
  padding: var(--space-sm) 0;
}

/* 2 and 3 line puzzles: columns span full width, equal widths */
#wordfinderPanel .wordfinder-combination .wordfinder-columns {
  width: 100%;
}

/* Tray Words: columns span full width, equal widths */
#wordfinderPanel .wordfinder-result > .wordfinder-columns {
  width: 100%;
}

#wordfinderPanel .wordfinder-column {
  min-width: 100px;
  flex-shrink: 0;
}

#wordfinderPanel .wordfinder-combination .wordfinder-column,
#wordfinderPanel .wordfinder-result > .wordfinder-columns .wordfinder-column {
  flex: 1;
  min-width: 0;
}

#wordfinderPanel .wordfinder-column h3 {
  font-size: 12px;
  font-weight: 600;
  text-decoration: underline;
  margin-bottom: var(--space-sm);
  color: var(--color-text);
  text-align: center;
  text-transform: uppercase;
}

#wordfinderPanel .wordfinder-column ul {
  list-style: none;
  padding: 0;
  margin: 0;
  height: 700px;
  overflow-y: scroll;
  scrollbar-width: thin;
  scrollbar-color: var(--color-border) transparent;
}

#wordfinderPanel .wordfinder-column ul::-webkit-scrollbar {
  width: 5px;
}

#wordfinderPanel .wordfinder-column ul::-webkit-scrollbar-track {
  background: transparent;
}

#wordfinderPanel .wordfinder-column ul::-webkit-scrollbar-thumb {
  background: var(--color-border);
  border-radius: 3px;
}

#wordfinderPanel .wordfinder-column ul::-webkit-scrollbar-thumb:hover {
  background: var(--color-text-muted);
}

#wordfinderPanel .wordfinder-column li {
  padding: var(--space-xs) var(--space-sm);
  margin: 2px 0;
  font-size: 13px;
  cursor: pointer;
  border-radius: 2px;
}

#wordfinderPanel .wordfinder-column li:focus {
  outline: 2px solid var(--color-primary);
  outline-offset: 1px;
}

#wordfinderPanel .wordfinder-column li.wordfinder-highlight {
  color: var(--color-success, #0a0);
}

#wordfinderPanel .wordfinder-column li.wordfinder-anagram {
  color: var(--color-primary);
}

/* Wordfinder saved pages */
#wordfinderPanel .wordfinder-saved-menu {
  position: relative;
  margin-left: auto;
}

#wordfinderPanel .wordfinder-saved-menu[hidden] {
  display: none;
}

#wordfinderPanel .wordfinder-saved-menu > .wordfinder-btn {
  min-width: 76px;
}

#wordfinderPanel .wordfinder-saved-panel {
  position: absolute;
  top: calc(100% + var(--space-sm));
  right: 0;
  width: 220px;
  max-height: 240px;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: var(--space-sm);
  z-index: 10;
  display: flex;
  flex-direction: column;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.14);
}

#wordfinderPanel .wordfinder-saved-panel[hidden] {
  display: none;
}

#wordfinderPanel .wordfinder-saved-list {
  overflow-y: auto;
  flex: 1;
}

#wordfinderPanel .wordfinder-saved-item {
  padding: var(--space-xs) var(--space-sm);
  font-size: 12px;
  cursor: pointer;
  border-radius: 2px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-sm);
}

#wordfinderPanel .wordfinder-saved-item > span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#wordfinderPanel .wordfinder-saved-item:hover {
  background: var(--color-bg-hover);
}

#wordfinderPanel .wordfinder-saved-item button {
  padding: 0;
  border: none;
  background: none;
  cursor: pointer;
  color: var(--color-text-muted);
  font-size: 14px;
}

