:root {
  --bg: #1a1210;
  --bg-elevated: #2a1c18;
  --bg-panel: #32221d;
  --border: rgba(255, 214, 170, 0.12);
  --text: #f6ebe3;
  --text-muted: #c4a996;
  --accent: #e8a45a;
  --accent-hover: #f0b872;
  --accent-text: #1a1210;
  --danger: #e85d5d;
  --poison: #6b2d8b;
  --poison-glow: #a855f7;
  --choco: #8b4513;
  --choco-light: #a0522d;
  --choco-dark: #5c2e0a;
  --choco-ridge: #c4783a;
  --eaten: rgba(255, 255, 255, 0.04);
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
  --radius: 16px;
  --radius-sm: 10px;
  --font: "DM Sans", system-ui, sans-serif;
  --display: "Fraunces", Georgia, serif;
  --focus: 0 0 0 3px rgba(232, 164, 90, 0.45);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  font-family: var(--font);
  color: var(--text);
  background: var(--bg);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.back-link {
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 50;
  font-size: 13px;
  color: var(--text-muted);
  text-decoration: none;
  background: rgba(42, 28, 24, 0.9);
  border: 1px solid var(--border);
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  box-sizing: border-box;
  padding: 0 14px;
  border-radius: 8px;
  backdrop-filter: blur(8px);
  transition: color 0.15s, border-color 0.15s;
}

.back-link:hover {
  color: var(--text);
  border-color: rgba(255, 214, 170, 0.28);
}

.bg-glow {
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(ellipse 80% 50% at 50% -10%, rgba(232, 164, 90, 0.18), transparent 55%),
    radial-gradient(ellipse 40% 40% at 90% 80%, rgba(107, 45, 139, 0.12), transparent 50%),
    radial-gradient(ellipse 35% 30% at 10% 70%, rgba(139, 69, 19, 0.15), transparent 50%);
  z-index: 0;
}

.app {
  position: relative;
  z-index: 1;
  max-width: 640px;
  margin: 0 auto;
  padding: 3.25rem 1.25rem 3rem;
}

.header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.brand {
  display: flex;
  gap: 0.85rem;
  align-items: center;
}

.brand-mark {
  font-size: 2rem;
  line-height: 1;
  filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.35));
}

.header h1 {
  font-family: var(--display);
  font-size: 1.85rem;
  font-weight: 700;
  margin: 0;
  letter-spacing: -0.02em;
}

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

.panel {
  background: linear-gradient(165deg, rgba(50, 34, 29, 0.95), rgba(36, 24, 20, 0.98));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.35rem 1.35rem 1.5rem;
}

.panel h2 {
  font-family: var(--display);
  font-size: 1.25rem;
  margin: 0 0 1.1rem;
  font-weight: 600;
}

.hidden {
  display: none !important;
}

.field-row {
  display: flex;
  align-items: flex-end;
  gap: 0.75rem;
  margin-bottom: 0.5rem;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  flex: 1;
}

