/* Telegram supplies the palette. Hard-coding colours here would make the app
   look wrong in half the clients and unreadable in some of them, so every
   colour is a theme variable with a sensible fallback for a browser that opened
   this page outside Telegram. */
:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #1a1a1a);
  --hint: var(--tg-theme-hint-color, #7a7a7a);
  --card: var(--tg-theme-secondary-bg-color, #f2f2f5);
  --accent: var(--tg-theme-button-color, #2f7fd6);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --link: var(--tg-theme-link-color, #2f7fd6);
  --danger: var(--tg-theme-destructive-text-color, #d2453f);

  /* Telegram Desktop can open a Mini App in a window far wider than a phone,
     and a 1200px line of Russian is unreadable. Everything shares one column
     width; the bars keep a full-width background and pad into it. */
  --column: 520px;
  --gutter: max(16px, calc((100% - var(--column)) / 2));
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* --- the header ---------------------------------------------------------- */

#bar {
  position: sticky;
  top: 0;
  z-index: 3;
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg);
  padding: 10px var(--gutter);
  border-bottom: 1px solid var(--card);
}

/* Which child the app is currently about, and the way to change it. It is the
   one piece of state every screen depends on, so it is on every screen. */
#child {
  flex: 1;
  min-width: 0;
  text-align: left;
  border: 0;
  border-radius: 999px;
  padding: 7px 14px;
  background: var(--card);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}

#child[data-empty="true"] {
  color: var(--hint);
  font-weight: 400;
}

#back {
  flex: 1;
  text-align: left;
}

#back,
#more {
  border: 0;
  background: none;
  color: var(--link);
  font-size: 15px;
  padding: 7px 4px;
  cursor: pointer;
}

#more {
  letter-spacing: 2px;
}

/* A request in flight, said quietly. A spinner in the middle of the screen for
   a query against a local database is theatre; a hairline is enough to explain
   why nothing has changed yet. */
#bar::after {
  content: "";
  position: absolute;
  left: 0;
  bottom: -1px;
  height: 2px;
  width: 0;
  background: var(--accent);
  transition: width 0.4s ease-out;
}

body[data-busy="true"] #bar::after {
  width: 70%;
}

/* --- screens ------------------------------------------------------------- */

#screens {
  flex: 1;
  width: 100%;
  display: flex;
  flex-direction: column;
}

.screen {
  flex: 1;
  width: 100%;
  max-width: var(--column);
  margin-inline: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.card {
  background: var(--card);
  border-radius: 14px;
  padding: 14px 16px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* What the parent said, echoed back so a screen that took a sentence reads as
   an exchange rather than as a form that swallowed it. */
.card.mine {
  background: var(--accent);
  color: var(--accent-text);
  align-self: flex-end;
  max-width: 85%;
}

.buttons {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 12px;
}

/* A row the handler put side by side stays side by side — yes and no, the two
   feedback taps, four delivery hours. */
.buttons.row {
  flex-direction: row;
  flex-wrap: wrap;
}

.buttons.row button {
  flex: 1 1 0;
  min-width: 72px;
}

.buttons button {
  width: 100%;
  border: 0;
  border-radius: 10px;
  padding: 12px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 15px;
  cursor: pointer;
}

.buttons button:disabled {
  opacity: 0.5;
}

.file {
  display: inline-block;
  margin-top: 12px;
  color: var(--link);
  text-decoration: underline;
  cursor: pointer;
}

.notice {
  color: var(--hint);
  font-size: 14px;
  text-align: center;
  padding: 24px 16px;
}

/* --- screens that want to look like themselves --------------------------- */

/* Presentation only, keyed by which command the screen is. Nothing below adds a
   word, a rule or a decision — the text and the buttons are the handlers' own.
   It is the difference between five features sharing one scroll and five
   screens, which is the whole reason this is a screen-based app. */

/* Sleep opens on a single answer to a single question, and it is usually read
   at arm's length in the dark. */
.screen[data-command="sleep"] > .card:first-child {
  font-size: 18px;
  line-height: 1.45;
  padding: 18px;
}

.screen[data-command="sleep"] > .card:first-child .buttons button {
  padding: 15px;
  font-size: 16px;
}

/* Pickers: a wall of one-per-line buttons is a list nobody reads to the end.
   Topics, situations and countries are short labels and belong in a grid. */
.screen[data-command="advice"] .buttons,
.screen[data-command="behaviour"] .buttons,
.screen[data-command="country"] .buttons,
.screen[data-command="daily"] .buttons {
  flex-direction: row;
  flex-wrap: wrap;
}

.screen[data-command="advice"] .buttons button,
.screen[data-command="behaviour"] .buttons button,
.screen[data-command="country"] .buttons button,
.screen[data-command="daily"] .buttons button {
  width: auto;
  flex: 1 1 40%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--accent);
}

/* Documents. The policy and the command list are the two things here that are
   read rather than tapped, and a chat bubble is the wrong shape for a page of
   prose. */
.screen[data-command="privacy"] > .card,
.screen[data-command="help"] > .card {
  background: none;
  padding: 0 2px;
}

.screen[data-command="privacy"] > .card + .card,
.screen[data-command="help"] > .card + .card {
  border-top: 1px solid var(--card);
  padding-top: 14px;
}

/* Erasure is the one button here that cannot be undone, and it should not look
   like the one that logs a nap. */
.screen[data-command="delete"] .buttons button {
  background: var(--danger);
}

/* --- the composer -------------------------------------------------------- */

#ask {
  position: sticky;
  bottom: 0;
  z-index: 2;
  display: flex;
  gap: 8px;
  padding: 10px var(--gutter);
  background: var(--bg);
  border-top: 1px solid var(--card);
}

