/*
 * pbv8 Mini App -- one stylesheet, mobile first.
 *
 * THE PALETTE IS TELEGRAM'S, WITH A FALLBACK FOR EVERY COLOUR. app.js
 * copies `themeParams` into `--tg-theme-*` custom properties and does it
 * again on every `themeChanged`; the local `--*` names below read those
 * with a fallback, so if the Telegram SDK never loads -- no network, an old
 * client, a browser opened by hand -- the page is still readable rather
 * than black on black. A fallback-less `var(--tg-theme-*)` is a blank page
 * in exactly the case where the operator most needs to see a badge, and a
 * test greps for one.
 *
 * MOBILE FIRST, LITERALLY: the base rules are the phone's, and the only
 * media query widens the column on a big screen. There is no max-width
 * query anywhere, because there is no desktop layout being narrowed down.
 *
 * THE LOUD STATES ARE NOT DECORATION. `.emergency`, `.badge-alarm` and
 * `.row-alarm` render the one condition that means a live trader is running
 * while pbv8 has it marked halted, and `.btn-panic` is the button that
 * stops it. They are deliberately the highest-contrast things on the page,
 * and a test asserts nothing here ever hides them.
 */

:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #10171f);
  --hint: var(--tg-theme-hint-color, #707579);
  --link: var(--tg-theme-link-color, #2481cc);
  --accent: var(--tg-theme-button-color, #2481cc);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --panel: var(--tg-theme-secondary-bg-color, #f2f3f5);
  --danger: var(--tg-theme-destructive-text-color, #d1453b);
  --line: rgba(128, 128, 128, 0.28);
  --radius: 12px;
  --gap: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: var(--tg-theme-bg-color, #17212b);
    --text: var(--tg-theme-text-color, #f5f5f5);
    --hint: var(--tg-theme-hint-color, #8b9aa9);
    --link: var(--tg-theme-link-color, #6ab3f3);
    --accent: var(--tg-theme-button-color, #5288c1);
    --accent-text: var(--tg-theme-button-text-color, #ffffff);
    --panel: var(--tg-theme-secondary-bg-color, #232e3c);
    --danger: var(--tg-theme-destructive-text-color, #ec5f5f);
  }
}

/* The shell shows and hides with the `hidden` attribute. Without this rule
   a `display:` declaration below would beat the attribute's default and
   leave the confirm sheet permanently across the money buttons. */
[hidden] {
  display: none !important;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0 var(--gap) calc(var(--gap) * 4);
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--bg);
  padding: var(--gap) 0 8px;
  border-bottom: 1px solid var(--line);
}

.brand {
  margin: 0 0 8px;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--hint);
}

.tabs {
  display: flex;
  gap: 8px;
}

.tab {
  flex: 1 1 0;
  padding: 9px 6px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--text);
  font: inherit;
  font-size: 15px;
  cursor: pointer;
}

.tab-on {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}

.screen {
  display: block;
  padding-top: var(--gap);
}

.screen-title {
  margin: 4px 0 var(--gap);
  font-size: 18px;
}

.card {
  background: var(--panel);
  border-radius: var(--radius);
  padding: var(--gap);
  margin-bottom: var(--gap);
}

.card-title {
  margin: 0 0 8px;
  font-size: 16px;
  word-break: break-word;
}

.field {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  margin: 4px 0;
  font-size: 15px;
}

.field-label {
  color: var(--hint);
  flex: 0 0 auto;
}

.field-value {
  text-align: right;
  word-break: break-word;
}

.notice,
.empty,
.muted,
.unknown {
  margin: 6px 0;
  color: var(--text);
  font-size: 15px;
  white-space: pre-wrap;
}

.empty,
.muted {
  color: var(--hint);
}

.unknown {
  color: var(--hint);
  font-style: italic;
}

.mono {
  margin: 4px 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
  color: var(--hint);
  white-space: pre-wrap;
  word-break: break-word;
}

/* -- the instance list ---------------------------------------------------- */

.list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.row {
  margin-bottom: 10px;
}

.row-btn {
  display: flex;
  flex-direction: column;
  gap: 3px;
  width: 100%;
  padding: var(--gap);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.row-title {
  font-weight: 600;
}

.row-badge,
.badge {
  font-size: 14px;
  color: var(--hint);
  white-space: pre-wrap;
}

.row-meta {
  font-size: 13px;
  color: var(--hint);
  word-break: break-word;
}

.badge {
  margin: 0 0 8px;
}

/* -- the loud states ------------------------------------------------------ */

/* A live trader that pbv8 has marked halted. Loudest thing on the page. */
.emergency {
  margin: 0 0 var(--gap);
  padding: var(--gap);
  border: 2px solid var(--danger);
  border-left-width: 8px;
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--danger);
  font-weight: 700;
  font-size: 16px;
  white-space: pre-wrap;
}

.badge-alarm {
  color: var(--danger);
  font-weight: 700;
}

.row-alarm .row-btn {
  border: 2px solid var(--danger);
}

/* -- snapshot freshness --------------------------------------------------- */

.freshness {
  margin: 8px 0 0;
  font-size: 13px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.fresh-ok {
  color: var(--hint);
}

/* stale, unknown and skewed all land here: anything not provably fresh is
   said out loud, never quietly. */
.fresh-warn {
  color: var(--danger);
  font-weight: 700;
}

.positions {
  list-style: none;
  margin: 10px 0 0;
  padding: 0;
}

.position {
  padding: 8px 0;
  border-top: 1px solid var(--line);
}

.position-head {
  display: block;
  font-weight: 600;
}

/* -- buttons -------------------------------------------------------------- */

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: var(--gap);
}

.btn {
  flex: 1 1 45%;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--accent-text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.btn-quiet {
  background: var(--panel);
  color: var(--text);
  border-color: var(--line);
  font-weight: 500;
}

.back {
  flex: 0 0 auto;
  min-height: 36px;
  margin-bottom: var(--gap);
}

.btn-stop,
.btn-close {
  background: var(--panel);
  color: var(--text);
  border-color: var(--line);
}

/* PANIC is the emergency halt and must never read as one more blue button:
   it is the only filled danger-coloured control on the page, and it is only
   ever reachable from behind the confirm sheet. */
.btn-panic {
  flex: 1 1 100%;
  background: var(--danger);
  border-color: var(--danger);
  color: #ffffff;
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

/* -- the result banner ---------------------------------------------------- */

.result {
  position: fixed;
  left: var(--gap);
  right: var(--gap);
  bottom: var(--gap);
  z-index: 3;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: var(--gap);
  border-radius: var(--radius);
  border: 2px solid var(--line);
  background: var(--panel);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.28);
  max-height: 60vh;
  overflow-y: auto;
}

.result-ok {
  border-color: var(--accent);
}

.result-bad {
  border-color: var(--danger);
}

.result-message {
  margin: 0;
  font-size: 15px;
  white-space: pre-wrap;
  word-break: break-word;
}

/* -- the confirm sheet ---------------------------------------------------- */

.sheet {
  position: fixed;
  inset: 0;
  z-index: 4;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(0, 0, 0, 0.55);
}

.sheet-card {
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  padding: var(--gap);
  border-radius: var(--radius) var(--radius) 0 0;
  background: var(--bg);
  border-top: 2px solid var(--line);
}

.sheet-panic {
  border-top: 6px solid var(--danger);
}

.sheet-message {
  margin: 0 0 var(--gap);
  font-size: 15px;
  white-space: pre-wrap;
  word-break: break-word;
}

.sheet-actions {
  display: flex;
  gap: 8px;
}

/* A wider screen gets a centred column -- the phone layout is the base and
   is never narrowed down from a desktop one. */
@media (min-width: 560px) {
  body {
    max-width: 34rem;
    margin: 0 auto;
  }

  .result {
    left: auto;
    right: auto;
    width: 32rem;
  }
}
