:root {
  --paper: #f7f4ed;
  --ink: #1d1d1b;
  --muted: #716d65;
  --line: #22211f;
  --panel: #fffaf0;
  --blue: #2563eb;
  --green: #1f8a4c;
  --red: #d13b3b;
  --gold: #d99a14;
  --shadow: 0 18px 40px rgba(37, 33, 28, 0.14);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  background: var(--paper);
  color: var(--ink);
}

body {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 10px;
  overflow: hidden;
  padding: 12px;
}

.start-screen {
  align-items: center;
  background: var(--paper);
  display: flex;
  inset: 0;
  justify-content: center;
  padding: 22px;
  position: fixed;
  z-index: 40;
}

.start-card {
  align-items: center;
  background: var(--panel);
  border: 2px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 18px;
  justify-items: center;
  max-width: 360px;
  padding: 28px;
  width: min(100%, 360px);
}

.start-card h1 {
  font-size: 44px;
  line-height: 1;
  margin: 0;
}

.start-mark {
  display: grid;
  grid-template-columns: repeat(3, 34px);
  grid-template-rows: repeat(3, 34px);
  transform: rotate(-1deg);
}

.start-mark span {
  background: #fffaf0;
  border: 1px solid var(--line);
}

.start-mark span:nth-child(2),
.start-mark span:nth-child(6) {
  background: #262626;
  background-image: url("assets/wall.png");
  background-position: center;
  background-size: 150%;
}

.start-mark span:nth-child(4),
.start-mark span:nth-child(8) {
  background-color: #fffaf0;
  background-position: center;
  background-repeat: no-repeat;
  background-size: 74%;
}

.start-mark span:nth-child(4) {
  background-image: url("assets/move-bonus.png");
}

.start-mark span:nth-child(8) {
  background-image: url("assets/point-positive.png");
}

.start-actions {
  display: grid;
  gap: 10px;
  width: 100%;
}

.start-actions .primary-action {
  margin: 0;
}

button,
input {
  font: inherit;
}

button {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #ffffff;
  color: var(--ink);
  cursor: pointer;
  padding: 8px 12px;
}

button.mode-tab[aria-current="page"] {
  background: var(--ink);
  color: #fff;
}

.icon-button {
  align-items: center;
  background: transparent;
  border: 0;
  border-radius: 0;
  display: inline-flex;
  font-size: 24px;
  font-weight: 400;
  height: 28px;
  justify-content: center;
  line-height: 1;
  min-height: 28px;
  padding: 0;
  width: 28px;
}

.icon-button:hover,
.icon-button:focus-visible {
  background: transparent;
  transform: scale(1.08);
}

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

.topbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  max-width: 1180px;
  width: 100%;
  margin: 0 auto;
}

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

h1 {
  margin-bottom: 2px;
  font-size: clamp(28px, 3.4vw, 40px);
  letter-spacing: 0;
}

h2 {
  margin-bottom: 10px;
  font-size: 18px;
}

.topbar p,
.status-text {
  color: var(--muted);
}

.topbar p {
  margin-bottom: 0;
}

.topbar nav,
.tool-row,
.mobile-pad,
.section-head {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(320px, 1fr) 360px;
  gap: 14px;
  max-width: 1180px;
  width: 100%;
  min-height: 0;
  height: 100%;
  margin: 0 auto;
  align-items: start;
}

.board-panel,
.side-panel,
.drawer,
.results {
  background: var(--panel);
  border: 2px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.board-panel {
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  min-height: 0;
  height: 100%;
  padding: 12px;
}

#canvasHost {
  width: 100%;
  min-height: 0;
  height: 100%;
  overflow: hidden;
}

canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
}

.side-panel {
  max-height: 100%;
  overflow: auto;
  padding: 14px;
  position: relative;
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}

.stat-grid div {
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  padding: 10px;
}

.stat-grid span {
  display: block;
  min-height: 32px;
  color: var(--muted);
  font-size: 12px;
}

.stat-grid strong {
  display: block;
  font-size: 28px;
}

