@font-face { font-family: 'Fraunces'; font-style: normal; font-weight: 600; font-display: swap; src: url('fonts/fraunces-600.woff2') format('woff2'); }
@font-face { font-family: 'Fraunces'; font-style: italic; font-weight: 500; font-display: swap; src: url('fonts/fraunces-italic-500.woff2') format('woff2'); }
@font-face { font-family: 'Atkinson'; font-style: normal; font-weight: 400; font-display: swap; src: url('fonts/atkinson-400.woff2') format('woff2'); }
@font-face { font-family: 'Atkinson'; font-style: normal; font-weight: 700; font-display: swap; src: url('fonts/atkinson-700.woff2') format('woff2'); }
@font-face { font-family: 'MJ CJK'; font-weight: 700; font-display: block; src: url('fonts/noto-serif-sc-tiles.woff2') format('woff2'); }

:root {
  --W: 60px; --H: 80px; --D: 7px;
  --ink: #1b2622;
  --vermilion: #c0392b;
  --pine: #276b46;
  --indigo: #2d5a86;
  --gold: #e6b84a;
  --bone: #f4ead3;
  --bone-2: #e6d8b6;
  --bone-edge: #c9b68a;
  --side: #5f8a63;
  --side-2: #40633f;
  --table: #17302a;
  --table-2: #0b1915;
  --felt-line: rgba(255, 255, 255, .035);
  --text: #efe6d2;
  --text-dim: rgba(239, 230, 210, .62);
  --panel: #1f2a27;
  --panel-edge: rgba(230, 184, 74, .28);
  --btn: rgba(255, 255, 255, .07);
  --btn-hover: rgba(255, 255, 255, .13);
  --shadow: rgba(0, 0, 0, .55);
  --sel: var(--gold);
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --table: #e8dcc2; --table-2: #cdbb97; --felt-line: rgba(0, 0, 0, .04);
    --text: #2a2521; --text-dim: rgba(42, 37, 33, .6);
    --panel: #fbf6ea; --panel-edge: rgba(192, 57, 43, .3);
    --btn: rgba(0, 0, 0, .06); --btn-hover: rgba(0, 0, 0, .12);
    --shadow: rgba(60, 40, 10, .35); --bone: #fcf7ea; --bone-2: #eee3c8;
    --side: #7d9c74; --side-2: #5a7a56; --sel: #c0392b;
    color-scheme: light;
  }
}
:root[data-theme="light"] {
  --table: #e8dcc2; --table-2: #cdbb97; --felt-line: rgba(0, 0, 0, .04);
  --text: #2a2521; --text-dim: rgba(42, 37, 33, .6);
  --panel: #fbf6ea; --panel-edge: rgba(192, 57, 43, .3);
  --btn: rgba(0, 0, 0, .06); --btn-hover: rgba(0, 0, 0, .12);
  --shadow: rgba(60, 40, 10, .35); --bone: #fcf7ea; --bone-2: #eee3c8;
  --side: #7d9c74; --side-2: #5a7a56; --sel: #c0392b;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: 'Atkinson', 'Segoe UI', system-ui, sans-serif;
  color: var(--text);
  background: radial-gradient(ellipse at 50% 30%, var(--table) 0%, var(--table-2) 100%) fixed;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}
body::before {
  content: ''; position: fixed; inset: 0; pointer-events: none; z-index: 0;
  background-image:
    repeating-linear-gradient(90deg, var(--felt-line) 0 1px, transparent 1px 7px),
    repeating-linear-gradient(0deg, var(--felt-line) 0 1px, transparent 1px 7px),
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='2' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='160' height='160' filter='url(%23n)' opacity='.35'/%3E%3C/svg%3E");
  mix-blend-mode: overlay; opacity: .5;
}

#app { position: relative; z-index: 1; height: 100%; height: 100dvh; display: grid; grid-template-rows: auto 1fr auto; }

