:root {
  color-scheme: light;
  --bg: #f5f7f6;
  --surface: #ffffff;
  --surface-2: #eef3f1;
  --ink: #1f2528;
  --muted: #657174;
  --line: #d6dddc;
  --human: #087f8c;
  --human-dark: #056673;
  --llm: #d95d39;
  --llm-dark: #b74627;
  --accent: #f2b84b;
  --good: #3f8f5f;
  --danger: #b73535;
  --shadow: 0 18px 48px rgba(21, 37, 42, 0.12);
  --radius: 8px;
  --cell-gap: 4px;
  --tabletop-image: url("assets/ui-tabletop.webp");
  --surface-texture: url("assets/ui-surface-texture.webp");
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  min-width: 320px;
  margin: 0;
  background-color: var(--bg);
  background-image:
    linear-gradient(
      180deg,
      rgba(245, 247, 246, 0.34) 0,
      rgba(245, 247, 246, 0.82) 360px,
      var(--bg) 760px
    ),
    var(--tabletop-image);
  background-position:
    top center,
    top center;
  background-repeat: no-repeat;
  background-size:
    100% 820px,
    min(1536px, 100vw) auto;
  color: var(--ink);
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

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

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  clip-path: inset(50%);
}

.app-shell {
  width: min(1440px, calc(100% - 32px));
  margin: 0 auto;
  padding: 24px 0 32px;
  position: relative;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 18px;
  padding: 14px;
  border: 1px solid rgba(214, 221, 220, 0.78);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.82);
  box-shadow: 0 14px 34px rgba(21, 37, 42, 0.11);
  backdrop-filter: blur(14px);
}

.brand {
  display: flex;
  align-items: center;
  min-width: 0;
  gap: 14px;
}

.brand-mark {
  width: 48px;
  height: 48px;
  flex: 0 0 auto;
  filter: drop-shadow(0 8px 14px rgba(5, 102, 115, 0.18));
}

.brand h1,
.panel h2,
.board-heading h2,
.log-panel h2 {
  margin: 0;
  letter-spacing: 0;
}

.brand h1 {
  font-size: 1.65rem;
  line-height: 1.1;
}

.brand p,
.panel p,
.board-heading p,
.log-panel p {
  margin: 4px 0 0;
  color: var(--muted);
  line-height: 1.45;
}

.top-actions,
.button-row,
.tool-buttons,
.log-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.button,
.icon-button {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  min-height: 40px;
  transition:
    background 120ms ease,
    border-color 120ms ease,
    transform 120ms ease;
}

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 14px;
  font-weight: 700;
  min-width: 0;
  text-align: center;
  white-space: nowrap;
}

.button:hover,
.icon-button:hover {
  border-color: #99aaa7;
  background: #f9fbfa;
}

.button:active,
.icon-button:active,
.piece-card:active {
  transform: translateY(1px);
}

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

.button-primary:hover {
  border-color: var(--human-dark);
  background: var(--human-dark);
}

.icon-button {
  width: 42px;
  padding: 0;
  font-size: 1.2rem;
  font-weight: 900;
}

.game-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(320px, 420px);
  gap: 18px;
  align-items: start;
}

.play-area,
.panel,
.log-panel {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.91), rgba(255, 255, 255, 0.91)),
    var(--surface-texture);
  background-size:
    auto,
    360px 360px;
  box-shadow: var(--shadow);
}

.play-area {
  overflow: hidden;
}

.status-strip {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 1px;
  background: var(--line);
  border-bottom: 1px solid var(--line);
}

.status-strip > div {
  min-width: 0;
  padding: 12px 14px;
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.92), rgba(255, 255, 255, 0.92)),
    var(--surface-texture);
  background-size:
    auto,
    280px 280px;
}

