:root {
  --bg: #0d2233;
  --bg2: #133349;
  --ink: #f6efe0;
  --muted: #a9bccb;
  --gold: #e7c46a;
  --cream: #f3e9d4;
  --cream-ink: #3b2c1c;
  --tray: #e9dcc0;
  --tray-edge: #c7b38d;
  --accent: #f0b429;
  --danger: #e2614a;
  --ok: #4cc99a;
  --cell: 30px;
  --ft: 26px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; overscroll-behavior: none; }
body {
  background-color: var(--bg);
  background-image:
    radial-gradient(ellipse at 50% 35%, rgba(46, 110, 150, .35), transparent 65%),
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='56' height='56' viewBox='0 0 56 56'%3E%3Cg fill='none' stroke='%23ffffff' stroke-opacity='.045' stroke-width='1.2'%3E%3Cpath d='M28 4L52 28 28 52 4 28z'/%3E%3Ccircle cx='28' cy='28' r='7'/%3E%3Cpath d='M0 0l8 8M56 0l-8 8M0 56l8-8M56 56l-8-8'/%3E%3C/g%3E%3C/svg%3E");
  color: var(--ink);
  font-family: Nunito, system-ui, -apple-system, Segoe UI, sans-serif;
  touch-action: manipulation;
  user-select: none; -webkit-user-select: none;
}
button { font: inherit; color: inherit; cursor: pointer; }
input { font: inherit; }
#app { position: fixed; inset: 0; display: flex; flex-direction: column; padding-top: var(--safe-t); padding-bottom: var(--safe-b); }

