:root {
  --bg: #0f1419;
  --surface: #1a222d;
  --surface2: #243040;
  --text: #e8eef4;
  --muted: #8fa3b8;
  --accent: #e85d4c;
  --accent2: #4ecdc4;
  --warn: #f4a261;
  --radius: 12px;
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.site-header {
  padding: 0.75rem 1rem;
  padding-left: max(1rem, env(safe-area-inset-left, 0));
  padding-right: max(1rem, env(safe-area-inset-right, 0));
  padding-top: max(0.75rem, env(safe-area-inset-top, 0));
  background: var(--surface);
  border-bottom: 1px solid var(--surface2);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem 1rem;
}

.header-brand {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.35rem 0.75rem;
}

.header-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.35rem;
  flex: 1 1 auto;
  min-width: 0;
  margin-left: auto;
  justify-content: flex-end;
}

.site-header h1 {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 700;
}

.site-header h1 span {
  color: var(--accent2);
}

main {
  flex: 1 1 auto;
  width: 100%;
  max-width: 960px;
  margin: 0 auto;
  padding: 1rem 1.25rem 1.5rem;
  padding-left: max(1rem, env(safe-area-inset-left, 0));
  padding-right: max(1rem, env(safe-area-inset-right, 0));
}

.site-footer {
  flex-shrink: 0;
  margin-top: auto;
  padding: 0.75rem 1rem;
  padding-bottom: max(0.75rem, env(safe-area-inset-bottom, 0));
  padding-left: max(1rem, env(safe-area-inset-left, 0));
  padding-right: max(1rem, env(safe-area-inset-right, 0));
  background: var(--surface);
  border-top: 1px solid var(--surface2);
}

.footer-actions {
  max-width: 960px;
  margin: 0 auto;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: center;
  align-items: stretch;
}

.footer-actions button {
  flex: 1 1 auto;
  min-width: 8rem;
  max-width: 20rem;
}

.panel {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1.25rem;
  margin-bottom: 1rem;
  border: 1px solid var(--surface2);
}

.panel h2 {
  margin: 0 0 0.75rem;
  font-size: 1.05rem;
  color: var(--accent2);
}

.row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: flex-end;
}

label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.85rem;
  color: var(--muted);
}

input[type="text"],
input[type="number"],
select,
textarea {
  font: inherit;
  padding: 0.45rem 0.65rem;
  border-radius: 8px;
  border: 1px solid var(--surface2);
  background: var(--bg);
  color: var(--text);
  min-width: 4rem;
}

input[type="number"] {
  width: 5rem;
}

textarea {
  width: 100%;
  min-height: 72px;
  resize: vertical;
}

