:root {
  --bg: #0f2a3d;
  --panel: rgba(255, 251, 240, 0.96);
  --ink: #2b2118;
  --muted: #7b6a58;
  --accent: #ff8c42;
  --accent-d: #d9661f;
  --ok: #2a9d8f;
  --bad: #e63946;
  --radius: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden; background: #a8dcff;
  font-family: ui-rounded, 'SF Pro Rounded', 'Nunito', system-ui, -apple-system, sans-serif;
  color: var(--ink); user-select: none; -webkit-user-select: none; overscroll-behavior: none;
}
#world { position: fixed; inset: 0; touch-action: none; }
#world canvas { display: block; touch-action: none; }
.hidden { display: none !important; }

button { font: inherit; color: inherit; cursor: pointer; border: none; }
input, select { font: inherit; }

.btn {
  background: #fff; border: 3px solid #3b2a1a; border-radius: 14px; padding: 10px 16px; font-weight: 800;
  box-shadow: 0 4px 0 #3b2a1a; transition: transform 0.06s, box-shadow 0.06s;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 #3b2a1a; }
.btn.primary { background: var(--accent); color: #fff; text-shadow: 0 2px 0 rgba(0, 0, 0, 0.2); }
.btn.big { width: 100%; padding: 14px; font-size: 1.15rem; }
.btn.small { padding: 6px 10px; font-size: 0.85rem; box-shadow: 0 3px 0 #3b2a1a; }
.btn.link { background: none; border: none; box-shadow: none; color: var(--muted); text-decoration: underline; }
.btn:disabled { opacity: 0.45; filter: grayscale(0.6); }
.btn.ok { background: var(--ok); color: #fff; }
.btn.bad { background: var(--bad); color: #fff; }

/* ---------- screens ---------- */
.screen {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; padding: 16px;
  background: linear-gradient(180deg, rgba(15, 42, 61, 0.15), rgba(15, 42, 61, 0.55)); z-index: 10;
}
.card {
  background: var(--panel); border: 4px solid #3b2a1a; border-radius: 24px; padding: 22px 20px; width: 100%; max-width: 380px;
  box-shadow: 0 8px 0 #3b2a1a, 0 20px 50px rgba(0, 0, 0, 0.35); display: flex; flex-direction: column; gap: 12px;
  max-height: calc(100% - 20px); overflow-y: auto;
}
.home-card { text-align: center; }
.logo { font-size: 3.2rem; line-height: 1; animation: float 3s ease-in-out infinite; }
@keyframes float { 50% { transform: translateY(-6px) rotate(-4deg); } }
h1 { margin: 0; font-size: 1.9rem; letter-spacing: -0.5px; color: #1d4e6b; text-shadow: 0 2px 0 #fff; }
.tag { margin: 0; color: var(--muted); font-size: 0.95rem; }
label { display: flex; flex-direction: column; gap: 4px; font-weight: 700; font-size: 0.85rem; text-align: left; color: var(--muted); }
label.inline { flex-direction: row; align-items: center; gap: 6px; }
input, select {
  border: 3px solid #3b2a1a; border-radius: 12px; padding: 10px 12px; font-size: 1.05rem; font-weight: 700; background: #fff; color: var(--ink);
}
#code { text-transform: uppercase; letter-spacing: 6px; text-align: center; flex: 1; min-width: 0; }
.row { display: flex; gap: 8px; align-items: center; }
.or { color: var(--muted); font-size: 0.85rem; }
.hint { font-size: 0.85rem; color: var(--muted); margin: 0; text-align: center; word-break: break-all; }

.lobby-code { text-align: center; font-size: 1.1rem; }
.lobby-code b { font-size: 2.4rem; letter-spacing: 6px; display: block; color: #1d4e6b; }
#seats { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
#seats li {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; background: #fff; border: 2px solid #e2d6c2; border-radius: 12px; font-weight: 700;
}
#seats .swatch { width: 22px; height: 22px; border-radius: 6px; border: 2px solid #3b2a1a; }
#seats .who { flex: 1; }
#seats .tagx { font-size: 0.75rem; color: var(--muted); font-weight: 600; }
#seats .kick { background: none; font-size: 1.1rem; }
.colors { display: flex; gap: 8px; justify-content: center; }
.colors button { width: 34px; height: 34px; border-radius: 10px; border: 3px solid #3b2a1a; }
.colors button.mine { outline: 3px solid var(--accent); outline-offset: 2px; }
.colors button.taken { opacity: 0.25; }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; z-index: 5; }
#hud > * { pointer-events: auto; }
#players {
  position: absolute; top: calc(6px + var(--safe-t)); left: 6px; right: 6px; display: flex; gap: 6px; overflow-x: auto;
  scrollbar-width: none; padding-bottom: 4px;
}
#players::-webkit-scrollbar { display: none; }
.pchip {
  flex: 0 0 auto; background: var(--panel); border: 3px solid #3b2a1a; border-radius: 14px; padding: 4px 8px 4px 6px;
  display: flex; align-items: center; gap: 6px; font-size: 0.78rem; font-weight: 800; box-shadow: 0 3px 0 #3b2a1a; min-width: 0;
}
.pchip.current { background: #fff3c4; transform: translateY(2px); animation: glow 1.4s ease-in-out infinite; }
@keyframes glow { 50% { box-shadow: 0 3px 0 #3b2a1a, 0 0 0 4px rgba(255, 196, 0, 0.55); } }
.pchip .av { width: 26px; height: 26px; border-radius: 8px; border: 2px solid #3b2a1a; display: grid; place-items: center; font-size: 0.85rem; }
.pchip .nm { max-width: 72px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pchip .vp { background: #1d4e6b; color: #fff; border-radius: 8px; padding: 1px 6px; }
.pchip .meta { color: var(--muted); font-weight: 700; display: flex; gap: 4px; }
.pchip.off { opacity: 0.6; }

#status {
  position: absolute; top: calc(52px + var(--safe-t)); left: 50%; transform: translateX(-50%); max-width: calc(100% - 24px);
  background: rgba(29, 78, 107, 0.92); color: #fff; padding: 7px 14px; border-radius: 999px; font-weight: 800; font-size: 0.9rem;
  display: flex; gap: 8px; align-items: center; box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25); text-align: center;
}
#status.mine { background: rgba(217, 102, 31, 0.95); }
.chip { background: #fff; color: var(--ink); border-radius: 999px; padding: 3px 10px; font-weight: 800; font-size: 0.8rem; }

#dice { position: absolute; top: calc(92px + var(--safe-t)); right: 10px; display: flex; gap: 6px; }
.die {
  width: 40px; height: 40px; background: #fff; border: 3px solid #3b2a1a; border-radius: 10px; box-shadow: 0 3px 0 #3b2a1a;
  display: grid; grid-template: repeat(3, 1fr) / repeat(3, 1fr); padding: 4px;
}
.die i { width: 7px; height: 7px; background: #2b2118; border-radius: 50%; place-self: center; }
.die.red i { background: var(--bad); }
.die.rolling { animation: roll 0.5s linear infinite; }
@keyframes roll { to { transform: rotate(360deg); } }
#dice.seven .die { border-color: var(--bad); }

#side-buttons { position: absolute; left: 8px; top: calc(96px + var(--safe-t)); display: flex; flex-direction: column; gap: 8px; }
.round {
  width: 42px; height: 42px; border-radius: 50%; background: var(--panel); border: 3px solid #3b2a1a; box-shadow: 0 3px 0 #3b2a1a; font-size: 1.1rem;
}

#bottom { position: absolute; left: 0; right: 0; bottom: 0; padding: 0 6px calc(6px + var(--safe-b)); display: flex; flex-direction: column; gap: 6px; }
#hand { display: flex; gap: 5px; justify-content: center; }
.rcard {
  flex: 1; max-width: 70px; background: var(--panel); border: 3px solid #3b2a1a; border-radius: 12px; text-align: center; padding: 3px 0 2px;
  box-shadow: 0 3px 0 #3b2a1a; position: relative; transition: transform 0.2s;
}
.rcard .ic { font-size: 1.3rem; line-height: 1.2; }
.rcard .n { font-weight: 900; font-size: 1rem; }
.rcard.zero { background: #e9e1d2; color: #a39684; }
.rcard.zero .ic { filter: grayscale(0.8); opacity: 0.6; }
.rcard.bump { animation: bump 0.5s; }
@keyframes bump { 30% { transform: translateY(-10px) scale(1.1); } }
.rcard .stripe { position: absolute; left: 0; right: 0; top: 0; height: 5px; border-radius: 9px 9px 0 0; }
#actions { display: flex; gap: 5px; }
.act {
  flex: 1; background: var(--panel); border: 3px solid #3b2a1a; border-radius: 14px; box-shadow: 0 4px 0 #3b2a1a; padding: 6px 0 4px;
  font-size: 1.35rem; display: flex; flex-direction: column; align-items: center; position: relative; line-height: 1.1;
}
.act span { font-size: 0.72rem; font-weight: 800; }
.act:active { transform: translateY(3px); box-shadow: 0 1px 0 #3b2a1a; }
.act.primary { background: var(--accent); color: #fff; }
.act:disabled { background: #ddd5c6; color: #a39684; }
.act b { font-weight: normal; }
.act:disabled b { filter: grayscale(1); opacity: 0.45; }
.act.pulse { animation: pulsebtn 1.2s ease-in-out infinite; }
@keyframes pulsebtn { 50% { transform: scale(1.06); } }
.badge { position: absolute; top: -6px; right: 4px; background: var(--bad); color: #fff; font-style: normal; font-size: 0.7rem; font-weight: 900; border-radius: 999px; padding: 1px 6px; border: 2px solid #3b2a1a; }

/* ---------- bottom sheet ---------- */
#sheet-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.3); z-index: 20; }
#sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21; background: var(--panel); border-top: 4px solid #3b2a1a;
  border-radius: 22px 22px 0 0; padding: 6px 14px calc(14px + var(--safe-b)); max-height: 78vh; overflow-y: auto;
  animation: up 0.22s ease-out; max-width: 560px; margin: 0 auto;
}
@keyframes up { from { transform: translateY(100%); } }
.grab { width: 44px; height: 5px; border-radius: 3px; background: #cdbfa8; margin: 2px auto 8px; }
#sheet h2 { margin: 4px 0 10px; font-size: 1.2rem; }
#sheet h3 { margin: 12px 0 6px; font-size: 0.95rem; color: var(--muted); }
.opt {
  width: 100%; display: flex; align-items: center; gap: 12px; padding: 10px 12px; margin-bottom: 8px; background: #fff;
  border: 3px solid #3b2a1a; border-radius: 14px; text-align: left; box-shadow: 0 3px 0 #3b2a1a;
}
.opt:disabled { opacity: 0.45; }
.opt .big { font-size: 1.7rem; }
.opt .t { flex: 1; font-weight: 800; }
.opt .t small { display: block; font-weight: 600; color: var(--muted); }
.cost { font-size: 0.95rem; letter-spacing: 1px; white-space: nowrap; }
.cost .miss { opacity: 0.35; }

.picker { display: flex; gap: 6px; justify-content: space-between; margin: 6px 0; }
.pick {
  flex: 1; background: #fff; border: 3px solid #3b2a1a; border-radius: 12px; padding: 6px 0; text-align: center; position: relative;
}
.pick .ic { font-size: 1.5rem; }
.pick .n { font-weight: 900; }
.pick.sel { background: #fff3c4; outline: 3px solid var(--accent); }
.pick small { display: block; font-size: 0.65rem; color: var(--muted); font-weight: 700; }
.stepper { display: flex; justify-content: center; gap: 2px; margin-top: 3px; }
.stepper button { width: 22px; height: 22px; border-radius: 6px; background: #f0e6d4; font-weight: 900; padding: 0; }
.tabs { display: flex; gap: 6px; margin-bottom: 8px; }
.tabs button { flex: 1; padding: 8px; border-radius: 12px; background: #f0e6d4; font-weight: 800; border: 3px solid transparent; }
.tabs button.on { background: #fff; border-color: #3b2a1a; }
.lbl { font-weight: 800; font-size: 0.85rem; color: var(--muted); margin-top: 8px; }
.responses { display: flex; flex-direction: column; gap: 6px; margin: 8px 0; }
.resp { display: flex; align-items: center; gap: 8px; background: #fff; border-radius: 12px; padding: 6px 10px; border: 2px solid #e2d6c2; font-weight: 700; }
.resp .who { flex: 1; }
.devlist .opt .count { font-weight: 900; background: #1d4e6b; color: #fff; border-radius: 8px; padding: 0 7px; }
.log { font-size: 0.85rem; display: flex; flex-direction: column; gap: 4px; max-height: 45vh; overflow-y: auto; }
.log div { padding: 4px 8px; border-left: 4px solid #ccc; background: #fff; border-radius: 6px; }
.standings { display: flex; flex-direction: column; gap: 6px; margin: 10px 0; }
.standings div { display: flex; gap: 10px; padding: 8px 10px; background: #fff; border-radius: 12px; font-weight: 800; align-items: center; border: 2px solid #e2d6c2; }
.standings .win { background: #fff3c4; border-color: #e8b923; }
.trophy { font-size: 3rem; text-align: center; animation: float 2s ease-in-out infinite; }
.costs-table div { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px dashed #d9ccb6; font-weight: 700; }

/* ---------- toasts & misc ---------- */
#toasts { position: fixed; left: 0; right: 0; top: calc(92px + var(--safe-t)); display: flex; flex-direction: column; align-items: center; gap: 6px; z-index: 30; pointer-events: none; }
.toast {
  background: rgba(43, 33, 24, 0.92); color: #fff; padding: 8px 14px; border-radius: 12px; font-weight: 700; font-size: 0.88rem;
  max-width: 88%; text-align: center; animation: tin 0.25s ease-out, tout 0.4s ease-in forwards; animation-delay: 0s, var(--life, 2.6s);
}
.toast.err { background: rgba(230, 57, 70, 0.95); }
.toast.good { background: rgba(42, 157, 143, 0.95); }
@keyframes tin { from { transform: translateY(-10px); opacity: 0; } }
@keyframes tout { to { opacity: 0; transform: translateY(-8px); } }
#tileinfo {
  position: fixed; left: 50%; bottom: calc(150px + var(--safe-b)); transform: translateX(-50%); background: var(--panel); border: 3px solid #3b2a1a;
  border-radius: 14px; padding: 8px 14px; font-weight: 700; font-size: 0.88rem; z-index: 8; box-shadow: 0 3px 0 #3b2a1a; text-align: center; max-width: 90%;
}
#conn { position: fixed; bottom: 50%; left: 50%; transform: translateX(-50%); background: var(--bad); color: #fff; padding: 10px 18px; border-radius: 12px; font-weight: 800; z-index: 40; }
.help p { margin: 6px 0; font-size: 0.92rem; line-height: 1.35; }

@media (min-width: 700px) {
  #bottom { max-width: 560px; margin: 0 auto; }
  .pchip .nm { max-width: 120px; }
}

/* ---------- spectator / demo ---------- */
#spectate-bar { display: none; }
body.spectating #bottom { display: none; }
body.spectating #spectate-bar {
  display: flex; align-items: center; gap: 10px; position: absolute; left: 8px; right: 8px; bottom: calc(10px + var(--safe-b));
  background: var(--panel); border: 3px solid #3b2a1a; border-radius: 14px; padding: 8px 10px; font-weight: 800; font-size: 0.85rem;
  box-shadow: 0 3px 0 #3b2a1a; max-width: 560px; margin: 0 auto;
}
body.spectating #spectate-bar span { flex: 1; }

#normal-start { display: flex; flex-direction: column; gap: 12px; }
.invite { background: #fff3c4; border: 3px dashed #e8b923; border-radius: 14px; padding: 10px; font-weight: 800; }
.invite b { letter-spacing: 4px; font-size: 1.3rem; color: #1d4e6b; }
input.need { border-color: var(--bad); animation: shake 0.35s; }
@keyframes shake { 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }
#seats .edit { background: none; font-size: 1rem; padding: 2px 4px; }
#seats input { flex: 1; min-width: 0; padding: 4px 8px; font-size: 1rem; }
.pchip.me { cursor: pointer; }
