:root {
  color-scheme: light;
  --panel: rgba(255, 250, 239, 0.94);
  --panel-strong: rgba(255, 247, 230, 0.96);
  --ink: #2f2416;
  --muted: #6c5d49;
  --line: rgba(83, 64, 37, 0.15);
  --brand: #9d4200;
  --brand-strong: #753000;
  --slate: #4b4d57;
  --danger: #8b1f1f;
  --success: #1f6140;
  --shadow: 0 10px 24px rgba(61, 40, 13, 0.1);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: Georgia, "Times New Roman", serif;
  color: var(--ink);
  background:
    radial-gradient(circle at top right, rgba(157, 66, 0, 0.16), transparent 30%),
    radial-gradient(circle at 20% 80%, rgba(31, 97, 64, 0.12), transparent 28%),
    linear-gradient(180deg, #f9f3e5 0%, #ebe0c4 100%);
}

button,
input,
textarea {
  font: inherit;
}

.shell {
  width: min(1080px, calc(100% - 1.25rem));
  margin: 0 auto;
  padding: 1rem 0 1.4rem;
}

.hero {
  margin-bottom: 0.6rem;
}

.eyebrow {
  margin: 0 0 0.2rem;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--brand-strong);
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(1.8rem, 4vw, 3rem);
  line-height: 1.02;
}

h2 {
  margin-bottom: 0;
  font-size: clamp(1.2rem, 2.3vw, 1.8rem);
}

h3 {
  margin-bottom: 0;
  font-size: 0.92rem;
}

.lede {
  max-width: 68ch;
  margin-top: 0.35rem;
  line-height: 1.4;
  font-size: 0.94rem;
  color: var(--muted);
}

.compact {
  margin-top: 0.3rem;
}

.panel,
.subpanel {
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  box-shadow: none;
}

.panel {
  padding: 0.75rem;
  box-shadow: var(--shadow);
}

.subpanel {
  padding: 0.45rem;
  background: transparent;
}

.panel-head,
.subpanel-head,
.layout-board-head {
  display: flex;
  gap: 0.35rem;
  align-items: flex-start;
  justify-content: space-between;
}

.stack {
  display: grid;
  gap: 0.45rem;
}

