:root {
  --bg: var(--tg-theme-bg-color, #12151c);
  --card: var(--tg-theme-secondary-bg-color, #1b1f2a);
  --fg: var(--tg-theme-text-color, #eef1f7);
  --dim: var(--tg-theme-hint-color, #8b93a7);
  --accent: var(--tg-theme-button-color, #4c8dff);
  --accent-fg: var(--tg-theme-button-text-color, #fff);
  --danger: #e2504a;
  --good: #3fb960;
  --line: rgba(255, 255, 255, .08);
  --radius: 14px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--bg); color: var(--fg);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", Roboto, sans-serif;
  overscroll-behavior: none;
}

#app {
  display: flex; flex-direction: column; height: 100%;
  padding: var(--safe-top, env(safe-area-inset-top)) 12px env(safe-area-inset-bottom);
  gap: 8px;
}

/* ---------- top bar ---------- */

#bar { padding-top: 8px; flex: none; }

/* wrap rather than scroll: a stat you cannot see is a stat you do not have */
#chips { display: flex; flex-wrap: wrap; gap: 6px; }

.chip {
  flex: none; display: flex; align-items: center; gap: 5px;
  background: var(--card); border: 1px solid var(--line);
  border-radius: 999px; padding: 5px 10px; font-size: 12.5px;
  font-variant-numeric: tabular-nums; cursor: pointer;
  transition: transform .12s, background .12s;
}
.chip:active { transform: scale(.94); background: var(--accent); color: var(--accent-fg); }
.chip .ico { font-size: 14px; }
.chip .bar {
  width: 34px; height: 4px; border-radius: 2px;
  background: rgba(255,255,255,.14); overflow: hidden;
}
.chip .bar i { display: block; height: 100%; border-radius: 2px; }

#where { margin-top: 6px; font-size: 12px; color: var(--dim); }

/* ---------- stage ---------- */

#stage { flex: 1 1 auto; position: relative; min-height: 0; }

/* Taken out of flow on purpose. As a flex item the canvas contributes its own
   bitmap size to the stage's content size, so growing the bitmap grows the
   stage, which grows the bitmap - and layout() feeds itself. Absolute means
   the stage is sized by the column flex alone, and JS pins the canvas to it. */
#scene { position: absolute; left: 0; top: 0; display: block; touch-action: manipulation; }

#hint {
  position: absolute; left: 0; right: 0; bottom: 6px; text-align: center;
  font-size: 12px; color: var(--dim); pointer-events: none;
}

/* ---------- journal ---------- */

#journal {
  flex: none; height: 108px; overflow-y: auto; overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  font-size: 12.5px; color: var(--dim); line-height: 1.45;
  border-left: 2px solid var(--line); padding-left: 9px;
}
/* a fixed height, not max-height: the box must not change size as lines
   arrive, or the stage resizes under the player mid-walk */
#journal div { overflow-wrap: anywhere; }
#journal div:last-child { color: var(--fg); }

#journal::-webkit-scrollbar { width: 4px; }
#journal::-webkit-scrollbar-thumb { background: var(--line); border-radius: 2px; }

/* ---------- action bar ---------- */

#actions {
  flex: none; display: grid; gap: 7px; padding-bottom: 10px;
  grid-template-columns: repeat(4, 1fr);
}
#actions.wide { grid-template-columns: repeat(3, 1fr); }

.act {
  background: var(--card); border: 1px solid var(--line); color: var(--fg);
  border-radius: var(--radius); padding: 11px 4px; font-size: 13px;
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  cursor: pointer; transition: transform .1s, opacity .1s;
}
.act:active { transform: scale(.95); }
.act[disabled] { opacity: .35; }
.act .glyph { font-size: 19px; line-height: 1; }
.act.primary { background: var(--accent); color: var(--accent-fg); border-color: transparent; }
.act.danger  { background: var(--danger); color: #fff; border-color: transparent; }
.act.good    { background: var(--good); color: #06240f; border-color: transparent; }

/* ---------- overlays ---------- */

.overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.55);
  display: flex; align-items: flex-end; justify-content: center;
  z-index: 40; animation: fade .15s ease;
}
.overlay.hidden { display: none; }
.card {
  background: var(--card); width: 100%; max-width: 520px;
  border-radius: 20px 20px 0 0; padding: 18px 18px calc(18px + env(safe-area-inset-bottom));
  animation: rise .2s cubic-bezier(.2,.9,.3,1);
}
@keyframes fade { from { opacity: 0 } }
@keyframes rise { from { transform: translateY(24px); opacity: .4 } }

.card h2 { margin: 0 0 4px; font-size: 19px; display: flex; align-items: center; gap: 8px; }
.card p  { margin: 6px 0; color: var(--dim); font-size: 13.5px; }
.card .row { display: flex; gap: 8px; margin-top: 14px; }
.card .row .act { flex: 1; }
.card dl { margin: 12px 0 0; display: grid; grid-template-columns: auto 1fr; gap: 5px 12px; font-size: 13.5px; }
.card dt { color: var(--dim); }
.card dd { margin: 0; font-variant-numeric: tabular-nums; }

/* ---------- bag and gear ---------- */

/* One colour per rarity, used on slots, on the worn row and on the item card,
   so a Fabled find reads the same everywhere it appears. */
.r0 { color: var(--fg); }
.r1 { color: #5fd1a0; }
.r2 { color: #5aa9ff; }
.r3 { color: #e0a13a; }

.card .dimly { color: var(--dim); font-size: 13px; font-weight: normal; }

.gear { display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px; margin: 12px 0 4px; }
.gear-slot {
  background: var(--bg); border: 1px solid var(--line); border-radius: 12px;
  padding: 8px 6px; text-align: center; min-height: 64px;
  display: flex; flex-direction: column; gap: 2px; justify-content: center;
}
.gear-slot[onclick] { cursor: pointer; }
.gs-e { font-size: 20px; line-height: 1; }
.gs-n { font-size: 11.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.gs-b { font-size: 10.5px; color: var(--dim); }

.slots { display: grid; grid-template-columns: repeat(8, 1fr); gap: 6px; margin-top: 10px; }
.slot {
  aspect-ratio: 1; border-radius: 11px; border: 1px solid var(--line);
  background: var(--bg); position: relative; padding: 0;
  display: flex; align-items: center; justify-content: center;
}
.slot.empty { opacity: .45; }
.slot .sl-e { font-size: 19px; line-height: 1; }
.slot .sl-q {
  position: absolute; right: 2px; bottom: 1px; font-size: 10px;
  color: var(--fg); background: rgba(0,0,0,.6); border-radius: 6px; padding: 0 3px;
}
.slot.r1 { border-color: #5fd1a0; }
.slot.r2 { border-color: #5aa9ff; }
.slot.r3 { border-color: #e0a13a; box-shadow: 0 0 7px rgba(224,161,58,.45); }

@media (max-width: 380px) {
  .slots { grid-template-columns: repeat(6, 1fr); }
}

/* ---------- village shop ---------- */

.goods { display: flex; flex-direction: column; gap: 7px; margin-top: 10px; }
.good {
  display: flex; align-items: center; gap: 10px;
  background: var(--bg); border: 1px solid var(--line);
  border-radius: 12px; padding: 8px 10px;
}
.good.gone { opacity: .4; }
.gd-e { font-size: 22px; line-height: 1; }
.gd-n { flex: 1; font-size: 13.5px; display: flex; flex-direction: column; }
.gd-n small { color: var(--dim); font-size: 11.5px; }
.good .act { flex: none; width: auto; padding: 7px 12px; font-size: 13px; }

/* ---------- toast ---------- */

/* Inside the stage, in the empty band the letterboxed board leaves above the
   hint and the journal. At the top it covered the stat chips; pinned to the
   viewport bottom it covered the action bar. Here it covers nothing. */
#toast {
  position: absolute; left: 50%; bottom: 30px;
  transform: translate(-50%, 16px); z-index: 60;
  background: rgba(0,0,0,.82); color: #fff; padding: 9px 15px;
  border-radius: 999px; font-size: 13.5px; opacity: 0;
  transition: opacity .18s, transform .18s; pointer-events: none;
  max-width: 88vw; text-align: center;
}
#toast.on { opacity: 1; transform: translate(-50%, 0); }

/* ---------- boot ---------- */

#boot {
  position: fixed; inset: 0; background: var(--bg); z-index: 90;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px; color: var(--dim);
}
#boot.gone { display: none; }
.spinner {
  width: 30px; height: 30px; border-radius: 50%;
  border: 3px solid var(--line); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg) } }

/* a line on a card that is a warning, not a description */
.card p.warn {
  color: #f0b45a; background: rgba(240,180,90,.08);
  border-left: 2px solid #f0b45a; padding: 7px 9px; border-radius: 0 8px 8px 0;
}
