:root {
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --ink: #161423;
  --ink2: #201d33;
  --ink3: #2c2942;
  --slot: #332b3c;
  --slot2: #4a3e4a;
  --line: #72596a;
  --line2: #a06b62;
  --gold: #f5c568;
  --cream: #f4e7c5;
  --muted: #a99bb2;
  --red: #e34a55;
  --green: #4fd080;
  --blue: #6cc7ff;
  --purple: #b66cff;
  --shadow: rgba(0,0,0,.56);
  --tg-bg: var(--tg-theme-bg-color, #161423);
  --tg-text: var(--tg-theme-text-color, #f4e7c5);
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
body {
  margin: 0;
  color: var(--cream);
  background: #090810;
  font-family: "Trebuchet MS", Verdana, system-ui, sans-serif;
  font-size: 14px;
  overflow-x: hidden;
  -webkit-font-smoothing: none;
  text-rendering: optimizeSpeed;
}
button, input { font: inherit; }
button { color: inherit; -webkit-tap-highlight-color: transparent; }
button.list-item { width: 100%; text-align: left; }

body:after {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 99;
  background:
    repeating-linear-gradient(0deg, rgba(255,255,255,.025) 0 1px, transparent 1px 4px),
    radial-gradient(circle at center, transparent 52%, rgba(0,0,0,.25));
  mix-blend-mode: screen;
  opacity: .45;
}

.world-bg { position: fixed; inset: 0; overflow: hidden; image-rendering: pixelated; background: linear-gradient(#9dc7d8 0 33%, #8eb8c7 34% 48%, #3f3c49 49% 50%, #252331 51%); }
.world-bg:after { content: ""; position: absolute; inset: 0; background: linear-gradient(180deg, transparent 0 52%, rgba(30,25,37,.38) 53%, rgba(10,9,15,.85) 83%); }
.cloud { position: absolute; height: 8px; background: #f3f1df; box-shadow: 16px 0 #f3f1df, 32px 0 #f3f1df, 8px -8px #f3f1df, 24px -8px #f3f1df; opacity: .92; }
.c1 { left: 18%; top: 7%; width: 44px; transform: scale(1.2); }
.c2 { left: 68%; top: 11%; width: 64px; transform: scale(1.35); }
.c3 { left: 44%; top: 5%; width: 36px; }
.mountains { position: absolute; left: -5%; right: -5%; bottom: 48%; height: 160px; opacity: .92; clip-path: polygon(0 92%, 8% 65%, 14% 74%, 22% 42%, 30% 78%, 38% 45%, 48% 78%, 58% 34%, 68% 75%, 77% 48%, 86% 73%, 100% 50%, 100% 100%, 0 100%); background: #6b7180; }
.m2 { bottom: 44%; height: 120px; opacity: .9; background: #474958; transform: translateX(45px); }
.ground { position: absolute; left: 0; right: 0; bottom: 0; height: 48%; background: linear-gradient(#77614f, #58473f 48%, #292434 49%); }
.ground:before { content: ""; position: absolute; inset: 0; background: repeating-linear-gradient(165deg, rgba(43,35,45,.5) 0 2px, transparent 2px 26px), repeating-linear-gradient(15deg, rgba(255,229,171,.12) 0 2px, transparent 2px 34px); }
.sprite { position: absolute; bottom: 8%; width: 12px; height: 18px; background: #161423; box-shadow: 0 -5px #e34a55, 0 18px #161423, 3px 7px #f5c568, -3px 7px #6cc7ff; z-index: 1; opacity: .85; }
.s1 { left: 16%; } .s2 { left: 48%; bottom: 10%; transform: scale(1.25); } .s3 { right: 15%; }

.app-shell { position: relative; z-index: 2; width: min(980px, 100%); min-height: 100dvh; margin: 0 auto; padding: 10px 10px calc(82px + var(--safe-bottom)); }
.pixel-frame { position: relative; background: rgba(20, 18, 32, .94); border: 2px solid #08070d; box-shadow: 0 0 0 2px var(--line), 0 0 0 4px rgba(0,0,0,.45), 0 10px 28px var(--shadow); }
.pixel-frame:before, .pixel-frame:after { content: ""; position: absolute; width: 8px; height: 8px; background: var(--gold); box-shadow: 0 0 0 2px #191320; opacity: .92; }
.pixel-frame:before { left: -3px; top: -3px; }
.pixel-frame:after { right: -3px; bottom: -3px; }

.hud { min-height: 62px; display: flex; justify-content: space-between; align-items: center; padding: 10px 12px; }
.hud-left { display: flex; gap: 10px; align-items: center; min-width: 0; }
.rank-diamond { display: grid; place-items: center; width: 34px; height: 34px; color: var(--green); background: #25233a; border: 2px solid var(--line); box-shadow: inset 0 0 0 2px #0c0a13; }
.player-avatar { flex: 0 0 42px; width: 42px; height: 42px; overflow: hidden; color: var(--gold); font-weight: 900; font-size: 15px; text-transform: uppercase; background: linear-gradient(180deg, #2a2738, #11101a); }
.player-avatar img { width: 100%; height: 100%; object-fit: cover; display: block; image-rendering: auto; }
#playerName { display: block; max-width: 66vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--gold); font-size: 15px; letter-spacing: .02em; }
#profileLine { display: block; max-width: 68vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); font-size: 11px; margin-top: 2px; }
.square-btn, .small-btn, .rpg-btn, .tab-btn, .nav-item { cursor: pointer; border: 2px solid #0a0810; background: linear-gradient(180deg, #392c36, #181522); box-shadow: inset 0 0 0 2px #68515e, 0 3px 0 #07060b; }
.square-btn { width: 36px; height: 30px; color: var(--gold); }
.resource-bar { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 2px; margin-top: 8px; padding: 3px; background: rgba(17,15,28,.94); }
.resource-bar div { min-width: 0; min-height: 38px; padding: 4px 2px 3px; background: #211d2f; text-align: center; border: 1px solid #3e344a; line-height: 1.05; }
.resource-bar b { color: var(--cream); display: block; overflow: hidden; text-overflow: ellipsis; font-size: 12px; line-height: 1.1; }
.resource-bar small { display: block; color: var(--muted); font-size: 9px; line-height: 1.05; margin-top: 1px; }

.main-window { margin-top: 10px; min-height: min(650px, calc(100dvh - 185px)); padding: 10px; background: rgba(22, 20, 35, .94); }
.loading-box { display: grid; place-items: center; min-height: 260px; color: var(--muted); }
.pixel-loader { width: 46px; height: 46px; background: var(--gold); box-shadow: 12px 0 #332b3c, -12px 0 #6cc7ff, 0 12px #4fd080, 0 -12px #e34a55; animation: spin steps(8) 1s infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.view-head { display: flex; align-items: end; justify-content: space-between; gap: 10px; padding: 2px 2px 8px; border-bottom: 2px solid #3b3144; margin-bottom: 10px; }
.view-head h2 { margin: 0; font-size: 18px; color: var(--gold); text-transform: uppercase; letter-spacing: .06em; }
.view-head small { color: var(--muted); font-size: 11px; }
.two-col { display: grid; grid-template-columns: 185px 1fr; gap: 10px; }
.side-card, .content-card { background: rgba(18,16,27,.72); border: 2px solid #0a0810; box-shadow: inset 0 0 0 2px #473947; padding: 8px; }
.side-card { min-height: 280px; }
.content-card { min-height: 280px; }

.grid { display: grid; gap: 8px; }
.grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px; }
.stat-box { background: #211d2f; border: 2px solid #0a0810; box-shadow: inset 0 0 0 2px #483b4b; padding: 8px 5px; text-align: center; }
.stat-box b { display: block; color: var(--gold); font-size: 16px; overflow: hidden; text-overflow: ellipsis; }
.stat-box span { display: block; color: var(--muted); font-size: 10px; text-transform: uppercase; }
.hr { height: 2px; margin: 12px 0; background: linear-gradient(90deg, transparent, #72596a, transparent); }
.note { color: var(--muted); font-size: 12px; line-height: 1.45; }
code { color: var(--gold); background: #0f0d18; padding: 2px 4px; }

.rpg-btn { width: 100%; min-height: 42px; padding: 8px 10px; color: var(--cream); text-align: center; font-weight: 800; line-height: 1.12; }
.rpg-btn small { color: var(--muted); font-weight: 600; }
.rpg-btn.primary { background: linear-gradient(180deg, #2f895e, #18472e); box-shadow: inset 0 0 0 2px #58d58b, 0 3px 0 #061108; color: white; }
.rpg-btn.danger { background: linear-gradient(180deg, #74313a, #351319); box-shadow: inset 0 0 0 2px #bd4a55, 0 3px 0 #07060b; }
.rpg-btn.ghost { background: linear-gradient(180deg, #2d293e, #171520); }
.rpg-btn:active, .small-btn:active, .tab-btn:active, .nav-item:active, .square-btn:active { transform: translateY(2px); box-shadow: inset 0 0 0 2px #68515e, 0 1px 0 #07060b; }
.rpg-btn:disabled, .small-btn:disabled, .tab-btn:disabled { opacity: .44; cursor: not-allowed; }
.small-btn { min-height: 30px; padding: 5px 8px; color: var(--gold); font-size: 12px; }
.roll-status { margin-bottom: 8px; padding: 8px; background: #211d2f; border: 2px solid #0a0810; box-shadow: inset 0 0 0 2px #3e344a; text-align: center; }
.roll-status b { display: block; color: var(--gold); font-size: 14px; }
.roll-status small { display: block; color: var(--muted); font-size: 11px; line-height: 1.35; margin-top: 3px; }
.roll-status.ready { box-shadow: inset 0 0 0 2px #36644a; }
.roll-status.wait { box-shadow: inset 0 0 0 2px #68515e; }
.roll-btn { margin-top: 2px; }

.list { display: grid; gap: 7px; }
.list-item { display: flex; justify-content: space-between; align-items: center; gap: 8px; background: #211d2f; border: 2px solid #0a0810; box-shadow: inset 0 0 0 2px #3e344a; padding: 8px; }
.list-item b { display: block; color: var(--cream); }
.list-button { cursor: pointer; }
.list-button:hover, .list-button:focus-visible { box-shadow: inset 0 0 0 2px var(--gold); outline: none; }
.list-item small { display: block; color: var(--muted); font-size: 11px; margin-top: 3px; }
.list-item span { color: var(--gold); white-space: nowrap; }

.tab-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(122px, 1fr)); gap: 6px; overflow: visible; padding: 3px 1px 8px; max-width: 100%; }
.tab-btn { min-width: 0; min-height: 34px; padding: 5px 8px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tab-btn.active { color: #fff; background: linear-gradient(180deg, #614053, #2a2131); box-shadow: inset 0 0 0 2px var(--gold), 0 3px 0 #07060b; }

.card-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.card-tile, .card-result { position: relative; background: #1b1927; border: 2px solid #0a0810; box-shadow: inset 0 0 0 2px #4b3d4a; padding: 9px; overflow: hidden; }
.card-tile:before, .card-result:before { content: ""; position: absolute; inset: 0; background: repeating-linear-gradient(135deg, rgba(255,255,255,.03) 0 2px, transparent 2px 12px); pointer-events: none; }
.card-tile b { position: relative; display: block; min-height: 34px; color: var(--cream); }
.card-meta { position: relative; color: var(--muted); font-size: 11px; line-height: 1.45; margin: 6px 0; }
.glyph { width: 36px; height: 36px; display: grid; place-items: center; background: #0f0d18; border: 2px solid #332b3c; margin-bottom: 6px; font-size: 20px; }
.card-result { text-align: center; min-height: 250px; display: grid; place-items: center; align-content: center; gap: 7px; }
.card-result h3 { margin: 0; color: var(--gold); font-size: 22px; }
.card-result p { margin: 0; }
.card-sigil { width: 82px; height: 82px; display: grid; place-items: center; background: #0f0d18; border: 3px solid #332b3c; box-shadow: inset 0 0 0 3px #211d2f; font-size: 38px; }
.rarity-chip { display: inline-block; padding: 4px 8px; background: #0f0d18; border: 2px solid #3e344a; color: var(--muted); font-size: 11px; }
.common { --rarity: #b7b7a4; } .rare { --rarity: #6cc7ff; } .epic { --rarity: #b66cff; } .legendary { --rarity: #f5c568; } .mythic { --rarity: #ff6070; }
.card-tile.common, .card-result.common { box-shadow: inset 0 0 0 2px #77776b; }
.card-tile.rare, .card-result.rare { box-shadow: inset 0 0 0 2px #3c86a7, 0 0 18px rgba(108,199,255,.16); }
.card-tile.epic, .card-result.epic { box-shadow: inset 0 0 0 2px #7e42b5, 0 0 18px rgba(182,108,255,.17); }
.card-tile.legendary, .card-result.legendary { box-shadow: inset 0 0 0 2px #d7983d, 0 0 20px rgba(245,197,104,.18); }
.card-tile.mythic, .card-result.mythic { box-shadow: inset 0 0 0 2px #d84b57, 0 0 22px rgba(255,96,112,.23); }
.card-tile .small-btn { width: 100%; margin-top: 4px; }
.card-tile.in-deck { outline: 2px solid rgba(245,197,104,.65); outline-offset: -6px; }
.small-btn:disabled, .rpg-btn:disabled, .tab-btn:disabled { opacity: .48; cursor: not-allowed; filter: grayscale(.3); }
.common { color: #d7d4be; } .rare { color: var(--blue); } .epic { color: var(--purple); } .legendary { color: var(--gold); } .mythic { color: #ff6070; }

.deck-grid { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px; }
.deck-slot { min-height: 86px; background: #181522; border: 2px solid #0a0810; box-shadow: inset 0 0 0 2px #3d3344; padding: 5px; text-align: center; color: var(--muted); font-size: 11px; }
.deck-slot b { color: var(--cream); display: block; }
input { width: 100%; min-height: 38px; background: #0f0d18; color: var(--cream); border: 2px solid #0a0810; box-shadow: inset 0 0 0 2px #3e344a; padding: 8px; outline: none; }
input:focus { box-shadow: inset 0 0 0 2px var(--gold); }

.bottom-nav { position: fixed; z-index: 20; left: 50%; bottom: calc(10px + var(--safe-bottom)); transform: translateX(-50%); width: min(960px, calc(100% - 20px)); display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; padding: 5px; }
.nav-item { min-height: 48px; padding: 5px 2px; color: var(--muted); font-size: 11px; }
.nav-item span { display: block; color: var(--gold); font-size: 17px; margin-bottom: 2px; }
.nav-item.active { color: #fff; background: linear-gradient(180deg, #6b4053, #2a2131); box-shadow: inset 0 0 0 2px var(--gold), 0 3px 0 #07060b; }

.toast { position: fixed; z-index: 120; left: 50%; bottom: calc(76px + var(--safe-bottom)); transform: translate(-50%, 16px); width: min(520px, calc(100% - 26px)); padding: 10px 12px; color: var(--cream); background: rgba(18,16,27,.97); border: 2px solid #0a0810; box-shadow: inset 0 0 0 2px var(--gold), 0 10px 30px rgba(0,0,0,.5); opacity: 0; pointer-events: none; transition: .16s ease; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.is-busy .main-window { filter: saturate(.85); }

@media (max-width: 740px) {
  body { font-size: 13px; }
  .app-shell { padding-left: 8px; padding-right: 8px; }
  .two-col { grid-template-columns: 1fr; }
  .side-card { min-height: auto; }
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  .card-grid { grid-template-columns: 1fr; }
  .deck-grid { grid-template-columns: repeat(3, 1fr); }
  .main-window { min-height: calc(100dvh - 155px); }
  .resource-bar { grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 2px; }
  .resource-bar div { min-height: 34px; padding: 3px 1px 2px; }
  .resource-bar b { font-size: 11px; }
  .resource-bar small { font-size: 8px; }
  .universe-grid { grid-template-columns: 1fr; }
  .tab-row { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .tab-btn { font-size: 12px; padding-left: 4px; padding-right: 4px; }
}

@media (max-width: 380px) {
  .resource-bar { margin-top: 6px; padding: 2px; gap: 1px; }
  .resource-bar div { min-height: 31px; padding: 2px 1px; }
  .resource-bar b { font-size: 10px; }
  .resource-bar small { font-size: 7px; }
}

@media (min-width: 900px) {
  .card-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

.universe-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.universe-card { position: relative; min-height: 120px; padding: 10px; text-align: left; color: var(--cream); background: #1b1927; border: 2px solid #0a0810; box-shadow: inset 0 0 0 2px #4b3d4a; overflow: hidden; }
.universe-card:before { content: ""; position: absolute; inset: 0; pointer-events: none; background: repeating-linear-gradient(135deg, rgba(255,255,255,.035) 0 2px, transparent 2px 12px); }
.universe-card > * { position: relative; }
.universe-card .u-emoji { display: grid; place-items: center; width: 42px; height: 42px; margin-bottom: 7px; background: #0f0d18; border: 2px solid #332b3c; font-size: 22px; }
.universe-card b { display: block; color: var(--gold); font-size: 14px; }
.universe-card small { display: block; color: var(--muted); line-height: 1.35; margin-top: 4px; }
.universe-card .u-progress { margin-top: 8px; color: var(--blue); font-size: 11px; }
.universe-card.active { box-shadow: inset 0 0 0 2px var(--gold), 0 0 18px rgba(245,197,104,.13); }
.universe-card:not(:disabled) { cursor: pointer; }
.universe-card:not(:disabled):focus-visible, .universe-card:not(:disabled):hover { box-shadow: inset 0 0 0 2px var(--gold), 0 0 20px rgba(245,197,104,.18); outline: none; }
.universe-card:disabled { opacity: .65; }
.universe-note { padding: 10px; background: #211d2f; border: 2px solid #0a0810; box-shadow: inset 0 0 0 2px #3e344a; }
.side-avatar-row { display: flex; gap: 8px; align-items: center; }
.side-avatar { flex: 0 0 34px; width: 34px; height: 34px; overflow: hidden; display: grid; place-items: center; background: #0f0d18; border: 2px solid #3e344a; color: var(--gold); }
.side-avatar img { width: 100%; height: 100%; object-fit: cover; }

@media (max-width: 740px) {
  .universe-grid { grid-template-columns: 1fr; }
  .player-avatar { flex-basis: 38px; width: 38px; height: 38px; }
}
