/* Telegram Web App theme: CSS variables come from Telegram.WebApp.themeParams (set in
   app.js), with dark fallbacks so the page looks right in a plain browser too. */

:root {
  --bg: var(--tg-theme-bg-color, #101418);
  --bg2: var(--tg-theme-secondary-bg-color, #1a2028);
  --text: var(--tg-theme-text-color, #e8ecf1);
  --hint: var(--tg-theme-hint-color, #8b95a3);
  --link: var(--tg-theme-link-color, #6ab0ff);
  --button: var(--tg-theme-button-color, #3b82f6);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --card: color-mix(in srgb, var(--bg2) 85%, #ffffff 15%);
  --line: color-mix(in srgb, var(--text) 12%, transparent);

  --c-common: #9aa3ad;
  --c-uncommon: #3ec46d;
  --c-rare: #3b82f6;
  --c-epic: #a855f7;
  --c-legendary: #f59e0b;

  --radius: 14px;
  --tab-h: 62px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

/* The `hidden` attribute must beat every `display:` rule below (.modal/.tabs are flex):
   without this the modal backdrop sat on top of the page at all times (reports #27/#28). */
[hidden] { display: none !important; }

/* Bottom inset: Telegram exposes its own safe-area vars (Bot API 8.0+); env() covers the
   rest. Both fall back to 0 so the page still works in a plain browser. */
:root {
  --inset-b: calc(var(--tg-safe-area-inset-bottom, 0px) + var(--tg-content-safe-area-inset-bottom, 0px) + env(safe-area-inset-bottom, 0px));
}

#app {
  padding: 12px 14px calc(var(--tab-h) + 32px + var(--inset-b));
  max-width: 560px;
  margin: 0 auto;
  min-height: var(--tg-viewport-stable-height, 100vh);
}

h1, h2, h3 { margin: 0 0 8px; font-weight: 700; }
h2 { font-size: 18px; }
h3 { font-size: 15px; color: var(--hint); text-transform: uppercase; letter-spacing: .04em; font-weight: 600; margin-top: 16px; }

.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 12px 14px;
  margin-bottom: 12px;
  border: 1px solid var(--line);
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: none;
  cursor: pointer;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 14px;
  border-radius: 12px;
  background: var(--button);
  color: var(--button-text);
  font-weight: 600;
  min-height: 42px;
  transition: transform .08s, opacity .15s;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.secondary { background: color-mix(in srgb, var(--text) 10%, transparent); color: var(--text); }
.btn.danger { background: #c0392b; color: #fff; }
.btn.block { width: 100%; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row > .btn { flex: 1; }

.loading { text-align: center; color: var(--hint); padding: 40px 0; }
.empty { color: var(--hint); text-align: center; padding: 18px 8px; }

/* ---------- tabs ---------- */
.tabs {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  height: calc(var(--tab-h) + var(--inset-b));
  padding-bottom: var(--inset-b);
  display: flex;
  background: color-mix(in srgb, var(--bg2) 92%, transparent);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
  z-index: 20;
}
.tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-size: 22px;
  color: var(--hint);
}
.tab span { font-size: 11px; font-weight: 600; }
.tab.active { color: var(--link); }

/* ---------- hero ---------- */
.hero-top {
  display: grid;
  grid-template-columns: 150px 1fr;
  gap: 12px;
  align-items: center;
}
@media (max-width: 380px) { .hero-top { grid-template-columns: 120px 1fr; } }

.avatar-wrap {
  width: 100%;
  aspect-ratio: 3 / 4;
  border-radius: var(--radius);
  background: radial-gradient(circle at 50% 30%, color-mix(in srgb, var(--link) 22%, transparent), transparent 70%), var(--bg2);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.avatar-wrap svg { width: 100%; height: 100%; }

.hero-name { font-size: 19px; font-weight: 800; margin-bottom: 2px; word-break: break-word; }
.hero-level { color: var(--hint); font-size: 13px; margin-bottom: 8px; }

.bar {
  height: 10px;
  border-radius: 6px;
  background: color-mix(in srgb, var(--text) 10%, transparent);
  overflow: hidden;
}
.bar > i {
  display: block;
  height: 100%;
  background: linear-gradient(90deg, var(--link), var(--button));
  border-radius: 6px;
  transition: width .4s ease;
}
.bar.small { height: 6px; }
.bar-label { display: flex; justify-content: space-between; font-size: 12px; color: var(--hint); margin-top: 3px; }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-top: 10px;
}
.stat {
  background: color-mix(in srgb, var(--text) 6%, transparent);
  border-radius: 10px;
  padding: 8px 6px;
  text-align: center;
}
.stat b { display: block; font-size: 17px; }
.stat span { font-size: 11px; color: var(--hint); }

.skill { display: grid; grid-template-columns: 26px 1fr auto; gap: 8px; align-items: center; padding: 6px 0; }
.skill .skill-name { font-weight: 600; font-size: 14px; }
.skill .skill-bonus { color: var(--c-uncommon); font-size: 12px; margin-left: 4px; }
.skill .skill-lvl { font-size: 13px; color: var(--hint); white-space: nowrap; }
.skill .bar { margin-top: 3px; }

.hint-line { color: var(--hint); font-size: 13px; margin-top: 8px; }

.equip-list { display: grid; grid-template-columns: 1fr; gap: 6px; }
.equip-row { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 10px; background: color-mix(in srgb, var(--text) 5%, transparent); }
.equip-row .slot-emoji { font-size: 18px; width: 24px; text-align: center; }
.equip-row .slot-name { color: var(--hint); font-size: 12px; width: 62px; }
.equip-row .item-name { flex: 1; font-size: 14px; font-weight: 600; word-break: break-word; }
.equip-row .item-name.empty-slot { color: var(--hint); font-weight: 400; }

/* ---------- appearance editor ---------- */
.appearance-panel { margin-top: 10px; }
.choice-row { display: flex; gap: 6px; flex-wrap: wrap; margin: 6px 0 10px; }
.choice {
  padding: 8px 12px;
  border-radius: 10px;
  background: color-mix(in srgb, var(--text) 8%, transparent);
  font-weight: 600;
  font-size: 14px;
  border: 2px solid transparent;
}
.choice.active { border-color: var(--link); background: color-mix(in srgb, var(--link) 18%, transparent); }
.swatch {
  width: 34px; height: 34px;
  border-radius: 50%;
  border: 3px solid transparent;
  box-shadow: 0 0 0 1px var(--line);
}
.swatch.active { border-color: var(--link); }

/* ---------- inventory ---------- */
.chests { display: flex; gap: 10px; overflow-x: auto; padding-bottom: 6px; scrollbar-width: none; }
.chests::-webkit-scrollbar { display: none; }
.chest {
  flex: 0 0 132px;
  background: var(--card);
  border-radius: var(--radius);
  border: 2px solid var(--rc, var(--c-common));
  padding: 10px;
  text-align: center;
  position: relative;
}
.chest .chest-emoji { font-size: 44px; line-height: 1; margin: 4px 0 6px; display: inline-block; filter: drop-shadow(0 0 8px var(--rc)); }
.chest .chest-rarity { font-size: 12px; font-weight: 700; color: var(--rc); margin-bottom: 8px; }
.chest .btn { padding: 8px 10px; min-height: 36px; font-size: 13px; width: 100%; }

.chest.shaking .chest-emoji { animation: shake .6s ease-in-out; }
.chest.flash::after {
  content: "";
  position: absolute;
  inset: -2px;
  border-radius: var(--radius);
  background: radial-gradient(circle, #fff 0%, var(--rc) 45%, transparent 75%);
  animation: flash .5s ease-out forwards;
  pointer-events: none;
}
@keyframes shake {
  0%, 100% { transform: translateX(0) rotate(0); }
  15% { transform: translateX(-5px) rotate(-6deg); }
  30% { transform: translateX(5px) rotate(6deg); }
  45% { transform: translateX(-6px) rotate(-8deg); }
  60% { transform: translateX(6px) rotate(8deg); }
  75% { transform: translateX(-3px) rotate(-3deg); }
  90% { transform: translateX(3px) rotate(3deg); }
}
@keyframes flash {
  0% { opacity: 0; transform: scale(.6); }
  30% { opacity: 1; transform: scale(1.15); }
  100% { opacity: 0; transform: scale(1.6); }
}

.items-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
@media (max-width: 340px) { .items-grid { grid-template-columns: repeat(2, 1fr); } }
.item {
  background: var(--card);
  border-radius: 12px;
  border: 2px solid var(--rc, var(--c-common));
  padding: 8px 6px 6px;
  text-align: center;
  position: relative;
  min-height: 96px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
}
.item .item-emoji { font-size: 30px; line-height: 1; filter: drop-shadow(0 0 6px var(--rc)); }
.item .item-name { font-size: 11px; font-weight: 600; line-height: 1.2; word-break: break-word; }
.item .item-power { font-size: 11px; color: var(--hint); }
.item .item-eq { position: absolute; top: 4px; right: 5px; font-size: 12px; }
.item.reveal { animation: pop .5s cubic-bezier(.2, 1.4, .4, 1); }
@keyframes pop {
  0% { transform: scale(.3); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

/* ---------- item modal ---------- */
.modal { position: fixed; inset: 0; z-index: 30; display: flex; align-items: flex-end; justify-content: center; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, .55); }
.modal-card {
  position: relative;
  width: 100%;
  max-width: 560px;
  background: var(--bg2);
  border-radius: 18px 18px 0 0;
  padding: 16px 16px calc(16px + var(--inset-b));
  animation: slideUp .22s ease-out;
  max-height: 85vh;
  overflow-y: auto;
}
@keyframes slideUp { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
.modal-title { font-size: 18px; font-weight: 800; word-break: break-word; }
.modal-sub { color: var(--rc, var(--hint)); font-weight: 700; font-size: 13px; margin-bottom: 10px; }
.modal-emoji { font-size: 56px; text-align: center; line-height: 1; margin: 6px 0 10px; filter: drop-shadow(0 0 12px var(--rc)); }
.kv { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.kv span:first-child { color: var(--hint); }
.modal .row { margin-top: 14px; }

/* ---------- arena ---------- */
.limits { display: flex; gap: 8px; }
.limits .stat { flex: 1; }
.opp { display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: center; }
.opp-name { font-weight: 700; font-size: 16px; word-break: break-word; }
.opp-meta { color: var(--hint); font-size: 12px; margin-bottom: 6px; }
.opp-stats { display: flex; gap: 8px; flex-wrap: wrap; font-size: 12px; color: var(--hint); }
.opp-stats b { color: var(--text); }
.win { text-align: center; min-width: 74px; }
.win b { display: block; font-size: 20px; }
.win span { font-size: 11px; color: var(--hint); }
.opp .btn { margin-top: 10px; grid-column: 1 / -1; }

/* ---------- toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tab-h) + 16px + var(--inset-b));
  transform: translateX(-50%);
  background: color-mix(in srgb, var(--text) 90%, transparent);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 12px;
  font-weight: 600;
  font-size: 14px;
  max-width: 90vw;
  z-index: 40;
  animation: slideUp .2s ease-out;
}

/* ---------- outside telegram ---------- */
.outside { display: flex; align-items: center; justify-content: center; min-height: 80vh; }
.outside-card { text-align: center; color: var(--hint); max-width: 300px; }
.outside-emoji { font-size: 64px; margin-bottom: 12px; }
.outside-card h2 { color: var(--text); }
