/* Crib Score web — ported from the iOS app's ThemeManager / GameView. */

:root,
[data-theme="depth"] {
  --bg: #1F201F; --text: #D8DBE2;
  --q: #2A2B2A; --q-active: #414241;
  --sec: #A9BCD0; --sec-active: #8A96A6;
  --p1: #58A4B0; --p1-active: #3D7C85;
  --p2: #FF8C42; --p2-active: #E47831;
  --bg-icon-opacity: .3;
  color-scheme: dark;
}
[data-theme="clarity"] {
  --bg: #FAFCFF; --text: #2C3E50;
  --q: #E6EEF8; --q-active: #D1E0F0;
  --sec: #8096B0; --sec-active: #607D9B;
  --p1: #3878A6; --p1-active: #2A5D84;
  --p2: #E67E22; --p2-active: #D35400;
  --bg-icon-opacity: .5;
  color-scheme: light;
}
[data-theme="bloom"] {
  --bg: #FDFAFC; --text: #2D2640;
  --q: #F0E6F6; --q-active: #E1D0EC;
  --sec: #A98FB0; --sec-active: #8C6D98;
  --p1: #E84393; --p1-active: #D03177;
  --p2: #8E44AD; --p2-active: #6C3483;
  --bg-icon-opacity: .5;
  color-scheme: light;
}
[data-theme="edge"] {
  --bg: #171717; --text: #EEEEEE;
  --q: #282828; --q-active: #3A3A3A;
  --sec: #ABABAB; --sec-active: #868686;
  --p1: #FFFFFF; --p1-active: #CCCCCC;
  --p2: #FF3B30; --p2-active: #D01B12;
  --bg-icon-opacity: .3;
  color-scheme: dark;
}

:root {
  --max-w: 560px;
  --gap: 8px;
  --key-h: clamp(31px, 4.9dvh, 48px);
}
@media (max-height: 700px) { :root { --gap: 6px; } }

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
  transition: background-color .25s ease;
}
button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  cursor: pointer;
  touch-action: manipulation;
}
button:focus { outline: none; }
button:focus-visible { outline: 2px solid var(--sec); outline-offset: 2px; }
svg { display: block; fill: currentColor; }

/* ---------- Background suit ---------- */
.bg-suit {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  overflow: hidden;
}
.bg-suit svg {
  position: absolute;
  left: 50%;
  top: 44.4%; /* iOS: height / 2.25 */
  width: min(140vw, calc(var(--max-w) * 1.4));
  aspect-ratio: 1;
  transform: translate(-50%, -50%);
  color: var(--q);
  opacity: var(--bg-icon-opacity);
}

/* ---------- Game layout ---------- */
#game {
  position: relative;
  z-index: 1;
  height: 100dvh;
  max-width: var(--max-w);
  margin: 0 auto;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto minmax(0, 1fr);
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.player {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  padding: 0 20px;
}
.player.p1 { transform: rotate(180deg); }

.name-row {
  display: flex;
  padding-top: clamp(6px, 1.6dvh, 15px);
}
.p2 .name-row { justify-content: flex-end; }

.tag {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px;
  border: 1px solid var(--q);
  border-radius: 4px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 2px;
  color: var(--text);
  transition: border-color .2s ease;
}
.tag svg { width: 12px; height: 12px; color: var(--q); transition: color .2s ease; }
.tag.dealer { border-color: var(--pc); }
.tag.dealer svg { color: var(--pc); }

.p1 { --pc: var(--p1); --pc-active: var(--p1-active); --oc: var(--p2-active); }
.p2 { --pc: var(--p2); --pc-active: var(--p2-active); --oc: var(--p1-active); }

.flex { flex: 1 1 0; min-height: 4px; }

.scores { display: flex; flex-direction: column; gap: 2px; }
.opp {
  text-align: right;
  font-size: 16px;
  font-weight: 700;
  color: var(--oc);
}
.score-row {
  display: flex;
  justify-content: flex-end;
  align-items: baseline;
  gap: 8px;
}
.pending {
  font-size: 18px;
  min-width: 1px;
  transition: opacity .2s ease;
}
.pending:empty { opacity: 0; }
.score {
  font-size: clamp(46px, 8.4dvh, 90px);
  font-weight: 700;
  line-height: 1;
  color: var(--pc);
  font-variant-numeric: tabular-nums;
}

.history {
  display: flex;
  gap: 5px;
  height: 22px;
  margin: 0 0 4px;
  padding-left: 5px;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  align-items: center;
  font-size: 14px;
  color: var(--pc);
  white-space: nowrap;
}
.history::-webkit-scrollbar { display: none; }
.history span { opacity: .8; flex: none; }
.history span.undone {
  opacity: .5;
  text-decoration: line-through;
  text-decoration-color: var(--text);
}
.history::after { content: ""; flex: 0 0 20px; }

/* Number pad */
.pad {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-auto-rows: var(--key-h);
  gap: var(--gap);
  padding-bottom: clamp(8px, 2.4dvh, 20px);
}
.key {
  border-radius: 4px;
  background: var(--q);
  color: var(--text);
  font-size: 20px;
  display: grid;
  place-items: center;
  transition: background-color .12s ease, opacity .15s ease;
}
.key svg { width: 18px; height: 18px; }
.key.active { background: var(--q-active); }
.key:disabled { opacity: .5; cursor: default; }
.key.del { grid-column: 4; grid-row: span 2; }
.key.confirm {
  grid-column: span 2;
  background: var(--pc);
  color: var(--bg);
}
.key.confirm.active { background: var(--pc-active); }
.key.long-hit { background: var(--q-active); transform: scale(.97); }

