/* ============================================================
   Каркас: сетка страницы, сайдбар, шапка, модалки, всплывающие слои, тосты.
   Только здесь задаются z-index (см. шкалу в base.css).
   ============================================================ */

/* ---------------- Сетка страницы ----------------
   Документ прокручивается сам. Никакой стеклянной рамки с blur вокруг —
   она делала бы себя контейнером для всех fixed-слоёв и обрезала подложки. */

/* Сетка — на обёртке, а не на <body>: диалоги и тосты лежат в <body> рядом с
   ней, и как grid-элементы они теряли бы центровку через margin: auto. */
.app {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  min-height: 100vh;
  min-height: 100dvh;
}

@media (min-width: 1024px) {
  .app { grid-template-columns: var(--sidebar-w) minmax(0, 1fr); }
}

.skip-link {
  position: absolute;
  top: 8px; left: 8px;
  z-index: var(--z-toast);
  padding: 10px 16px;
  border-radius: var(--r-sm);
  background: var(--brand);
  color: #fff;
  font-weight: 600;
  transform: translateY(-200%);
}
.skip-link:focus { transform: none; }

/* ---------------- Сайдбар ---------------- */

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: var(--white);
  color: var(--dark);
  padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
}

/* Уже lg — выезжающая панель поверх содержимого. */
@media (max-width: 1023.98px) {
  .sidebar {
    position: fixed;
    top: 0; bottom: 0; left: 0;
    z-index: var(--z-drawer);
    width: min(320px, 86vw);
    height: 100vh;
    height: 100dvh;
    border-radius: 0 var(--r-lg) var(--r-lg) 0;
    box-shadow: var(--shadow);
    translate: -100% 0;
    visibility: hidden;
    transition: translate 0.25s ease, visibility 0s linear 0.25s;
    padding-top: calc(18px + env(safe-area-inset-top));
  }
  .sidebar[data-open] {
    translate: none;
    visibility: visible;
    transition: translate 0.25s ease;
  }
}

/* От lg — колонка сетки, липнет к верху, высотой в окно. */
@media (min-width: 1024px) {
  .sidebar {
    position: sticky;
    top: 16px;
    height: calc(100vh - 32px);
    height: calc(100dvh - 32px);
    margin: 16px 0 16px 16px;
    border-radius: var(--r-lg);
    box-shadow: var(--shadow-sm);
    padding: 22px 18px;
  }
}

.sidebar-overlay {
  position: fixed;
  inset: 0;
  z-index: var(--z-overlay);
  background: var(--scrim);
}

@media (min-width: 1024px) {
  .sidebar-overlay { display: none !important; }
}

.sidebar__top {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 14px;
}

.sidebar__logo {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 1.5rem;
  font-weight: 700;
  padding: 4px 6px;
  border-radius: var(--r-sm);
  flex: 1;
  min-width: 0;
}

.sidebar__logo-mark { width: 30px; height: 30px; color: var(--brand); }

/* Специфичность выше .icon-btn из components.css — иначе он перебьёт display. */
.icon-btn.sidebar__close { display: none; }

@media (max-width: 1023.98px) {
  .icon-btn.sidebar__close { display: inline-flex; }
}

.nav-section {
  font-size: var(--fs-xs);
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--gray-5);
  margin: 16px 8px 8px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: var(--h-ctl);
  padding: 8px 14px;
  border-radius: var(--r-md);
  font-size: var(--fs-base);
  font-weight: 500;
  color: var(--dark);
  transition: background 0.15s ease;
}

.nav-item:hover { background: var(--gray-2); }
.nav-item.active { background: var(--brand); color: #fff; }
.nav-item .icon { width: 19px; height: 19px; }

.nav-sub { display: flex; flex-direction: column; gap: 2px; margin-top: 6px; }
.nav-sub .nav-item { min-height: var(--h-ctl-sm); font-size: var(--fs-md); padding: 6px 14px; }
.nav-sub .nav-item .icon { width: 17px; height: 17px; }

.sidebar__hint { padding: 10px 8px 0; font-size: var(--fs-md); color: var(--gray-5); }
.sidebar__quota { padding: 14px 8px 0; }
.sidebar__footer { margin-top: auto; padding-top: 16px; }

/* ---------------- Колонка содержимого ---------------- */

.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  padding: 0 12px calc(24px + env(safe-area-inset-bottom));
}