.primary-action {
  width: 100%;
  margin: 12px 0;
  background: var(--ink);
  color: #fff;
  align-items: center;
  display: inline-flex;
  gap: 8px;
  justify-content: center;
}

.primary-action.is-loading::after {
  animation: spin 0.75s linear infinite;
  border: 2px solid rgba(255, 255, 255, 0.45);
  border-top-color: #fff;
  border-radius: 999px;
  content: "";
  height: 16px;
  width: 16px;
}

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

.secondary-action {
  width: 100%;
  margin: -4px 0 12px;
}

.mobile-pad {
  justify-content: center;
  margin-top: 8px;
}

.mobile-pad button {
  min-width: 70px;
  font-size: 22px;
  font-weight: 700;
  line-height: 1;
}

.hidden {
  display: none !important;
}

.results,
.drawer {
  margin-top: 14px;
  padding: 12px;
}

.modal {
  align-items: center;
  background: rgba(29, 29, 27, 0.32);
  display: flex;
  inset: 0;
  justify-content: center;
  padding: 18px;
  position: fixed;
  z-index: 20;
}

.modal.hidden {
  display: none !important;
}

.modal-card {
  margin-top: 0;
  max-height: min(78vh, 560px);
  overflow: auto;
  width: min(460px, calc(100vw - 36px));
}

.modal-card > .section-head {
  align-items: flex-start;
}

.modal-card .icon-button {
  margin-top: -3px;
}

.loading-modal {
  z-index: 30;
}

.loading-card {
  align-items: center;
  background: var(--panel);
  border: 2px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 12px;
  justify-items: center;
  padding: 22px 26px;
}

.loader-ring {
  animation: spin 0.75s linear infinite;
  border: 3px solid rgba(29, 29, 27, 0.18);
  border-top-color: var(--ink);
  border-radius: 999px;
  height: 34px;
  width: 34px;
}

.welcome-card {
  display: grid;
  gap: 12px;
}

.welcome-card p {
  color: var(--muted);
  line-height: 1.35;
  margin-bottom: 0;
}

.welcome-card .help-list {
  gap: 7px;
}

.welcome-card .primary-action {
  margin: 0;
}

.never-row {
  align-items: center;
  display: flex;
  gap: 8px;
}

.never-row input {
  width: auto;
}

.section-head {
  align-items: center;
  justify-content: space-between;
}

.archive-list {
  display: grid;
  gap: 8px;
  max-height: 260px;
  overflow: auto;
}

.archive-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  padding: 8px;
}

.archive-item strong,
.archive-item span {
  display: block;
}

.archive-item span {
  color: var(--muted);
  font-size: 13px;
}

.help-list {
  display: grid;
  gap: 8px;
  margin: 0;
  padding-left: 20px;
}

.help-list li {
  color: var(--muted);
  line-height: 1.35;
}

.distribution {
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  height: 130px;
  margin: 12px 0;
  position: relative;
  overflow: hidden;
  padding: 10px;
}

.distribution::before {
  content: "";
  position: absolute;
  inset: 10px;
  background:
    linear-gradient(to top, rgba(29, 29, 27, 0.08) 1px, transparent 1px) 0 0 / 100% 25%,
    linear-gradient(to right, rgba(29, 29, 27, 0.08) 1px, transparent 1px) 0 0 / 20% 100%;
}

.distribution-bar {
  position: absolute;
  inset: 10px;
}

.distribution-column {
  background: var(--gold);
  border: 1px solid var(--line);
  border-radius: 5px 5px 0 0;
  bottom: 0;
  min-width: 12px;
  position: absolute;
  transform: translateX(-50%);
  width: 8%;
  z-index: 1;
}

.distribution-column.mine {
  background: var(--green);
}

.distribution-column.optimal {
  box-shadow: inset 0 0 0 4px rgba(217, 154, 20, 0.35);
}

.bar-row {
  display: grid;
  grid-template-columns: 52px 1fr 36px;
  gap: 8px;
  align-items: center;
}

.bar-track {
  height: 22px;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: #fff;
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  background: var(--gold);
}

