/* ============================================================== foundations */

:root {
  color-scheme: dark;

  --table-1: #16241f;
  --table-2: #0d1714;
  --felt: #1d3a30;
  --panel: rgba(255, 255, 255, 0.045);
  --panel-line: rgba(255, 255, 255, 0.1);
  --text: #eef2ef;
  --text-dim: #9fb0a8;
  --accent: #e0b062;
  --accent-ink: #2a1e08;
  --danger: #e0685f;
  --good: #6fd39a;
  --shadow: rgba(0, 0, 0, 0.5);

  /* The tile itself is ivory on any table, so its palette is theme-independent. */
  --face-bg: #f7f2e5;
  --face-edge: #e4d9be;
  --face-edge-dark: #a89a79;
  --note-bg: #22332c;
  --face-ink: #2f3436;
  --face-red: #c2362e;
  --face-green: #2f7d4f;
  --face-blue: #2a5a93;
  --face-gold: #d79b2b;
  --face-font: ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;

  --tile-w: 52px;
  --tile-h: 70px;
  --tile-edge: 6px;
  --radius: 10px;

  --ui-font: ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto,
    'Helvetica Neue', Arial, sans-serif;
}

:root[data-theme='light'] {
  color-scheme: light;
  --table-1: #ddcfb4;
  --table-2: #bfab88;
  --felt: #a8936d;
  --panel: rgba(0, 0, 0, 0.05);
  --panel-line: rgba(0, 0, 0, 0.12);
  --text: #2a241b;
  --text-dim: #6b6152;
  --accent: #9a6a12;
  --accent-ink: #fff8e8;
  --danger: #a8352c;
  --good: #1f7a4c;
  --shadow: rgba(70, 52, 22, 0.34);
  --note-bg: #fbf5e8;
}

* { box-sizing: border-box; }

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

body {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  font-family: var(--ui-font);
  color: var(--text);
  background:
    radial-gradient(120% 90% at 50% 0%, var(--table-1) 0%, var(--table-2) 100%);
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
}

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  z-index: 200;
  padding: 10px 16px;
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: 0 0 8px 0;
  font-weight: 600;
}
.skip-link:focus { left: 0; }

/* ==================================================================== chrome */

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 12px clamp(12px, 3vw, 28px);
  border-bottom: 1px solid var(--panel-line);
  background: var(--panel);
}

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

.brand-mark {
  width: 26px;
  height: 33px;
  color: var(--accent);
  flex: none;
}

.brand h1 {
  margin: 0;
  font-size: clamp(1rem, 2.6vw, 1.3rem);
  font-weight: 700;
  letter-spacing: -0.01em;
  white-space: nowrap;
}
.brand h1 span { color: var(--text-dim); font-weight: 500; }

.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 10px;
}

.field { display: flex; flex-direction: column; gap: 3px; }
.field-label {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  padding-left: 2px;
}

select {
  font: inherit;
  font-size: 0.86rem;
  padding: 7px 10px;
  min-width: 11.5rem;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--panel-line);
  border-radius: 8px;
}
select:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.btn-row { display: flex; gap: 6px; flex-wrap: wrap; }

.btn {
  font: inherit;
  font-size: 0.86rem;
  font-weight: 600;
  padding: 8px 13px;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--panel-line);
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.08s;
  white-space: nowrap;
}
.btn:hover:not(:disabled) { background: color-mix(in srgb, var(--accent) 18%, transparent); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: 0.38; cursor: not-allowed; }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.btn-primary {
  background: var(--accent);
  color: var(--accent-ink);
  border-color: transparent;
}
.btn-primary:hover:not(:disabled) { background: color-mix(in srgb, var(--accent) 85%, white); }

.btn-icon { padding: 8px 11px; font-size: 0.95rem; line-height: 1; }
.btn-icon .icon-off { display: none; }
.btn-icon[aria-pressed='false'] .icon-on { display: none; }
.btn-icon[aria-pressed='false'] .icon-off { display: inline; }
:root[data-theme='light'] #btn-theme .icon-on { display: none; }
:root[data-theme='light'] #btn-theme .icon-off { display: inline; }

/* ================================================================ statusbar */

.statusbar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px clamp(14px, 3vw, 30px);
  padding: 9px clamp(12px, 3vw, 28px);
  border-bottom: 1px solid var(--panel-line);
  font-variant-numeric: tabular-nums;
}