.meta-grid {
  display: grid;
  gap: 0.45rem;
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.field {
  display: grid;
  gap: 0.18rem;
}

.field-wide {
  grid-column: span 2;
}

.field span {
  font-size: 0.74rem;
  font-weight: 700;
}

input[type="text"],
input[type="file"],
textarea {
  width: 100%;
  border: 1px solid rgba(83, 64, 37, 0.2);
  border-radius: 8px;
  padding: 0.44rem 0.55rem;
  background: rgba(255, 253, 248, 0.86);
  font-size: 0.9rem;
}

textarea {
  resize: vertical;
}

.toggle-row,
.actions,
.layout {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.compact-actions {
  margin-top: 0.25rem;
}

.layout {
  margin-top: 0.45rem;
  align-items: flex-start;
}

.layout > * {
  flex: 1 1 300px;
}

.checkbox {
  display: inline-flex;
  gap: 0.28rem;
  align-items: center;
  font-size: 0.8rem;
}

.inline-control {
  margin-top: 0.1rem;
}

button {
  border: 0;
  border-radius: 8px;
  padding: 0.36rem 0.58rem;
  background: linear-gradient(135deg, var(--brand) 0%, var(--brand-strong) 100%);
  color: #fff8ef;
  font-weight: 700;
  cursor: pointer;
  transition: transform 120ms ease, box-shadow 120ms ease, opacity 120ms ease;
  box-shadow: 0 2px 6px rgba(117, 48, 0, 0.14);
  font-size: 0.86rem;
}

button:hover:not(:disabled) {
  transform: translateY(-1px);
}

button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  box-shadow: none;
}

button.secondary {
  background: linear-gradient(135deg, #5e6472 0%, #444956 100%);
}

button.danger {
  background: linear-gradient(135deg, #b52d2d 0%, #821818 100%);
}

.builder-grid {
  display: grid;
  gap: 0.45rem;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  margin-top: 0.45rem;
}

.button-card {
  padding: 0.5rem;
  border: 1px solid rgba(83, 64, 37, 0.13);
  border-radius: 8px;
  background: rgba(255, 251, 240, 0.24);
  display: grid;
  gap: 0.38rem;
}

.button-card-top,
.link-row {
  display: flex;
  gap: 0.3rem;
  align-items: center;
  justify-content: space-between;
}

.card-index {
  margin-bottom: 0.08rem;
  font-size: 0.8rem;
  font-weight: 700;
}

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

.icon-button {
  padding: 0.22rem 0.36rem;
  background: rgba(75, 77, 87, 0.1);
  color: var(--slate);
  box-shadow: none;
  font-size: 0.74rem;
}

.layout-builder-shell {
  margin-top: 0.45rem;
  padding: 0.35rem;
  border-radius: 8px;
  border: 1px solid rgba(83, 64, 37, 0.12);
  background: transparent;
}

.builder-board,
.puzzle-board {
  position: relative;
  container-type: inline-size;
  --cell: calc(100cqw / 32);
  width: 100%;
  margin-top: 0.45rem;
  border-radius: 8px;
  border: 1px dashed rgba(83, 64, 37, 0.2);
  background:
    linear-gradient(to right, rgba(83, 64, 37, 0.08) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(83, 64, 37, 0.08) 1px, transparent 1px),
    linear-gradient(180deg, rgba(255, 252, 244, 0.9), rgba(240, 229, 202, 0.9)),
    radial-gradient(circle at 20% 30%, rgba(157, 66, 0, 0.05), transparent 26%),
    radial-gradient(circle at 80% 65%, rgba(31, 97, 64, 0.05), transparent 28%);
  background-size:
    calc(100% / 32) 100%,
    100% calc(100% / 32),
    auto,
    auto,
    auto;
  aspect-ratio: 1 / 1;
  overflow: hidden;
}

.solution-list {
  display: flex;
  gap: 0.3rem;
  flex-wrap: wrap;
  margin-top: 0.35rem;
}

.builder-preview {
  touch-action: none;
  cursor: grab;
}

.builder-preview.dragging {
  opacity: 0.85;
}

.builder-preview:hover:not(:disabled),
.builder-preview:focus-visible:not(:disabled) {
  transform: translate(-50%, -50%);
  background: linear-gradient(180deg, rgba(255, 248, 232, 0.98), rgba(231, 216, 182, 0.98));
}

.solution-chip {
  position: relative;
  min-width: 78px;
  max-width: 180px;
  min-height: 72px;
  padding: 1.15rem 0.55rem 0.45rem;
  border-radius: 8px;
  border: 1px solid rgba(83, 64, 37, 0.16);
  background: linear-gradient(180deg, rgba(255, 251, 240, 0.95), rgba(236, 224, 195, 0.96));
  color: var(--ink);
  box-shadow: 0 2px 6px rgba(61, 40, 13, 0.05);
  cursor: grab;
  display: grid;
  gap: 0.4rem;
  place-items: center;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  appearance: none;
}

.solution-chip.dragging {
  opacity: 0.5;
}

.dm-solution-list {
  margin-top: 0.35rem;
}

.solution-preview {
  position: relative;
  min-width: 88px;
  min-height: 78px;
  max-width: 160px;
  padding: 1.15rem 0.55rem 0.55rem;
  border-radius: 8px;
  border: 1px solid rgba(83, 64, 37, 0.16);
  background: linear-gradient(180deg, rgba(255, 251, 240, 0.95), rgba(236, 224, 195, 0.96));
  color: var(--ink);
  display: grid;
  gap: 0.6rem;
  place-items: center;
  text-align: center;
  cursor: grab;
  appearance: none;
}

.solution-face {
  display: grid;
  gap: 0.18rem;
  place-items: center;
  width: 100%;
  min-height: 100%;
}

.solution-remove {
  position: absolute;
  top: 0.22rem;
  right: 0.22rem;
  border: 1px solid rgba(83, 64, 37, 0.14);
  border-radius: 6px;
  background: rgba(255, 252, 244, 0.92);
  color: var(--muted);
  font-size: 0.54rem;
  line-height: 1;
  padding: 0.12rem 0.24rem;
  cursor: pointer;
}

.solution-remove:hover,
.solution-remove:focus-visible {
  background: rgba(255, 245, 224, 0.96);
  color: var(--ink);
}

.solution-preview.current {
  outline: 3px solid rgba(157, 66, 0, 0.2);
}

.solution-preview.done {
  border-color: rgba(31, 97, 64, 0.3);
  background: linear-gradient(180deg, rgba(229, 247, 238, 0.96), rgba(204, 234, 218, 0.96));
}

.solution-preview.dragging {
  opacity: 0.5;
}

.link-panel {
  margin-top: 0.45rem;
  padding: 0.35rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: transparent;
  display: grid;
  gap: 0.3rem;
}

.link-panel summary {
  cursor: pointer;
  font-weight: 700;
}

.link-panel-body {
  margin-top: 0.28rem;
  display: grid;
  gap: 0.3rem;
}

.link-slot,
.compact-field {
  display: grid;
  gap: 0.3rem;
}

.link-slot {
  gap: 0.25rem;
}

.compact-field span,
.link-row strong,
.link-row small {
  display: block;
}

.compact-field span {
  font-size: 0.74rem;
  font-weight: 700;
}

.link-list {
  display: grid;
  gap: 0.25rem;
}

.link-row {
  display: grid;
  gap: 0.3rem;
  padding: 0.32rem 0.4rem;
  border-radius: 8px;
  background: rgba(255, 252, 244, 0.28);
  border: 1px solid rgba(83, 64, 37, 0.08);
}

.link-copy {
  display: grid;
  gap: 0.18rem;
}

.link-row small {
  margin-top: 0;
  color: var(--muted);
  font-size: 0.68rem;
}

.link-url {
  font-size: 0.68rem;
  line-height: 1.2;
  color: rgba(83, 64, 37, 0.82);
  word-break: break-all;
}

.link-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
}

.link-actions button {
  flex: 0 0 auto;
}

.player-select-grid {
  margin-top: 0.35rem;
  display: grid;
  gap: 0.3rem;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
}

.player-pill {
  border-radius: 8px;
  padding: 0.42rem;
  text-align: left;
  display: grid;
  gap: 0.2rem;
}

.player-pill small {
  opacity: 0.85;
  font-size: 0.68rem;
}

.puzzle-button {
  position: absolute;
  width: calc(var(--cell) * var(--w, 4));
  min-height: calc(var(--cell) * var(--h, 4));
  padding: clamp(0.3rem, 0.9cqw, 0.75rem);
  transform: translate(-50%, -50%);
  border-radius: 10px;
  border: 1px solid rgba(83, 64, 37, 0.22);
  background: linear-gradient(180deg, rgba(255, 251, 240, 0.96), rgba(237, 226, 198, 0.98));
  color: var(--ink);
  display: grid;
  gap: clamp(0.15rem, 0.5cqw, 0.5rem);
  place-items: center;
  text-align: center;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.9);
  font-size: clamp(0.45rem, 1.2cqw, 0.95rem);
  overflow: hidden;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.button-face-label {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1 1 auto;
  width: 100%;
  max-width: 100%;
  min-height: 0;
  line-height: 0.92;
  text-align: center;
  white-space: normal;
  overflow-wrap: anywhere;
  word-break: break-word;
  text-wrap: balance;
  font-weight: 700;
}

.puzzle-button img {
  display: block;
  width: min(78%, 100%);
  height: auto;
  max-height: 68%;
  object-fit: contain;
  pointer-events: none;
  user-select: none;
}

.solution-chip img,
.solution-preview img {
  width: min(68px, 100%);
  height: 68px;
  object-fit: contain;
  pointer-events: none;
  user-select: none;
}

.puzzle-button.correct-hit {
  border-color: rgba(31, 97, 64, 0.32);
  background: linear-gradient(180deg, rgba(229, 247, 238, 0.96), rgba(204, 234, 218, 0.96));
}

.puzzle-button:hover:not(:disabled),
.puzzle-button:focus-visible:not(:disabled) {
  transform: translate(-50%, -50%);
  background: linear-gradient(180deg, rgba(255, 248, 232, 0.98), rgba(231, 216, 182, 0.98));
}

.puzzle-button:active:not(:disabled) {
  transform: translate(-50%, -50%);
  filter: brightness(0.96);
}

.puzzle-button.next-up {
  outline: 3px solid rgba(157, 66, 0, 0.22);
}

.puzzle-button.dm-layout-editable {
  cursor: grab;
  touch-action: none;
}

.puzzle-button.dm-layout-editable.dragging {
  opacity: 0.86;
}

.resize-handle {
  position: absolute;
  display: block;
  width: clamp(10px, 1.5cqw, 16px);
  height: clamp(10px, 1.5cqw, 16px);
  padding: 0;
  border-radius: 4px;
  border: 1px solid rgba(255, 255, 255, 0.55);
  background: rgba(157, 66, 0, 0.86);
  box-shadow: none;
  z-index: 2;
}

.resize-handle:hover,
.resize-handle:focus-visible {
  transform: none;
  background: rgba(117, 48, 0, 0.94);
}

.resize-nw {
  top: 0.2rem;
  left: 0.2rem;
  cursor: nwse-resize;
}

.resize-ne {
  top: 0.2rem;
  right: 0.2rem;
  cursor: nesw-resize;
}

.resize-se {
  right: 0.2rem;
  bottom: 0.2rem;
  cursor: nwse-resize;
}

.resize-sw {
  left: 0.2rem;
  bottom: 0.2rem;
  cursor: nesw-resize;
}

.badge {
  padding: 0.22rem 0.36rem;
  border-radius: 6px;
  font-size: 0.66rem;
  font-weight: 700;
  background: rgba(111, 96, 77, 0.12);
}

.badge[data-state="open"] {
  background: rgba(31, 97, 64, 0.14);
  color: var(--success);
}

.badge[data-state="locked"] {
  background: rgba(139, 31, 31, 0.12);
  color: var(--danger);
}

.badge[data-state="solved"] {
  background: rgba(31, 97, 64, 0.19);
  color: var(--success);
}

.history {
  margin: 0;
  padding-left: 0.8rem;
  display: grid;
  gap: 0.25rem;
}

.history-item {
  display: flex;
  gap: 0.22rem;
  align-items: baseline;
  flex-wrap: wrap;
}

.history-status {
  display: inline-block;
  padding: 0.04rem 0.18rem;
  border-radius: 6px;
  font-size: 0.56rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.history-status.ok {
  background: rgba(31, 97, 64, 0.12);
  color: var(--success);
}

.history-status.fail {
  background: rgba(139, 31, 31, 0.12);
  color: var(--danger);
}

.history-status.neutral {
  background: rgba(75, 77, 87, 0.1);
  color: var(--slate);
}

.history-item.ok::marker {
  color: var(--success);
}

.history-item.fail::marker {
  color: var(--danger);
}

.history-item.neutral::marker {
  color: var(--slate);
}

.banner {
  margin-top: 0.45rem;
  padding: 0.32rem 0.44rem;
  border-radius: 8px;
  border: 1px solid transparent;
  font-size: 0.84rem;
}

.banner.info {
  background: rgba(157, 66, 0, 0.08);
  border-color: rgba(157, 66, 0, 0.14);
}

.banner.error {
  background: rgba(139, 31, 31, 0.08);
  border-color: rgba(139, 31, 31, 0.14);
}

.muted {
  margin: 0;
  color: var(--muted);
  line-height: 1.28;
  font-size: 0.82rem;
}

.hidden {
  display: none;
}

body[data-theme="night"] {
  color-scheme: dark;
  color: #e6e7f2;
  background:
    radial-gradient(circle at top right, rgba(63, 102, 214, 0.22), transparent 30%),
    radial-gradient(circle at 18% 78%, rgba(53, 184, 160, 0.12), transparent 28%),
    linear-gradient(180deg, #090d19 0%, #131a2b 52%, #0d1220 100%);
}

body[data-theme="night"] .eyebrow {
  color: #8cb8ff;
}

body[data-theme="night"] .lede,
body[data-theme="night"] .muted,
body[data-theme="night"] .link-url,
body[data-theme="night"] .link-row small,
body[data-theme="night"] .card-hint {
  color: #9ea8c9;
}

body[data-theme="night"] .panel,
body[data-theme="night"] .subpanel {
  border-color: rgba(140, 168, 255, 0.14);
  background: rgba(17, 24, 40, 0.9);
  box-shadow: none;
}

body[data-theme="night"] .subpanel {
  background: transparent;
}

body[data-theme="night"] input[type="text"],
body[data-theme="night"] input[type="file"],
body[data-theme="night"] textarea {
  border-color: rgba(140, 168, 255, 0.18);
  background: rgba(8, 14, 28, 0.86);
  color: #eef3ff;
}

body[data-theme="night"] button {
  background: linear-gradient(135deg, #355fc8 0%, #223f8f 100%);
  color: #f5f8ff;
  box-shadow: 0 12px 22px rgba(16, 36, 92, 0.3);
}

body[data-theme="night"] button.secondary {
  background: linear-gradient(135deg, #2f384f 0%, #1c2437 100%);
}

body[data-theme="night"] button.danger {
  background: linear-gradient(135deg, #8a2946 0%, #651733 100%);
}

body[data-theme="night"] .button-card,
body[data-theme="night"] .layout-builder-shell,
body[data-theme="night"] .link-panel,
body[data-theme="night"] .link-row {
  border-color: rgba(140, 168, 255, 0.12);
  background: rgba(24, 32, 54, 0.18);
}

body[data-theme="night"] .icon-button,
body[data-theme="night"] .solution-remove {
  background: rgba(34, 45, 73, 0.92);
  color: #d9e4ff;
  border-color: rgba(140, 168, 255, 0.14);
  box-shadow: none;
}

body[data-theme="night"] .solution-remove:hover,
body[data-theme="night"] .solution-remove:focus-visible,
body[data-theme="night"] .icon-button:hover,
body[data-theme="night"] .icon-button:focus-visible {
  background: rgba(48, 64, 102, 0.96);
  color: #ffffff;
}

body[data-theme="night"] .resize-handle {
  border-color: rgba(230, 239, 255, 0.4);
  background: rgba(53, 95, 200, 0.9);
}

body[data-theme="night"] .resize-handle:hover,
body[data-theme="night"] .resize-handle:focus-visible {
  background: rgba(72, 124, 255, 0.96);
}

body[data-theme="night"] .builder-board,
body[data-theme="night"] .puzzle-board {
  border-color: rgba(140, 168, 255, 0.18);
  background:
    linear-gradient(to right, rgba(140, 168, 255, 0.08) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(140, 168, 255, 0.08) 1px, transparent 1px),
    linear-gradient(180deg, rgba(8, 13, 25, 0.94), rgba(16, 24, 41, 0.94)),
    radial-gradient(circle at 20% 30%, rgba(140, 168, 255, 0.08), transparent 26%),
    radial-gradient(circle at 80% 65%, rgba(53, 184, 160, 0.08), transparent 28%);
}

body[data-theme="night"] .puzzle-button,
body[data-theme="night"] .solution-chip,
body[data-theme="night"] .solution-preview {
  border-color: rgba(140, 168, 255, 0.2);
  background: linear-gradient(180deg, rgba(26, 36, 61, 0.96), rgba(14, 21, 38, 0.98));
  color: #eef3ff;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05);
}

body[data-theme="night"] .builder-preview:hover:not(:disabled),
body[data-theme="night"] .builder-preview:focus-visible:not(:disabled),
body[data-theme="night"] .puzzle-button:hover:not(:disabled),
body[data-theme="night"] .puzzle-button:focus-visible:not(:disabled) {
  background: linear-gradient(180deg, rgba(36, 51, 84, 0.98), rgba(21, 31, 53, 0.98));
}

body[data-theme="night"] .puzzle-button.correct-hit,
body[data-theme="night"] .solution-preview.done {
  border-color: rgba(53, 184, 160, 0.34);
  background: linear-gradient(180deg, rgba(19, 63, 74, 0.96), rgba(11, 40, 47, 0.98));
}

body[data-theme="night"] .solution-preview.current,
body[data-theme="night"] .puzzle-button.next-up {
  outline-color: rgba(140, 168, 255, 0.28);
}

body[data-theme="night"] .history-item,
body[data-theme="night"] .banner.info,
body[data-theme="night"] .banner.error {
  border-color: rgba(140, 168, 255, 0.12);
  background: rgba(18, 26, 44, 0.74);
}

body[data-theme="night"] .history-status.ok {
  background: rgba(53, 184, 160, 0.18);
  color: #8fe7d5;
}

body[data-theme="night"] .history-status.fail {
  background: rgba(191, 85, 122, 0.18);
  color: #ffb0c9;
}

body[data-theme="night"] .history-status.neutral {
  background: rgba(95, 116, 173, 0.18);
  color: #c1d2ff;
}

body[data-theme="night"] .banner.info {
  border-color: rgba(140, 168, 255, 0.2);
}

body[data-theme="night"] .banner.error {
  border-color: rgba(191, 85, 122, 0.2);
}

body[data-theme="night"] img.button-image-invert {
  filter: invert(1);
}

@media (max-width: 980px) {
  .meta-grid,
  .builder-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .field-wide {
    grid-column: span 2;
  }
}

@media (max-width: 720px) {
  .shell {
    width: min(100%, calc(100% - 1rem));
    padding-top: 0.7rem;
  }

  .panel,
  .subpanel {
    border-radius: 8px;
  }

  .panel-head,
  .subpanel-head,
  .layout-board-head,
  .link-row,
  .button-card-top {
    flex-direction: column;
    align-items: stretch;
  }

  .meta-grid,
  .builder-grid {
    grid-template-columns: 1fr;
  }

  .field-wide {
    grid-column: auto;
  }
}
