/* Shared UI components for the new portal.
 * Loaded after tokens.css, before any page-specific CSS.
 * The old portal (index.html / styles.css) does NOT load this file.
 *
 * Contains: reset, body base, topbar, statusline, btn, chip, fchip,
 *           modal, toast, loading-note, switch, focus ring, mono.
 * Page CSS adds layout and page-specific overrides on top.
 */

/* ------------------------------------------------------------------- reset */

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
}

/* -------------------------------------------------------------------- base */

body {
  background: var(--page);
  color: var(--ink);
  font-family: 'Manrope', -apple-system, system-ui, 'Segoe UI', sans-serif;
  font-size: var(--fs-13);
  line-height: 1.5;
}

.mono {
  font-family: 'IBM Plex Mono', ui-monospace, monospace;
  font-variant-numeric: tabular-nums;
  font-weight: 500;
}

.up   { color: var(--up); }
.down { color: var(--down); }
.dim  { color: var(--muted); }

/* ------------------------------------------------------------ focus ring */

a:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
summary:focus-visible,
[tabindex]:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

/* ----------------------------------------------------------------- topbar */

.topbar {
  position: relative;
  z-index: 40;
  height: 52px;
  background: var(--canvas);
  border-bottom: 1px solid var(--hair);
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 22px;
}

.topbar h1 {
  margin: 0;
  font-size: var(--fs-18);
  font-weight: 700;
  letter-spacing: -0.02em;
  white-space: nowrap;
}

.topbar .spacer { flex: 1; }

.topbar .back-link {
  color: var(--ink2);
  text-decoration: none;
  font-size: var(--fs-13);
  font-weight: 600;
  padding: 6px 10px;
  border-radius: var(--r-sm);
}

.topbar .back-link:hover { background: var(--surface); }

.topbar .back-link.active {
  color: var(--accent-700);
  font-weight: 700;
  background: var(--accent-soft);
  cursor: default;
}

.topbar .back-link.active:hover { background: var(--accent-soft); }

.topbar select {
  font-family: inherit;
  font-size: var(--fs-13);
  color: var(--ink);
  background: var(--canvas);
  border: 1px solid var(--hair);
  border-radius: var(--r);
  padding: 5px 8px;
  max-width: 320px;
  min-height: 30px;
}

/* REDATA brand monogram */
.cnav-brand {
  display: flex;
  align-items: center;
  font-family: 'IBM Plex Mono', ui-monospace, monospace;
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0.12em;
  color: var(--accent);
  white-space: nowrap;
}

/* ------------------------------------------------------------- statusline */

.statusline {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px 18px;
  padding: 9px 22px;
  font-size: var(--fs-12);
  color: var(--ink2);
  border-bottom: 1px solid var(--hair2);
  background: var(--canvas);
  min-height: 36px;
}

.statusline b { color: var(--ink); }
.statusline .sel-counter { color: var(--accent-700); font-weight: 600; }
.statusline .error { color: var(--down); font-weight: 600; }

/* ----------------------------------------------------------------- buttons */

.btn {
  appearance: none;
  font-family: inherit;
  font-size: var(--fs-125);
  font-weight: 600;
  color: var(--ink);
  background: var(--canvas);
  border: 1px solid var(--hair);
  border-radius: var(--r);
  min-height: 28px;
  padding: 4px 10px;
  cursor: pointer;
}

.btn:hover:not(:disabled) { background: var(--surface); }
.btn:active:not(:disabled) { transform: scale(0.98); }

.btn:disabled {
  color: var(--faint);
  cursor: not-allowed;
}

/* Primary CTA — flat accent, no shadow */
.btn-primary,
.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  min-height: 30px;
}

.btn-primary:hover:not(:disabled),
.btn.primary:hover:not(:disabled) { background: var(--accent-700); border-color: var(--accent-700); }