#bar {
  display: flex; align-items: center; gap: 16px; padding: 10px clamp(12px, 3vw, 28px);
  padding-top: calc(10px + env(safe-area-inset-top));
}
.brand { display: flex; align-items: baseline; gap: 10px; }
.mark {
  font-family: 'MJ CJK', serif; font-weight: 700; font-size: 20px; line-height: 1; color: var(--bone);
  background: var(--vermilion); padding: 5px 6px 4px; border-radius: 4px; align-self: center;
  box-shadow: 0 2px 0 #7e221a;
}
h1 { font-family: 'Fraunces', serif; font-style: italic; font-weight: 500; font-size: 26px; margin: 0; letter-spacing: .01em; }
.stats { display: flex; gap: 18px; margin-left: auto; }
.stat { display: flex; flex-direction: column; align-items: flex-end; line-height: 1.1; }
.stat-label { font-size: 11px; text-transform: uppercase; letter-spacing: .14em; color: var(--text-dim); }
.stat-value { font-family: 'Fraunces', serif; font-weight: 600; font-size: 20px; font-variant-numeric: tabular-nums; }
.actions { display: flex; gap: 6px; }
.act {
  font: inherit; color: var(--text); background: var(--btn); border: 1px solid transparent; border-radius: 10px;
  padding: 7px 11px; display: flex; align-items: center; gap: 7px; cursor: pointer; transition: background .15s, transform .1s;
}
.act:hover { background: var(--btn-hover); }
.act:active { transform: translateY(1px); }
.act:disabled { opacity: .4; cursor: default; }
.act .ico { font-size: 15px; line-height: 1; }
.act:focus-visible, .btn:focus-visible { outline: 2px solid var(--sel); outline-offset: 2px; }

#table { position: relative; min-height: 0; display: grid; place-items: center; padding: 12px; overflow: hidden; }
#board-wrap { position: relative; }
#board { position: absolute; left: 0; top: 0; transform-origin: 0 0; }

#status { margin: 0; padding: 6px 16px calc(8px + env(safe-area-inset-bottom)); text-align: center; font-size: 13px; color: var(--text-dim); min-height: 1.6em; }