@media (min-width: 640px) {
  .main { padding: 0 20px 28px; }
}

@media (min-width: 1024px) {
  .main { padding: 0 24px 28px 20px; }
}

.content { display: flex; flex-direction: column; min-width: 0; }
.content > * + * { margin-top: var(--gap); }
.content > .mb-3 + * { margin-top: 0; }

/* ---------------- Шапка ---------------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: var(--topbar-h);
  padding: 10px 0;
}

/* На телефоне шапка липнет к верху: бургер всегда под рукой.
   Размытие тут допустимо — внутри шапки нет всплывающих меню. */
@media (max-width: 1023.98px) {
  .topbar {
    position: sticky;
    top: 0;
    z-index: var(--z-sticky);
    margin: 0 -12px;
    padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
    background: color-mix(in srgb, var(--bg) 80%, transparent);
    -webkit-backdrop-filter: blur(12px);
    backdrop-filter: blur(12px);
  }
}

@media (min-width: 640px) and (max-width: 1023.98px) {
  .topbar { margin: 0 -20px; padding-left: 20px; padding-right: 20px; }
}

@media (min-width: 1024px) {
  .topbar { min-height: 76px; padding: 22px 0 14px; gap: 14px; }
}

.topbar__text { min-width: 0; flex: 1 1 auto; }

.topbar__title {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  font-size: var(--fs-xl);
  font-weight: 700;
  line-height: 1.2;
  overflow-wrap: anywhere;
}

@media (min-width: 640px) {
  .topbar__title { font-size: var(--fs-2xl); }
}

@media (min-width: 1024px) {
  .topbar__title { font-size: var(--fs-3xl); }
}

.topbar__title .tone { font-size: var(--fs-xs); }

.topbar__subtitle {
  color: var(--gray-6);
  font-size: var(--fs-md);
  margin-top: 2px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.topbar__tools { display: flex; align-items: center; gap: 8px; flex: 0 0 auto; }

.icon-btn.burger { display: inline-flex; }

@media (min-width: 1024px) {
  .icon-btn.burger { display: none; }
}

.tzchip {
  display: none;
  align-items: center;
  gap: 8px;
  height: var(--h-ctl);
  padding: 0 14px;
  border-radius: var(--r-pill);
  background: var(--white);
  color: var(--gray-6);
  font-size: var(--fs-sm);
  font-weight: 600;
  cursor: default;
  box-shadow: var(--shadow-sm);
}

@media (min-width: 640px) {
  .tzchip { display: inline-flex; }
}

/* ---------------- Модальные окна ----------------
   Нативный <dialog>: верхний слой, фокус внутри, Escape — бесплатно.
   Уже md — шторка снизу, от md — окно по центру, карточка файла — справа. */

dialog.modal {
  /* margin: auto возвращает центровку — общий сброс `* { margin: 0 }` её снимает */
  margin: auto;
  padding: 0;
  border: 0;
  background: var(--white);
  color: var(--dark);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow);
  width: min(100% - 32px, var(--modal-w, 460px));
  max-width: none;
  max-height: calc(100vh - 32px);
  max-height: calc(100dvh - 32px);
  overflow: visible;
}

dialog.modal[open] {
  display: flex;
  flex-direction: column;
  animation: modal-in 0.2s ease;
}

dialog.modal::backdrop {
  background: var(--scrim);
  -webkit-backdrop-filter: blur(3px);
  backdrop-filter: blur(3px);
}

.modal--sm { --modal-w: 360px; }
.modal--token { --modal-w: 640px; }
.modal--upload { --modal-w: 760px; }
.modal--cache { --modal-w: 860px; }

