:root {
  --bg: #e8eef5;
  --panel: #fff;
  --border: #c5d0df;
  --accent: #2a5f9e;
  --err: #b00020;
  --muted: #5a6570;
}

* {
  box-sizing: border-box;
}

html {
  height: 100%;
}

body {
  margin: 0;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, sans-serif;
  background: var(--bg);
  color: #1a1f24;
  line-height: 1.45;
}

header.app-header {
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  padding: 0.75rem 1.25rem;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
}

header.app-header .play-exit {
  order: 1;
  margin-right: 0.5rem;
}

header.app-header .tabs {
  order: 2;
}

header.app-header h1 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 650;
}

header.app-header h1 a.app-title-link {
  color: inherit;
  text-decoration: none;
}

header.app-header h1 a.app-title-link:hover {
  text-decoration: underline;
}

nav.tabs {
  display: flex;
  gap: 0.35rem;
  margin-left: auto;
}

nav.tabs button {
  border: 1px solid var(--border);
  background: var(--bg);
  padding: 0.35rem 0.75rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
}

nav.tabs button.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

nav.tabs button.about-trigger {
  border-color: #2a6fbf;
  background: #e8f0fe;
  color: #0b57d0;
  font-weight: 600;
}

nav.tabs button.about-trigger:hover {
  background: #d2e3fc;
}

.app-about-overlay {
  position: fixed;
  inset: 0;
  z-index: 10000;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 1.25rem 1rem 2rem;
  box-sizing: border-box;
  overflow: auto;
  background: rgba(0, 0, 0, 0.45);
}

.app-about-overlay.hidden {
  display: none !important;
}

.app-about-panel {
  max-width: 36rem;
  width: 100%;
  margin: 0.5rem auto 0;
  background: var(--panel);
  color: #1a1f24;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem 1.15rem 1.1rem;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.22);
  font-size: 0.95rem;
  line-height: 1.5;
}

.app-about-panel h2 {
  margin: 0 0 0.55rem 0;
  font-size: 1.1rem;
}

.app-about-panel p {
  margin: 0.45em 0;
}

.app-about-panel a {
  color: var(--accent);
}

.app-about-actions {
  margin-top: 0.9rem;
  text-align: right;
}

.app-about-actions button {
  border: 1px solid var(--border);
  background: var(--bg);
  padding: 0.35rem 0.75rem;
  border-radius: 6px;
  cursor: pointer;
  font: inherit;
  font-size: 0.92rem;
}

@media print {
  .app-about-overlay {
    display: none !important;
  }
  body.playing-puzzle .app-header,
  body.playing-puzzle .panel,
  body.playing-puzzle .play-exit {
    display: none !important;
  }
  body.playing-puzzle main.app-main {
    padding: 0;
    display: block;
  }
  body.playing-puzzle .puzzle-player-frame {
    position: static;
    width: 100%;
    min-height: 0;
    height: auto;
    border: none;
    border-radius: 0;
    margin: 0;
  }
}

main.app-main {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  max-width: none;
  margin: 0;
  padding: 0.5rem 1rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

main.app-main > .panel {
  flex: 0 0 auto;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.75rem 1rem;
  margin-bottom: 0;
}

#panel-create {
  overflow: visible;
}

.panel h2 {
  margin: 0 0 0.65rem;
  font-size: 1.05rem;
}

label.field {
  display: block;
  margin-bottom: 0.65rem;
  font-size: 0.88rem;
}

label.field span {
  display: block;
  margin-bottom: 0.2rem;
  color: var(--muted);
}

input[type='text'],
input[type='number'],
textarea {
  width: 100%;
  max-width: 100%;
  padding: 0.45rem 0.55rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font: inherit;
}

textarea {
  min-height: 5rem;
  font-family: ui-monospace, monospace;
}

.msg-error {
  color: var(--err);
  font-size: 0.9rem;
  margin: 0.35rem 0;
}

.msg-ok {
  color: #0d6b3e;
  font-size: 0.9rem;
}

.anagram-banner {
  font-family: ui-monospace, 'Courier New', monospace;
  font-size: 1.05rem;
  letter-spacing: 0.015em;
  margin: 0.5rem 0;
  white-space: pre-wrap;
  word-break: break-word;
  width: fit-content;
  max-width: 100%;
  box-sizing: border-box;
}

