/* webapp/static/app.css — палитра Telegram, один стиль на Mini App и сайт.
 *
 * Токены объявлены ТРИЖДЫ и в таком порядке:
 *   :root                                  — светлая по умолчанию
 *   @media (prefers-color-scheme: dark)    — системная тёмная
 *   :root[data-theme="dark"]               — явная тёмная
 *
 * Внутри Telegram app.js переписывает --tg-* из themeParams прямо на
 * :root — пользовательская тема клиента (их там десятки) побеждает обе
 * наши. Поэтому ни один цвет не задан напрямую: только через переменную,
 * иначе часть интерфейса осталась бы в нашей палитре, а часть — в его.
 */

:root {
  --bg:          #ffffff;
  --bg-elev:     #f4f4f5;
  --bg-sunken:   #ebedf0;
  --surface:     #ffffff;
  --text:        #0f0f0f;
  --text-dim:    #707579;
  --hint:        #8d969c;
  --accent:      #2481cc;
  --accent-text: #ffffff;
  --link:        #2481cc;
  --border:      #e4e6ea;
  --ok:          #1f9d55;
  --warn:        #c8860d;
  --danger:      #d64541;
  --shadow:      0 1px 2px rgba(16, 24, 40, .06), 0 4px 16px rgba(16, 24, 40, .05);

  --radius:      14px;
  --radius-sm:   10px;
  --gap:         14px;
  --sidebar-w:   248px;
  --topbar-h:    56px;
  --tabbar-h:    58px;

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
          "Helvetica Neue", Arial, "Noto Sans", sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas,
          "Liberation Mono", monospace;

  /* Отступы под вырезы и жест-бары. На десктопе env() даёт 0. */
  --safe-top:    env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left:   env(safe-area-inset-left, 0px);
  --safe-right:  env(safe-area-inset-right, 0px);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg:        #17212b;
    --bg-elev:   #232e3c;
    --bg-sunken: #0e1621;
    --surface:   #1d2733;
    --text:      #ffffff;
    --text-dim:  #aab8c2;
    --hint:      #708499;
    --accent:    #5288c1;
    --link:      #6ab3f3;
    --border:    #2b3a4a;
    --ok:        #4dcb6b;
    --warn:      #e3a84a;
    --danger:    #ec6a5e;
    --shadow:    0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .28);
  }
}

:root[data-theme="dark"] {
  --bg:        #17212b;
  --bg-elev:   #232e3c;
  --bg-sunken: #0e1621;
  --surface:   #1d2733;
  --text:      #ffffff;
  --text-dim:  #aab8c2;
  --hint:      #708499;
  --accent:    #5288c1;
  --link:      #6ab3f3;
  --border:    #2b3a4a;
  --ok:        #4dcb6b;
  --warn:      #e3a84a;
  --danger:    #ec6a5e;
  --shadow:    0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .28);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  /* Горизонтальной прокрутки у страницы нет никогда: широкое (таблицы,
     длинные строки) скроллится внутри своего контейнера. */
  overflow-x: hidden;
}

body { overscroll-behavior-y: none; }

[hidden] { display: none !important; }

a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }

.muted { color: var(--text-dim); }
.mono  { font-family: var(--mono); font-size: .92em; }

/* =========================================================
   Splash
   ========================================================= */
.splash {
  position: fixed;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 12px;
  justify-items: center;
  background: var(--bg);
  z-index: 50;
}
.splash-mark { font-size: 40px; color: var(--accent); animation: pulse 1.4s ease-in-out infinite; }
.splash-text { color: var(--text-dim); font-size: 14px; }

@keyframes pulse { 0%, 100% { opacity: .35; } 50% { opacity: 1; } }

@media (prefers-reduced-motion: reduce) {
  .splash-mark, .spinner { animation: none; }
}

/* =========================================================
   Вход
   ========================================================= */
.login, .gate {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: calc(24px + var(--safe-top)) max(16px, var(--safe-left))
           calc(24px + var(--safe-bottom)) max(16px, var(--safe-right));
  background: var(--bg-sunken);
}

.login-card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px 22px 22px;
}
.login-mark { font-size: 34px; color: var(--accent); line-height: 1; }
.login-card h1 { margin: 14px 0 6px; font-size: 21px; font-weight: 600; }
.login-card p { margin: 0 0 18px; font-size: 14px; }

.tabs {
  display: flex;
  gap: 4px;
  padding: 3px;
  background: var(--bg-sunken);
  border-radius: var(--radius-sm);
  margin-bottom: 18px;
}
.tab {
  flex: 1;
  padding: 9px 10px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--text-dim);
  font: 600 13px/1 var(--font);
  cursor: pointer;
}
.tab.is-active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.field { display: block; margin-bottom: 14px; }
.field > span {
  display: block;
  margin-bottom: 6px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: .02em;
  text-transform: uppercase;
  color: var(--hint);
}
.field input {
  width: 100%;
  padding: 12px 13px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  /* 16px — порог, ниже которого Safari на iOS зумит страницу при фокусе
     и ломает вёрстку до конца сессии. */
  font: 16px/1.3 var(--font);
}
.field input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent);
}
.code-input {
  text-align: center;
  letter-spacing: .5em;
  font-family: var(--mono) !important;
  font-size: 22px !important;
  padding-left: .5em !important;
}