/* Tiles */
.tile {
  position: absolute; width: var(--W); height: var(--H); padding: 0; border: 0; background: transparent;
  cursor: pointer; font: inherit; -webkit-appearance: none; appearance: none; outline: none;
  transition: transform .12s ease;
}
.tile::before {
  content: ''; position: absolute; inset: 0; border-radius: 7px;
  background: linear-gradient(135deg, var(--side) 0%, var(--side-2) 100%);
  transform: translate(var(--D), var(--D));
  box-shadow: 2px 3px 6px var(--shadow);
}
.tile .face {
  position: absolute; inset: 0; border-radius: 6px; overflow: hidden;
  background: radial-gradient(ellipse at 30% 20%, #fff 0%, var(--bone) 42%, var(--bone-2) 100%);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .5), inset -1px -2px 0 var(--bone-edge);
  display: block;
}
.tile .face svg { width: 100%; height: 100%; display: block; }
.tile:not(.free) .face { filter: brightness(.9) saturate(.8); }
.tile:not(.free) { cursor: default; }
.tile.free:hover .face { background: radial-gradient(ellipse at 30% 20%, #fff 0%, #fff6e3 45%, var(--bone-2) 100%); }
.tile.sel { transform: translate(-4px, -5px); }
.tile.sel .face { box-shadow: 0 0 0 3px var(--sel), inset 0 0 0 1px rgba(255, 255, 255, .6), 0 0 18px 2px rgba(230, 184, 74, .55); }
.tile.hint .face { animation: hint 1.2s ease-in-out infinite; }
.tile:focus-visible .face { box-shadow: 0 0 0 3px var(--indigo); }
.tile.nope { animation: nope .35s ease; }
.tile.gone { animation: gone .32s ease forwards; pointer-events: none; }
.tile.hidden { display: none; }
.tile.deal { animation: deal .5s cubic-bezier(.2, .9, .3, 1.2) both; }
.tile.flip .face { animation: flip .4s ease both; }

@keyframes hint { 0%, 100% { box-shadow: 0 0 0 2px var(--sel), inset 0 0 0 1px rgba(255,255,255,.5); } 50% { box-shadow: 0 0 0 5px var(--sel), 0 0 20px 3px rgba(230, 184, 74, .6), inset 0 0 0 1px rgba(255,255,255,.5); } }
@keyframes nope { 20% { transform: translateX(-3px); } 50% { transform: translateX(3px); } 80% { transform: translateX(-2px); } }
@keyframes gone { to { transform: translate(-6px, -24px) scale(.6); opacity: 0; } }
@keyframes deal { from { transform: translate(0, -40px) scale(.85); opacity: 0; } }
@keyframes flip { 0% { transform: rotateY(0); } 50% { transform: rotateY(90deg); } 100% { transform: rotateY(0); } }

/* Overlays */
.overlay {
  position: fixed; inset: 0; z-index: 50000; display: grid; place-items: center; padding: 16px;
  background: rgba(8, 14, 12, .62); backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
  animation: fade .2s ease both;
}
.overlay[hidden] { display: none; }
@keyframes fade { from { opacity: 0; } }
.panel {
  width: min(560px, 100%); max-height: 100%; overflow: auto; background: var(--panel); color: var(--text);
  border-radius: 18px; padding: clamp(20px, 4vw, 36px); box-shadow: 0 30px 60px rgba(0, 0, 0, .5), 0 0 0 1px var(--panel-edge);
  animation: rise .35s cubic-bezier(.2, .9, .3, 1.1) both;
}
.panel.small { width: min(440px, 100%); }
@keyframes rise { from { transform: translateY(18px) scale(.98); opacity: 0; } }
.eyebrow { margin: 0 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .18em; color: var(--vermilion); font-weight: 700; }
.title { font-family: 'Fraunces', serif; font-weight: 600; font-size: clamp(30px, 6vw, 44px); line-height: 1.02; margin: 0 0 14px; letter-spacing: -.01em; }
.title em { font-style: italic; font-weight: 500; color: var(--gold); }
:root[data-theme="light"] .title em { color: var(--vermilion); }
@media (prefers-color-scheme: light) { :root:not([data-theme="dark"]) .title em { color: var(--vermilion); } }
.lead { margin: 0 0 18px; font-size: 15px; line-height: 1.5; color: var(--text-dim); }
.fine { margin: 16px 0 0; font-size: 12px; line-height: 1.5; color: var(--text-dim); }

.layouts { display: grid; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 10px; margin-bottom: 18px; }
.layout {
  font: inherit; color: inherit; text-align: left; background: var(--btn); border: 2px solid transparent; border-radius: 12px;
  padding: 10px; cursor: pointer; display: flex; flex-direction: column; gap: 6px; transition: background .15s, border-color .15s;
}
.layout:hover { background: var(--btn-hover); }
.layout[aria-pressed="true"] { border-color: var(--sel); }
.layout svg { width: 100%; height: auto; display: block; }
.layout .name { font-weight: 700; font-size: 14px; }
.layout .meta { font-size: 12px; color: var(--text-dim); }

.menu-row { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 18px; }
.btn {
  font: inherit; font-weight: 700; color: var(--text); background: var(--btn); border: 0; border-radius: 12px; padding: 12px 20px;
  cursor: pointer; transition: background .15s, transform .1s;
}
.btn:hover { background: var(--btn-hover); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--vermilion); color: #fff7ea; box-shadow: 0 3px 0 #7e221a; }
.btn.primary:hover { background: #d2443a; }
.btn.primary:active { transform: translateY(2px); box-shadow: 0 1px 0 #7e221a; }

.settings { display: flex; flex-wrap: wrap; align-items: center; gap: 16px; }
.seg { border: 0; padding: 0; margin: 0; display: flex; align-items: center; gap: 6px; }
.seg legend { float: left; font-size: 12px; text-transform: uppercase; letter-spacing: .14em; color: var(--text-dim); margin-right: 6px; }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; inset: 0; }
.seg span { display: inline-block; padding: 6px 11px; border-radius: 8px; background: var(--btn); font-size: 13px; cursor: pointer; }
.seg input:checked + span { background: var(--sel); color: var(--ink); font-weight: 700; }
.seg input:focus-visible + span { outline: 2px solid var(--sel); outline-offset: 2px; }
.switch { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer; }
.switch input { width: 18px; height: 18px; accent-color: var(--vermilion); }

@media (max-width: 640px) {
  #bar { gap: 10px; flex-wrap: wrap; padding-bottom: 6px; }
  h1 { font-size: 22px; }
  .stats { gap: 12px; }
  .stat-value { font-size: 17px; }
  .actions { width: 100%; justify-content: space-between; order: 3; }
  .act { flex: 1; justify-content: center; padding: 8px 6px; font-size: 13px; }
  #table { padding: 6px; }
}
@media (max-width: 380px) { .act span:not(.ico) { display: none; } .act .ico { font-size: 18px; } }
@media (prefers-reduced-motion: reduce) {
  .tile, .tile.deal, .tile.gone, .tile.hint .face, .tile.flip .face, .overlay, .panel { animation: none !important; transition: none !important; }
  .tile.gone { opacity: 0; }
}