/* Progress bars */
.bars { padding: 0 34px; }
.bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding-left: 52px; /* 44px menu-button space + 8px spacing, as in iOS */
  margin: clamp(5px, 1.1dvh, 8px) 0;
}
.bar[data-player="1"] { --pc: var(--p1); }
.bar[data-player="2"] { --pc: var(--p2); }
.track {
  position: relative;
  flex: 1;
  height: 6px;
  border-radius: 3px;
  background: var(--q);
}
.fill {
  position: absolute;
  inset: 0 auto 0 0;
  width: 0;
  border-radius: 3px;
  background: var(--pc);
  transition: width .5s ease-in-out;
}
.track i {
  position: absolute;
  top: -1px;
  width: 2px;
  height: 8px;
  background: var(--bg);
  transform: translateX(-1px);
}
.track i:nth-of-type(1) { left: 25%; }
.track i:nth-of-type(2) { left: 50%; }
.track i:nth-of-type(3) { left: 75%; }
.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--q);
  flex: none;
  transition: background-color .3s ease;
}
.dot.won { background: var(--pc); }

/* Menu button */
#menu-btn {
  position: fixed;
  z-index: 5;
  top: 50%;
  left: calc(max(0px, (100vw - var(--max-w)) / 2) + 20px + env(safe-area-inset-left));
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 12px;
  border-radius: 4px;
  background: var(--bg);
}
#menu-btn span { width: 28px; height: 2px; background: var(--q-active); }

/* Slide-out menu */
#menu-layer { position: fixed; inset: 0; z-index: 20; }
#menu-layer[hidden] { display: none; }
.scrim {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, .4);
  opacity: 0;
  transition: opacity .25s ease-out;
}
#menu {
  position: absolute;
  top: 0; bottom: 0; left: 0;
  width: calc(250px + env(safe-area-inset-left));
  padding: calc(20px + env(safe-area-inset-top)) 20px 20px calc(20px + env(safe-area-inset-left));
  background: var(--bg);
  transform: translateX(-100%);
  transition: transform .25s ease-out;
  overflow-y: auto;
}
#menu-layer.open .scrim { opacity: 1; }
#menu-layer.open #menu { transform: none; }
.menu-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 40px; }
.menu-head h2 { font-size: 28px; font-weight: 700; }
.menu-close { padding: 10px; border-radius: 4px; background: var(--q); }
.menu-close svg { width: 20px; height: 20px; }
.menu-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 30px;
}
.menu-item svg { width: 22px; height: 22px; margin: 4px; }
#menu h3 {
  font-size: 16px;
  font-weight: 700;
  color: var(--sec-active);
  margin: 10px 0 15px;
}
.theme-opt {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 8px 0;
  margin-bottom: 15px;
  font-size: 16px;
  text-align: left;
}
.theme-opt .check { width: 20px; display: grid; place-items: center; }
.theme-opt svg { width: 16px; height: 16px; }

/* Game over */
#gameover {
  position: fixed;
  inset: 0;
  z-index: 10;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) max(calc((100vw - var(--max-w)) / 2), env(safe-area-inset-right)) env(safe-area-inset-bottom) max(calc((100vw - var(--max-w)) / 2), env(safe-area-inset-left));
  animation: slide-in .3s ease-out;
}
#gameover[hidden] { display: none; }
@keyframes slide-in { from { transform: translateX(30%); opacity: 0; } }
#gameover > *:not(.bg-suit) { position: relative; z-index: 1; }
#gameover h1 {
  font-size: min(28px, 7vw);
  font-weight: 700;
  padding: max(30px, 4dvh) 20px 0;
  margin-bottom: max(6dvh, 25px);
}
.go-rows { flex: 1; }
.go-row { padding: 5px 20px; }
.go-row[data-player="1"] { --pc: var(--p1); }
.go-row[data-player="2"] { --pc: var(--p2); }
.go-name { display: flex; justify-content: space-between; align-items: center; }
.go-name span { font-size: min(20px, 5vw); font-weight: 700; }
.go-name b { font-size: min(14px, 3.5vw); letter-spacing: 2px; color: var(--pc); visibility: hidden; }
.go-row.winner .go-name b { visibility: visible; }
.go-score {
  text-align: right;
  font-size: min(56px, 14vw);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.go-row.winner .go-score { color: var(--pc); }
#gameover hr {
  border: 0;
  height: 1px;
  background: var(--sec);
  margin: max(15px, 2.5dvh) 20px;
}
.go-actions { display: flex; flex-direction: column; gap: 10px; padding: 0 20px max(30px, 4.5dvh); }
.go-actions button {
  height: min(60px, 8dvh);
  min-height: 44px;
  border-radius: 4px;
  font-size: min(18px, 4.5vw);
  font-weight: 500;
  letter-spacing: 1px;
  color: var(--bg);
  transition: opacity .15s ease;
}
#go-undo { background: var(--sec); }
#go-new { background: var(--p1); }
.go-actions button:disabled { opacity: .5; cursor: default; }
.go-actions button:active:not(:disabled) { filter: brightness(.9); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0s !important; animation-duration: 0s !important; }
}

.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