.btn-primary:disabled { background: var(--faint); border-color: var(--faint); color: #fff; }
.btn.primary:disabled { background: var(--accent-soft); border-color: var(--accent-soft); color: var(--faint); }

/* Small variant */
.btn.sm,
.btn-sm { min-height: 24px; padding: 2px 8px; font-size: var(--fs-12); }

/* Text/link variant */
.btn-link {
  background: none;
  border-color: transparent;
  color: var(--ink2);
  font-weight: 400;
}

.btn-link:hover:not(:disabled) { background: none; color: var(--ink); }

.btn-danger-text { color: var(--down); }

/* ------------------------------------------------------------------- chips */

/* Status chip — filled pill */
.chip {
  display: inline-block;
  font-size: var(--fs-11);
  font-weight: 600;
  color: #fff;
  border-radius: 999px;
  padding: 3px 9px;
  white-space: nowrap;
}

/* Shared status modifiers */
.chip.st-none  { background: var(--status-none); }
.chip.st-work  { background: var(--surface); color: var(--status-work); }
.chip.st-ready { background: var(--accent); }
.chip.st-mgmt  { background: var(--status-mgmt); }

/* Filter toggle chip */
.fchip {
  appearance: none;
  font-family: inherit;
  font-size: var(--fs-12);
  font-weight: 600;
  color: var(--ink2);
  background: var(--canvas);
  border: 1px solid var(--hair);
  border-radius: 999px;
  padding: 4px 12px;
  min-height: 28px;
  cursor: pointer;
  white-space: nowrap;
}

.fchip:hover { background: var(--surface); }

.fchip[aria-pressed="true"] {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent-700);
}

.fchip .cnt { color: var(--muted); font-weight: 600; }
.fchip[aria-pressed="true"] .cnt { color: var(--accent-700); }

/* Attention variant — «Не настроен» count chip. Rose so the worklist stands out
 * among the neutral status chips; the leading dot echoes the row rail color. */
.fchip.attn {
  color: var(--status-none);
  border-color: var(--status-none-line);
  background: var(--status-none-soft);
}
.fchip.attn:hover { background: var(--status-none-soft2); }
.fchip.attn .cnt { color: var(--status-none); }
.fchip.attn[aria-pressed="true"] {
  background: var(--status-none);
  border-color: var(--status-none);
  color: #fff;
}
.fchip.attn[aria-pressed="true"] .cnt { color: #fff; }
.fchip.attn[aria-pressed="true"] .fchip-dot { background: #fff; }
.fchip-dot {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--status-none);
  margin-right: 5px;
  vertical-align: middle;
}

/* ------------------------------------------------------------------- modal */

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(20, 24, 40, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.modal {
  background: var(--canvas);
  border: 1px solid var(--hair);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-pop);
  width: 560px;
  max-width: 100%;
  max-height: calc(100vh - 48px);
  display: flex;
  flex-direction: column;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 16px 10px;
  border-bottom: 1px solid var(--hair2);
}

.modal-head h2 { margin: 0; font-size: var(--fs-14); font-weight: 600; }

.modal-body {
  padding: 12px 16px;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 10px 16px 12px;
  border-top: 1px solid var(--hair2);
}

/* Pop-close button shared across modal + popover headers */
.pop-close {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: var(--fs-14);
  line-height: 1;
  width: 24px;
  height: 24px;
  border-radius: var(--r-xs);
  cursor: pointer;
  padding: 0;
}

.pop-close:hover { background: var(--surface); color: var(--ink); }

/* -------------------------------------------------------------------- toast */

.toast {
  position: fixed;
  right: 22px;
  bottom: 22px;
  z-index: 70;
  max-width: 420px;
  background: var(--ink);
  color: #fff;
  font-size: var(--fs-125);
  border-radius: var(--r);
  box-shadow: var(--shadow-pop);
  padding: 9px 14px;
  opacity: 0;
  transform: translateY(6px);
  pointer-events: none;
  transition: opacity 0.15s ease, transform 0.15s ease;
}

.toast.show { opacity: 1; transform: translateY(0); }
.toast.err  { background: var(--down); }

/* ----------------------------------------------------------------- loading */

.loading-note {
  padding: 24px 22px;
  color: var(--muted);
  font-size: var(--fs-13);
}

/* Empty / loading card-style placeholder */
.cards-empty {
  padding: 40px 16px;
  text-align: center;
  color: var(--muted);
  font-size: var(--fs-13);
  margin: 0;
  background: var(--canvas);
  border: 1px dashed var(--hair);
  border-radius: var(--r-lg);
}

/* ------------------------------------------------------------------ toggle switch */

/* Generic accessible switch (checkbox + knob span).
 * Usage: <label class="switch"><input type="checkbox"><span class="knob"></span></label> */
.switch {
  position: relative;
  display: inline-flex;
  align-items: center;
  width: 32px;
  height: 18px;
  cursor: pointer;
  flex: none;
}

.switch input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}