button {
  font: inherit;
  cursor: pointer;
  padding: 0.5rem 1rem;
  min-height: 2.75rem;
  border-radius: 8px;
  border: none;
  background: var(--accent2);
  color: var(--bg);
  font-weight: 600;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

button.secondary {
  background: var(--surface2);
  color: var(--text);
}

button.danger {
  background: #8b4049;
  color: #fff;
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.mode-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1rem;
}

.mode-card {
  padding: 1rem;
  min-height: 2.75rem;
  border-radius: var(--radius);
  background: var(--surface2);
  border: 2px solid transparent;
  cursor: pointer;
  text-align: left;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  font: inherit;
  color: inherit;
}

.mode-card:hover {
  border-color: var(--accent2);
}

.mode-card h3 {
  margin: 0 0 0.5rem;
  color: var(--text);
}

.mode-card p {
  margin: 0;
  font-size: 0.9rem;
  color: var(--muted);
}

.table-wrap {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

th,
td {
  padding: 0.5rem 0.6rem;
  text-align: left;
  border-bottom: 1px solid var(--surface2);
}

th {
  color: var(--muted);
  font-weight: 600;
}

tr.winner td {
  color: var(--accent2);
  font-weight: 700;
}

/* Hearts: lowest score is best */
tr.hearts-best td {
  color: #7dd3c0;
  font-weight: 700;
}

.badge {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-size: 0.75rem;
  background: var(--surface2);
  color: var(--muted);
}

.rules-game-tabs {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(4.75rem, 1fr));
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.rules-tab {
  font: inherit;
  cursor: pointer;
  padding: 0.6rem 0.5rem;
  min-height: 2.75rem;
  border-radius: 8px;
  border: 2px solid var(--surface2);
  background: var(--bg);
  color: var(--text);
  font-weight: 600;
  font-size: 0.95rem;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.rules-tab-active,
.rules-tab[aria-selected="true"] {
  border-color: var(--accent2);
  background: var(--surface2);
  color: var(--accent2);
}

.rules-drawer .hint a {
  color: var(--accent2);
}

.rules-drawer {
  position: fixed;
  top: 0;
  right: 0;
  width: min(28rem, 100%);
  max-width: 100vw;
  height: 100%;
  height: 100dvh;
  background: var(--surface);
  border-left: 1px solid var(--surface2);
  padding: 1rem;
  padding-top: max(1rem, env(safe-area-inset-top, 0));
  padding-bottom: max(1rem, env(safe-area-inset-bottom, 0));
  padding-left: max(1rem, env(safe-area-inset-left, 0));
  padding-right: max(1rem, env(safe-area-inset-right, 0));
  overflow-y: auto;
  overscroll-behavior: contain;
  transform: translateX(100%);
  transition: transform 0.2s ease;
  z-index: 100;
  box-shadow: -8px 0 24px rgba(0, 0, 0, 0.35);
}

.rules-drawer.open {
  transform: translateX(0);
}

.rules-drawer h2 {
  margin-top: 0;
}

.rules-result {
  margin-top: 1rem;
}

.rules-item {
  padding: 0.75rem;
  margin-bottom: 0.5rem;
  background: var(--surface2);
  border-radius: 8px;
}

.rules-item strong {
  display: block;
  margin-bottom: 0.35rem;
  color: var(--warn);
}

.hidden {
  display: none !important;
}

.subgrid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.hint {
  font-size: 0.8rem;
  color: var(--muted);
  margin-top: 0.5rem;
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 99;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}

.overlay.show {
  opacity: 1;
  pointer-events: auto;
}

.adv-player-block {
  margin-bottom: 1.25rem;
  padding: 1rem;
  background: var(--bg);
  border-radius: 8px;
  border: 1px solid var(--surface2);
}

.adv-player-block h4 {
  margin: 0 0 0.75rem;
  color: var(--text);
}

.rules-drawer-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.rules-drawer-actions button {
  flex: 1 1 auto;
  min-width: 6rem;
}

#hearts-table select {
  width: 100%;
  max-width: 100%;
  margin-top: 0.25rem;
  font-size: 1rem;
}

#cribbage-table th {
  vertical-align: top;
}

.cribbage-board {
  margin-top: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.cribbage-track {
  background: linear-gradient(165deg, #3d2914 0%, #2a1c0e 45%, #1f1509 100%);
  border: 2px solid #5c4224;
  border-radius: var(--radius);
  padding: 0.65rem 0.5rem 0.75rem;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.06);
}

.cribbage-track-header {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.35rem 0.75rem;
  margin-bottom: 0.5rem;
  padding: 0 0.25rem;
}

.cribbage-track-name {
  font-weight: 600;
  color: #f0e6d8;
}

.cribbage-track-score {
  font-size: 0.9rem;
  color: #c9b89a;
  font-variant-numeric: tabular-nums;
}

.cribbage-track-path {
  padding: 0.35rem;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.15);
  overflow-x: auto;
}

.cribbage-board-grid {
  display: grid;
  grid-template-columns: 1.6rem repeat(var(--cribbage-cols), minmax(8px, 1fr)) 1.1rem;
  grid-template-rows: auto auto;
  gap: 0.4rem 2px;
  align-items: center;
  min-width: min-content;
}

.cribbage-board-grid .cribbage-start {
  grid-row: 1;
  grid-column: 1;
}

.cribbage-board-grid .cribbage-grid-spacer {
  grid-row: 2;
  grid-column: 1;
}

.cribbage-board-grid .cribbage-hole {
  justify-self: center;
}

.cribbage-board-grid .cribbage-hole--group-end {
  margin-right: 3px;
}

.cribbage-finish-cell {
  grid-row: 2;
  grid-column: -1;
  justify-self: center;
}

.cribbage-hole-group {
  display: flex;
  gap: 3px;
  padding: 2px 4px;
  margin: 0 1px;
  border-radius: 4px;
}

.cribbage-hole-group:nth-child(odd) {
  background: rgba(0, 0, 0, 0.12);
}

.cribbage-hole {
  position: relative;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  flex-shrink: 0;
  background: #1a1208;
  border: 1px solid #0a0704;
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.6);
}

.cribbage-hole--mark::after {
  content: "";
  position: absolute;
  inset: 2px;
  border-radius: 50%;
  border: 1px solid rgba(201, 184, 154, 0.35);
}

.cribbage-hole--finish {
  width: 14px;
  height: 14px;
  border-color: #8b6914;
  box-shadow: inset 0 0 0 1px rgba(244, 196, 80, 0.25);
}

