/* ============================================================
   layout.css — сетка приложения
   .app-shell — CSS Grid: header / [sidebar | canvas] / footer
   Footer «прилипает» к низу; при переполнении контента уезжает вниз.
   ============================================================ */

.app-shell {
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-rows: var(--header-h) 1fr var(--footer-h);
  grid-template-columns: 100%;
  grid-template-areas:
    "header"
    "main"
    "footer";
  width: 100%;
  overflow: hidden;
}

/* ---------- Header ---------- */
.app-header {
  grid-area: header;
  display: flex;
  align-items: center;
  gap: var(--space-4);
  padding: 0 var(--space-4);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 50;
  backdrop-filter: saturate(1.2);
}

.brand {
  display: flex; align-items: center; gap: var(--space-3);
  min-width: 0;
}
.brand__mark {
  display: inline-flex; align-items: center; justify-content: center;
  width: 34px; height: 34px;
  border-radius: var(--radius-sm);
  background: var(--accent-soft);
  color: var(--accent);
  flex-shrink: 0;
}
.brand__text { display: flex; flex-direction: column; line-height: 1.15; min-width: 0; }
.brand__title { font-weight: 700; font-size: 14px; white-space: nowrap; }
.brand__subtitle {
  font-size: 11px; color: var(--text-faint);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* Tabs */
.tabs {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 4px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-left: auto;
}
.tab {
  display: inline-flex; align-items: center; gap: 8px;
  height: 34px; padding: 0 16px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-muted);
  font-weight: 600; font-size: 13px;
  cursor: pointer;
  transition: background .15s ease, color .15s ease, box-shadow .15s ease;
  white-space: nowrap;
}
.tab:hover { color: var(--text); }
.tab.is-active {
  background: var(--surface);
  color: var(--accent);
  box-shadow: var(--shadow-sm);
}
.tab svg { flex-shrink: 0; }

.header__tools { display: inline-flex; align-items: center; gap: var(--space-2); }

/* theme toggle icons */
.icon-sun, .icon-moon { display: none; }
[data-theme="light"] .icon-moon { display: block; }
[data-theme="dark"] .icon-sun { display: block; }

/* ---------- Main ---------- */
.app-main {
  grid-area: main;
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  grid-template-rows: 1fr;
  grid-template-areas: "sidebar canvas";
  min-height: 0; /* критично для вложенных скроллов */
  overflow: hidden;
}

/* ---------- Sidebar ---------- */
.app-sidebar {
  grid-area: sidebar;
  border-right: 1px solid var(--border);
  background: var(--surface);
  overflow-y: auto;
  overflow-x: hidden;
}

.panel { padding: var(--space-4); border-bottom: 1px solid var(--border); }
.panel:last-child { border-bottom: none; }

.panel__head {
  display: flex; align-items: center; justify-content: space-between;
  gap: var(--space-2);
  margin-bottom: var(--space-4);
}
.panel__title { font-size: 14px; font-weight: 700; }
.panel__badge {
  font-size: 10px; font-weight: 700;
  text-transform: uppercase; letter-spacing: .06em;
  color: var(--accent);
  background: var(--accent-soft);
  padding: 3px 8px;
  border-radius: 999px;
}
.panel__body { display: flex; flex-direction: column; gap: var(--space-4); }

/* ---------- Canvas area ---------- */
.app-canvas {
  grid-area: canvas;
  min-width: 0;
  min-height: 0;
  position: relative;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

/* View — занимает всю центральную область; одновременно активен один */
.view {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s ease;
}
.view.is-active { opacity: 1; pointer-events: auto; }

.view__toolbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}
.view__title { font-size: 13px; font-weight: 700; color: var(--text); }
.view__tools { display: inline-flex; align-items: center; gap: var(--space-2); flex-wrap: wrap; }

.view__stage {
  flex: 1 1 auto;
  min-height: 0;
  padding: var(--space-4);
  display: flex;
  overflow: hidden;
}

/* Split into two stages (section | unfold) — вертикально, одно под другим */
.stage-split {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  width: 100%;
  min-height: 0;
  max-height: 100%;
  flex: 1 1 auto;
  overflow: hidden;
}
.stage-split .stage {
  flex: 1 1 0;
  min-height: 0;
  overflow: hidden;
}

.stage {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}
.stage--full { width: 100%; }

.stage__label {
  padding: 8px var(--space-3);
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-faint);
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
  flex-shrink: 0;
}
.stage__canvas {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
}
.stage__canvas svg, .stage__canvas canvas {
  display: block;
  width: 100%;
  height: 100%;
}

/* ---------- Readout strip ---------- */
.view__readout {
  display: flex;
  flex-wrap: wrap;
  align-items: stretch;
  gap: 1px;
  background: var(--border);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.readout-item {
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: var(--space-3) var(--space-4);
  background: var(--surface);
  min-width: 0;
}
.readout-item__k {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-faint);
}
.readout-item__v {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 16px;
  font-weight: 700;
  color: var(--accent);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---------- Footer ---------- */
.app-footer {
  grid-area: footer;
  display: flex;
  align-items: center;
  gap: var(--space-4);
  padding: 0 var(--space-4);
  background: var(--surface);
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-muted);
  position: relative;
  z-index: 40;
}
.footer__status { display: inline-flex; align-items: center; gap: 8px; font-weight: 600; color: var(--text); }
.status-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--ok);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 20%, transparent);
}
.status-dot.is-busy { background: var(--warn); box-shadow: 0 0 0 3px color-mix(in srgb, var(--warn) 20%, transparent); animation: pulse 1s ease-in-out infinite; }
.status-dot.is-error { background: var(--danger); box-shadow: 0 0 0 3px color-mix(in srgb, var(--danger) 20%, transparent); }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .45; } }

.footer__meta { display: inline-flex; align-items: center; gap: 8px; color: var(--text-faint); flex-wrap: wrap; }
.footer__seg { white-space: nowrap; }
.footer__sep { color: var(--border-strong); }
.footer__export { margin-left: auto; display: inline-flex; align-items: center; gap: 6px; }

/* ---------- Адаптив ---------- */
@media (max-width: 1024px) {
  :root { --sidebar-w: 280px; }
}

@media (max-width: 768px) {
  :root { --header-h: auto; --footer-h: auto; }
  .app-shell { grid-template-rows: auto 1fr auto; }

  .app-header {
    flex-wrap: wrap;
    padding: var(--space-2) var(--space-3);
    gap: var(--space-2);
  }
  .brand__subtitle { display: none; }
  .tabs { order: 3; margin-left: 0; width: 100%; }
  .tab { flex: 1; justify-content: center; }
  .header__tools { margin-left: auto; }

  .app-main {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
    grid-template-areas:
      "sidebar"
      "canvas";
    overflow: visible;
  }
  .app-sidebar {
    border-right: none;
    border-bottom: 1px solid var(--border);
    max-height: 42vh;
  }

  .view__toolbar { padding: var(--space-2) var(--space-3); }
  .view__stage { padding: var(--space-3); }

  .readout-item { padding: var(--space-2) var(--space-3); }
  .readout-item__v { font-size: 14px; }

  .app-footer { flex-wrap: wrap; padding: var(--space-2) var(--space-3); gap: var(--space-2); }
  .footer__meta { display: none; }
  .footer__export { margin-left: 0; }
}

/* безопасная зона снизу (мобильные) */
@supports (padding: env(safe-area-inset-bottom)) {
  .app-footer { padding-bottom: env(safe-area-inset-bottom); }
}
