:root {
  --ink: #2a2433;
  --muted: #6d6578;
  --card: rgba(255, 255, 255, 0.9);
  --line: rgba(42, 36, 51, 0.12);
  --accent: #ff8a3d;
  --accent-ink: #5a2a08;
  --good: #2f9e5f;
  --bad: #d8404a;
  --radius: 14px;
  --shadow: 0 6px 24px rgba(30, 20, 50, 0.16);
  font-family: 'Fredoka', system-ui, -apple-system, 'Segoe UI', sans-serif;
  color: var(--ink);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #8fd3ff; }
body { -webkit-user-select: none; user-select: none; touch-action: manipulation; }
canvas#game { position: fixed; inset: 0; display: block; }
[hidden] { display: none !important; }
kbd {
  font: 600 0.78em ui-monospace, Menlo, monospace;
  background: rgba(0, 0, 0, 0.07); border: 1px solid rgba(0, 0, 0, 0.12); border-bottom-width: 2px;
  border-radius: 5px; padding: 1px 5px;
}

/* ---------- loading ---------- */
.loading {
  position: fixed; inset: 0; display: grid; place-content: center; justify-items: center; gap: 12px;
  background: linear-gradient(#8fd3ff, #e9f7ff); z-index: 50; font-size: 18px; color: #2a4a66;
}
.loading-tile {
  width: 72px; height: 72px; border-radius: 16px; background: #ffb35c; color: #5a2a08;
  display: grid; place-content: center; font-size: 36px; font-weight: 700; box-shadow: 0 6px 0 #d48a3c;
  animation: hop 0.9s ease-in-out infinite;
}
@keyframes hop { 50% { transform: translateY(-10px); } }
.loading.error { background: #fff4f0; color: var(--ink); text-align: center; padding: 24px; }
.loading.error p { max-width: 520px; }

/* ---------- HUD ---------- */
.card { background: var(--card); backdrop-filter: blur(8px); border-radius: var(--radius); box-shadow: var(--shadow); }
.hud {
  position: fixed; top: 0; left: 0; right: 0; padding: max(12px, env(safe-area-inset-top)) 12px 0;
  display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; pointer-events: none; z-index: 10;
}
.hud > * { pointer-events: auto; }
.hud-stats { padding: 10px 14px; min-width: 180px; }
.hud-square { font-size: 15px; color: var(--muted); }
.hud-square b { font-size: 28px; color: var(--ink); font-weight: 700; margin-left: 2px; font-variant-numeric: tabular-nums; }
.hud-coins { display: flex; align-items: center; gap: 6px; font-size: 18px; margin: 2px 0 4px; font-variant-numeric: tabular-nums; }
.hud-sub { font-size: 13px; color: var(--muted); line-height: 1.35; }
.hud-sub .warn { color: #b4561b; font-weight: 600; }
.hud-sub .ok { color: var(--good); font-weight: 600; }
.coin {
  display: inline-block; width: 16px; height: 16px; border-radius: 50%; vertical-align: -2px; margin-right: 3px;
  background: radial-gradient(circle at 35% 35%, #fff4a8, #ffd23f 45%, #e0a10e); box-shadow: inset 0 0 0 1.5px #c48a08;
}
.hud-buttons { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

.btn {
  font: 600 15px 'Fredoka', system-ui, sans-serif; color: var(--ink);
  background: #fff; border: none; border-radius: 12px; padding: 9px 14px; cursor: pointer;
  box-shadow: 0 3px 0 rgba(42, 36, 51, 0.18), var(--shadow); transition: transform 0.08s;
}
.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(42, 36, 51, 0.18); }
.btn:disabled { opacity: 0.5; cursor: default; transform: none; }
.btn.primary { background: var(--accent); color: #fff; box-shadow: 0 3px 0 #c9631f, var(--shadow); }
.btn.ghost { background: transparent; box-shadow: none; color: var(--muted); }
.btn.icon { padding: 9px 12px; font-size: 17px; }
.btn.big { font-size: 17px; padding: 14px 16px; flex: 1; }
.btn.small { font-size: 13px; padding: 6px 10px; }
.btn.danger { color: var(--bad); }
.pill { display: inline-block; min-width: 20px; padding: 0 6px; border-radius: 10px; background: #ffe3cf; color: var(--accent-ink); font-size: 12px; text-align: center; }
.pill.full { background: var(--bad); color: #fff; }

.zone-banner {
  position: fixed; top: 22%; left: 50%; transform: translate(-50%, -10px); opacity: 0; transition: all 0.5s;
  background: rgba(20, 16, 30, 0.72); color: #fff; padding: 10px 22px; border-radius: 999px; font-size: 18px;
  pointer-events: none; z-index: 9; white-space: nowrap;
}
.zone-banner.show { opacity: 1; transform: translate(-50%, 0); }
.zone-banner small { opacity: 0.75; margin-left: 8px; }

.caption {
  position: fixed; left: 50%; bottom: 150px; transform: translateX(-50%); max-width: min(520px, 90vw);
  background: rgba(255, 255, 255, 0.92); padding: 8px 16px; border-radius: 12px; font-size: 15px; text-align: center;
  box-shadow: var(--shadow); opacity: 0; transition: opacity 0.3s; pointer-events: none; z-index: 8;
}
.caption.show { opacity: 1; }

.cp-banner {
  position: fixed; left: 50%; bottom: 84px; transform: translateX(-50%); z-index: 11;
  display: flex; align-items: center; gap: 14px; padding: 10px 12px 10px 18px; max-width: 94vw; font-size: 15px;
  border: 2px solid #ffb35c;
}
.hint {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); font-size: 14px; color: rgba(20, 16, 30, 0.75);
  background: rgba(255, 255, 255, 0.75); padding: 6px 14px; border-radius: 999px; transition: opacity 1s; white-space: nowrap;
}
.hint.gone { opacity: 0; pointer-events: none; }

/* ---------- comments ---------- */
.panel {
  position: fixed; right: 12px; top: 128px; bottom: 12px; width: 330px; z-index: 12;
  display: flex; flex-direction: column; background: var(--card); backdrop-filter: blur(10px);
  border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; user-select: text; -webkit-user-select: text;
}
.panel-head { display: flex; justify-content: space-between; align-items: flex-start; padding: 12px 14px 8px; border-bottom: 1px solid var(--line); }
.panel-title { font-size: 18px; font-weight: 600; }
.panel-sub { font-size: 13px; color: var(--muted); }
.x { background: none; border: none; font-size: 24px; line-height: 1; cursor: pointer; color: var(--muted); padding: 0 4px; }
.c-list { flex: 1; overflow-y: auto; padding: 6px 14px; }
.c-empty { color: var(--muted); font-size: 14px; padding: 18px 0; text-align: center; }
.c-item { padding: 9px 0; border-bottom: 1px solid var(--line); }
.c-item:last-child { border-bottom: none; }
.c-meta { display: flex; gap: 8px; align-items: baseline; font-size: 12px; color: var(--muted); }
.c-meta b { color: var(--ink); font-size: 13px; font-weight: 600; }
.c-meta .me { color: var(--accent); }
.c-meta button { margin-left: auto; }
.c-body { font-size: 15px; line-height: 1.35; word-wrap: break-word; overflow-wrap: anywhere; white-space: pre-wrap; margin-top: 2px; }
.c-form { padding: 10px 14px 12px; border-top: 1px solid var(--line); }
.c-form textarea {
  width: 100%; resize: none; font: 15px 'Fredoka', system-ui, sans-serif; border: 1.5px solid var(--line); border-radius: 10px; padding: 8px 10px;
  background: #fff; color: var(--ink);
}
.c-form textarea:focus { outline: none; border-color: var(--accent); }
.c-foot { display: flex; justify-content: space-between; align-items: center; margin-top: 6px; font-size: 12px; color: var(--muted); }
.c-note { font-size: 13px; color: var(--muted); margin-top: 6px; }
.c-note:empty { display: none; }

/* ---------- touch ---------- */
.touch { position: fixed; bottom: 16px; left: 0; right: 0; display: none; justify-content: space-between; padding: 0 16px; pointer-events: none; z-index: 7; }
.tbtn {
  pointer-events: auto; width: 68px; height: 68px; border-radius: 20px; border: none; font-size: 26px; color: var(--ink);
  background: rgba(255, 255, 255, 0.85); box-shadow: 0 4px 0 rgba(0, 0, 0, 0.15), var(--shadow); touch-action: none;
}
.tbtn:active { transform: translateY(3px); }
@media (pointer: coarse) { .touch { display: flex; } .hint { display: none; } .demo-badge { left: 50%; transform: translateX(-50%); bottom: 24px; } }

/* ---------- dialogs ---------- */
.dlg {
  border: none; border-radius: 20px; padding: 22px 24px 16px; width: min(460px, 94vw); color: var(--ink);
  font-family: 'Fredoka', system-ui, sans-serif; box-shadow: 0 20px 60px rgba(20, 10, 40, 0.35);
}
.dlg::backdrop { background: rgba(20, 16, 30, 0.45); }
.dlg h2 { margin: 0 0 4px; font-size: 24px; }
.dlg .sub { margin: 0 0 14px; color: var(--muted); font-size: 15px; }
.dlg-foot { display: flex; justify-content: flex-end; margin-top: 14px; }
.arena { display: flex; align-items: center; justify-content: center; gap: 24px; margin: 6px 0; }
.fighter { display: grid; justify-items: center; gap: 4px; color: var(--muted); font-size: 14px; }
.hand { width: 88px; height: 88px; border-radius: 22px; background: #f3eefa; display: grid; place-content: center; font-size: 46px; }
.hand.shake { animation: shake 0.18s ease-in-out infinite alternate; }
@keyframes shake { from { transform: rotate(-12deg); } to { transform: rotate(12deg) translateY(-4px); } }
.vs { font-weight: 700; color: var(--muted); }
.result { text-align: center; font-size: 18px; min-height: 26px; margin: 10px 0; }
.result.win { color: var(--good); font-weight: 600; }
.result.lose { color: var(--bad); font-weight: 600; }
.choices { display: flex; gap: 8px; }

.shop { display: flex; gap: 16px; align-items: flex-start; }
#shopPreview { width: 140px; height: 140px; border-radius: 16px; background: linear-gradient(#bfe6ff, #e9f7ff); flex: none; }
.shop-list { flex: 1; display: grid; gap: 8px; }
.shop-item { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 10px 12px; border: 1.5px solid var(--line); border-radius: 12px; }
.shop-item.owned { border-color: #bfe8cf; background: #f3fbf6; }
.shop-item .nm { font-weight: 600; }
.shop-item .pr { font-size: 13px; color: var(--muted); }
@media (max-width: 480px) { .shop { flex-direction: column; align-items: center; } .shop-list { width: 100%; } }

.field { display: grid; gap: 6px; font-size: 14px; color: var(--muted); margin: 10px 0 14px; }
.row { display: flex; gap: 8px; }
.field input, .acct input {
  flex: 1; font: 16px 'Fredoka', system-ui, sans-serif; padding: 9px 12px; border-radius: 10px; border: 1.5px solid var(--line); color: var(--ink); min-width: 0;
}
.field input:focus, .acct input:focus { outline: none; border-color: var(--accent); }
.acct { background: #faf7fd; border-radius: 14px; padding: 12px 14px; font-size: 15px; margin-top: 10px; }
.acct p { margin: 0 0 10px; }
.acct .small { font-size: 13px; color: var(--muted); }
.acct-status { font-size: 14px; line-height: 1.35; margin-top: 8px; padding: 8px 10px; border-radius: 10px; }
.acct-status:empty { display: none; }
.acct-status.ok { background: #e6f6ec; color: #1f7a47; }
.acct-status.err { background: #fdecee; color: #b8323c; }

/* ---------- leaderboard ---------- */
.board { max-height: min(56vh, 520px); overflow-y: auto; margin: 0 -6px; padding: 0 6px; }
.board-row { display: grid; grid-template-columns: 44px 1fr auto; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 10px; }
.board-row:nth-child(odd) { background: #faf7fd; }
.board-row.me { background: #fff1e6; outline: 2px solid #ffb35c; }
.board-rank { font-weight: 700; color: var(--muted); font-variant-numeric: tabular-nums; text-align: center; }
.board-rank.medal { font-size: 20px; }
.board-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.board-name small { color: var(--accent); font-weight: 600; margin-left: 6px; }
.board-cp { font-variant-numeric: tabular-nums; color: var(--ink); white-space: nowrap; }
.board-cp span { color: var(--muted); font-size: 13px; }
.board-me { background: #faf7fd; border-radius: 12px; padding: 10px 12px; margin-bottom: 10px; font-size: 15px; }
.board-me:empty { display: none; }
.board-empty { text-align: center; color: var(--muted); padding: 24px 0; }

/* ---------- toasts ---------- */
.toasts { position: fixed; top: 140px; left: 50%; transform: translateX(-50%); display: grid; gap: 8px; z-index: 60; pointer-events: none; }
.toast { background: rgba(20, 16, 30, 0.88); color: #fff; padding: 9px 16px; border-radius: 12px; font-size: 15px; animation: toast-in 0.25s; max-width: 90vw; text-align: center; }
.toast.err { background: #b8323c; }
@keyframes toast-in { from { opacity: 0; transform: translateY(-6px); } }
.demo-badge { position: fixed; left: 12px; bottom: 12px; font-size: 12px; background: rgba(20, 16, 30, 0.7); color: #fff; padding: 4px 10px; border-radius: 999px; z-index: 6; }

/* ---------- small screens: comments become a bottom sheet ---------- */
@media (max-width: 760px) {
  .panel { left: 8px; right: 8px; top: auto; width: auto; bottom: 8px; height: 52vh; }
  .hud-stats { min-width: 0; }
  .hud-square b { font-size: 22px; }
  .btn { padding: 8px 11px; font-size: 14px; }
  .cp-banner { bottom: 100px; flex-direction: column; gap: 8px; text-align: center; }
  .caption { bottom: 190px; }
  .panel { height: 42vh; }
  .comments-open .touch { bottom: calc(42vh + 18px); }
  .comments-open .cp-banner { bottom: calc(42vh + 100px); }
  .comments-open .caption { opacity: 0 !important; }
}
