/* ============================================================
   Online Bring Me Game - mobile-first styles
   Hand-written (no CSS framework) so phones on mobile data
   download ~5 KB instead of a few hundred.
   ============================================================ */

:root {
  --bg: #f4f4fb;
  --card: #ffffff;
  --ink: #1e1b3a;
  --muted: #6b6a80;
  --line: #e3e2ef;
  --primary: #4f46e5;
  --primary-ink: #ffffff;
  --danger: #dc2626;
  --heart: #e11d48;
  --prompt: #fde047;
  --win: #16a34a;
  --tap: 48px;                 /* minimum touch target (spec: >= 44px) */
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }
body {
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--bg);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

h1, h2 { margin: 0 0 8px; line-height: 1.2; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.2rem; }
.muted { color: var(--muted); font-size: .92rem; }
.center { text-align: center; }
.error { color: var(--danger); min-height: 1.2em; margin: 8px 0 0; font-size: .92rem; }

/* ---------- form controls (all >= 48px tall) ---------- */
label { display: block; font-weight: 600; font-size: .9rem; margin: 12px 0 4px; }
input {
  width: 100%; min-height: var(--tap); padding: 0 14px;
  font: inherit; font-size: 16px;            /* 16px stops iOS zooming on focus */
  color: var(--ink); background: #fff;
  border: 2px solid var(--line); border-radius: 12px; outline: none;
}
input:focus { border-color: var(--primary); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: var(--tap); min-width: var(--tap); padding: 0 18px;
  font: inherit; font-weight: 700; color: var(--ink);
  background: #fff; border: 2px solid var(--line); border-radius: 12px;
  cursor: pointer; touch-action: manipulation; user-select: none;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .5; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.danger { color: var(--danger); border-color: #f3c0c0; }
.btn.big { width: 100%; min-height: 60px; font-size: 1.15rem; border-radius: 16px; }

.row { display: flex; gap: 8px; align-items: center; }
.row > input { flex: 1; min-width: 0; }
.row > .btn { flex: 0 0 auto; }
.host-panel .row + .row > .btn, #hostFinal > .btn { flex: 1; }

/* ---------- login ---------- */
.login { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 400px; padding: 24px 20px;
  background: var(--card); border-radius: 20px; box-shadow: 0 8px 30px rgba(30, 27, 58, .1);
  text-align: center;
}
.login-card form { text-align: left; }
.login-card .btn { width: 100%; margin-top: 16px; }
.logo { font-size: 3rem; }

/* ---------- app shell: header / tabs / panel fill the screen ---------- */
.app { height: 100vh; height: 100dvh; display: flex; flex-direction: column; max-width: 640px; margin: 0 auto; background: var(--bg); }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 6px 8px 6px 14px; padding-top: calc(6px + env(safe-area-inset-top, 0px));
  background: var(--primary); color: #fff;
}
.who { display: flex; align-items: center; gap: 8px; min-width: 0; }
.who strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.score { background: rgba(255, 255, 255, .2); padding: 2px 10px; border-radius: 99px; font-weight: 700; white-space: nowrap; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: #4ade80; flex: 0 0 auto; }
.dot.off { background: #fca5a5; }
.top-actions { display: flex; }
.icon-btn {
  width: var(--tap); height: var(--tap); font-size: 1.3rem; color: #fff;
  background: none; border: 0; border-radius: 12px; cursor: pointer;
}

.offline { background: #fef3c7; color: #92400e; text-align: center; padding: 6px; font-weight: 600; font-size: .9rem; }

.tabs { display: flex; background: var(--card); border-bottom: 1px solid var(--line); }
.tab {
  flex: 1; min-height: var(--tap); font: inherit; font-weight: 700; color: var(--muted);
  background: none; border: 0; border-bottom: 3px solid transparent; cursor: pointer;
}
.tab.active { color: var(--primary); border-bottom-color: var(--primary); }
.badge {
  display: inline-block; min-width: 20px; padding: 0 6px; margin-left: 4px;
  font-size: .75rem; line-height: 20px; color: #fff; background: var(--heart); border-radius: 99px;
}

.panel { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.scroll { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 0 12px 16px; }
.bottom-bar { padding: 8px 12px calc(8px + var(--safe-b)); background: var(--card); border-top: 1px solid var(--line); }

/* ---------- sticky "Bring Me" prompt ---------- */
.prompt {
  position: sticky; top: 0; z-index: 5;
  margin: 0 -12px 12px; padding: 12px 16px;
  background: var(--prompt); color: #3a2f00;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .12);
}
.prompt-label { font-size: .8rem; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; opacity: .75; }
.prompt-text { font-size: 1.4rem; font-weight: 800; line-height: 1.2; overflow-wrap: anywhere; }
.prompt-status { font-weight: 700; margin-top: 2px; }
.prompt-status:empty, .prompt-text:empty { display: none; }
.prompt.closed { background: #dcfce7; color: #14532d; }
.prompt.ended { background: #e0e7ff; color: #312e81; }

.host-panel { display: grid; gap: 8px; margin-bottom: 12px; padding: 12px; background: var(--card); border-radius: 16px; }

/* ---------- photo feed ---------- */
.feed { display: grid; gap: 12px; }
.card { background: var(--card); border-radius: 16px; overflow: hidden; border: 3px solid transparent; }
.card.winner { border-color: var(--win); }
.card.mine { border-color: #c7d2fe; }
.card.mine.winner { border-color: var(--win); }
.card-head { display: flex; align-items: center; gap: 8px; padding: 8px 12px; font-weight: 700; }
.card-head .num { color: var(--muted); }
.card-head .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-head time { color: var(--muted); font-weight: 400; font-size: .85rem; }
.card .photo { position: relative; background: #111; }
.card img { display: block; width: 100%; max-height: 60vh; object-fit: contain; }
.card .win-tag { padding: 8px 12px; font-weight: 800; color: var(--win); }
.heart {
  width: 100%; min-height: 56px; font: inherit; font-size: 1.1rem; font-weight: 800;
  color: #fff; background: var(--heart); border: 0; cursor: pointer; touch-action: manipulation;
}
.heart:disabled { opacity: .5; }

/* optimistic "uploading" overlay on the player's own photo */
.uploading {
  position: absolute; inset: 0; display: flex; flex-direction: column; gap: 10px;
  align-items: center; justify-content: center;
  color: #fff; font-weight: 700; background: rgba(0, 0, 0, .45);
}
.spinner { width: 36px; height: 36px; border: 4px solid rgba(255, 255, 255, .35); border-top-color: #fff; border-radius: 50%; animation: spin .8s linear infinite; }
.uploading.failed .spinner { display: none; }
.uploading .btn { min-height: 44px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- chat ---------- */
.chat { padding-top: 12px; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: 85%; padding: 8px 12px; background: var(--card); border-radius: 14px; align-self: flex-start; overflow-wrap: anywhere; }
.msg .from { display: block; font-size: .78rem; font-weight: 700; color: var(--primary); }
.msg.me { align-self: flex-end; background: #e0e7ff; }
.msg.host { background: #fef9c3; }
.msg.host .from { color: #a16207; }
.msg.system { align-self: center; background: none; color: var(--muted); font-size: .88rem; font-weight: 600; text-align: center; }

/* ---------- leaderboard ---------- */
.board { width: 100%; border-collapse: collapse; }
.board td, .board th { padding: 10px 6px; text-align: left; border-bottom: 1px solid var(--line); }
.board th { font-size: .8rem; color: var(--muted); text-transform: uppercase; }
.board .amt { text-align: right; font-weight: 800; white-space: nowrap; }
.board tr.me td { background: #eef2ff; }
.board .pending { color: var(--muted); font-style: italic; }
.final { margin-top: 12px; padding: 16px 12px; background: var(--card); border-radius: 16px; }
.final .row { margin-top: 12px; flex-wrap: wrap; }
.my-gcash { margin-top: 12px; padding: 12px; background: #f0fdf4; border-radius: 12px; }
.my-gcash .btn { margin-top: 8px; width: 100%; }

/* ---------- camera ---------- */
.camera { position: fixed; inset: 0; z-index: 50; background: #000; display: flex; flex-direction: column; }
.camera video { flex: 1; min-height: 0; width: 100%; object-fit: contain; background: #000; }
.cam-prompt {
  position: absolute; top: 0; left: 0; right: 0; padding: 12px 16px; padding-top: calc(12px + env(safe-area-inset-top, 0px));
  color: #3a2f00; background: var(--prompt); font-weight: 800; text-align: center;
}
.cam-error { position: absolute; top: 35%; left: 0; right: 0; padding: 0 24px; color: #fff; text-align: center; }
.cam-controls {
  display: flex; align-items: center; justify-content: space-around;
  padding: 16px 16px calc(16px + var(--safe-b)); background: #000;
}
.cam-btn { width: 56px; height: 56px; font-size: 1.4rem; color: #fff; background: rgba(255, 255, 255, .15); border: 0; border-radius: 50%; }
.shutter { width: 80px; height: 80px; border-radius: 50%; background: #fff; border: 6px solid #9ca3af; cursor: pointer; touch-action: manipulation; }
.shutter:active { background: #d1d5db; }

/* ---------- modals / lightbox / toast ---------- */
.modal { position: fixed; inset: 0; z-index: 60; display: flex; align-items: flex-end; justify-content: center; background: rgba(15, 12, 40, .55); }
.modal-card {
  width: 100%; max-width: 480px; max-height: 85vh; overflow-y: auto;
  padding: 20px 16px calc(16px + var(--safe-b)); background: var(--card); border-radius: 20px 20px 0 0;
}
.modal-card .btn { width: 100%; margin-top: 10px; }
@media (min-width: 520px) { .modal { align-items: center; } .modal-card { border-radius: 20px; } }

.lightbox { position: fixed; inset: 0; z-index: 70; background: rgba(0, 0, 0, .92); display: flex; align-items: center; justify-content: center; }
.lightbox img { max-width: 100%; max-height: 100%; }

.toast {
  position: fixed; left: 50%; bottom: calc(90px + var(--safe-b)); transform: translateX(-50%); z-index: 80;
  width: max-content; max-width: 90%; padding: 12px 18px; color: #fff; background: #1e1b3a; border-radius: 99px;
  font-weight: 700; text-align: center; box-shadow: 0 6px 20px rgba(0, 0, 0, .3);
}
.toast.win { background: var(--win); font-size: 1.1rem; }

/* ============================================================
   v2 - visual polish
   ============================================================ */
body { background: linear-gradient(160deg, #eef0ff 0%, #f7f2ff 55%, #fff4e6 100%) fixed; }
.app { background: transparent; }
.topbar { background: linear-gradient(90deg, #4338ca, #7c3aed); box-shadow: 0 2px 10px rgba(67, 56, 202, .3); z-index: 6; }
.brand { display: none; font-weight: 800; letter-spacing: .01em; }
.chat-title { display: none; }
.btn.primary { background: linear-gradient(180deg, #6366f1, #4f46e5); box-shadow: 0 3px 10px rgba(79, 70, 229, .3); }
.login-card { box-shadow: 0 12px 40px rgba(67, 56, 202, .18); }
.prompt { background: linear-gradient(90deg, #fde047, #fbbf24); border-radius: 0 0 18px 18px; }
.prompt.closed { background: linear-gradient(90deg, #bbf7d0, #dcfce7); }
.prompt.ended { background: linear-gradient(90deg, #c7d2fe, #e0e7ff); }
.host-panel, .final { box-shadow: 0 4px 16px rgba(30, 27, 58, .07); }
.card { box-shadow: 0 4px 16px rgba(30, 27, 58, .1); animation: pop .25s ease-out; }
.card.winner { box-shadow: 0 0 0 3px rgba(22, 163, 74, .25), 0 8px 24px rgba(22, 163, 74, .25); }
.card .win-tag { background: #f0fdf4; text-align: center; }
.card img { cursor: zoom-in; }
.msg { box-shadow: 0 1px 3px rgba(30, 27, 58, .08); animation: pop .2s ease-out; }
.msg.system { box-shadow: none; }
.msg.me { background: #4f46e5; color: #fff; }
@keyframes pop { from { opacity: 0; transform: translateY(6px) scale(.98); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) { .card, .msg { animation: none; } }

/* ============================================================
   Tablets and laptops (>= 768px): Game and Chat side by side.
   The photo feed becomes a grid; the chat stays visible on the right.
   ============================================================ */
@media (min-width: 768px) {
  .app {
    max-width: none; display: grid;
    grid-template-columns: minmax(0, 1fr) clamp(280px, 30vw, 400px);
    grid-template-rows: auto auto minmax(0, 1fr);
  }
  .topbar { grid-column: 1 / -1; grid-row: 1; padding-left: 20px; }
  .offline { grid-column: 1 / -1; grid-row: 2; }
  .tabs { display: none; }
  .brand { display: inline; font-size: 1.15rem; margin-right: 8px; }

  #panelGame { grid-column: 1; grid-row: 3; }
  #panelChat {
    grid-column: 2; grid-row: 3; margin: 12px 12px 12px 0;
    background: rgba(255, 255, 255, .75); border: 1px solid var(--line); border-radius: 18px; overflow: hidden;
  }
  .chat-title { display: block; padding: 12px 16px; font-weight: 800; background: #fff; border-bottom: 1px solid var(--line); }
  #panelChat .bottom-bar { padding-bottom: 8px; }
  #panelChat .msg { max-width: 92%; }
  #panelChat .msg:not(.me):not(.host):not(.system) { background: #fff; }

  #gameScroll { padding: 0 16px 20px; }
  .prompt { margin: 0 0 14px; padding: 16px 22px; }
  .prompt-text { font-size: 2rem; }
  .host-panel { grid-template-columns: minmax(0, 1fr) auto; align-items: center; }
  .host-panel .row + .row > .btn { flex: 0 0 auto; }

  /* photo wall */
  .feed { grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); align-items: start; gap: 14px; }
  .card img { aspect-ratio: 4 / 3; object-fit: cover; max-height: none; }
  .heart { min-height: 48px; font-size: 1rem; }

  .final { max-width: 720px; margin-left: auto; margin-right: auto; }
  .bottom-bar .btn.big { max-width: 420px; display: flex; margin: 0 auto; }
  #camBar { background: transparent; border-top: 0; }
  .camera video { object-fit: contain; }
}