.bar-fill.mine {
  background: var(--green);
}

label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 13px;
}

input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  padding: 9px;
}

.creator-login,
.creator-form {
  display: grid;
  gap: 12px;
}

.creator-drawer {
  max-height: calc(100vh - 118px);
  overflow: auto;
}

.creator-mode {
  grid-template-rows: auto minmax(0, 1fr);
  overflow: auto;
}

.creator-mode .topbar nav,
.creator-mode .board-panel,
.creator-mode .side-panel > :not(#creatorPanel) {
  display: none;
}

.creator-mode .app-shell {
  display: block;
  height: auto;
  max-width: 1180px;
}

.creator-mode .side-panel {
  max-height: none;
  overflow: visible;
  padding: 0;
  background: transparent;
  border: 0;
  border-radius: 0;
  box-shadow: none;
}

.creator-mode .creator-drawer {
  min-height: calc(100vh - 104px);
  max-height: none;
  margin-top: 0;
}

.creator-mode .creator-form {
  grid-template-columns: minmax(260px, 360px) 1fr;
  align-items: start;
}

.creator-mode .creator-review,
.creator-mode .field-grid,
.creator-mode .palette,
.creator-mode .tool-row,
.creator-mode .creator-optimal,
.creator-mode #creatorStatus {
  grid-column: 1;
}

.creator-mode .creator-grid {
  grid-column: 2;
  grid-row: 1 / span 6;
  align-self: start;
  max-height: calc(100vh - 180px);
}

.creator-review {
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  padding: 10px;
}

.creator-review h3 {
  font-size: 15px;
  margin: 0;
}

.creator-puzzle-list {
  display: grid;
  gap: 6px;
  max-height: 190px;
  overflow: auto;
  padding-top: 8px;
}

.creator-puzzle-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  border: 1px solid rgba(34, 33, 31, 0.35);
  border-radius: 6px;
  padding: 7px;
}

.creator-puzzle-actions {
  display: flex;
  gap: 6px;
}

.creator-puzzle-actions button {
  min-height: 34px;
  padding: 6px 9px;
}

.creator-puzzle-item strong,
.creator-puzzle-item span {
  display: block;
}

.creator-puzzle-item span {
  color: var(--muted);
  font-size: 12px;
}

.field-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

.palette {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.palette label {
  display: flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  padding: 6px;
}

.palette input {
  width: auto;
}

.creator-grid {
  display: grid;
  gap: 3px;
  overflow: auto;
  max-width: 100%;
  padding: 4px;
  background: #e6dfd1;
  border: 1px solid var(--line);
  border-radius: 7px;
}

.creator-cell {
  width: 38px;
  aspect-ratio: 1;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: #fffaf0;
  display: grid;
  place-items: center;
  font-size: 11px;
  line-height: 1;
  padding: 0;
  position: relative;
}

.creator-cell.on-optimal {
  outline: 3px solid var(--gold);
  outline-offset: -4px;
}

.creator-cell.optimal-start {
  outline-color: var(--green);
}

.creator-cell.optimal-end {
  outline-color: var(--red);
}

.creator-cell.on-optimal::after {
  align-items: center;
  background: var(--gold);
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--ink);
  content: attr(data-step);
  display: flex;
  font-size: 10px;
  font-weight: 700;
  height: 18px;
  justify-content: center;
  line-height: 1;
  position: absolute;
  right: 2px;
  top: 2px;
  width: 18px;
}

.creator-optimal {
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  padding: 10px;
}

.creator-optimal strong {
  display: block;
  margin-bottom: 8px;
}

.creator-optimal ol {
  display: grid;
  gap: 4px;
  margin: 0;
  max-height: 180px;
  overflow: auto;
  padding-left: 24px;
}

.creator-optimal li {
  color: var(--muted);
  font-size: 13px;
}

.cell-wall {
  background: #262626;
  color: #fff;
}

.cell-start {
  background: #dff3ff;
}

.cell-finish {
  background: #f2e3ff;
}

.cell-moves {
  background: #e9ffe9;
}

