:root {
  color-scheme: light dark;
  --bg: #f6f7fb;
  --panel: #ffffff;
  --ink: #1c2030;
  --muted: #5c6377;
  --accent: #3a6ff7;
  --accent-ink: #ffffff;
  --tile: #ffffff;
  --tile-border: #d7dbea;
  --tile-selected: #3a6ff7;
  --danger: #c2372f;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161f;
    --panel: #1d2130;
    --ink: #eef0f8;
    --muted: #9aa1b8;
    --accent: #7c9bff;
    --accent-ink: #101322;
    --tile: #232840;
    --tile-border: #3a4160;
    --tile-selected: #7c9bff;
    --danger: #ff8a80;
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  background: var(--bg);
  color: var(--ink);
  font-family:
    system-ui,
    -apple-system,
    "Segoe UI",
    Roboto,
    sans-serif;
}

.app {
  width: 100%;
  max-width: 26rem;
  padding: 1.25rem 1rem 2rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.app-header {
  position: relative;
  text-align: center;
}

.how-to {
  position: absolute;
  top: 0;
  right: 0;
  width: 1.9rem;
  height: 1.9rem;
  border-radius: 50%;
  border: 1px solid var(--tile-border);
  background: var(--panel);
  color: var(--muted);
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
}

.app-header h1 {
  margin: 0;
  font-size: 2rem;
  letter-spacing: 0.02em;
}

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

.tiles {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.5rem;
}

.tile {
  appearance: none;
  border: 2px solid var(--tile-border);
  border-radius: 0.75rem;
  background: var(--tile);
  color: var(--ink);
  font-size: 1.35rem;
  font-weight: 600;
  padding: 0.9rem 0.25rem;
  cursor: pointer;
  touch-action: manipulation;
  transition:
    border-color 120ms ease,
    background 120ms ease,
    transform 120ms ease;
}

.tile:active {
  transform: scale(0.96);
}

.tile.selected {
  border-color: var(--tile-selected);
  background: var(--accent);
  color: var(--accent-ink);
}

.tile.hint {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 35%, transparent);
}

.operators {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.5rem;
}

.operator {
  appearance: none;
  border: 2px solid var(--tile-border);
  border-radius: 0.75rem;
  background: var(--panel);
  color: var(--ink);
  font-size: 1.35rem;
  padding: 0.55rem 0.25rem;
  cursor: pointer;
  touch-action: manipulation;
}

.operator.selected {
  border-color: var(--tile-selected);
  background: var(--accent);
  color: var(--accent-ink);
}

.message {
  min-height: 1.4em;
  margin: 0;
  text-align: center;
  font-size: 0.95rem;
  color: var(--muted);
}

.message.error {
  color: var(--danger);
}

.hint-steps {
  margin: 0;
  padding-left: 1.4rem;
  font-variant-numeric: tabular-nums;
  font-size: 0.95rem;
  line-height: 1.6;
  color: var(--accent);
}

.hint-steps:empty {
  display: none;
}

.steps-panel {
  background: var(--panel);
  border: 1px solid var(--tile-border);
  border-radius: 0.75rem;
  padding: 0.75rem 1rem;
  min-height: 4.5rem;
}

.steps-panel h2 {
  margin: 0 0 0.4rem;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.steps {
  margin: 0;
  padding-left: 1.4rem;
  font-variant-numeric: tabular-nums;
  font-size: 1rem;
  line-height: 1.6;
}

.steps:empty::after {
  content: "No moves yet";
  display: block;
  margin-left: -1.4rem;
  color: var(--muted);
  font-size: 0.9rem;
}

.controls {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.5rem;
}

.controls button {
  appearance: none;
  border: 1px solid var(--tile-border);
  border-radius: 0.6rem;
  background: var(--panel);
  color: var(--ink);
  font-size: 0.9rem;
  padding: 0.55rem 0.25rem;
  cursor: pointer;
  touch-action: manipulation;
}

.controls button:disabled {
  opacity: 0.45;
  cursor: default;
}

.controls #new-puzzle {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgb(10 12 20 / 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 10;
}

.overlay[hidden] {
  display: none;
}

.dialog {
  position: relative;
  background: var(--panel);
  border: 2px solid var(--accent);
  border-radius: 0.9rem;
  padding: 1.25rem 1rem 1rem;
  width: 100%;
  max-width: 24rem;
  max-height: 80vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  align-items: center;
  text-align: center;
  box-shadow: 0 12px 40px rgb(0 0 0 / 0.35);
}

.dialog .solution-list {
  font-size: 0.85rem;
}

.dialog.dead-end {
  border-color: var(--danger);
}

.dialog-close {
  position: absolute;
  top: 0.4rem;
  right: 0.6rem;
  appearance: none;
  border: none;
  background: none;
  color: var(--muted);
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
  padding: 0.2rem 0.4rem;
}

.dialog-title {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 700;
}

.dialog.dead-end .dialog-title {
  color: var(--danger);
}

.dialog-sub {
  margin: 0;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.dialog-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: center;
}

.dialog-actions button {
  appearance: none;
  border: 1px solid var(--tile-border);
  border-radius: 0.6rem;
  background: var(--panel);
  color: var(--ink);
  font-size: 0.9rem;
  padding: 0.55rem 0.9rem;
  cursor: pointer;
  touch-action: manipulation;
}

.dialog-actions #overlay-new-puzzle,
.dialog-actions .primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}

.guide-list {
  margin: 0;
  padding-left: 1.3rem;
  text-align: left;
  font-size: 0.92rem;
  line-height: 1.6;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

/* Short landscape phones: compact the dialog so actions stay reachable. */
@media (max-height: 480px) {
  .dialog {
    max-height: 94vh;
    gap: 0.35rem;
    padding: 0.75rem 0.9rem;
  }

  .dialog-title {
    font-size: 1.05rem;
  }

  .guide-list {
    font-size: 0.8rem;
    line-height: 1.4;
    gap: 0.2rem;
  }

  .solution-list {
    max-height: 30vh;
  }
}

.solution-list {
  margin: 0;
  padding: 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  text-align: left;
  font-variant-numeric: tabular-nums;
  font-size: 0.9rem;
  max-height: 12rem;
  overflow-y: auto;
  align-self: stretch;
}

.solution-list:empty {
  display: none;
}

.solution-list li {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.25rem 0.4rem;
  border: 1px solid var(--tile-border);
  border-radius: 0.5rem;
  padding: 0.35rem 0.6rem;
  background: var(--bg);
}

.solution-list .step {
  white-space: nowrap;
}

.solution-list .sep {
  color: var(--muted);
}

.solution-list .more {
  border: none;
  background: none;
  color: var(--muted);
  font-size: 0.85rem;
}


@media (max-width: 380px) {
  .tile {
    font-size: 1.15rem;
    padding: 0.75rem 0.2rem;
  }

  .controls {
    grid-template-columns: repeat(2, 1fr);
  }
}