.btn {
  display: block;
  width: 100%;
  padding: 13px 16px;
  margin-bottom: 10px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font: 600 15px/1.2 var(--font);
  text-align: center;
  cursor: pointer;
}
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-primary:hover:not(:disabled) { filter: brightness(1.08); }
.btn-ghost {
  background: transparent;
  color: var(--link);
  border-color: var(--border);
}
.btn-ghost:hover:not(:disabled) { background: var(--bg-elev); }
.btn-sm { width: auto; padding: 7px 12px; font-size: 13px; margin: 0; }
.btn-danger { background: var(--danger); color: #fff; }

.waiting { display: flex; align-items: center; gap: 9px; color: var(--text-dim); font-size: 14px; }
.spinner {
  width: 15px; height: 15px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.login-msg { margin: 14px 0 0 !important; font-size: 13px; min-height: 1.2em; }
.login-msg.is-error { color: var(--danger); }
.login-msg.is-ok    { color: var(--ok); }

/* =========================================================
   Каркас приложения
   Мобильный: одна колонка, навигация снизу.
   ≥900px: сайдбар слева, нижняя панель убирается.
   ========================================================= */
.app {
  display: grid;
  grid-template-areas: "topbar" "view" "tabbar";
  grid-template-rows: auto 1fr auto;
  grid-template-columns: 100%;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg-sunken);
}

.sidebar { display: none; }

.topbar {
  grid-area: topbar;
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  height: calc(var(--topbar-h) + var(--safe-top));
  padding: var(--safe-top) max(14px, var(--safe-right)) 0 max(14px, var(--safe-left));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid var(--border);
}
.topbar h1 {
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.topbar-right { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }

.chip {
  padding: 5px 10px;
  border-radius: 999px;
  background: var(--bg-elev);
  color: var(--text-dim);
  font: 600 12px/1 var(--font);
  white-space: nowrap;
}
.icon-btn {
  width: 34px; height: 34px;
  display: grid; place-items: center;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
  color: var(--text-dim);
  font-size: 16px;
  cursor: pointer;
}
.icon-btn:hover { color: var(--text); }
.icon-btn.is-busy { animation: spin .8s linear infinite; }

.view {
  grid-area: view;
  padding: var(--gap) max(14px, var(--safe-left)) var(--gap) max(14px, var(--safe-right));
  /* Запас под нижнюю панель: без него последняя карточка прячется под ней. */
  padding-bottom: calc(var(--tabbar-h) + var(--safe-bottom) + var(--gap));
  min-width: 0;
}

.tabbar {
  grid-area: tabbar;
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 25;
  display: flex;
  height: calc(var(--tabbar-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
  border-top: 1px solid var(--border);
}
.tabbar button {
  flex: 1;
  display: grid;
  place-items: center;
  gap: 2px;
  border: 0;
  background: transparent;
  color: var(--hint);
  font: 500 10px/1.2 var(--font);
  cursor: pointer;
  padding: 0 2px;
}
.tabbar button .ic { font-size: 19px; line-height: 1; }
.tabbar button.is-active { color: var(--accent); }

/* =========================================================
   Карточки и сетки
   ========================================================= */
.grid { display: grid; gap: var(--gap); grid-template-columns: 1fr; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  min-width: 0;
}
.card-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}
.card-head h2 { margin: 0; font-size: 15px; font-weight: 600; }
.card-head .more { font-size: 13px; }

.stat { display: grid; gap: 2px; }
.stat-value { font: 600 26px/1.1 var(--font); font-variant-numeric: tabular-nums; }
.stat-label { font-size: 12px; color: var(--hint); }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; font-size: 13px; }
.kv dt { color: var(--hint); }
.kv dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }

/* Строка-элемент списка: аккаунт, задача, транзакция */
.row {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 11px 0;
  border-bottom: 1px solid var(--border);
  min-width: 0;
}
.row:last-child { border-bottom: 0; }
.row-main { flex: 1; min-width: 0; }
.row-title {
  font-weight: 600;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.row-sub {
  font-size: 12.5px;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.row-actions { display: flex; gap: 6px; flex-shrink: 0; }

.dot {
  width: 9px; height: 9px;
  border-radius: 50%;
  flex-shrink: 0;
  background: var(--hint);
}
.dot.is-ok     { background: var(--ok); }
.dot.is-warn   { background: var(--warn); }
.dot.is-danger { background: var(--danger); }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--bg-elev);
  color: var(--text-dim);
  font: 600 11px/1.6 var(--font);
  white-space: nowrap;
}
.badge.is-ok     { background: color-mix(in srgb, var(--ok) 18%, transparent);     color: var(--ok); }
.badge.is-warn   { background: color-mix(in srgb, var(--warn) 20%, transparent);   color: var(--warn); }
.badge.is-danger { background: color-mix(in srgb, var(--danger) 18%, transparent); color: var(--danger); }
.badge.is-accent { background: color-mix(in srgb, var(--accent) 18%, transparent); color: var(--accent); }

.empty { padding: 28px 10px; text-align: center; color: var(--text-dim); font-size: 14px; }

/* Полоса активности: простая, без библиотек */
.bars { display: flex; align-items: flex-end; gap: 5px; height: 76px; }
.bars .bar {
  position: relative;
  flex: 1;
  min-width: 4px;
  min-height: 2px;
  background: var(--accent);
  border-radius: 3px 3px 0 0;
  opacity: .9;
  overflow: hidden;
}
/* Доля ошибок за день — красным сверху столбца. */
.bars .bar i {
  position: absolute;
  left: 0; right: 0; top: 0;
  background: var(--danger);
}
.bars-axis {
  display: flex;
  gap: 5px;
  margin-top: 6px;
  font-size: 10px;
  color: var(--hint);
}
.bars-axis span { flex: 1; text-align: center; }

/* Единственное место, где разрешена горизонтальная прокрутка */
.scroll-x { overflow-x: auto; -webkit-overflow-scrolling: touch; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 14px);
  transform: translateX(-50%);
  z-index: 60;
  max-width: calc(100vw - 32px);
  padding: 11px 16px;
  border-radius: 11px;
  background: var(--text);
  color: var(--bg);
  font-size: 13.5px;
  box-shadow: var(--shadow);
}
.toast.is-error { background: var(--danger); color: #fff; }

/* =========================================================
   Планшет: карточки в две колонки, навигация всё ещё снизу
   ========================================================= */
@media (min-width: 600px) {
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid .span-2 { grid-column: span 2; }
  .view { padding-left: max(20px, var(--safe-left)); padding-right: max(20px, var(--safe-right)); }
  .login-card { padding: 32px 28px 26px; }
}

/* =========================================================
   Ноутбук и выше: сайдбар вместо нижней панели
   ========================================================= */
@media (min-width: 900px) {
  .app {
    grid-template-areas: "sidebar topbar" "sidebar view";
    grid-template-rows: auto 1fr;
    grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  }

  .sidebar {
    grid-area: sidebar;
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
    height: 100vh;
    height: 100dvh;
    padding: calc(18px + var(--safe-top)) 12px calc(14px + var(--safe-bottom));
    background: var(--bg);
    border-right: 1px solid var(--border);
  }
  .brand { display: flex; align-items: center; gap: 9px; padding: 0 10px 18px; }
  .brand-mark { color: var(--accent); font-size: 20px; }
  .brand-name { font-weight: 600; font-size: 16px; }

  .nav { display: grid; gap: 2px; }
  .nav button {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 10px 11px;
    border: 0;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-dim);
    font: 500 14px/1.2 var(--font);
    text-align: left;
    cursor: pointer;
  }
  .nav button .ic { font-size: 16px; width: 20px; text-align: center; }
  .nav button:hover { background: var(--bg-elev); color: var(--text); }
  .nav button.is-active {
    background: color-mix(in srgb, var(--accent) 15%, transparent);
    color: var(--accent);
    font-weight: 600;
  }

  .sidebar-foot {
    margin-top: auto;
    padding: 12px 10px 0;
    border-top: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
  }
  .who { min-width: 0; }
  .who-name { font-size: 13px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .who-sub  { font-size: 11.5px; color: var(--hint); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  .tabbar { display: none; }
  .view {
    padding: 20px 24px 28px;
    padding-bottom: 28px;
  }
  .topbar { padding-left: 24px; padding-right: 24px; }
  .topbar h1 { font-size: 19px; }
  .toast { bottom: 24px; left: calc(50% + var(--sidebar-w) / 2); }
}

/* =========================================================
   Широкий десктоп: три колонки и ограниченная ширина контента,
   чтобы на 27" строки не растягивались на весь экран
   ========================================================= */
@media (min-width: 1280px) {
  .grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .grid .span-2 { grid-column: span 2; }
  .grid .span-3 { grid-column: span 3; }
  .view { padding: 24px 32px 36px; }
}

@media (min-width: 1680px) {
  .view > .grid { max-width: 1480px; }
}

/* Короткие экраны в ландшафте (телефон лёг на бок): срезаем вертикальные
   отступы, иначе карточка входа не влезает и теряет кнопку. */
@media (max-height: 520px) {
  .login, .gate { align-items: start; padding-top: 12px; }
  .login-card { padding: 18px 18px 16px; }
  .login-card h1 { margin-top: 8px; font-size: 18px; }
  .login-mark, .splash-mark { font-size: 24px; }
}