/* ---------- tiles ---------- */
.tile {
  --s: var(--ft);
  width: var(--s); height: var(--s);
  display: inline-grid; place-items: center;
  border-radius: calc(var(--s) * .16);
  position: relative; flex: none;
  box-shadow: inset 0 -2px 0 rgba(0,0,0,.25), inset 0 1px 0 rgba(255,255,255,.35), 0 2px 3px rgba(0,0,0,.35);
  transition: transform .18s ease, opacity .18s ease, filter .18s;
}
.tile::after { /* glaze highlight */
  content: ""; position: absolute; inset: 0; border-radius: inherit; pointer-events: none;
  background: radial-gradient(circle at 28% 22%, rgba(255,255,255,.55), rgba(255,255,255,0) 38%),
              linear-gradient(160deg, rgba(255,255,255,.12), rgba(0,0,0,.12));
}
.tile svg { position: absolute; left: 6%; top: 6%; width: 88%; height: 88%; }
.c0 { background: linear-gradient(145deg, #2f6bd0, #163f8c); }
.c1 { background: linear-gradient(145deg, #ffd25a, #eaa21d); }
.c2 { background: linear-gradient(145deg, #e4573c, #a92b1a); }
.c3 { background: linear-gradient(145deg, #34343f, #111117); }
.c4 { background: linear-gradient(145deg, #35b98f, #1a7c61); }
.tile.first {
  background: linear-gradient(145deg, #fff8e6, #e3d2ab);
  color: #7a4b12; font-family: Cinzel, serif; font-weight: 700; font-size: calc(var(--s) * .55);
}
.slot {
  --s: var(--cell);
  width: var(--s); height: var(--s); border-radius: calc(var(--s) * .16);
  background: rgba(60, 40, 20, .12); box-shadow: inset 0 1px 3px rgba(60,40,20,.35);
  display: grid; place-items: center; position: relative; flex: none;
}
.slot > .tile { --s: var(--cell); }
.wallslot { opacity: 1; }
.wallslot .ghost { position: absolute; inset: 0; border-radius: inherit; opacity: .22; filter: saturate(.6); }
.wallslot .ghost svg { position: absolute; left: 10%; top: 10%; width: 80%; height: 80%; }

/* ---------- generic UI ---------- */
.btn {
  border: 0; border-radius: 999px; padding: 14px 26px; font-weight: 800; letter-spacing: .02em;
  background: linear-gradient(180deg, #ffd36b, #e9a821); color: #3a2405;
  box-shadow: 0 4px 0 #a86f0c, 0 8px 18px rgba(0,0,0,.35);
  transition: transform .1s, box-shadow .1s;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 #a86f0c, 0 3px 8px rgba(0,0,0,.3); }
.btn.ghost { background: rgba(255,255,255,.08); color: var(--ink); box-shadow: inset 0 0 0 1.5px rgba(255,255,255,.25); }
.btn.ghost:active { box-shadow: inset 0 0 0 1.5px rgba(255,255,255,.4); }
.btn[disabled] { opacity: .45; pointer-events: none; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 12px; border: 0;
  background: rgba(255,255,255,.08); display: grid; place-items: center;
}
.icon-btn svg { width: 20px; height: 20px; }
.screen { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 18px; padding: 24px 20px; overflow-y: auto; text-align: center; }
.logo { width: 132px; height: 132px; filter: drop-shadow(0 10px 22px rgba(0,0,0,.45)); animation: spin 40s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
h1.title { font-family: Cinzel, serif; font-size: 44px; margin: 0; letter-spacing: .08em; color: var(--cream); text-shadow: 0 3px 0 rgba(0,0,0,.35); }
.tag { color: var(--muted); margin: -8px 0 6px; max-width: 320px; line-height: 1.4; }
.row { display: flex; gap: 10px; align-items: center; justify-content: center; flex-wrap: wrap; }
.field {
  background: rgba(255,255,255,.08); border: 1.5px solid rgba(255,255,255,.2); color: var(--ink);
  border-radius: 14px; padding: 13px 16px; width: 200px; text-align: center; outline: none;
}
.field:focus { border-color: var(--gold); }
.code { font-family: Cinzel, serif; font-size: 40px; letter-spacing: .25em; color: var(--gold); margin: 0; padding-left: .25em; }
.card { background: rgba(255,255,255,.06); border-radius: 20px; padding: 18px; width: min(360px, 100%); box-shadow: inset 0 0 0 1px rgba(255,255,255,.08); }
.plist { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.plist li { display: flex; align-items: center; gap: 10px; background: rgba(255,255,255,.06); border-radius: 12px; padding: 10px 12px; text-align: left; }
.plist .dot { width: 10px; height: 10px; border-radius: 50%; background: #61707c; flex: none; }
.plist .dot.on { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.plist .you { margin-left: auto; font-size: 12px; color: var(--gold); font-weight: 800; }
.small { font-size: 13px; color: var(--muted); }
.err { color: #ffb4a6; min-height: 1.2em; font-size: 14px; }
.link { background: none; border: 0; color: var(--gold); text-decoration: underline; padding: 4px; }

/* ---------- game layout ---------- */
.topbar { display: flex; align-items: center; gap: 8px; padding: 6px 10px; flex: none; }
.topbar .brand { font-family: Cinzel, serif; font-weight: 700; letter-spacing: .08em; color: var(--cream); font-size: 17px; }
.topbar .round { font-size: 12px; color: var(--muted); }
.topbar .sp { flex: 1; }
.game { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.opps { display: flex; gap: 6px; padding: 2px 8px 4px; flex: none; justify-content: center; }
.opp {
  flex: 1 1 0; max-width: 170px; min-width: 0; background: rgba(255,255,255,.06); border-radius: 12px; padding: 5px 6px 6px;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.07); border: 0; text-align: left; position: relative;
  display: flex; flex-direction: column; justify-content: flex-start;
  transition: box-shadow .25s, background .25s;
}
.opp.turn { background: rgba(240,180,41,.14); box-shadow: inset 0 0 0 2px var(--accent), 0 0 16px rgba(240,180,41,.35); }
.opp .nm { display: flex; align-items: center; gap: 5px; font-size: 12px; font-weight: 800; white-space: nowrap; overflow: hidden; }
.opp .nm span.n { overflow: hidden; text-overflow: ellipsis; }
.opp .sc { margin-left: auto; color: var(--gold); font-family: Cinzel, serif; font-size: 14px; }
.opp .dot { width: 7px; height: 7px; border-radius: 50%; background: #61707c; flex: none; }
.opp .dot.on { background: var(--ok); }
.mini { --cell: var(--mc, 9px); display: flex; gap: 3px; justify-content: center; margin-top: 4px; }
.mini .slot { border-radius: 2px; box-shadow: none; background: rgba(255,255,255,.1); }
.mini .slot > .tile, .mini .tile { box-shadow: none; border-radius: 2px; }
.mini .tile::after, .mini .tile svg { display: none; }
.mini .lines, .mini .wall { gap: 1.5px; }
.mini .floorcount { font-size: 10px; color: #ffb4a6; text-align: center; margin-top: 2px; min-height: 12px; }

.market { flex: 1; min-height: 0; display: grid; place-items: center; position: relative; }
.ring { position: relative; width: var(--W, 320px); height: var(--W, 320px); }
.factory {
  position: absolute; width: var(--fd); height: var(--fd); border-radius: 50%;
  transform: translate(-50%, -50%);
  background: radial-gradient(circle at 40% 35%, #f8efdc, #dccaa3 70%, #bea77a);
  box-shadow: 0 6px 14px rgba(0,0,0,.4), inset 0 -3px 6px rgba(0,0,0,.18), inset 0 0 0 3px rgba(255,255,255,.35);
  display: grid; place-content: center; grid-template-columns: repeat(2, var(--ft)); gap: calc(var(--ft) * .14);
  transition: opacity .3s, transform .3s;
}
.factory.empty { opacity: .35; transform: translate(-50%, -50%) scale(.85); }
.factory .tile, .center .tile { cursor: pointer; }
.center {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: var(--cd); min-height: calc(var(--ft) * 1.6);
  display: flex; flex-wrap: wrap; gap: calc(var(--ft) * .45) calc(var(--ft) * .5); justify-content: center; align-content: center;
  padding: 8px; border-radius: 22px;
}
.stack { position: relative; width: var(--ft); height: var(--ft); cursor: pointer; }
.stack .tile { position: absolute; left: 0; top: 0; }
.stack .cnt {
  position: absolute; right: calc(var(--ft) * -.42); bottom: calc(var(--ft) * -.36); background: #0b1a27; color: var(--ink);
  border-radius: 999px; font-size: 11px; font-weight: 800; padding: 1px 6px; box-shadow: 0 0 0 1.5px rgba(255,255,255,.4);
  pointer-events: none; z-index: 2;
}
.center-label { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); font-family: Cinzel, serif; color: rgba(255,255,255,.12); font-size: 15px; letter-spacing: .3em; pointer-events: none; }
.selectable .tile.pick { transform: translateY(-4px) scale(1.12); filter: brightness(1.12); z-index: 3; }
.selectable .tile.pick::before { content: ""; position: absolute; inset: -3px; border-radius: inherit; box-shadow: 0 0 0 2.5px #fff, 0 0 14px 3px rgba(255,236,170,.85); }
.dim .tile:not(.pick) { opacity: .45; }
.market.waiting .tile { cursor: default; }

/* my board */
.me { flex: none; padding: 0 8px 6px; }
.tray {
  margin: 0 auto; width: max-content; max-width: 100%;
  background: linear-gradient(180deg, #f1e6cd, var(--tray)); color: var(--cream-ink);
  border-radius: 18px; padding: 8px 10px 8px;
  box-shadow: 0 10px 24px rgba(0,0,0,.45), inset 0 0 0 3px var(--tray-edge), inset 0 0 0 5px #f8f0dd;
  transition: box-shadow .3s;
}
.tray.turn { box-shadow: 0 0 0 3px var(--accent), 0 0 26px rgba(240,180,41,.55), 0 10px 24px rgba(0,0,0,.45), inset 0 0 0 3px var(--tray-edge), inset 0 0 0 5px #f8f0dd; }
.tray-head { display: flex; align-items: center; gap: 8px; font-weight: 800; font-size: 14px; margin: 0 2px 6px; }
.tray-head .score { margin-left: auto; font-family: Cinzel, serif; font-size: 20px; color: #7a4b12; }
.tray-head .turnflag { font-size: 11px; background: var(--accent); color: #3a2405; padding: 2px 8px; border-radius: 999px; animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .55; } }
.boardgrid { display: flex; gap: calc(var(--cell) * .45); align-items: flex-start; }
.lines, .wall { display: grid; gap: calc(var(--cell) * .12); }
.line { display: flex; gap: calc(var(--cell) * .12); justify-content: flex-end; border-radius: 8px; padding: 0; position: relative; }
.wall .wrow { display: flex; gap: calc(var(--cell) * .12); }
.target { cursor: pointer; }
.line.target::before, .floor.target::before {
  content: ""; position: absolute; inset: -3px -4px; border-radius: 9px; pointer-events: none;
  box-shadow: 0 0 0 2.5px #2f9e7c, 0 0 12px rgba(47,158,124,.6); animation: pulse 1.1s ease-in-out infinite;
}
.line.target .slot.preview, .floor.target .slot.preview { background: rgba(47,158,124,.28); }
.line.blocked { opacity: .45; }
.arrow { width: calc(var(--cell) * .5); display: grid; place-items: center; align-self: stretch; color: rgba(60,40,20,.25); font-size: 14px; }
.floorwrap { margin-top: calc(var(--cell) * .3); display: flex; align-items: flex-end; gap: 8px; }
.floor { display: flex; gap: calc(var(--cell) * .12); position: relative; }
.floor .fs { display: grid; justify-items: center; gap: 1px; }
.floor .pen { font-size: 10px; font-weight: 800; color: #a5402f; line-height: 1; }
.floorlabel { font-size: 11px; font-weight: 800; color: #8b6f4a; text-transform: uppercase; letter-spacing: .08em; margin-bottom: 14px; }
.wall .slot.fresh > .tile { animation: land .7s cubic-bezier(.2,1.6,.4,1); }
@keyframes land { 0% { transform: scale(1.8); opacity: 0; } 100% { transform: scale(1); opacity: 1; } }
.ptsfloat { position: absolute; left: 50%; top: -6px; transform: translateX(-50%); font-weight: 800; color: #0d7a57; font-size: 13px; pointer-events: none; animation: rise 1.6s ease-out forwards; z-index: 5; text-shadow: 0 1px 0 #fff; }
@keyframes rise { 0% { opacity: 0; transform: translate(-50%, 4px); } 20% { opacity: 1; } 100% { opacity: 0; transform: translate(-50%, -22px); } }

.hint { text-align: center; font-size: 13px; color: var(--muted); min-height: 20px; padding: 2px 10px 4px; flex: none; }
.hint b { color: var(--ink); }

/* flying tiles */
#fx { position: fixed; inset: 0; pointer-events: none; z-index: 50; }
#fx .tile { position: fixed; left: 0; top: 0; transition: transform .55s cubic-bezier(.5,.05,.3,1), opacity .2s; will-change: transform; z-index: 50; }

/* toast + modals */
#toast { position: fixed; left: 50%; top: calc(var(--safe-t) + 54px); transform: translate(-50%, -20px); opacity: 0; transition: .3s; z-index: 60;
  background: rgba(8, 20, 30, .92); padding: 10px 16px; border-radius: 14px; box-shadow: 0 8px 24px rgba(0,0,0,.5), inset 0 0 0 1px rgba(255,255,255,.12);
  font-size: 14px; max-width: 90vw; text-align: center; pointer-events: none; }
#toast.show { opacity: 1; transform: translate(-50%, 0); }
.modal { position: fixed; inset: 0; background: rgba(4, 12, 20, .72); backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px); z-index: 70; display: grid; place-items: center; padding: 20px; animation: fade .2s; }
@keyframes fade { from { opacity: 0; } }
.sheet { background: linear-gradient(180deg, #163a52, #0f2a3d); border-radius: 22px; padding: 20px; width: min(420px, 100%); max-height: 86vh; overflow: auto; box-shadow: 0 20px 50px rgba(0,0,0,.6), inset 0 0 0 1px rgba(255,255,255,.1); text-align: left; }
.sheet h2 { font-family: Cinzel, serif; margin: 0 0 10px; color: var(--cream); letter-spacing: .05em; }
.sheet p, .sheet li { line-height: 1.45; color: #d6e2ea; font-size: 14px; }
.sheet ul { padding-left: 18px; }
.results { display: grid; gap: 8px; margin: 10px 0 16px; }
.res { display: grid; grid-template-columns: 28px 1fr auto; gap: 4px 10px; align-items: center; background: rgba(255,255,255,.06); padding: 10px 12px; border-radius: 12px; }
.res.win { background: rgba(240,180,41,.18); box-shadow: inset 0 0 0 1.5px var(--accent); }
.res .rk { font-family: Cinzel, serif; font-size: 20px; color: var(--gold); }
.res .pts { font-family: Cinzel, serif; font-size: 22px; }
.res .bd { grid-column: 2 / 4; font-size: 12px; color: var(--muted); }

@media (min-aspect-ratio: 5/4) and (min-width: 700px) {
  .game { display: grid; grid-template-columns: 1fr auto; grid-template-rows: auto 1fr auto; }
  .opps { grid-column: 1 / 3; }
  .market { grid-row: 2 / 4; }
  .me { grid-column: 2; grid-row: 2; align-self: center; padding-right: 20px; }
  .hint { grid-column: 2; grid-row: 3; }
}