.cell-points.positive {
  background: #fff5cc;
}

.cell-points.negative {
  background: #ffe1dd;
}

@media (max-width: 860px) {
  body {
    height: auto;
    min-height: 100%;
    overflow: auto;
    padding: 8px;
    gap: 6px;
  }

  .topbar {
    align-items: stretch;
    flex-direction: column;
    gap: 6px;
  }

  h1 {
    font-size: 26px;
    line-height: 1;
    margin-bottom: 2px;
  }

  .topbar p {
    font-size: 14px;
    line-height: 1.2;
  }

  .topbar nav {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 7px;
  }

  body.backend-disabled .topbar nav {
    grid-template-columns: minmax(0, 1fr);
  }

  .topbar nav button {
    min-height: 34px;
    padding: 4px 6px;
  }

  .app-shell {
    grid-template-columns: 1fr;
    height: auto;
    gap: 8px;
  }

  .board-panel {
    height: min(52svh, 392px);
    min-height: 328px;
    order: 0;
    padding: 7px;
  }

  #canvasHost {
    min-height: 0;
  }

  .side-panel {
    order: 1;
    max-height: none;
    overflow: visible;
    padding: 8px;
  }

  .stat-grid {
    gap: 6px;
    margin-bottom: 6px;
  }

  .stat-grid div {
    padding: 7px;
  }

  .stat-grid span {
    min-height: 0;
    font-size: 12px;
  }

  .stat-grid strong {
    font-size: 24px;
    line-height: 1.1;
    margin-top: 4px;
  }

  .tool-row {
    gap: 7px;
    order: 2;
    margin-bottom: 6px;
  }

  .tool-row button {
    min-height: 36px;
    padding: 5px 9px;
  }

  .primary-action {
    margin: 0 0 8px;
    min-height: 38px;
    order: 3;
  }

  .side-panel {
    display: flex;
    flex-direction: column;
  }

  .stat-grid {
    order: 1;
  }

  .status-text {
    align-items: center;
    display: flex;
    font-size: 14px;
    min-height: 24px;
    line-height: 1.25;
    margin-bottom: 0;
    margin-top: 0;
    order: 4;
  }

  .results,
  .secondary-action,
  .creator-drawer {
    order: 5;
  }

  .mobile-pad {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 6px;
    margin-top: 4px;
  }

  .mobile-pad button {
    min-width: 0;
    min-height: 28px;
    padding: 2px 6px;
    font-size: 19px;
  }

  body.infinite-mode {
    gap: 4px;
    padding: 6px;
  }

  body.infinite-mode .topbar {
    gap: 3px;
  }

  body.infinite-mode h1 {
    font-size: 23px;
    margin-bottom: 0;
  }

  body.infinite-mode .topbar p {
    font-size: 13px;
  }

  body.infinite-mode .topbar nav {
    gap: 5px;
  }

  body.infinite-mode .topbar nav button {
    min-height: 31px;
    padding: 3px 5px;
  }

  body.infinite-mode .board-panel {
    height: min(57svh, 430px);
    min-height: 386px;
    padding: 6px;
  }

  body.infinite-mode .mobile-pad {
    gap: 5px;
    margin-top: 3px;
  }

  body.infinite-mode .mobile-pad button {
    min-height: 26px;
  }

  body.infinite-mode .side-panel {
    padding: 6px;
  }

  body.infinite-mode .stat-grid {
    gap: 5px;
    margin-bottom: 5px;
  }

  body.infinite-mode .stat-grid div {
    padding: 5px 7px;
  }

  body.infinite-mode .stat-grid span {
    font-size: 11px;
  }

  body.infinite-mode .stat-grid strong {
    font-size: 21px;
    margin-top: 1px;
  }

  body.infinite-mode .tool-row {
    gap: 5px;
    margin-bottom: 5px;
  }

  body.infinite-mode .tool-row button {
    min-height: 31px;
    padding: 3px 8px;
  }

  body.infinite-mode .primary-action {
    margin-bottom: 5px;
    min-height: 34px;
  }

  body.infinite-mode .secondary-action {
    margin: 0;
    min-height: 34px;
    order: 4;
  }

  body.infinite-mode .status-text {
    display: none;
  }

  body.backend-disabled.infinite-mode {
    gap: clamp(8px, 1.2svh, 14px);
    height: 100dvh;
    min-height: 100dvh;
    overflow: hidden;
    padding: clamp(8px, 1.2svh, 14px);
    padding-bottom: max(18px, env(safe-area-inset-bottom, 0px));
  }

  body.backend-disabled.infinite-mode .topbar {
    align-items: center;
    flex-direction: row;
    gap: 12px;
    min-height: clamp(44px, 6svh, 58px);
  }

  body.backend-disabled.infinite-mode .topbar > div {
    min-width: 0;
  }

  body.backend-disabled.infinite-mode h1 {
    font-size: clamp(27px, 7vw, 34px);
    line-height: 1.05;
    margin-bottom: 2px;
  }

  body.backend-disabled.infinite-mode .topbar p {
    font-size: clamp(12px, 3.2vw, 14px);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  body.backend-disabled.infinite-mode .topbar nav {
    display: flex;
    flex: 0 0 auto;
    gap: 0;
  }

  body.backend-disabled.infinite-mode .topbar nav button {
    min-height: clamp(36px, 4.8svh, 42px);
    min-width: 64px;
    padding: 5px 10px;
  }

  body.backend-disabled.infinite-mode .app-shell {
    align-content: start;
    align-items: start;
    display: grid;
    gap: clamp(8px, 1.2svh, 14px);
    grid-template-columns: 1fr;
    grid-template-rows: auto auto;
    height: auto;
    min-height: 0;
  }

  body.backend-disabled.infinite-mode .board-panel {
    height: auto;
    min-height: 0;
    padding: clamp(6px, 1svh, 10px);
    width: 100%;
  }

  body.backend-disabled.infinite-mode #canvasHost {
    aspect-ratio: var(--puzzle-aspect, 1 / 1);
    height: auto;
    max-height: 58svh;
    width: 100%;
  }

  body.backend-disabled.infinite-mode .mobile-pad {
    gap: clamp(5px, 0.8svh, 8px);
    margin-top: clamp(3px, 0.7svh, 7px);
  }

  body.backend-disabled.infinite-mode .mobile-pad button {
    min-height: clamp(26px, 3.6svh, 34px);
  }

  body.backend-disabled.infinite-mode .side-panel {
    flex: 0 0 auto;
    padding: clamp(10px, 1.5svh, 14px);
    width: 100%;
  }

  body.backend-disabled.infinite-mode .stat-grid {
    gap: clamp(7px, 1svh, 10px);
    margin-bottom: clamp(8px, 1.2svh, 12px);
  }

  body.backend-disabled.infinite-mode .stat-grid div {
    padding: clamp(8px, 1.2svh, 12px);
  }

  body.backend-disabled.infinite-mode .stat-grid span {
    font-size: clamp(12px, 3.2vw, 14px);
  }

  body.backend-disabled.infinite-mode .stat-grid strong {
    font-size: clamp(24px, 7vw, 32px);
    margin-top: 4px;
  }

  body.backend-disabled.infinite-mode .tool-row {
    gap: clamp(7px, 1svh, 10px);
    margin-bottom: clamp(8px, 1.1svh, 12px);
  }

  body.backend-disabled.infinite-mode .tool-row button {
    min-height: clamp(34px, 4.6svh, 42px);
  }

  body.backend-disabled.infinite-mode .primary-action,
  body.backend-disabled.infinite-mode .secondary-action {
    min-height: clamp(40px, 5.2svh, 48px);
  }

  .creator-mode .creator-form {
    grid-template-columns: 1fr;
  }

  .creator-mode .field-grid,
  .creator-mode .palette,
  .creator-mode .tool-row,
  .creator-mode .creator-optimal,
  .creator-mode #creatorStatus,
  .creator-mode .creator-grid {
    grid-column: auto;
    grid-row: auto;
  }
}