.anagram-out {
  font-family: ui-monospace, monospace;
  font-size: 1.1rem;
  font-weight: 600;
  min-height: 1.5em;
  margin: 0.35rem 0 0.75rem;
}

#anagram-draft .anagram-draft-invalid {
  color: var(--err);
  font-weight: 600;
}

#anagram-draft.anagram-draft-blocked {
  outline: 2px solid var(--err);
  border-radius: 4px;
  padding: 0 0.2rem;
}

#anagram-draft.anagram-draft-blocked-flash {
  animation: anagram-draft-block-pulse 0.35s ease;
}

@keyframes anagram-draft-block-pulse {
  0%,
  100% {
    background: transparent;
  }
  50% {
    background: #fde8ea;
  }
}

.freq-table {
  font-family: ui-monospace, monospace;
  font-size: 0.78rem;
  border-collapse: collapse;
  margin-top: 0.5rem;
}

.freq-table td,
.freq-table th {
  border: 1px solid var(--border);
  padding: 0.2rem 0.45rem;
  text-align: right;
}

.anagram-layout {
  display: flex;
  flex-wrap: nowrap;
  align-items: flex-start;
  gap: 0.75rem 1rem;
}

.anagram-main {
  flex: 1 1 0;
  min-width: 0;
  max-width: 100%;
  align-self: flex-start;
}

.anagram-sidebar {
  /* As wide as the frequency tables need; no inner horizontal scroll. */
  flex: 0 0 auto;
  width: max-content;
  max-width: 100%;
  overflow: visible;
  position: static;
  align-self: flex-start;
  padding: 0.35rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
}

.anagram-sidebar .freq-table {
  margin-top: 0;
  font-size: 0.68rem;
}

.freq-scroll {
  overflow: visible;
}

.freq-scroll .freq-table {
  width: max-content;
}

.freq-summary {
  width: 100%;
  margin-bottom: 0.5rem;
}

.freq-summary-wide {
  text-align: left;
  color: var(--muted);
  font-weight: 500;
}

.freq-split {
  display: flex;
  flex-wrap: nowrap;
  gap: 0.65rem 1rem;
  align-items: flex-start;
  width: max-content;
  max-width: 100%;
}

.freq-col {
  flex: 0 0 auto;
  width: max-content;
  max-width: 100%;
}

/* Capitals table is naturally narrower than lowercase. */
.freq-split .freq-col:first-child {
  flex: 0 0 auto;
}

.freq-col-title {
  font-size: 0.72rem;
  font-weight: 650;
  color: var(--muted);
  margin: 0 0 0.2rem;
}

@media (max-width: 640px) {
  .anagram-layout {
    flex-wrap: wrap;
  }
  .anagram-sidebar {
    flex: 1 1 100%;
    width: 100%;
    max-width: none;
  }
}

body.mode-browse:not(.playing-puzzle) .puzzle-player-frame,
body.mode-import:not(.playing-puzzle) .puzzle-player-frame,
body.mode-create:not(.playing-puzzle) .puzzle-player-frame {
  display: none !important;
}

body.playing-puzzle nav.tabs {
  display: none;
}

body.playing-puzzle .puzzle-player-frame {
  display: block !important;
  flex: 1 1 auto;
  min-height: min(70vh, 100%);
  margin-top: 0;
}

.play-exit {
  border: 1px solid var(--border);
  background: var(--bg);
  padding: 0.35rem 0.65rem;
  border-radius: 6px;
  cursor: pointer;
  font: inherit;
  font-size: 0.9rem;
}

.anagram-auth-warn {
  color: var(--err);
  font-weight: 600;
  font-size: 0.9rem;
  margin: 0 0 0.35rem;
  padding: 0.35rem 0.5rem;
  background: #fde8ea;
  border-radius: 6px;
  border: 1px solid #f0c2c8;
}

.anagram-msg-error {
  color: var(--err) !important;
  font-weight: 600;
}

.word-bank-label {
  margin: 0.65rem 0 0.25rem;
  font-size: 0.82rem;
  font-weight: 650;
  color: var(--muted);
}

.word-bank-chips {
  margin-bottom: 0.35rem;
}

.word-bank-item {
  display: inline-flex;
  align-items: center;
  gap: 0.15rem;
  margin: 0.2rem 0.35rem 0.2rem 0;
}

