/* Layout: one column, board on a green felt table, wooden rack under it, big buttons. */
:root {
  --table: #163f2e;
  --table-deep: #0d2a1e;
  --ink: #f3efe4;
  --muted: #a9c4b5;
  --sq: #ddd3b7;
  --grid: #0f3022;
  --tw: #c5432f;
  --dw: #e9a59d;
  --tl: #2e6ea3;
  --dl: #a3cde5;
  --tile: #f3dfb2;
  --tile-top: #fcefcf;
  --tile-edge: #b98f52;
  --tile-ink: #2b2118;
  --gold: #f2b53a;
  --wood: #6b4527;
  --wood-deep: #452a17;
  --card: #f8f0dd;
  --card-ink: #2b2118;
  --board: min(calc(100vw - 12px), 640px);
  --cell: calc(var(--board) / 15);
  --rt: min(12.4vw, 58px);
  color-scheme: dark;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: radial-gradient(120% 80% at 50% 30%, var(--table), var(--table-deep));
  background-color: var(--table-deep);
  color: var(--ink);
  font: 16px/1.35 -apple-system, "SF Pro Text", "Avenir Next", system-ui, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
#app {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding-top: max(10px, env(safe-area-inset-top));
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  padding-left: max(6px, env(safe-area-inset-left));
  padding-right: max(6px, env(safe-area-inset-right));
}
/* Landscape on a tablet: board on the left, everything else in a column on the right. */
#app.wide {
  display: grid;
  grid-template-columns: var(--board) var(--side);
  grid-template-areas: "board top" "board note" "board actions" "board log" "rack log";
  grid-template-rows: auto auto auto minmax(0, 1fr) auto;
  justify-content: center;
  align-items: start;
  column-gap: 20px;
  row-gap: 12px;
}
#app.wide #board { grid-area: board; }
#app.wide #top { grid-area: top; width: 100%; }
#app.wide .note { grid-area: note; width: 100%; }
#app.wide #rackRow { grid-area: rack; width: 100%; }
#app.wide #actions { grid-area: actions; width: 100%; }
#app.wide #logWrap { grid-area: log; width: 100%; height: 100%; min-height: 0; overflow: hidden; }