.stat { display: flex; align-items: baseline; gap: 6px; }
.stat-value { font-size: 1.02rem; font-weight: 700; }
.stat-label {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-dim);
}
.stat-wide { margin-left: auto; }
.statusbar .stat-wide + .stat-wide { margin-left: 0; }

.seed {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  letter-spacing: 0.06em;
  cursor: pointer;
  border-bottom: 1px dashed var(--panel-line);
}
.seed:hover { color: var(--accent); }

/* ==================================================================== board */

.board {
  position: relative;
  box-shadow: inset 0 0 120px rgba(0, 0, 0, 0.22);
  flex: 1 1 auto;
  min-height: 0;
  padding: clamp(10px, 2.5vw, 26px);
  overflow: hidden;
}
.board:focus { outline: none; }

.board-inner {
  position: absolute;
  left: 50%;
  top: 50%;
  transform-origin: center center;
  will-change: transform;
}

.board-note {
  position: absolute;
  bottom: 8px;
  left: 50%;
  transform: translateX(-50%);
  margin: 0;
  padding: 6px 14px;
  font-size: 0.8rem;
  color: var(--text-dim);
  background: var(--note-bg);
  border: 1px solid var(--panel-line);
  border-radius: 999px;
  box-shadow: 0 4px 14px var(--shadow);
  pointer-events: none;
  z-index: 5;
  opacity: 0;
  transition: opacity 0.2s;
  max-width: calc(100% - 24px);
  text-align: center;
}
.board-note.show { opacity: 1; }

/* ===================================================================== tile */

.tile {
  position: absolute;
  width: var(--tile-w);
  height: var(--tile-h);
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
  font: inherit;
  color: inherit;
  -webkit-tap-highlight-color: transparent;
  transition: transform 0.12s ease, opacity 0.2s ease;
}

.tile__side {
  position: absolute;
  inset: 0;
  border-radius: var(--radius);
  background: linear-gradient(150deg, var(--face-edge) 0%, var(--face-edge) 42%, var(--face-edge-dark) 100%);
  box-shadow: -3px 5px 9px var(--shadow), inset -1px -1px 0 rgba(0, 0, 0, 0.16);
}

.tile__face {
  position: absolute;
  top: 0;
  left: 0;
  width: calc(100% - var(--tile-edge));
  height: calc(100% - var(--tile-edge));
  border-radius: calc(var(--radius) - 2px);
  background: linear-gradient(160deg, #fffdf7 0%, var(--face-bg) 58%, #ebe3cf 100%);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.12), inset 1px 1px 0 rgba(255, 255, 255, 0.9);
  overflow: hidden;
  display: block;
}

.tile-face { display: block; width: 100%; height: 100%; }

/* Free tiles lift a little under the pointer; blocked ones do not respond. */
.tile.free:hover .tile__face,
.tile.free:focus-visible .tile__face { filter: brightness(1.04); }
.tile.free:hover { transform: translate(1px, -2px); }

.tile:focus-visible { outline: none; }
.tile:focus-visible .tile__side {
  box-shadow: 0 0 0 3px var(--accent), -3px 5px 9px var(--shadow);
}

.tile.blocked { cursor: default; }
.board.dim-blocked .tile.blocked .tile__face { filter: brightness(0.68) saturate(0.5); }
.board.dim-blocked .tile.blocked .tile__side { filter: brightness(0.74) saturate(0.6); }

.tile.selected .tile__side {
  background: linear-gradient(145deg, var(--accent) 0%, color-mix(in srgb, var(--accent) 60%, black) 100%);
}
.tile.selected .tile__face {
  box-shadow: inset 0 0 0 2px var(--accent), inset 0 0 18px color-mix(in srgb, var(--accent) 35%, transparent);
}
.tile.selected { transform: translate(1px, -3px); }

.tile.hinted .tile__face { animation: hint-pulse 1s ease-in-out 3; }
@keyframes hint-pulse {
  0%, 100% { box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.09); }
  50% { box-shadow: inset 0 0 0 3px var(--good), inset 0 0 20px color-mix(in srgb, var(--good) 45%, transparent); }
}

.tile.nope { animation: nope 0.32s ease; }
@keyframes nope {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-4px); }
  45% { transform: translateX(4px); }
  70% { transform: translateX(-2px); }
}

