:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --fg: var(--tg-theme-text-color, #1b1b1f);
  --hint: var(--tg-theme-hint-color, #7d7f87);
  --link: var(--tg-theme-link-color, #2b7bd6);
  --accent: var(--tg-theme-button-color, #e8701a);
  --accent-fg: var(--tg-theme-button-text-color, #ffffff);
  --card: var(--tg-theme-secondary-bg-color, #f2f3f5);
  --sep: rgba(127, 127, 127, 0.25);
  color-scheme: light dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--fg); }
/* Safe areas: Telegram's insets for the device cut-outs and, in fullscreen,
   for its own overlay controls (content-safe-area); zero in the sheet. */
:root {
  --safe-top: calc(var(--tg-safe-area-inset-top, 0px) + var(--tg-content-safe-area-inset-top, 0px));
  --safe-right: calc(var(--tg-safe-area-inset-right, 0px) + var(--tg-content-safe-area-inset-right, 0px));
  --safe-bottom: calc(var(--tg-safe-area-inset-bottom, 0px) + var(--tg-content-safe-area-inset-bottom, 0px));
  --safe-left: calc(var(--tg-safe-area-inset-left, 0px) + var(--tg-content-safe-area-inset-left, 0px));
}
body {
  font: 16px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  padding: calc(12px + var(--safe-top)) calc(14px + var(--safe-right)) calc(24px + var(--safe-bottom) + env(safe-area-inset-bottom)) calc(14px + var(--safe-left));
  -webkit-text-size-adjust: 100%;
}
/* With the top bar showing, it carries the top inset itself. */
body:has(.topbar:not([hidden])) { padding-top: 0; }
a { color: var(--link); }
[hidden] { display: none !important; }
.status { margin: 8px 0 14px; padding: 10px 12px; border-radius: 10px; background: var(--card); color: var(--hint); }
.home-title { font-size: 22px; margin: 6px 0 14px; }
.apps { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.app-link {
  display: flex; align-items: center; gap: 12px; padding: 14px; border-radius: 12px;
  background: var(--card); color: var(--fg); text-decoration: none; font-weight: 600;
}
.app-icon { font-size: 22px; width: 28px; text-align: center; }
.empty { color: var(--hint); }
/* The reading surface: a shown document is always light, whatever Telegram's
   theme; shell.js sets html.paper while one is open. */
html.paper {
  --bg: #ffffff;
  --fg: #1f2328;
  --hint: #656d76;
  --link: #0b6b74;
  --card: #f3f5f6;
  --sep: rgba(31, 35, 40, 0.12);
  color-scheme: light;
}
/* The in-app top bar: the primary "where am I / go up" control. */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 6px;
  margin: 0 calc(-14px - var(--safe-right)) 10px calc(-14px - var(--safe-left));
  padding: calc(6px + var(--safe-top)) calc(10px + var(--safe-right)) 6px calc(8px + var(--safe-left));
  background: var(--bg); border-bottom: 1px solid var(--sep);
  -webkit-user-select: none; user-select: none;
}
.tb-up {
  flex: none; width: 34px; height: 34px; border: 0; border-radius: 8px; padding: 0;
  background: transparent; color: var(--link); font: 600 20px/1 system-ui, sans-serif; cursor: pointer;
}
.tb-up:active { background: var(--card); }
.tb-crumbs { flex: 1; min-width: 0; display: flex; align-items: center; gap: 4px; overflow: hidden; white-space: nowrap; font-size: 14px; }
.tb-crumb { flex: none; border: 0; background: none; padding: 4px 2px; font: inherit; color: var(--link); cursor: pointer; }
span.tb-crumb { cursor: default; }
.tb-here { color: var(--fg); font-weight: 600; flex: 0 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.tb-sep { flex: none; color: var(--hint); }
/* The app's one action at the bar's end ("Open in browser"). */
.tb-action {
  flex: none; border: 1px solid var(--sep); border-radius: 8px; padding: 4px 10px; background: transparent;
  color: var(--link); font: 600 13px/1.3 system-ui, sans-serif; white-space: nowrap; cursor: pointer;
}
.tb-action:active { background: var(--card); }
.tb-action:disabled { opacity: .6; cursor: default; }
/* A second, icon-only action (download) and the menu it opens under the bar. */
.tb-icon { padding: 4px 9px; font-size: 16px; line-height: 1.3; }
.tb-menu {
  position: absolute; z-index: 25; right: calc(8px + var(--safe-right)); top: 100%; margin-top: 4px; min-width: 150px;
  display: grid; padding: 4px; border: 1px solid var(--sep); border-radius: 10px; background: var(--bg);
  box-shadow: 0 6px 24px rgba(0, 0, 0, .18);
}
.tb-menu[hidden] { display: none; }
.tb-menu-item {
  border: 0; border-radius: 7px; padding: 10px 12px; background: transparent; color: var(--fg);
  font: 500 15px/1.3 system-ui, sans-serif; text-align: left; cursor: pointer;
}
.tb-menu-item:active { background: var(--card); }
/* The left-edge swipe: a strip over an HTML frame (which swallows touches)
   and, while a swipe runs, the body following the finger over the underlay
   (a child of <html>, under the body) with a chip naming where back leads.
   With reduced motion the chip alone shows, over the page. */
.edge-strip { position: fixed; z-index: 30; top: 0; bottom: 0; left: 0; width: calc(20px + var(--safe-left)); background: transparent; }
html.swiping { overflow-x: hidden; }
html.swiping body { min-height: 100vh; box-shadow: -6px 0 18px rgba(0, 0, 0, .18); }
.edge-under { position: fixed; z-index: -1; inset: 0; background: var(--card); pointer-events: none; }
.edge-under::after { content: ""; position: absolute; inset: 0; background: #000; opacity: var(--eu-dim, .2); }
.eu-chip {
  position: absolute; z-index: 1; top: 50%; left: calc(12px + var(--safe-left)); transform: translateY(-50%);
  display: flex; align-items: center; gap: 6px; max-width: 60vw; padding: 6px 12px 6px 8px; border-radius: 16px;
  background: var(--bg); color: var(--link); font: 600 15px/1.3 system-ui, sans-serif; opacity: var(--eu-reveal, 0);
}
.eu-arrow { flex: none; font-size: 18px; transition: transform .12s ease-out; }
.eu-label { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.eu-label:empty { display: none; }
.edge-under.armed .eu-arrow { transform: translateX(-3px); }
.edge-under.still { z-index: 31; background: transparent; }
.edge-under.still::after { display: none; }
.edge-under.still .eu-chip { box-shadow: 0 1px 6px rgba(0, 0, 0, .2); }
.edge-under.still:not(.armed) .eu-chip { opacity: calc(var(--eu-reveal, 0) * .6); }
@media (prefers-reduced-motion: reduce) { .eu-arrow { transition: none; } }