.modal__inner {
  display: flex;
  flex-direction: column;
  min-height: 0;
  max-height: inherit;
  flex: 1 1 auto;
}

.modal__header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 20px var(--pad-card) 0;
  flex: 0 0 auto;
}

.modal__icon {
  width: 36px; height: 36px;
  padding: 0;
  border-radius: 11px;
  justify-content: center;
  flex: 0 0 auto;
}

.modal__title {
  font-size: var(--fs-lg);
  font-weight: 700;
  flex: 1;
  min-width: 0;
  overflow-wrap: anywhere;
}

.modal__close { margin-left: auto; flex: 0 0 auto; }

.modal__body {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  padding: 12px var(--pad-card) 20px;
  scrollbar-width: thin;
}

.modal__text { color: var(--gray-6); font-size: var(--fs-base); }
.modal__text + .stack, .modal__text + .cache-grid { margin-top: 16px; }

.modal__footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  flex-wrap: wrap;
  padding: 14px var(--pad-card) 20px;
  border-top: 1px solid var(--gray-2);
  flex: 0 0 auto;
}

.modal__footer .btn { flex: 1 1 auto; }

@media (min-width: 640px) {
  .modal__footer .btn { flex: 0 0 auto; }
}

@keyframes modal-in {
  from { opacity: 0; transform: translateY(12px) scale(0.98); }
  to { opacity: 1; transform: none; }
}

/* Уже md: любая модалка — шторка снизу на всю ширину. */
@media (max-width: 767.98px) {
  dialog.modal {
    width: 100%;
    margin: auto 0 0;
    border-radius: var(--r-lg) var(--r-lg) 0 0;
    max-height: calc(100vh - 24px);
    max-height: calc(100dvh - 24px);
  }

  dialog.modal[open] { animation: sheet-in 0.22s ease; }

  .modal__footer { padding-bottom: calc(16px + env(safe-area-inset-bottom)); }

  /* Большие формы и карточка файла — во весь экран, с постоянным футером. */
  .modal--full, .modal--drawer {
    height: 100vh;
    height: 100dvh;
    max-height: none;
    border-radius: 0;
  }
  .modal--full .modal__header, .modal--drawer .modal__header { padding-top: calc(16px + env(safe-area-inset-top)); }
}

@keyframes sheet-in {
  from { opacity: 0; transform: translateY(32px); }
  to { opacity: 1; transform: none; }
}

/* От md: карточка файла — панель у правого края. */
@media (min-width: 768px) {
  .modal--drawer {
    width: min(520px, 100% - 32px);
    height: 100vh;
    height: 100dvh;
    max-height: none;
    margin: 0 0 0 auto;
    border-radius: var(--r-lg) 0 0 var(--r-lg);
  }
  .modal--drawer[open] { animation: drawer-in 0.24s ease; }
}

@keyframes drawer-in {
  from { opacity: 0; transform: translateX(32px); }
  to { opacity: 1; transform: none; }
}

/* ---------------- Всплывающие слои (меню, селекты) ----------------
   Базовые стили меню — в components.css. Здесь только «поднятое» состояние:
   координаты ставит popover.js. На телефоне — шторка снизу. */

.dropdown__menu.is-floating,
.cselect__menu.is-floating {
  display: block;
  position: fixed;
  top: auto; right: auto; bottom: auto; left: auto;
  z-index: var(--z-popover);
  max-height: calc(100vh - 32px);
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
  animation: menu-in 0.12s ease;
}

@keyframes menu-in {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: none; }
}

.dropdown__menu.is-sheet,
.cselect__menu.is-sheet {
  left: 0 !important;
  right: 0 !important;
  bottom: 0 !important;
  top: auto !important;
  width: auto !important;
  max-height: min(70vh, 70dvh);
  border-radius: var(--r-lg) var(--r-lg) 0 0;
  border: 0;
  padding: 12px 12px calc(12px + env(safe-area-inset-bottom));
  background: transparent;
  box-shadow: none;
  animation: sheet-in 0.2s ease;
}