.switch input:disabled { cursor: not-allowed; }

/* Touch devices: the visible 32×18 toggle stays as-is (row layouts depend on
 * it), but the invisible input that actually takes the tap grows to ~54×40 —
 * the input is the only clickable layer, so no visual change. */
@media (pointer: coarse) {
  .switch input {
    inset: -11px;
    width: auto;
    height: auto;
  }
}

.switch .knob {
  pointer-events: none;
  width: 32px;
  height: 18px;
  border-radius: 999px;
  background: var(--hair);
  transition: background 0.12s ease;
}

.switch .knob::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--canvas);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.18);
  transition: left 0.12s ease;
}

.switch input:checked + .knob { background: var(--status-mgmt); }
.switch input:checked + .knob::after { left: 16px; }
.switch input:disabled + .knob { opacity: 0.45; }
.switch input:focus-visible + .knob {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

/* --------------------------------------------------------------------- nrail */
/* Persistent left navigation rail — single shared component (js/core/nav_rail.js)
 * mounted on every new-portal page (pricing-settings/-detail/-groups/-presets).
 * Replaces the old per-page topbar section-nav dropdowns. Collapsed/expanded
 * state persists in localStorage; collapsed = icons only, expanded = labels. */

body.has-nrail {
  margin-left: 56px;
  transition: margin-left 0.18s ease;
}
body.has-nrail.nrail-expanded { margin-left: 260px; }

.nrail {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 45; /* above topbar (40), below modal-backdrop (60) */
  width: 56px;
  background: var(--canvas);
  border-right: 1px solid var(--hair);
  display: flex;
  flex-direction: column;
  padding: 8px 0;
  overflow-x: hidden;
  overflow-y: auto;
  transition: width 0.18s ease;
}
.nrail.expanded { width: 260px; }

.nrail-toggle {
  appearance: none;
  border: none;
  background: transparent;
  height: 36px;
  width: 56px;
  min-width: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  font-size: 16px;
  color: var(--ink2);
  cursor: pointer;
  margin-bottom: 4px;
  flex-shrink: 0;
}
.nrail.expanded .nrail-toggle { width: 100%; justify-content: flex-start; padding-left: 16px; }
.nrail-toggle:hover { background: var(--surface); }

.nrail-toggle-label {
  display: none;
  font-size: var(--fs-11);
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}
.nrail.expanded .nrail-toggle-label { display: inline; }

.nrail-sep { height: 1px; background: var(--hair2); margin: 6px 14px; flex-shrink: 0; }

/* «Выйти» pinned to the rail bottom (margin-top:auto in the flex column). */
.nrail-foot { margin-top: auto; padding-top: 6px; flex-shrink: 0; }
.nrail-foot::before {
  content: ""; display: block; height: 1px; background: var(--hair2); margin: 0 14px 6px;
}
.nrail-logout { color: var(--muted); }
.nrail-logout .nrail-icon { color: inherit; }
.nrail-logout:hover { color: #b91c1c; background: var(--surface); }
.nrail-logout:hover .nrail-icon { color: #b91c1c; }

.nrail-group-title {
  display: none;
  padding: 10px 16px 4px;
  font-size: var(--fs-105);
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--faint);
  flex-shrink: 0;
}
.nrail.expanded .nrail-group-title { display: block; }
.nrail-group:first-of-type .nrail-group-title { padding-top: 0; }

.nrail-item {
  display: flex;
  align-items: center;
  gap: 12px;
  height: 38px;
  padding: 0 16px;
  color: var(--ink);
  text-decoration: none;
  font-weight: 600;
  font-size: var(--fs-13);
  white-space: nowrap;
  border-left: 2.5px solid transparent;
  flex-shrink: 0;
}
.nrail-item:hover { background: var(--surface); }
.nrail-item.active {
  background: var(--accent-soft);
  color: var(--accent-700);
  border-left-color: var(--accent);
}

.nrail-icon { flex: 0 0 18px; display: flex; align-items: center; justify-content: center; color: var(--ink2); }
.nrail-icon svg { width: 18px; height: 18px; display: block; }
.nrail-item.active .nrail-icon { color: inherit; }
.nrail-label { display: none; }
.nrail.expanded .nrail-label { display: inline; }

.nrail-soon {
  display: none;
  margin-left: auto;
  font-size: 10px;
  font-weight: 700;
  color: #92400e;
  background: #fef3c7;
  border: 1px solid #fde68a;
  border-radius: var(--r-xs);
  padding: 0 5px;
  white-space: nowrap;
}
.nrail.expanded .nrail-soon { display: inline-block; }

/* «к удалению» — легаси-табы в блоке «Архив»; красная плашка отличает их от
   «в разработке» (жёлтой). Показывается только в развёрнутой рельсе. */
.nrail-archived {
  display: none;
  margin-left: auto;
  font-size: 10px;
  font-weight: 700;
  color: #991b1b;
  background: #fee2e2;
  border: 1px solid #fecaca;
  border-radius: var(--r-xs);
  padding: 0 5px;
  white-space: nowrap;
}
.nrail.expanded .nrail-archived { display: inline-block; }

/* collapsed: active item reads as a solid rounded square, not a thin strip */
.nrail:not(.expanded) .nrail-item {
  justify-content: center;
  padding: 0;
  margin: 2px 10px;
  height: 40px;
  border-radius: var(--r);
  border-left: none;
}
.nrail:not(.expanded) .nrail-item.active {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 2px 6px rgba(15, 95, 184, 0.35);
}

/* Collapsed rail shows only the toggle (☰) + «Главная». The other icons were
 * inviting dead clicks — a collapsed icon with no label is a guessing game and
 * users kept tapping them expecting navigation. Everything reappears on expand.
 * The toggle and the home item stay full-fledged buttons (hover + tooltip). */
.nrail:not(.expanded) .nrail-item:not([data-rail-id="general"]) { display: none; }
.nrail:not(.expanded) .nrail-sep { display: none; }
.nrail:not(.expanded) .nrail-foot { display: none; }

/* Embedded inside another new-portal page's iframe (?embed=1) — the outer
 * page already shows a rail, so the inner copy stays hidden (same treatment
 * as .page-nav / #back-link in embed mode). */
html.embed-mode .nrail { display: none; }
html.embed-mode body.has-nrail { margin-left: 0 !important; }

/* Mobile bottom tab-bar — the client-facing subset of the rail, shown only
 * where the side rail collapses (<=640px). Hidden on desktop and in embeds so
 * the phone never ends up with no navigation at all. */
.nrail-tabbar { display: none; }
html.embed-mode .nrail-tabbar { display: none !important; }

@media (max-width: 640px) {
  /* Side rail collapses on phones (not enough width for a persistent side
   * nav); the bottom tab-bar takes over navigation. */
  .nrail { display: none; }
  body.has-nrail { margin-left: 0; }

  .nrail-tabbar {
    display: flex;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 45; /* same tier as the rail; modal-backdrop (60) still covers it */
    background: var(--canvas);
    border-top: 1px solid var(--hair);
    padding: 4px 2px calc(4px + env(safe-area-inset-bottom, 0px));
    box-shadow: 0 -2px 12px rgba(15, 23, 42, 0.08);
  }
  .nrail-tab {
    flex: 1 1 0;
    min-width: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    padding: 4px 2px;
    text-decoration: none;
    color: var(--ink2);
    font-size: 11px;
    font-weight: 600;
  }
  .nrail-tab-icon { display: flex; color: inherit; }
  .nrail-tab-icon svg { width: 22px; height: 22px; display: block; }
  .nrail-tab-label {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .nrail-tab.active { color: var(--accent); }

  /* Content clears the fixed bar (≈56px + safe-area). */
  body.has-nrail-tabbar { padding-bottom: calc(60px + env(safe-area-inset-bottom, 0px)); }
}
