:root {
  --bg0: #070b14;
  --bg1: #0d1526;
  --felt: #0f3d2e;
  --felt-deep: #0a2a20;
  --gold: #f0c14b;
  --gold-dim: #b8942f;
  --rose: #e11d48;
  --blue: #2563eb;
  --ink: #e8eefc;
  --muted: #8b9bb8;
  --glass: rgba(16, 24, 42, 0.72);
  --line: rgba(255, 255, 255, 0.08);
  --ok: #22c55e;
  --warn: #f59e0b;
  --danger: #ef4444;
  --shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
  --radius: 18px;
  --tile-w: 42px;
  --tile-h: 58px;
  --font: "Outfit", system-ui, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(1200px 600px at 10% -10%, #1a2b55 0%, transparent 55%),
    radial-gradient(900px 500px at 100% 0%, #3b0d2a 0%, transparent 50%),
    linear-gradient(180deg, var(--bg0), var(--bg1));
  overflow-x: hidden;
  min-height: 100%;
}

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; }
input, select {
  width: 100%;
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
  outline: none;
}
input:focus, select:focus { border-color: rgba(240,193,75,0.45); box-shadow: 0 0 0 3px rgba(240,193,75,0.12); }

.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: #fda4af; min-height: 1.2em; font-size: 0.92rem; }
.fine { color: var(--muted); font-size: 0.78rem; line-height: 1.4; }

.view { display: none; min-height: 100vh; }
.view.active { display: block; }

.glass {
  background: var(--glass);
  border: 1px solid var(--line);
  backdrop-filter: blur(16px);
  box-shadow: var(--shadow);
  border-radius: var(--radius);
}

/* AUTH */
#view-auth {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  position: relative;
}
.auth-bg {
  position: absolute; inset: 0;
  background:
    radial-gradient(circle at 20% 30%, rgba(225,29,72,0.18), transparent 30%),
    radial-gradient(circle at 80% 20%, rgba(37,99,235,0.18), transparent 28%),
    radial-gradient(circle at 50% 80%, rgba(240,193,75,0.08), transparent 35%);
  pointer-events: none;
}
.auth-card { width: min(440px, 100%); padding: 28px; position: relative; z-index: 1; }
.brand { display: flex; gap: 14px; align-items: center; margin-bottom: 18px; }
.brand h1 { margin: 0; font-size: 1.6rem; letter-spacing: -0.02em; }
.brand p { margin: 2px 0 0; }
.brand-badge {
  width: 56px; height: 56px; border-radius: 16px;
  display: grid; place-items: center;
  font-weight: 800; font-size: 1.1rem;
  background: linear-gradient(145deg, #e11d48, #9f1239);
  box-shadow: 0 10px 30px rgba(225,29,72,0.35), inset 0 1px 0 rgba(255,255,255,0.25);
}
.brand-badge.sm { width: 36px; height: 36px; border-radius: 10px; font-size: 0.8rem; }
.tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 16px; }
.tab {
  padding: 10px; border-radius: 12px;
  background: rgba(255,255,255,0.04);
  color: var(--muted);
}
.tab.active { background: rgba(240,193,75,0.12); color: var(--gold); border: 1px solid rgba(240,193,75,0.25); }
.auth-form { display: grid; gap: 12px; }
.auth-form label { display: grid; gap: 6px; font-size: 0.86rem; color: var(--muted); }
.avatar-pick { display: flex; flex-wrap: wrap; gap: 8px; }
.avatar-opt {
  width: 40px; height: 40px; border-radius: 12px;
  border: 2px solid transparent;
  background: #152038;
  display: grid; place-items: center;
  font-size: 1.2rem;
}
.avatar-opt.selected { border-color: var(--gold); }

