:root {
  --ink: #121212;
  --muted: #6b6b6b;
  --paper: #ffffff;
  --paper-edge: #e5e5e3;
  --bg: #f5f5f3;
  --black: #0b0b0c;
  --green: #19a463;
  --green-dark: #128049;
  --red: #ff0000;
  --red-dark: #d90000;
  --blue: #2f6f9f;
  --glass: rgba(0, 0, 0, 0.04);
  --glass-strong: rgba(0, 0, 0, 0.08);
  --border: rgba(0, 0, 0, 0.14);
  --shadow: rgba(0, 0, 0, 0.18);
  --radius: 16px;
  --radius-sm: 10px;
  --ease: cubic-bezier(0.2, 0.7, 0.3, 1);
  font-family: "Poppins", system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color-scheme: light;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  color: var(--ink);
  background: var(--bg);
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
}

#app { height: 100%; }

.screen { display: none; height: 100%; }
.screen.active { display: flex; }
.hidden { display: none !important; }

a { color: inherit; }
::selection { background: var(--green); color: #fff; }

:focus-visible {
  outline: 2px solid var(--green);
  outline-offset: 2px;
}

/* ---------------- HOME ---------------- */
#screen-home {
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: max(20px, env(safe-area-inset-top)) 20px max(20px, env(safe-area-inset-bottom));
}
.home-card {
  background: var(--paper);
  border: 1px solid var(--black);
  border-radius: 22px;
  padding: clamp(22px, 5vw, 36px);
  width: min(420px, 92vw);
  box-shadow: 0 24px 50px var(--shadow);
  animation: rise 0.35s var(--ease);
}
@keyframes rise {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}
.logo {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-size: clamp(32px, 8vw, 44px);
  font-weight: 800;
  letter-spacing: 2px;
  margin: 0 0 4px;
  text-align: center;
  color: var(--black);
}
.logo-k {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 3px solid var(--black);
  border-radius: 6px;
  padding: 0 6px;
  margin-right: 4px;
}
.logo-k::before {
  content: "";
  position: absolute;
  top: -5px;
  left: -5px;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: var(--red);
  border: 2px solid var(--paper);
}
.tagline { text-align: center; color: var(--muted); margin: 0 0 24px; font-size: 14px; }
.field { display: block; margin-bottom: 16px; }
.field span { display: block; font-size: 13px; margin-bottom: 6px; color: var(--muted); }
input[type="text"], input[type="number"] {
  width: 100%;
  padding: 13px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--paper);
  color: var(--ink);
  font-size: 16px;
  transition: box-shadow 0.15s var(--ease), border-color 0.15s var(--ease);
}
input:focus { outline: none; border-color: var(--green); box-shadow: 0 0 0 3px rgba(25, 164, 99, 0.25); }
.btn {
  width: 100%;
  padding: 14px;
  border-radius: var(--radius-sm);
  border: none;
  background: var(--black);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  min-height: 44px;
  transition: transform 0.12s var(--ease), background 0.15s var(--ease), box-shadow 0.15s var(--ease);
}
.btn:hover { background: #2a2a2a; transform: translateY(-1px); }
.btn:active { transform: translateY(1px); }
.btn-primary {
  background: var(--green);
  color: #fff;
  box-shadow: 0 8px 20px rgba(25, 164, 99, 0.3);
}
.btn-primary:hover { background: var(--green-dark); }
.btn-ghost { background: transparent; border: 1px solid var(--border); color: var(--ink); }
.btn-ghost:hover { background: var(--glass); }
.btn.small { width: auto; padding: 9px 14px; font-size: 14px; min-height: 38px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
.divider { text-align: center; margin: 22px 0 16px; position: relative; color: var(--muted); font-size: 13px; }
.divider::before, .divider::after {
  content: ""; position: absolute; top: 50%; width: 34%; height: 1px; background: var(--border);
}
.divider::before { left: 0; }
.divider::after { right: 0; }
.join-row { display: flex; gap: 10px; }
.code-input { text-transform: uppercase; letter-spacing: 6px; font-weight: 700; text-align: center; }
.join-row .btn { width: 110px; flex: none; }
.error { color: var(--red); min-height: 18px; font-size: 14px; text-align: center; margin: 10px 0 0; }
.link-btn { background: none; border: none; color: var(--green-dark); cursor: pointer; font-size: 14px; margin-top: 10px; width: 100%; font-weight: 600; }
.rules-box { margin-top: 14px; font-size: 13px; line-height: 1.6; color: var(--ink); max-height: 40vh; overflow: auto; }
.rules-box h4 { color: var(--green-dark); margin: 12px 0 4px; }
.rules-box ul { padding-left: 18px; margin: 4px 0; }
.credits { margin-top: 22px; color: var(--muted); font-size: 13px; text-align: center; }

/* ---------------- AUTH ---------------- */
.auth-box { margin-bottom: 16px; }
.btn-google { background: #fff; color: #3c4043; border: 1px solid var(--border); }
.btn-google:hover { background: #f1f3f4; }
.hint { color: var(--muted); font-size: 13px; text-align: center; margin: 0 0 16px; }
.account-pill {
  display: flex; align-items: center; gap: 10px; background: var(--glass);
  border: 1px solid var(--border);
  border-radius: 999px; padding: 6px 10px 6px 6px;
}
.avatar { width: 32px; height: 32px; border-radius: 50%; object-fit: cover; flex: none; }
.avatar-fallback {
  display: flex; align-items: center; justify-content: center; background: var(--green);
  color: #fff; font-weight: 800;
}
.account-name { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dev-login { display: flex; gap: 8px; flex-wrap: wrap; }
.dev-login input { flex: 1; min-width: 120px; }
.dev-login .btn { width: auto; flex: none; }

/* ---------------- END SCORE ---------------- */
.endscore-row {
  display: flex; align-items: center; justify-content: center; gap: 8px; margin: 14px 0;
}
.endscore-row input {
  width: 80px; text-align: center; padding: 8px; font-weight: 700;
}
.endscore-row input:disabled {
  background: var(--glass); color: var(--ink); border-color: transparent; opacity: 1; -webkit-text-fill-color: var(--ink);
}

/* ---------------- LOBBY ---------------- */
#screen-lobby { align-items: center; justify-content: center; padding: 20px; }
.lobby-card {
  background: var(--paper);
  border: 1px solid var(--black);
  border-radius: 22px;
  padding: clamp(20px, 5vw, 30px);
  width: min(460px, 94vw);
  text-align: center;
  box-shadow: 0 24px 50px var(--shadow);
  animation: rise 0.35s var(--ease);
}
.code-badge {
  display: inline-block;
  background: var(--black);
  color: #fff;
  font-weight: 800;
  letter-spacing: 4px;
  padding: 4px 14px;
  border-radius: 8px;
}
.code-badge.small { font-size: 13px; padding: 2px 8px; letter-spacing: 2px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.player-list { list-style: none; padding: 0; margin: 14px 0; }
.player-list li {
  display: flex; align-items: center; gap: 10px; justify-content: center;
  padding: 10px; margin: 6px 0; background: var(--glass); border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  transition: border-color 0.2s;
}
.player-list .host-tag { font-size: 11px; background: var(--green); color: #fff; padding: 1px 6px; border-radius: 6px; font-weight: 700; }
.player-list .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--green); box-shadow: 0 0 6px rgba(25, 164, 99, 0.6); }
.player-list .dot.off { background: #999; box-shadow: none; }

/* ---------------- GAME ---------------- */
#screen-game {
  flex-direction: column; gap: 6px; position: relative; overflow: hidden;
  padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  width: 100%;
}
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 8px; min-width: 90px; }
.topbar-right { justify-content: flex-end; }
.banner {
  flex: 1; text-align: center; font-weight: 700; font-size: 15px;
  color: var(--green-dark); min-height: 22px;
}
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%; border: 1px solid var(--border);
  background: var(--paper); color: var(--ink); font-size: 16px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 1px 3px var(--shadow);
  transition: background 0.15s;
}
.icon-btn:hover { background: var(--glass); }

.opponents {
  display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; align-items: flex-start;
  min-height: 120px;
}
.player-pod {
  background: var(--paper); border-radius: var(--radius-sm); padding: 8px 10px; min-width: 128px;
  border: 2px solid var(--border); box-shadow: 0 1px 4px var(--shadow);
  transition: border-color 0.2s, box-shadow 0.2s;
}
.player-pod.current { border-color: var(--green); box-shadow: 0 0 16px rgba(25, 164, 99, 0.35); animation: pulse 1.8s ease-in-out infinite; }
.player-pod.caller { border-color: var(--red); }
.player-pod.disconnected { opacity: 0.45; }
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 12px rgba(25, 164, 99, 0.25); }
  50% { box-shadow: 0 0 20px rgba(25, 164, 99, 0.5); }
}
.pod-head { display: flex; align-items: center; gap: 6px; font-size: 13px; margin-bottom: 6px; }
.pod-name { font-weight: 700; max-width: 90px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pod-score { margin-left: auto; background: var(--glass-strong); border-radius: 8px; padding: 1px 7px; font-size: 12px; font-variant-numeric: tabular-nums; }
.pod-tags { display: flex; gap: 4px; }
.tag { font-size: 10px; padding: 1px 5px; border-radius: 5px; font-weight: 700; color: #fff; }
.tag.dealer { background: var(--black); }
.tag.turn { background: var(--green); }
.tag.kombio { background: var(--red); }
.hand-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 5px; }

/* Cards */
.card {
  width: 46px; height: 64px; border-radius: 8px; position: relative;
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 20px; user-select: none;
  box-shadow: 0 2px 4px var(--shadow);
  transition: transform 0.12s var(--ease), box-shadow 0.12s var(--ease);
}
.card.back {
  background-color: #141416;
  background-image:
    radial-gradient(var(--red) 1.5px, transparent 1.6px);
  background-size: 10px 10px;
  background-position: 0 0;
  border: 2px solid var(--black);
  color: rgba(255, 255, 255, 0.85);
}
.card .card-logo { font-size: 16px; letter-spacing: 1px; }
.card.face {
  background: var(--paper); color: var(--ink); border: 1px solid var(--paper-edge);
}
.card.face.neg { color: var(--blue); }
.card.face.high { color: var(--red); }
.card.empty { background: var(--glass); border: 2px dashed var(--border); box-shadow: none; }
.card.slot { cursor: default; }
.card.selectable { cursor: pointer; outline: 2px solid transparent; }
.card.selectable:hover { outline: 2px solid var(--green); transform: translateY(-3px); }
.card.selected { outline: 3px solid var(--green); transform: translateY(-3px); }
.card.peek { animation: flip 0.25s ease; }
@keyframes flip { from { transform: rotateY(90deg); } to { transform: rotateY(0); } }

.hand-grid .card { width: 42px; height: 58px; font-size: 18px; }

/* Table center */
.table-center { display: flex; align-items: center; justify-content: center; gap: 24px; flex-wrap: wrap; padding: 4px; flex: 1; }
.pile-group { display: flex; gap: 20px; align-items: center; }
.pile { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.pile .card { width: 54px; height: 76px; font-size: 24px; }
.pile.deck { cursor: pointer; }
.pile.deck:hover .card.back { transform: translateY(-3px); }
.pile-count, .pile-label { font-size: 11px; color: var(--muted); }
.pile.discard .card { cursor: default; }
.drawn-zone {
  display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 6px 12px;
  background: var(--paper); border-radius: 12px; border: 1px solid var(--green);
  box-shadow: 0 1px 4px var(--shadow);
  animation: rise 0.2s var(--ease);
}
.drawn-zone .card { width: 54px; height: 76px; font-size: 24px; }
.drawn-label { font-size: 12px; color: var(--green-dark); font-weight: 700; }

/* Your area */
.you-area { display: flex; justify-content: center; }
.you-pod {
  background: var(--paper); border-radius: var(--radius); padding: 10px 16px;
  border: 2px solid var(--border); box-shadow: 0 2px 8px var(--shadow);
}
.you-pod.current { border-color: var(--green); box-shadow: 0 0 20px rgba(25, 164, 99, 0.4); }
.you-pod.caller { border-color: var(--red); }
.you-pod .pod-head { justify-content: center; font-size: 15px; }
.you-pod .hand-grid .card { width: 58px; height: 80px; font-size: 26px; }
.you-pod .hand-grid { gap: 8px; }

/* Action bar */
.action-bar { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; min-height: 46px; padding-bottom: 2px; }
.action-bar .btn { width: auto; padding: 12px 18px; min-height: 44px; }
.btn.danger { background: var(--red); }
.btn.danger:hover { background: var(--red-dark); }
.prompt {
  text-align: center; background: var(--paper); border: 1px solid var(--border);
  border-radius: 10px; padding: 8px 14px; margin: 0 auto; max-width: 520px; font-size: 14px;
  box-shadow: 0 1px 4px var(--shadow);
}

/* Side panel: log + chat */
#side {
  position: absolute; right: 8px; top: 54px; width: 240px; max-height: 70vh;
  display: flex; flex-direction: column; gap: 8px;
  background: var(--paper); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 8px;
  box-shadow: 0 8px 30px var(--shadow);
  transform: translateX(120%); transition: transform 0.25s var(--ease); z-index: 5;
}
#side.open { transform: translateX(0); }
.log { flex: 1; overflow-y: auto; font-size: 12px; line-height: 1.5; color: var(--muted); max-height: 40vh; }
.log div { padding: 2px 0; border-bottom: 1px solid var(--border); }
.chat-messages { max-height: 20vh; overflow-y: auto; font-size: 12px; }
.chat-messages b { color: var(--green-dark); }
#chat-form input { font-size: 14px; padding: 10px 12px; }

/* Overlay (round/game over) */
.overlay {
  position: absolute; inset: 0; background: rgba(0, 0, 0, 0.6);
  display: flex; align-items: center; justify-content: center; z-index: 20; padding: 16px;
  backdrop-filter: blur(2px);
}
.overlay-card {
  background: var(--paper); border: 1px solid var(--black); border-radius: 18px; padding: 24px;
  width: min(520px, 94vw); max-height: 88vh; overflow: auto;
  box-shadow: 0 30px 70px var(--shadow);
  animation: rise 0.3s var(--ease);
}
.overlay-card h2 { text-align: center; color: var(--black); margin-top: 0; }
.score-table { width: 100%; border-collapse: collapse; margin: 14px 0; }
.score-table th, .score-table td { padding: 8px; text-align: left; border-bottom: 1px solid var(--border); font-size: 14px; }
.score-table .revealed-hand { font-family: ui-monospace, "SF Mono", Menlo, monospace; color: var(--green-dark); }
.score-table tr.winner td { color: var(--green-dark); font-weight: 700; }
.overlay-card .btn { margin-top: 8px; }

/* Toast */
.toast {
  position: fixed; bottom: max(20px, env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%);
  background: var(--black); color: #fff; padding: 10px 18px; border-radius: 10px;
  font-size: 14px; z-index: 50; border: 1px solid rgba(255, 255, 255, 0.15);
  max-width: min(90vw, 420px); text-align: center;
}

/* ---------------- RESPONSIVE ---------------- */

/* Small phones */
@media (max-width: 420px) {
  .pile .card, .drawn-zone .card { width: 42px; height: 60px; font-size: 18px; }
  .you-pod .hand-grid .card { width: 44px; height: 62px; font-size: 19px; }
  .you-pod { padding: 8px 10px; }
  .pod-name { max-width: 64px; }
}

/* Phones/small tablets: sticky bottom action bar, full-width side drawer */
@media (max-width: 640px) {
  #side { width: 88vw; max-height: 78vh; }
  .banner { font-size: 13px; }
  .action-bar {
    position: sticky; bottom: 0; left: 0; right: 0;
    margin: 6px -8px -8px; padding: 10px 8px calc(10px + env(safe-area-inset-bottom));
    background: linear-gradient(180deg, transparent, var(--bg) 30%);
  }
}

/* Desktop: turn the chat/log drawer into a permanent sidebar */
@media (min-width: 1080px) {
  #screen-game.active {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 300px;
    grid-template-rows: auto auto 1fr auto auto;
    column-gap: 18px;
    max-width: 1240px;
    margin: 0 auto;
  }
  .topbar, .opponents, .table-center, .you-area, .action-bar, .prompt {
    grid-column: 1;
  }
  #side {
    position: static;
    grid-column: 2;
    grid-row: 1 / -1;
    width: auto;
    height: 100%;
    max-height: none;
    transform: none;
  }
  .side-toggle { display: none; }
  .home-card, .lobby-card { width: min(460px, 92vw); }
}