#text {
  flex: 1;
  min-width: 0;
  border: 0;
  border-radius: 999px;
  padding: 11px 16px;
  background: var(--card);
  color: var(--text);
  font-size: 16px; /* under 16px, iOS zooms the whole page on focus */
}

#send {
  border: 0;
  border-radius: 50%;
  width: 42px;
  height: 42px;
  flex: 0 0 auto;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 18px;
  cursor: pointer;
}

#send:disabled {
  opacity: 0.5;
}

/* --- the tab bar --------------------------------------------------------- */

/* The child-scoped features, always one tap away, in the place a phone puts
   navigation: at the bottom, where a thumb already is. */
#tabs {
  position: sticky;
  bottom: 0;
  z-index: 2;
  display: flex;
  background: var(--bg);
  border-top: 1px solid var(--card);
  padding: 6px var(--gutter) calc(6px + env(safe-area-inset-bottom));
}

#tabs button {
  flex: 1 1 0;
  min-width: 0;
  border: 0;
  background: none;
  color: var(--hint);
  font-size: 12px;
  line-height: 1.2;
  padding: 6px 2px;
  border-radius: 8px;
  cursor: pointer;
  /* One line each, whatever the language. Without nowrap a label that does not
     fit wraps instead of clipping, and one two-line tab makes the whole bar
     taller than the other four. "Behaviour" is the longest today and fits; the
     next locale added is the one this is for. */
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#tabs button[aria-current="true"] {
  color: var(--accent);
  font-weight: 600;
}

/* --- the settings sheet -------------------------------------------------- */

#sheet {
  position: fixed;
  inset: 0;
  z-index: 4;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}

#sheet[hidden] {
  display: none;
}

#sheet-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
}

#sheet-body {
  position: relative;
  background: var(--bg);
  border-radius: 16px 16px 0 0;
  padding: 8px var(--gutter) calc(16px + env(safe-area-inset-bottom));
  max-height: 80vh;
  overflow-y: auto;
}

#sheet-title {
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--hint);
  margin: 8px 2px 12px;
}

#settings {
  display: flex;
  flex-direction: column;
}

#settings button {
  border: 0;
  background: none;
  color: var(--text);
  font-size: 16px;
  text-align: left;
  padding: 14px 2px;
  cursor: pointer;
}

#settings button + button {
  border-top: 1px solid var(--card);
}

/* Same reasoning as the delete screen: the item that wipes everything should
   not be the same colour as the one that shows the policy. */
#settings button[data-command="delete"] {
  color: var(--danger);
}