.label {
  display: block;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.status-strip strong {
  display: block;
  margin-top: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.board-wrap {
  padding: clamp(14px, 2.8vw, 28px);
}

.board-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 14px;
}

.player-badge {
  flex: 0 0 auto;
  border-radius: 999px;
  padding: 7px 12px;
  background: rgba(8, 127, 140, 0.12);
  color: var(--human-dark);
  font-weight: 900;
}

.player-badge.llm {
  background: rgba(217, 93, 57, 0.14);
  color: var(--llm-dark);
}

.board {
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  gap: var(--cell-gap);
  width: min(100%, 680px);
  aspect-ratio: 1;
  margin: 0 auto;
  padding: var(--cell-gap);
  border: 2px solid #223033;
  border-radius: var(--radius);
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.12) 25%, transparent 25%) 0 0 /
      20px 20px,
    linear-gradient(rgba(34, 48, 51, 0.9), rgba(34, 48, 51, 0.9)),
    var(--surface-texture) center / 320px 320px,
    #223033;
}

.cell {
  position: relative;
  min-width: 0;
  min-height: 0;
  border: 0;
  border-radius: 5px;
  background-image:
    linear-gradient(rgba(246, 250, 249, 0.84), rgba(246, 250, 249, 0.84)),
    var(--surface-texture);
  background-size:
    auto,
    180px 180px;
  background-color: #f6faf9;
  color: #fff;
  font-weight: 900;
  outline-offset: -3px;
  transition:
    background 100ms ease,
    transform 100ms ease,
    box-shadow 100ms ease;
}

.cell::after {
  content: attr(data-label);
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: clamp(0.7rem, 2vw, 1rem);
}

.cell.human {
  background-image:
    linear-gradient(145deg, rgba(8, 127, 140, 0.94), rgba(10, 166, 166, 0.94)),
    var(--surface-texture);
  background-size:
    auto,
    160px 160px;
  background-blend-mode: normal, multiply;
  box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.12);
}

.cell.llm {
  background-image:
    linear-gradient(145deg, rgba(217, 93, 57, 0.94), rgba(242, 122, 82, 0.94)),
    var(--surface-texture);
  background-size:
    auto,
    160px 160px;
  background-blend-mode: normal, multiply;
  box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.12);
}

.cell.ghost-valid {
  background: rgba(63, 143, 95, 0.24);
  box-shadow: inset 0 0 0 2px var(--good);
}

.cell.ghost-invalid {
  background: rgba(183, 53, 53, 0.18);
  box-shadow: inset 0 0 0 2px var(--danger);
}

.cell.drop-target {
  transform: scale(0.98);
}

.control-column {
  display: grid;
  gap: 14px;
}

.panel,
.log-panel {
  padding: 16px;
}

.panel-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.piece-rack {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  max-height: 292px;
  overflow: auto;
  padding-right: 2px;
}

.piece-card {
  display: grid;
  grid-template-columns: 58px minmax(0, 1fr);
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 68px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.9), rgba(255, 255, 255, 0.9)),
    var(--surface-texture);
  background-size:
    auto,
    260px 260px;
  padding: 8px;
  text-align: left;
}

.piece-card.selected {
  border-color: var(--human);
  box-shadow: 0 0 0 2px rgba(8, 127, 140, 0.18);
}

.piece-card[draggable="true"] {
  cursor: grab;
}

.piece-card[draggable="true"]:active {
  cursor: grabbing;
}

.piece-meta {
  min-width: 0;
}

.piece-meta strong {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.piece-meta span {
  display: block;
  color: var(--muted);
  font-size: 0.82rem;
  margin-top: 2px;
}

.mini-piece,
.piece-preview {
  display: grid;
  justify-content: start;
  align-content: center;
}

.mini-piece {
  grid-template-columns: repeat(5, 9px);
  grid-auto-rows: 9px;
  gap: 2px;
  width: 54px;
  height: 54px;
}

.piece-preview {
  grid-template-columns: repeat(5, 22px);
  grid-auto-rows: 22px;
  gap: 4px;
  min-height: 132px;
  margin-top: 12px;
  padding: 12px;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  background-image:
    linear-gradient(rgba(238, 243, 241, 0.82), rgba(238, 243, 241, 0.82)),
    var(--surface-texture);
  background-size:
    auto,
    240px 240px;
}

.mini-block,
.preview-block {
  border-radius: 3px;
  background-image:
    linear-gradient(145deg, rgba(242, 184, 75, 0.96), rgba(255, 205, 107, 0.96)),
    var(--surface-texture);
  background-size:
    auto,
    120px 120px;
  background-blend-mode: normal, multiply;
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.14);
}