.tile.gone {
  opacity: 0;
  transform: scale(0.75) translateY(-10px);
  pointer-events: none;
}

.tile.matched .tile__face { box-shadow: inset 0 0 0 3px var(--good); }

@media (prefers-reduced-motion: reduce) {
  .tile, .tile__face { transition: none; }
  .tile.hinted .tile__face { animation: none; box-shadow: inset 0 0 0 3px var(--good); }
  .tile.nope { animation: none; }
}

/* ================================================================== dialogs */

.dlg {
  border: 1px solid var(--panel-line);
  border-radius: 16px;
  padding: 24px 26px;
  max-width: min(94vw, 34rem);
  color: var(--text);
  background: linear-gradient(160deg, var(--table-1), var(--table-2));
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5);
}
.dlg-wide { max-width: min(94vw, 46rem); }
.dlg::backdrop { background: rgba(0, 0, 0, 0.6); backdrop-filter: blur(3px); }
.dlg h2 { margin: 0 0 10px; font-size: 1.35rem; }
.dlg h3 { margin: 20px 0 8px; font-size: 0.95rem; color: var(--accent); }
.dlg p { margin: 0 0 12px; line-height: 1.6; color: var(--text-dim); }
.dlg strong { color: var(--text); }

.dlg-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 18px;
}

.end-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(7rem, 1fr));
  gap: 10px;
  margin: 16px 0 0;
}
.end-stats > div {
  padding: 10px 12px;
  background: var(--panel);
  border: 1px solid var(--panel-line);
  border-radius: 10px;
}
.end-stats dt {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-dim);
}
.end-stats dd { margin: 3px 0 0; font-size: 1.15rem; font-weight: 700; }

.help-body { max-height: min(60vh, 34rem); overflow-y: auto; padding-right: 4px; }
.keys { margin: 0; padding-left: 20px; color: var(--text-dim); line-height: 1.9; }
kbd {
  font: inherit;
  font-size: 0.8em;
  padding: 1px 6px;
  border: 1px solid var(--panel-line);
  border-bottom-width: 2px;
  border-radius: 5px;
  background: var(--panel);
  color: var(--text);
}

.tile-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(44px, 1fr));
  gap: 7px;
  margin-top: 6px;
}
.gallery-tile {
  position: relative;
  aspect-ratio: 60 / 80;
  border-radius: 7px;
  background: linear-gradient(160deg, #fffdf6, var(--face-bg));
  box-shadow: 0 1px 3px var(--shadow), inset 0 0 0 1px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

/* =================================================================== footer */

.footer {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 8px;
  padding: 10px clamp(12px, 3vw, 28px) calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--panel-line);
  font-size: 0.75rem;
  color: var(--text-dim);
}
.footer a { color: var(--text-dim); }
.footer a:hover { color: var(--accent); }

/* =================================================================== narrow */

@media (max-width: 720px) {
  .topbar { gap: 8px; padding: 8px 12px; }
  .brand-mark { width: 21px; height: 27px; }
  .brand h1 { font-size: 0.95rem; }

  /* Row one is the board picker beside the icon buttons, row two the actions. */
  .controls {
    display: grid;
    grid-template-columns: 1fr auto;
    align-items: end;
    gap: 7px;
    width: 100%;
  }
  .field { grid-row: 1; grid-column: 1; }
  .btn-row-icons { grid-row: 1; grid-column: 2; }
  .btn-row:not(.btn-row-icons) { grid-row: 2; grid-column: 1 / -1; }
  select { width: 100%; min-width: 0; padding: 6px 8px; }
  .btn { flex: 1 1 auto; padding: 8px 6px; font-size: 0.78rem; }
  .btn-row-icons .btn { flex: 0 0 auto; padding: 7px 9px; }

  .statusbar { gap: 3px 12px; padding: 7px 12px; }
  .stat-value { font-size: 0.92rem; }
  .stat-label { font-size: 0.6rem; letter-spacing: 0.05em; }
  .stat-wide { margin-left: 0; }

  .board { padding: 8px; }
  .board-note { font-size: 0.72rem; padding: 5px 11px; bottom: 6px; }

  .footer { font-size: 0.68rem; justify-content: center; }
  .footer span { display: none; }
}