.word-bank-restore {
  font: inherit;
  font-size: 0.82rem;
  padding: 0.2rem 0.45rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: #eef6ff;
  cursor: pointer;
}

.word-bank-restore:hover {
  background: #dceeff;
}

.word-bank-drop {
  font: inherit;
  font-size: 0.75rem;
  padding: 0.05rem 0.25rem;
  line-height: 1.2;
  border: none;
  background: transparent;
  color: var(--err);
  cursor: pointer;
  border-radius: 4px;
}

.word-bank-drop:hover {
  background: #fde8ea;
}

.draft-list-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.35rem 0.75rem;
  margin-bottom: 0.25rem;
}

.draft-list-label {
  margin: 0;
  font-size: 0.84rem;
  font-weight: 650;
  color: var(--muted);
}

.draft-show-published {
  font-size: 0.78rem;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  cursor: pointer;
  user-select: none;
}

.draft-list-summary {
  margin: 0 0 0.25rem;
  font-size: 0.78rem;
  color: var(--muted);
}

.draft-msg {
  min-height: 1.25em;
  font-size: 0.88rem;
  margin: 0.25rem 0;
}

.draft-list {
  margin-bottom: 0.35rem;
}

.draft-list-compact {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.15rem 0.45rem;
  background: #fafbfd;
}

.draft-list-compact > .hint,
.draft-list-compact > .msg-error {
  padding: 0.25rem 0;
  font-size: 0.8rem;
}

.draft-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 0.25rem 0.55rem;
  padding: 0.12rem 0;
  font-size: 0.8rem;
  border-bottom: 1px solid #e8edf3;
}

.draft-row:last-child {
  border-bottom: none;
}

.draft-row-current {
  background: #eef4fc;
  margin: 0 -0.25rem;
  padding-left: 0.25rem;
  padding-right: 0.25rem;
  border-radius: 4px;
}

.draft-row-published-guess .draft-row-title {
  color: var(--muted);
}

.draft-row-title {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.draft-row-meta {
  font-size: 0.74rem;
  color: var(--muted);
  font-weight: 400;
  white-space: nowrap;
}

.draft-row-actions {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 0.2rem;
}

.draft-row-actions button {
  font: inherit;
  font-size: 0.72rem;
  padding: 0.08rem 0.32rem;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: #f4f7fb;
  cursor: pointer;
}

.draft-row-actions button:hover {
  background: #e0e8f2;
}

.draft-action-danger {
  color: var(--err) !important;
  border-color: #e8c4c9 !important;
}

body.mode-edit-published #create-toolbar,
body.mode-edit-published #drafts-wrap {
  display: none !important;
}

.edit-published-bar {
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
  padding: 0.35rem 0.5rem;
  border-radius: 6px;
  background: #eef4fc;
  border: 1px solid #c5d6eb;
}

.edit-published-bar.hidden {
  display: none;
}

.puzzle-table-wrap {
  overflow-x: auto;
  margin-top: 0.35rem;
}

table.puzzle-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
}

table.puzzle-table th,
table.puzzle-table td {
  border-bottom: 1px solid var(--border);
  padding: 0.45rem 0.5rem;
  text-align: left;
  vertical-align: top;
}

table.puzzle-table thead th {
  background: #eef3f9;
  font-weight: 600;
  white-space: nowrap;
}

table.puzzle-table .sort-th {
  font: inherit;
  font-weight: 600;
  background: transparent;
  border: 0;
  padding: 0;
  cursor: pointer;
  color: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
}

table.puzzle-table .sort-th:hover {
  color: var(--accent);
}

table.puzzle-table td.col-num,
table.puzzle-table th.col-num {
  text-align: right;
}

table.puzzle-table td.col-actions {
  white-space: nowrap;
}

table.puzzle-table td.col-actions button {
  font: inherit;
  font-size: 0.82rem;
  padding: 0.2rem 0.5rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: #f4f7fb;
  cursor: pointer;
}

table.puzzle-table td.col-actions button:hover {
  background: #e0e8f2;
}

table.puzzle-table td.puzzle-empty {
  color: var(--muted);
}

table.puzzle-table tr.saved-puzzle-row td a {
  color: var(--accent);
  font-weight: 600;
}

.freq-table td.freq-under {
  text-align: right;
  color: var(--accent);
  font-weight: 500;
}