.selected-tools {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 12px;
}

.settings-grid {
  display: grid;
  gap: 10px;
}

.settings-grid label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 800;
}

.settings-grid input,
.settings-grid select,
.manual-json textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  color: var(--ink);
  min-height: 38px;
  padding: 8px 10px;
}

.settings-grid input:focus,
.settings-grid select:focus,
.manual-json textarea:focus {
  outline: 3px solid rgba(8, 127, 140, 0.22);
  border-color: var(--human);
}

.button-row {
  margin-top: 10px;
}

.button-row .button {
  flex: 1 1 120px;
}

.manual-json {
  margin-top: 12px;
  border-top: 1px solid var(--line);
  padding-top: 12px;
}

.manual-json summary {
  color: var(--muted);
  cursor: pointer;
  font-weight: 800;
}

.manual-json textarea {
  min-height: 112px;
  margin: 10px 0 8px;
  resize: vertical;
  font-family:
    "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-size: 0.86rem;
}

.commentary-box {
  min-height: 112px;
  max-height: 240px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
  background-image:
    linear-gradient(rgba(238, 243, 241, 0.86), rgba(238, 243, 241, 0.86)),
    var(--surface-texture);
  background-size:
    auto,
    280px 280px;
  color: #2d383a;
  line-height: 1.55;
  white-space: pre-wrap;
}

.log-panel {
  margin-top: 18px;
}

.turn-log {
  display: grid;
  gap: 8px;
  max-height: 300px;
  margin: 0;
  overflow: auto;
  padding: 0;
  list-style: none;
}

.turn-log li {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  padding: 10px 12px;
}

.turn-log strong {
  display: block;
  margin-bottom: 3px;
}

.turn-log span {
  display: block;
  color: var(--muted);
  line-height: 1.4;
}

.turn-log small {
  display: block;
  margin-top: 7px;
  color: #435052;
  font-family:
    "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-size: 0.78rem;
  line-height: 1.45;
  word-break: break-word;
}

.log-actions {
  justify-content: flex-end;
}

.file-button {
  min-height: 40px;
  cursor: pointer;
}

.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 20;
  width: min(360px, calc(100% - 36px));
  border: 1px solid rgba(31, 37, 40, 0.15);
  border-radius: var(--radius);
  background: #223033;
  color: #fff;
  padding: 12px 14px;
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateY(14px);
  pointer-events: none;
  transition:
    opacity 140ms ease,
    transform 140ms ease;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

@media (max-width: 1040px) {
  .game-layout {
    grid-template-columns: 1fr;
  }

  .control-column {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 760px) {
  body {
    background-image:
      linear-gradient(
        180deg,
        rgba(245, 247, 246, 0.38) 0,
        rgba(245, 247, 246, 0.88) 260px,
        var(--bg) 520px
      ),
      var(--tabletop-image);
    background-size:
      100% 560px,
      auto 420px;
  }

  .app-shell {
    width: min(100% - 20px, 720px);
    padding-top: 12px;
  }

  .topbar,
  .board-heading,
  .panel-header,
  .selected-tools {
    align-items: stretch;
    flex-direction: column;
  }

  .top-actions .button,
  .top-actions,
  .log-actions,
  .log-actions .button {
    width: 100%;
  }

  .button,
  .icon-button {
    min-height: 44px;
  }

  .brand h1 {
    font-size: 1.4rem;
  }

  .brand p,
  .panel p,
  .board-heading p,
  .log-panel p {
    font-size: 0.94rem;
  }

  .status-strip {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .status-strip > div:first-child {
    grid-column: span 2;
  }

  .control-column {
    grid-template-columns: 1fr;
  }

  .piece-rack {
    grid-template-columns: 1fr;
    max-height: none;
  }

  .board {
    --cell-gap: 3px;
  }
}

@media (max-width: 420px) {
  .app-shell {
    width: 100%;
    padding: 8px 10px 24px;
  }

  .topbar,
  .panel,
  .log-panel {
    padding: 12px;
  }

  .board-wrap {
    padding: 12px 10px 14px;
  }

  .button {
    padding: 0 10px;
    font-size: 0.92rem;
  }

  .status-strip > div {
    padding: 10px 12px;
  }
}