.field span {
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.field input,
.field select {
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.65rem 0.75rem;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.field input:focus,
.field select:focus {
  border-color: var(--accent);
  box-shadow: var(--focus);
}

.field input {
  width: 100%;
  font-variant-numeric: tabular-nums;
  font-size: 1.15rem;
  font-weight: 600;
}

.times {
  font-size: 1.4rem;
  color: var(--text-muted);
  padding-bottom: 0.55rem;
  user-select: none;
}

.field-select {
  margin: 1rem 0 1.25rem;
}

.hint {
  margin: 0 0 1.15rem;
  font-size: 0.88rem;
  color: var(--text-muted);
}

.mode-fieldset {
  border: none;
  margin: 0 0 0.25rem;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.mode-fieldset legend {
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
  padding: 0;
}

.mode-card {
  display: block;
  cursor: pointer;
}

.mode-card input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.mode-card-body {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  padding: 0.85rem 1rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: rgba(0, 0, 0, 0.2);
  transition: border-color 0.15s, background 0.15s, box-shadow 0.15s;
}

.mode-card-body strong {
  font-weight: 600;
}

.mode-card-body span:last-child {
  font-size: 0.88rem;
  color: var(--text-muted);
}

.mode-card input:focus-visible + .mode-card-body {
  box-shadow: var(--focus);
}

.mode-card input:checked + .mode-card-body {
  border-color: rgba(232, 164, 90, 0.55);
  background: rgba(232, 164, 90, 0.1);
}

.btn {
  font: inherit;
  font-weight: 600;
  border: none;
  border-radius: 999px;
  min-height: 44px;
  padding: 0.65rem 1.15rem;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s, color 0.15s, opacity 0.15s;
}

.btn:active:not(:disabled) {
  transform: scale(0.98);
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary {
  background: linear-gradient(180deg, var(--accent-hover), var(--accent));
  color: var(--accent-text);
  box-shadow: 0 4px 16px rgba(232, 164, 90, 0.28);
}

.btn-primary:hover:not(:disabled) {
  background: linear-gradient(180deg, #f5c489, var(--accent-hover));
}

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid transparent;
}

.btn-ghost:hover:not(:disabled) {
  color: var(--text);
  background: rgba(255, 255, 255, 0.05);
  border-color: var(--border);
}

.btn-block {
  width: 100%;
  padding: 0.85rem 1.15rem;
  font-size: 1.02rem;
}

/* Game */
.game-top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 1.15rem;
}

.status {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
  flex: 1 1 12rem;
}

.status.thinking {
  color: var(--accent);
}

.status.win {
  color: #7ddea0;
}

.status.lose {
  color: var(--danger);
}

.game-actions {
  display: flex;
  gap: 0.35rem;
}

.board-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.85rem;
}

.board {
  display: grid;
  gap: 4px;
  padding: 12px;
  background: linear-gradient(145deg, #1f1410, #140d0a);
  border-radius: 14px;
  border: 1px solid rgba(255, 214, 170, 0.08);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
  /* cell size set via CSS vars from JS */
  --cell: 48px;
  grid-auto-rows: var(--cell);
  grid-template-columns: repeat(var(--cols, 5), var(--cell));
  user-select: none;
  touch-action: manipulation;
}

.cell {
  width: var(--cell);
  height: var(--cell);
  border: none;
  border-radius: 8px;
  padding: 0;
  position: relative;
  cursor: default;
  transition:
    transform 0.12s ease,
    filter 0.12s ease,
    opacity 0.25s ease,
    box-shadow 0.12s ease;
}

.cell.present {
  cursor: pointer;
  background:
    linear-gradient(145deg, var(--choco-light) 0%, var(--choco) 45%, var(--choco-dark) 100%);
  box-shadow:
    inset 0 1px 0 rgba(255, 200, 140, 0.35),
    inset 0 -2px 4px rgba(0, 0, 0, 0.25),
    0 2px 4px rgba(0, 0, 0, 0.3);
}

.cell.present::after {
  content: "";
  position: absolute;
  inset: 22%;
  border-radius: 3px;
  border: 1px solid rgba(255, 200, 140, 0.12);
  pointer-events: none;
}

.cell.present:hover:not(:disabled) {
  transform: translateY(-2px) scale(1.04);
  filter: brightness(1.12);
  box-shadow:
    inset 0 1px 0 rgba(255, 200, 140, 0.45),
    0 6px 14px rgba(0, 0, 0, 0.35);
  z-index: 1;
}

.cell.present:focus-visible {
  outline: none;
  box-shadow: var(--focus), inset 0 1px 0 rgba(255, 200, 140, 0.35);
  z-index: 1;
}

.cell.poison.present {
  background:
    radial-gradient(circle at 40% 35%, rgba(232, 164, 90, 0.35), transparent 50%),
    linear-gradient(145deg, #8b3fa8 0%, var(--poison) 50%, #3d1458 100%);
  box-shadow:
    inset 0 1px 0 rgba(232, 180, 255, 0.35),
    0 0 12px rgba(168, 85, 247, 0.35),
    0 2px 4px rgba(0, 0, 0, 0.3);
}

.cell.poison.present::before {
  content: "☠";
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: calc(var(--cell) * 0.42);
  line-height: 1;
  opacity: 0.9;
  pointer-events: none;
}

.cell.poison.present::after {
  display: none;
}

.cell.eaten {
  background: var(--eaten);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.03);
  cursor: default;
  pointer-events: none;
}

.cell.preview-chomp:not(.eaten) {
  filter: brightness(1.25) saturate(1.15);
  outline: 2px solid rgba(232, 164, 90, 0.7);
  outline-offset: -2px;
  z-index: 1;
}

.cell.just-eaten {
  animation: chomp-out 0.35s ease forwards;
}

@keyframes chomp-out {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  40% {
    transform: scale(1.08);
    opacity: 0.85;
  }
  100% {
    transform: scale(0.6);
    opacity: 0;
  }
}

.board.locked .cell.present {
  cursor: wait;
}

.board.locked .cell.present:hover {
  transform: none;
  filter: none;
}

.board-caption {
  margin: 0;
  text-align: center;
  font-size: 0.82rem;
  color: var(--text-muted);
  max-width: 28rem;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 1rem;
  margin-top: 1.15rem;
  font-size: 0.82rem;
  color: var(--text-muted);
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}

.swatch {
  width: 14px;
  height: 14px;
  border-radius: 4px;
}

.swatch.choco {
  background: linear-gradient(145deg, var(--choco-light), var(--choco-dark));
}

.swatch.poison {
  background: linear-gradient(145deg, #8b3fa8, var(--poison));
}

.swatch.gone {
  background: var(--eaten);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.12);
}

/* Modal */
.modal {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-panel);
  color: var(--text);
  padding: 0;
  max-width: min(420px, calc(100vw - 2rem));
  box-shadow: var(--shadow);
}

.modal::backdrop {
  background: rgba(10, 6, 5, 0.72);
  backdrop-filter: blur(4px);
}

.modal-inner {
  padding: 1.35rem 1.4rem 1.4rem;
  margin: 0;
}

.modal-inner h2 {
  font-family: var(--display);
  margin: 0 0 0.85rem;
  font-size: 1.3rem;
}

.modal-inner ol {
  margin: 0 0 1rem;
  padding-left: 1.2rem;
  color: var(--text-muted);
}

.modal-inner li {
  margin-bottom: 0.5rem;
}

.modal-inner li strong {
  color: var(--text);
}

.modal-note {
  font-size: 0.88rem;
  color: var(--text-muted);
  margin: 0 0 1.15rem;
}

.modal-inner .btn {
  width: 100%;
}

@media (max-width: 480px) {
  .app {
    padding: 4.25rem 0.85rem 2.5rem;
  }

  .board {
    --cell: min(48px, calc((100vw - 4rem) / var(--cols, 5)));
    gap: 3px;
    padding: 8px;
  }

  .header h1 {
    font-size: 1.55rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .cell,
  .btn {
    transition: none;
  }

  .cell.just-eaten {
    animation: none;
  }
}