.freq-table th {
  text-align: left;
  color: var(--err);
  font-weight: 600;
}

/* iPad / soft keyboard: invisible focus target so typing reaches the letter pool */
.anagram-touch-input {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 1px;
  height: 1px;
  margin: 0;
  padding: 0;
  border: 0;
  opacity: 0.02;
  z-index: 50;
  font-size: 16px;
}

.word-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin: 0.5rem 0;
}

.word-chips button {
  font: inherit;
  font-size: 0.82rem;
  padding: 0.2rem 0.45rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: #f4f7fb;
  cursor: pointer;
}

.word-chips button:hover {
  background: #e0e8f2;
}

.word-chips button.word-chip-pool-hint {
  outline: 2px solid #f9a825;
  outline-offset: 1px;
  background: #fff8e1;
  border-color: #e65100;
}

.word-chips button.word-chip-pool-hint:hover {
  background: #ffecb3;
}

.row-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.row-actions button,
button.primary {
  padding: 0.45rem 0.9rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--panel);
  cursor: pointer;
  font: inherit;
}

button.primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

button.primary:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.hidden {
  display: none !important;
}

#step-anagram:focus {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.puzzle-player-frame {
  flex: 1 1 auto;
  width: 100%;
  min-height: 35vh;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  margin-top: 0.25rem;
}

body:not(.mode-create) .puzzle-player-frame {
  flex: 1 1 auto;
  min-height: 45vh;
}

.puzzle-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.puzzle-list li {
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

.puzzle-list a {
  color: var(--accent);
  font-weight: 600;
}

.hint {
  font-size: 0.85rem;
  color: var(--muted);
  margin: 0.25rem 0 0;
}

.help-details {
  margin: 0 0 0.5rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.25rem 0.5rem;
  background: #f4f7fb;
}

.help-details summary {
  cursor: pointer;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--accent);
  list-style-position: outside;
  margin: 0.15rem 0;
}

.help-details .hint {
  margin-top: 0.35rem;
}

.help-span-full {
  flex: 1 1 100%;
  width: 100%;
  min-width: 100%;
}

.grid-dim {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  align-items: flex-end;
}

.grid-dim label.field {
  width: 8rem;
  margin-bottom: 0;
}

.grid-preview-wrap {
  margin: 0.75rem 0 1rem;
}

.grid-seed-metric {
  margin: 0.1rem 0 0.35rem;
  font-size: 0.88rem;
}

.grid-preview-label {
  font-size: 0.82rem;
  font-weight: 650;
  color: var(--muted);
  margin: 0 0 0.35rem;
}

.grid-preview-frame {
  display: block;
  width: 100%;
  min-height: min(52vh, 30rem);
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
}

.source-links-panel {
  margin: 0.85rem 0 1rem;
  padding: 0.75rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #f8fafc;
}

.source-links-panel.hidden {
  display: none !important;
}

.source-links-heading {
  margin: 0 0 0.35rem;
  font-size: 0.98rem;
  font-weight: 650;
}

.source-links-intro {
  margin: 0 0 0.65rem;
  font-size: 0.85rem;
}

.source-links-rows {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  margin-bottom: 0.55rem;
}

.source-link-fieldset {
  border: 1px solid var(--border);
  border-radius: 6px;
  margin: 0;
  padding: 0.55rem 0.65rem 0.65rem;
  background: #fff;
}

.source-link-fieldset legend {
  font-size: 0.82rem;
  font-weight: 650;
  padding: 0 0.25rem;
}

.source-link-fields {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 0.75rem;
  align-items: flex-end;
}

.source-link-fields label.field {
  flex: 1 1 10rem;
  margin-bottom: 0;
  min-width: min(100%, 10rem);
}

.source-link-fields label.field span {
  font-size: 0.82rem;
}

.source-link-remove {
  flex: 0 0 auto;
  border: 1px solid var(--border);
  background: var(--bg);
  padding: 0.3rem 0.55rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.85rem;
  margin-bottom: 0.15rem;
}

.btn-add-source-link {
  border: 1px solid var(--accent);
  background: #e8f0fe;
  color: var(--accent);
  padding: 0.35rem 0.65rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.88rem;
  font-weight: 600;
}

.btn-add-source-link:hover {
  background: #d2e3fc;
}