/* Затемнение и подложка шторки — псевдоэлементы самого меню: так они работают
   и внутри <dialog>, где отдельный слой с body остался бы под окном. */
.dropdown__menu.is-sheet::before,
.cselect__menu.is-sheet::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -2;
  background: var(--scrim);
}

.dropdown__menu.is-sheet::after,
.cselect__menu.is-sheet::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: var(--white);
  border-radius: inherit;
  box-shadow: var(--shadow);
}

.is-sheet .dropdown__item,
.is-sheet .cselect__option {
  min-height: 48px;
  padding: 12px 14px;
  font-size: 1rem;
  border-radius: var(--r-md);
}

/* ---------------- Подсказка графика ---------------- */

.ttip {
  position: fixed;
  z-index: var(--z-tip);
  min-width: 160px;
  max-width: min(320px, calc(100vw - 16px));
  padding: 10px 12px;
  border-radius: var(--r-sm);
  background: var(--ink);
  color: #fff;
  box-shadow: var(--shadow);
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.12s ease;
}
.ttip.is-open { opacity: 1; }

.ttip__title { font-size: var(--fs-sm); font-weight: 600; color: rgba(255, 255, 255, 0.65); margin-bottom: 6px; }
.ttip__row { display: flex; align-items: center; gap: 8px; font-size: var(--fs-md); line-height: 1.7; }
.ttip__dot { width: 8px; height: 8px; border-radius: 2px; flex: 0 0 auto; }
.ttip__name { color: rgba(255, 255, 255, 0.8); }
.ttip__value { margin-left: auto; font-weight: 600; font-variant-numeric: tabular-nums; }

/* ---------------- Тосты ----------------
   popover="manual": контейнер живёт в верхнем слое и виден поверх модалок. */

.toasts {
  position: fixed;
  inset: auto 12px calc(12px + env(safe-area-inset-bottom)) 12px;
  z-index: var(--z-toast);
  margin: 0;
  padding: 0;
  border: 0;
  width: auto;
  max-width: none;
  height: auto;
  max-height: none;
  overflow: visible;
  background: transparent;
  color: inherit;
  display: flex;
  flex-direction: column;
  gap: 10px;
  pointer-events: none;
}

.toasts:empty { display: none; }

@media (min-width: 640px) {
  .toasts { inset: auto 24px 24px auto; width: 360px; }
}

.toast {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 14px 14px 14px 16px;
  border-radius: var(--r-md);
  background: var(--white);
  color: var(--dark);
  border: 1px solid var(--gray-2);
  border-left: 4px solid var(--gray-4);
  box-shadow: var(--shadow);
  font-size: var(--fs-base);
  pointer-events: auto;
  animation: toast-in 0.25s ease;
}

.toast.is-leaving { animation: toast-out 0.2s ease forwards; }
.toast > .icon { margin-top: 1px; }
.toast__body { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.toast__detail { font-size: var(--fs-sm); color: var(--gray-5); margin-top: 2px; }

.toast--ok    { border-left-color: var(--ok); }
.toast--error { border-left-color: var(--danger); }
.toast--warn  { border-left-color: var(--warn); }
.toast--info  { border-left-color: var(--brand); }
.toast--ok > .icon    { color: var(--ok); }
.toast--error > .icon { color: var(--danger); }
.toast--warn > .icon  { color: var(--warn); }
.toast--info > .icon  { color: var(--brand); }

.toast__action {
  color: var(--brand);
  font-weight: 600;
  font-size: var(--fs-sm);
  padding: 6px 8px;
  border-radius: var(--r-sm);
  white-space: nowrap;
  min-height: 32px;
}
.toast__action:hover { background: var(--brand-soft); }

.toast__close {
  display: grid;
  place-items: center;
  width: 32px; height: 32px;
  margin: -6px -6px -6px 0;
  border-radius: 50%;
  color: var(--gray-5);
  flex: 0 0 auto;
}
.toast__close:hover { background: var(--gray-2); color: var(--dark); }

@keyframes toast-in { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: none; } }
@keyframes toast-out { to { opacity: 0; transform: translateY(8px); } }