.cribbage-peg {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 7px;
  height: 14px;
  margin: -7px 0 0 -3.5px;
  border-radius: 3px 3px 2px 2px;
  pointer-events: none;
  z-index: 2;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}

.cribbage-peg--behind {
  background: linear-gradient(180deg, #9aa8b5 0%, #6b7a88 100%);
  opacity: 0.85;
  transform: translateX(-2px);
}

.cribbage-peg--front {
  background: linear-gradient(180deg, var(--accent2) 0%, #2a9d8f 100%);
  transform: translateX(2px);
}

.cribbage-peg--front.cribbage-peg--win {
  background: linear-gradient(180deg, #ffd166 0%, #e9a319 100%);
}

.cribbage-start {
  flex-shrink: 0;
  align-self: center;
  width: 1.75rem;
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #a89070;
  padding: 0 0.15rem;
  text-align: center;
  line-height: 1.1;
}

.cribbage-deal-log {
  margin-top: 1rem;
  padding: 0.75rem 0;
  border-top: 1px solid var(--surface2);
}

.cribbage-deal-log summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--text);
}

.ohhell-hand-nav {
  margin-bottom: 0.25rem;
}

#ohhell-entry-table input[type="number"] {
  width: 4rem;
  max-width: 100%;
}

#ohhell-entry-table .ohhell-pts {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: var(--accent2);
}

#ohhell-entry-table tr.ohhell-exact td {
  background: rgba(78, 205, 196, 0.08);
}

.ohhell-score-sheet summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--text);
}

#ohhell-sheet-table {
  font-size: 0.85rem;
}

#ohhell-sheet-table th,
#ohhell-sheet-table td {
  text-align: center;
  padding: 0.35rem 0.25rem;
}

#ohhell-sheet-table th:first-child,
#ohhell-sheet-table td:first-child {
  text-align: left;
}

.ginrummy-calc summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--text);
}

#ginrummy-calc-preview {
  margin-top: 0.5rem;
  margin-bottom: 0;
}

@media (max-width: 640px) {
  .cribbage-hole {
    width: 9px;
    height: 9px;
  }

  .cribbage-hole-group {
    gap: 2px;
    padding: 2px 3px;
  }

  .cribbage-peg {
    width: 6px;
    height: 12px;
    margin: -6px 0 0 -3px;
  }
}

.join-game-row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

.join-game-row label {
  flex: 1 1 12rem;
  margin: 0;
}

.join-game-status {
  margin-top: 0;
  margin-bottom: 0.75rem;
}

.game-id-badge code {
  font-family: ui-monospace, "Cascadia Code", "Segoe UI Mono", monospace;
  font-weight: 700;
  letter-spacing: 0.05em;
}

.badge-btn {
  font-size: 0.85rem;
  padding: 0.35rem 0.65rem;
  border-radius: 999px;
  white-space: nowrap;
  background: var(--accent2);
  color: var(--bg);
  border: none;
  font-weight: 600;
}

.badge-btn:hover {
  filter: brightness(1.08);
}

#sync-status {
  font-size: 0.8rem;
  color: var(--muted);
}

@media (max-width: 640px) {
  .site-header {
    flex-direction: column;
    align-items: stretch;
  }

  .header-meta {
    width: 100%;
  }

  .header-brand {
    width: 100%;
  }

  .footer-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    max-width: none;
  }

  #btn-save-scores {
    grid-column: 1 / -1;
  }

  .footer-actions button {
    max-width: none;
    width: 100%;
  }

  main {
    padding: 0.75rem 1rem 1.25rem;
  }

  .panel {
    padding: 1rem;
  }

  .mode-cards {
    grid-template-columns: 1fr;
  }

  input[type="text"],
  input[type="number"],
  select,
  textarea {
    font-size: 1rem;
    max-width: 100%;
  }

  input[type="number"] {
    width: 100%;
    max-width: 8rem;
  }

  .row {
    flex-direction: column;
    align-items: stretch;
  }

  .row label {
    width: 100%;
  }

  .subgrid {
    grid-template-columns: 1fr 1fr;
  }

  .table-wrap {
    margin-left: -0.25rem;
    margin-right: -0.25rem;
  }

  table {
    font-size: 0.85rem;
  }

  th,
  td {
    padding: 0.45rem 0.35rem;
  }

  .rules-game-tabs {
    grid-template-columns: 1fr;
  }

  .rules-drawer {
    width: 100%;
    border-left: none;
  }

  .adv-player-block .subgrid {
    grid-template-columns: 1fr;
  }
}

@media (prefers-reduced-motion: reduce) {
  .rules-drawer,
  .overlay {
    transition: none;
  }
}