/* Scores */
#top {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  width: min(100%, 640px);
}
.who {
  display: flex;
  flex-direction: column;
  padding: 6px 12px;
  border-radius: 12px;
  background: rgba(0, 0, 0, .22);
  border: 2px solid transparent;
  transition: border-color .2s;
  min-width: 0;
}
#sAi { text-align: right; }
.who span { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.who strong { font-size: 28px; line-height: 1.1; font-variant-numeric: tabular-nums; }
.who.turn { border-color: var(--gold); }
.who.bump strong { animation: bump .5s ease-out; }
@keyframes bump { 0% { transform: scale(1); } 30% { transform: scale(1.35); color: var(--gold); } 100% { transform: scale(1); } }
.mid { display: flex; flex-direction: column; align-items: center; gap: 4px; }
#bag { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }

/* Board */
#board {
  width: var(--board);
  height: var(--board);
  display: grid;
  grid-template-columns: repeat(15, minmax(0, 1fr));
  grid-template-rows: repeat(15, minmax(0, 1fr));
  gap: 1px;
  padding: 3px;
  background: var(--grid);
  border-radius: 8px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .4);
  touch-action: none;
}
.cell {
  min-width: 0;
  min-height: 0;
  background: var(--sq);
  display: grid;
  place-items: center;
  position: relative;
  border-radius: 2px;
  overflow: visible;
}
.p1 { background: var(--dl); color: #1c4864; }
.p2 { background: var(--tl); color: #fff; }
.p3 { background: var(--dw); color: #7a2a22; }
.p4 { background: var(--tw); color: #fff; }
.lab { font-size: calc(var(--cell) * .34); font-weight: 700; letter-spacing: .02em; opacity: .9; }
.star { font-size: calc(var(--cell) * .62); color: #7a2a22; line-height: 1; }
.cell.over { outline: 3px solid var(--gold); outline-offset: -1px; z-index: 3; background: #ffe39a; }
.cell.over > span { opacity: .3; }

/* Tiles */
.tile {
  position: relative;
  display: grid;
  place-items: center;
  background: linear-gradient(var(--tile-top), var(--tile));
  color: var(--tile-ink);
  border-radius: 7px;
  box-shadow: 0 2px 0 var(--tile-edge), 0 3px 5px rgba(0, 0, 0, .35);
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-weight: 800;
  touch-action: none;
  cursor: grab;
}
.tile b { line-height: 1; font-weight: 800; }
.tile i { position: absolute; right: 7%; bottom: 3%; font-style: normal; font-size: .36em; font-weight: 700; }
.tile.blank b { color: #a06a24; }
.cell .tile {
  position: absolute;
  inset: 0;
  border-radius: calc(var(--cell) * .16);
  font-size: calc(var(--cell) * .62);
  box-shadow: 0 1px 0 var(--tile-edge), 0 1px 2px rgba(0, 0, 0, .3);
  z-index: 1;
}
.cell .tile.pending { box-shadow: 0 0 0 2px var(--gold), 0 1px 0 var(--tile-edge); z-index: 2; }
.cell.last .tile { background: linear-gradient(#fff6cf, #f5dc8a); }
.cell.bad .tile,
.cell.bad .tile.pending {
  background: linear-gradient(#ffd6cd, #f2988a);
  color: #6e140a;
  box-shadow: 0 0 0 2px #c5432f, 0 1px 2px rgba(0, 0, 0, .3);
}
.cell .tile.hint {
  background: linear-gradient(#fff6cf, #f7d977);
  box-shadow: 0 0 0 2px #e07b00;
  animation: glow 1.1s ease-in-out infinite alternate;
}
@keyframes glow { from { filter: brightness(1); } to { filter: brightness(1.12); transform: scale(1.06); } }
.pop { animation: pop .2s ease-out; }
@keyframes pop { 0% { transform: scale(1.4); } 100% { transform: scale(1); } }
.drop { animation: drop .5s cubic-bezier(.2, .8, .3, 1.25) both; }
@keyframes drop {
  0% { transform: translateY(calc(var(--cell) * -3)) scale(1.6) rotate(-8deg); opacity: 0; }
  70% { transform: translateY(2px) scale(.97); opacity: 1; }
  100% { transform: none; }
}

/* Rack */
#rackRow {
  display: flex;
  gap: 8px;
  align-items: stretch;
  width: min(100%, 600px);
}
#rackRow #playBtn {
  flex: 0 0 96px;
  min-height: 0;
  font-size: 19px;
  line-height: 1.15;
  padding: 6px 4px;
}
#rack {
  display: flex;
  gap: 6px;
  justify-content: center;
  align-items: center;
  flex: 1 1 auto;
  min-width: 0;
  min-height: calc(var(--rt) + 20px);
  padding: 8px 10px 12px;
  border-radius: 12px;
  background: linear-gradient(var(--wood), var(--wood-deep));
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, .12), 0 6px 14px rgba(0, 0, 0, .4);
  touch-action: none;
}
#rack .tile, #rack .slot { width: var(--rt); height: var(--rt); flex: 0 0 auto; font-size: calc(var(--rt) * .6); }
#rack .slot { border-radius: 7px; background: rgba(0, 0, 0, .18); }
#rack .tile { transition: transform .15s; }
#rack .tile.sel { transform: translateY(-9px); box-shadow: 0 0 0 3px var(--gold), 0 6px 10px rgba(0, 0, 0, .35); }
.tile.lifted { opacity: .25; }
.ghost {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 60;
  width: 66px;
  height: 66px;
  font-size: 40px;
  pointer-events: none;
  opacity: .85;
}

/* Messages and buttons */
.note {
  width: min(100%, 640px);
  height: 124px;
  overflow-y: auto;
  padding: 10px 14px;
  border-radius: 12px;
  background: rgba(0, 0, 0, .25);
  font-size: 17px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  align-content: flex-start;
  gap: 6px 12px;
}
.note .line { flex: 1 1 220px; min-width: 0; }
.note .ai { color: var(--muted); font-size: 15px; flex-basis: 100%; }
.note em { font-style: normal; font-weight: 700; color: var(--gold); letter-spacing: .03em; }
.note .bad { color: #ffb4a6; font-weight: 700; }
#actions {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
  width: min(100%, 600px);
}
.btn {
  font: inherit;
  font-weight: 700;
  font-size: 15px;
  border: 0;
  border-radius: 12px;
  padding: 10px 8px;
  min-height: 50px;
  background: #2a5c45;
  color: var(--ink);
  flex: 1 1 0;
  cursor: pointer;
  box-shadow: 0 2px 0 rgba(0, 0, 0, .35);
}
.btn:active { transform: translateY(1px); box-shadow: none; }
.btn:disabled { opacity: .45; }
.btn:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }
.btn.play { background: var(--gold); color: #2b1d05; flex: 1.7 1 0; font-size: 17px; }
.btn.small { min-height: 36px; padding: 6px 14px; font-size: 14px; flex: none; }
.btn.light { background: #e8dcc0; color: var(--card-ink); }
.note .btn { flex: none; min-height: 36px; padding: 4px 16px; }
#app.wide .note { height: auto; min-height: 84px; overflow: visible; }

#logWrap { width: min(100%, 520px); }
#logWrap h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 6px 0; font-weight: 600; }
#log { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
#log li { display: grid; grid-template-columns: 110px 1fr auto; gap: 8px; padding: 6px 10px; border-radius: 8px; background: rgba(0, 0, 0, .18); font-size: 15px; }
#log li b { letter-spacing: .04em; }
#log li span:last-child { font-variant-numeric: tabular-nums; font-weight: 700; }

/* Sheets */
.sheet {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(5, 20, 13, .62);
  -webkit-backdrop-filter: blur(3px);
  backdrop-filter: blur(3px);
}
.card {
  width: min(100%, 440px);
  max-height: calc(100dvh - 40px);
  overflow: auto;
  background: var(--card);
  color: var(--card-ink);
  border-radius: 20px;
  padding: 22px 20px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, .5);
  display: grid;
  gap: 14px;
  animation: rise .25s ease-out;
}
@keyframes rise { from { transform: translateY(20px) scale(.97); opacity: 0; } to { transform: none; opacity: 1; } }
.card h1 { margin: 0; font-size: 26px; text-wrap: balance; }
.card p { margin: 0; }
.card .muted { color: #6d5d47; font-size: 15px; }
.levels { display: grid; gap: 10px; }
.level {
  font: inherit;
  text-align: left;
  border: 2px solid #d8c8a6;
  background: #fffaf0;
  color: var(--card-ink);
  border-radius: 14px;
  padding: 12px 14px;
  cursor: pointer;
  display: grid;
  gap: 2px;
}
.level strong { font-size: 19px; }
.level span { font-size: 14px; color: #6d5d47; }
.level:active { border-color: var(--gold); }
.hist { list-style: none; padding: 0; margin: 0; display: grid; gap: 4px; font-size: 14px; }
.hist li { display: flex; justify-content: space-between; gap: 8px; padding: 6px 10px; background: #efe4cb; border-radius: 8px; font-variant-numeric: tabular-nums; }
.win { color: #23733f; font-weight: 700; }
.lose { color: #9c3a26; font-weight: 700; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row .btn { flex: 1 1 0; }
.picker { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; }
.picker .tile, .swap .tile { aspect-ratio: 1; font-size: 26px; border: 0; cursor: pointer; }
.swap { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; }
.swap .tile { width: 48px; }
.swap .tile.on { transform: translateY(-6px); box-shadow: 0 0 0 3px #e07b00, 0 6px 10px rgba(0, 0, 0, .3); }

.bravo { text-align: center; justify-items: center; background: linear-gradient(#fff8e6, #f7e7bd); }
.bravo .hy { font-size: 34px; font-weight: 800; line-height: 1.25; color: #8a2e1c; font-family: "Noto Sans Armenian", "Arial Unicode MS", sans-serif; text-wrap: balance; }
.bravo .fr { font-size: 17px; color: #6d5d47; }
.bravo .word { display: flex; gap: 5px; justify-content: center; flex-wrap: wrap; }
.bravo .word .tile { width: 42px; height: 42px; font-size: 25px; animation: hop .7s ease-in-out infinite alternate; }
@keyframes hop { from { transform: translateY(0); } to { transform: translateY(-10px); } }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 70;
  background: #2b2118;
  color: #fff;
  padding: 12px 18px;
  border-radius: 12px;
  font-size: 16px;
  max-width: calc(100vw - 32px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, .4);
}

@media (max-width: 499px) {
  #rackRow #playBtn { flex-basis: 72px; font-size: 16px; }
  #rack { gap: 4px; padding: 8px 6px 12px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