.btn {
  background: rgba(255,255,255,0.06);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 11px 16px;
  font-weight: 600;
  transition: 0.15s ease;
}
.btn:hover { transform: translateY(-1px); background: rgba(255,255,255,0.1); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }
.btn.primary {
  background: linear-gradient(135deg, #f0c14b, #d4a017);
  color: #1a1200;
  border: none;
  box-shadow: 0 8px 24px rgba(240,193,75,0.25);
}
.btn.ghost { background: transparent; }
.btn.block { width: 100%; }
.btn.sm { padding: 8px 10px; font-size: 0.82rem; border-radius: 10px; }
.btn.warn { background: rgba(245,158,11,0.15); border-color: rgba(245,158,11,0.35); color: #fbbf24; }
.btn.danger { background: rgba(239,68,68,0.15); border-color: rgba(239,68,68,0.35); color: #fca5a5; }

/* LOBBY */
.topbar {
  margin: 14px;
  padding: 12px 16px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.brand.mini { display: flex; align-items: center; gap: 10px; }
.topbar-user { display: flex; align-items: center; gap: 10px; }
.chip-pill, .user-pill {
  display: flex; align-items: center; gap: 8px;
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 12px;
  font-weight: 600;
}
.avatar {
  width: 28px; height: 28px; border-radius: 50%;
  display: grid; place-items: center;
  background: linear-gradient(145deg, #1e3a5f, #0f172a);
  font-size: 0.95rem;
}

.lobby-grid {
  display: grid;
  grid-template-columns: 300px 1fr 260px;
  gap: 14px;
  padding: 0 14px 20px;
}
.panel { padding: 18px; }
.panel.grow { min-height: 70vh; }
.panel h2 { margin: 0 0 14px; font-size: 1.1rem; }
.panel-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.panel-head h2 { margin: 0; }
#form-create { display: grid; gap: 10px; }
#form-create label { display: grid; gap: 6px; font-size: 0.84rem; color: var(--muted); }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.check { display: flex !important; align-items: center; gap: 8px; flex-direction: row !important; }
.check input { width: auto; }
.rules-box {
  margin-top: 18px; padding: 12px;
  border-radius: 14px; background: rgba(255,255,255,0.03);
  border: 1px dashed rgba(255,255,255,0.1);
  font-size: 0.84rem; color: var(--muted);
}
.rules-box h3 { margin: 0 0 8px; color: var(--ink); font-size: 0.92rem; }
.rules-box ul { margin: 0; padding-left: 18px; }
.rules-box li { margin: 4px 0; }

.table-list { display: grid; gap: 10px; }
.table-card {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  padding: 14px;
  border-radius: 14px;
  background: rgba(255,255,255,0.03);
  border: 1px solid var(--line);
  align-items: center;
}
.table-card h3 { margin: 0 0 4px; font-size: 1rem; }
.table-card .meta { color: var(--muted); font-size: 0.82rem; display: flex; flex-wrap: wrap; gap: 8px; }
.badge {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 8px; border-radius: 999px; font-size: 0.75rem;
  background: rgba(255,255,255,0.06);
}
.badge.live { background: rgba(34,197,94,0.15); color: #86efac; }
.badge.wait { background: rgba(59,130,246,0.15); color: #93c5fd; }
.badge.lock { background: rgba(245,158,11,0.15); color: #fcd34d; }

.leaderboard { margin: 0; padding-left: 18px; }
.leaderboard li { margin: 8px 0; color: var(--muted); }
.leaderboard strong { color: var(--ink); }
.online-hint { margin-top: 18px; color: var(--muted); font-size: 0.82rem; display: flex; align-items: center; gap: 8px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.dot.live { background: var(--ok); box-shadow: 0 0 0 4px rgba(34,197,94,0.15); animation: pulse 1.6s infinite; }
@keyframes pulse { 50% { opacity: 0.55; } }

/* TABLE / GAME */
#view-table { min-height: 100vh; }
.table-stage {
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  grid-template-columns: 1fr 280px;
  grid-template-areas:
    "top top"
    "felt chat"
    "rack chat";
  gap: 10px;
  padding: 10px;
}
.table-topbar {
  grid-area: top;
  display: flex; align-items: center; gap: 12px;
  padding: 8px 4px;
}
.table-title { flex: 1; }
.table-title strong { display: block; }
.table-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.felt {
  grid-area: felt;
  position: relative;
  min-height: 420px;
  border-radius: 28px;
  background:
    radial-gradient(circle at 50% 40%, #146b4d 0%, var(--felt) 45%, var(--felt-deep) 100%);
  box-shadow:
    inset 0 0 80px rgba(0,0,0,0.35),
    0 20px 50px rgba(0,0,0,0.4),
    inset 0 1px 0 rgba(255,255,255,0.08);
  border: 3px solid #6b4f1d;
  overflow: hidden;
}
.felt-ring {
  position: absolute; inset: 14px;
  border: 2px solid rgba(240,193,75,0.18);
  border-radius: 22px;
  pointer-events: none;
}
.seat {
  position: absolute;
  min-width: 140px;
  padding: 10px 12px;
  border-radius: 14px;
  background: rgba(0,0,0,0.28);
  border: 1px solid rgba(255,255,255,0.08);
  backdrop-filter: blur(6px);
}
.seat-top { top: 18px; left: 50%; transform: translateX(-50%); text-align: center; }
.seat-left { left: 18px; top: 42%; transform: translateY(-50%); }
.seat-right { right: 18px; top: 42%; transform: translateY(-50%); text-align: right; }
.seat .name { font-weight: 700; }
.seat .sub { color: rgba(255,255,255,0.7); font-size: 0.8rem; }
.seat.turn {
  box-shadow: 0 0 0 2px var(--gold), 0 0 24px rgba(240,193,75,0.35);
}
.seat .hand-backs {
  display: flex; justify-content: center; gap: 2px; margin-top: 6px; flex-wrap: wrap; max-width: 180px;
}
.tile-back-mini {
  width: 14px; height: 20px; border-radius: 3px;
  background: linear-gradient(160deg, #1e3a5f, #0b172b);
  border: 1px solid rgba(255,255,255,0.15);
}

.center-well {
  position: absolute;
  left: 50%; top: 48%;
  transform: translate(-50%, -50%);
  display: flex; align-items: center; gap: 18px;
}
.pile {
  width: 76px; min-height: 96px;
  border-radius: 14px;
  background: rgba(0,0,0,0.22);
  border: 1px dashed rgba(255,255,255,0.15);
  display: grid; place-items: center;
  cursor: pointer;
  transition: 0.15s ease;
}
.pile:hover { background: rgba(255,255,255,0.06); }
.pile .label, .indicator-wrap .label {
  font-size: 0.72rem; color: rgba(255,255,255,0.7); text-align: center;
}
.tile-stack {
  width: 42px; height: 56px; border-radius: 6px;
  background:
    linear-gradient(145deg, #234, #0b172b 60%),
    repeating-linear-gradient(45deg, rgba(255,255,255,0.05) 0 2px, transparent 2px 6px);
  box-shadow: 4px 4px 0 #0a1424, 8px 8px 0 #071018;
  border: 1px solid rgba(255,255,255,0.15);
}
#wall-count { font-family: var(--mono); font-weight: 700; color: var(--gold); font-size: 0.85rem; }
.indicator-wrap { text-align: center; }
.okey-hint {
  margin-top: 6px; font-size: 0.75rem; color: #fde68a;
  background: rgba(0,0,0,0.25); padding: 4px 8px; border-radius: 999px;
}

.open-area {
  position: absolute;
  left: 50%; bottom: 16px; transform: translateX(-50%);
  display: flex; gap: 10px; flex-wrap: wrap; justify-content: center;
  max-width: 80%;
}
.meld-group {
  display: flex; gap: 2px;
  padding: 4px; border-radius: 8px;
  background: rgba(0,0,0,0.2);
  border: 1px solid rgba(255,255,255,0.08);
}

/* TILES */
.tile {
  width: var(--tile-w);
  height: var(--tile-h);
  border-radius: 7px;
  background: linear-gradient(180deg, #fffef8 0%, #f3efe2 100%);
  box-shadow:
    0 2px 0 #c9c2ad,
    0 6px 12px rgba(0,0,0,0.25),
    inset 0 1px 0 #fff;
  display: grid;
  place-items: center;
  position: relative;
  user-select: none;
  font-family: var(--mono);
  font-weight: 700;
  font-size: 1.05rem;
  color: #111;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}
.tile.sm { width: 34px; height: 46px; font-size: 0.9rem; border-radius: 5px; }
.tile .num { line-height: 1; }
.tile .pip {
  position: absolute; top: 5px; left: 6px;
  width: 7px; height: 7px; border-radius: 50%;
}
.tile.c-red { color: #dc2626; }
.tile.c-red .pip { background: #dc2626; }
.tile.c-black { color: #111827; }
.tile.c-black .pip { background: #111827; }
.tile.c-blue { color: #2563eb; }
.tile.c-blue .pip { background: #2563eb; }
.tile.c-yellow { color: #ca8a04; }
.tile.c-yellow .pip { background: #eab308; }
.tile.c-joker {
  color: #7c3aed;
  background: linear-gradient(180deg, #f5f3ff, #ede9fe);
}
.tile.c-joker .pip { background: #7c3aed; }
.tile.is-okey {
  box-shadow: 0 0 0 2px #f59e0b, 0 6px 14px rgba(245,158,11,0.35);
}
.tile.is-okey::after {
  content: "OK";
  position: absolute; bottom: 3px; right: 4px;
  font-size: 0.55rem; color: #b45309; font-weight: 800;
}
.tile.selected {
  transform: translateY(-12px) scale(1.05);
  box-shadow: 0 0 0 2px #2563eb, 0 12px 20px rgba(37,99,235,0.35);
  z-index: 2;
}
.tile.face-down {
  background: linear-gradient(160deg, #1e3a5f, #0b172b);
  color: transparent;
  box-shadow: 0 2px 0 #071018, 0 6px 12px rgba(0,0,0,0.3);
}
.tile.face-down::before {
  content: "";
  position: absolute; inset: 6px;
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 4px;
  background:
    repeating-linear-gradient(45deg, rgba(255,255,255,0.05) 0 2px, transparent 2px 7px);
}

/* RACK / ISTAKA */
.rack-wrap {
  grid-area: rack;
  padding: 6px 4px 10px;
}
.rack-info {
  display: flex; align-items: center; gap: 12px; margin-bottom: 8px; flex-wrap: wrap;
}
.seat-me { min-width: 140px; font-weight: 700; }
.turn-bar {
  flex: 1; height: 8px; border-radius: 999px;
  background: rgba(255,255,255,0.08); overflow: hidden; min-width: 100px;
}
.turn-fill {
  height: 100%; width: 100%;
  background: linear-gradient(90deg, var(--gold), var(--rose));
  transform-origin: left center;
  transition: width 0.2s linear;
}
.rack-tools { display: flex; gap: 6px; flex-wrap: wrap; }
.istaka {
  border-radius: 16px;
  padding: 10px;
  background:
    linear-gradient(180deg, #6b4226 0%, #4a2c17 40%, #3a2212 100%);
  box-shadow: inset 0 2px 0 rgba(255,255,255,0.12), 0 12px 30px rgba(0,0,0,0.35);
  border: 2px solid #2a170c;
}
.istaka-wood {
  border-radius: 12px;
  background:
    repeating-linear-gradient(90deg, rgba(255,255,255,0.03) 0 2px, transparent 2px 8px),
    linear-gradient(180deg, #8b5a2b, #5c3a1e);
  padding: 12px 10px 16px;
  box-shadow: inset 0 8px 16px rgba(0,0,0,0.25);
}
.istaka-groove {
  min-height: 78px;
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  align-content: flex-start;
  padding: 8px;
  border-radius: 10px;
  background: linear-gradient(180deg, rgba(0,0,0,0.28), rgba(0,0,0,0.12));
  border: 1px solid rgba(0,0,0,0.35);
  box-shadow: inset 0 4px 10px rgba(0,0,0,0.35);
}
.selection-hint { margin-top: 6px; color: var(--muted); font-size: 0.8rem; text-align: center; }

.side-chat {
  grid-area: chat;
  display: flex; flex-direction: column;
  min-height: 200px;
  max-height: calc(100vh - 20px);
  padding: 12px;
}
.chat-log {
  flex: 1; overflow: auto; display: flex; flex-direction: column; gap: 8px;
  padding-right: 4px; font-size: 0.88rem;
}
.chat-item { padding: 8px 10px; border-radius: 10px; background: rgba(255,255,255,0.04); }
.chat-item b { color: var(--gold); }
.chat-form { display: flex; gap: 8px; margin-top: 10px; }
.chat-form input { flex: 1; }

.modal {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0,0,0,0.55);
  display: grid; place-items: center;
  padding: 20px;
}
.modal-card { width: min(420px, 100%); padding: 22px; }
.modal-card h2 { margin-top: 0; }
.modal-card input { margin: 10px 0 16px; }

.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: #111827; color: #fff; padding: 12px 18px; border-radius: 999px;
  border: 1px solid rgba(255,255,255,0.1); z-index: 100;
  box-shadow: var(--shadow); max-width: 90vw;
}

@media (max-width: 1100px) {
  .lobby-grid { grid-template-columns: 1fr; }
  .table-stage {
    grid-template-columns: 1fr;
    grid-template-areas:
      "top"
      "felt"
      "rack"
      "chat";
  }
  .side-chat { max-height: 220px; }
  :root { --tile-w: 36px; --tile-h: 50px; }
}

@media (max-width: 640px) {
  .topbar { flex-direction: column; align-items: stretch; }
  .topbar-user { justify-content: space-between; }
  .seat { min-width: 110px; font-size: 0.85rem; }
  .center-well { gap: 10px; scale: 0.92; }
  .rack-tools .btn { padding: 8px; }
}
