/*
 * MaxOS v2 — дизайн-система интерфейса.
 * Токены в :root, тёмная тема через prefers-color-scheme и
 * data-theme (на случай ручного переключателя темы в будущем).
 */

:root {
  /* Фирменные цвета ЭСМ: красный — основной акцент/действие,
     тёмно-синий — структурные элементы (сайдбар, подписи), как на логотипе. */
  --bg: #eef1f4;
  --surface: #ffffff;
  --surface-2: #e7ebf0;
  --text: #16202b;
  --text-muted: #57677a;
  --border: #d8dfe7;
  --accent: #d2201b;
  --accent-contrast: #ffffff;
  --accent-soft: #fbe4e2;
  --critical: #b3261e;
  --critical-soft: #f8e1df;
  --warning: #a15c00;
  --warning-soft: #f7e6cc;
  --good: #2e7d4f;
  --good-soft: #dcefe2;
  --brand-navy: #163a5c;
  --sidebar-bg: #12283f;
  --logo-red: var(--accent);
  --logo-navy: var(--brand-navy);

  /* Категориальная палитра для плиток дашборда (8 оттенков, фиксированный
     порядок, провалидировано на CVD-безопасность — см. dataviz skill
     references/palette.md). 15 плиток циклически используют эти 8 слотов;
     это UI-идентичность (позиция + подпись + пиктограмма), а не кодирование
     данных, поэтому повтор оттенка на 9-й+ плитке допустим. */
  --hue-1: #2a78d6; /* синий */
  --hue-2: #1baf7a; /* бирюзовый */
  --hue-3: #c98500; /* жёлтый — затемнён под контраст на белом */
  --hue-4: #008300; /* зелёный */
  --hue-5: #4a3aa7; /* фиолетовый */
  --hue-6: #e34948; /* красный */
  --hue-7: #c5487a; /* маджента — затемнена под контраст на белом */
  --hue-8: #eb6834; /* оранжевый */
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1721;
    --surface: #16232f;
    --surface-2: #1c2b39;
    --text: #e7edf3;
    --text-muted: #93a4b6;
    --border: #29394a;
    --accent: #ec4741;
    --accent-contrast: #ffffff;
    --accent-soft: #3a1c1a;
    --critical: #ef6b62;
    --critical-soft: #3a1f1c;
    --warning: #e0a941;
    --warning-soft: #3a2c11;
    --good: #79d4a0;
    --good-soft: #173327;
    --brand-navy: #7fa9cf;
    --sidebar-bg: #0a1b2c;

    --hue-1: #3987e5;
    --hue-2: #199e70;
    --hue-3: #c98500;
    --hue-4: #2fa62f;
    --hue-5: #9085e9;
    --hue-6: #e66767;
    --hue-7: #d55181;
    --hue-8: #d95926;
  }
}

/* ---------- Тема оформления (Настройки → Отображение) ----------
   Только 2 варианта: 'default' (светлая, она же системная — без атрибута,
   следует prefers-color-scheme) и 'dark' (принудительно тёмная). Явный
   data-theme="dark" побеждает системную тему в обе стороны (селектор с
   атрибутом весит больше, чем :root внутри media-запроса). */

:root[data-theme="dark"] {
  --bg: #0d1721;
  --surface: #16232f;
  --surface-2: #1c2b39;
  --text: #e7edf3;
  --text-muted: #93a4b6;
  --border: #29394a;
  --accent: #ec4741;
  --accent-contrast: #ffffff;
  --accent-soft: #3a1c1a;
  --critical: #ef6b62;
  --critical-soft: #3a1f1c;
  --warning: #e0a941;
  --warning-soft: #3a2c11;
  --good: #79d4a0;
  --good-soft: #173327;
  --brand-navy: #7fa9cf;
  --sidebar-bg: #0a1b2c;

  --hue-1: #3987e5;
  --hue-2: #199e70;
  --hue-3: #c98500;
  --hue-4: #2fa62f;
  --hue-5: #9085e9;
  --hue-6: #e66767;
  --hue-7: #d55181;
  --hue-8: #d95926;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
}

a { color: var(--accent); }

h1, h2, h3, h4 { text-wrap: balance; margin: 0 0 12px; letter-spacing: -0.01em; }
h1 { font-size: 24px; }
h2 { font-size: 19px; }
h3 { font-size: 16px; }

table { font-variant-numeric: tabular-nums; }

/* ---------- Layout: топбар (лого + Назад/Главное меню + заголовок) + content.
   Бокового меню нет — у каждой страницы вместо дерева разделов есть свой
   "уровень выше" (см. layout_head($title, $backUrl)). */

.app-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  border-top: 3px solid var(--accent);
  padding: 10px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex: none;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 16px;
  min-width: 0;
}

.topbar-brand {
  display: flex;
  flex: none;
  align-items: baseline;
  font-weight: 800;
  font-size: 18px;
  letter-spacing: -0.01em;
  color: var(--accent);
  text-decoration: none;
}

.topbar-nav {
  display: flex;
  gap: 8px;
  flex: none;
}

.topbar-title {
  margin: 0;
  font-size: 17px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar .company-badge {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .04em;
  color: var(--accent);
  background: var(--accent-soft);
  padding: 4px 10px;
  border-radius: 20px;
}

.topbar .user-menu {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 14px;
  color: var(--text-muted);
  flex: none;
}

.topbar .user-menu form { margin: 0; }

.content { padding: 24px; flex: 1; overflow-y: auto; min-height: 0; }

@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; }
  .topbar-title { display: none; }
}

/* ---------- Components ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 18px;
}

/* .card используется и как кликабельная плитка-ссылка (напр. crm.php) —
   лёгкая hover-подсветка, чтобы это читалось. */
a.card:hover {
  border-color: var(--accent);
  box-shadow: 0 6px 16px -10px rgb(0 0 0 / 0.3);
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  font-weight: 600;
  padding: 8px 16px;
  border-radius: 8px;
  border: 1px solid transparent;
  cursor: pointer;
  text-decoration: none;
  line-height: 1.2;
}

.btn-primary { background: var(--accent); color: var(--accent-contrast); }
.btn-secondary { background: var(--surface-2); color: var(--text); border-color: var(--border); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; color: var(--text-muted); margin-bottom: 4px; }
.field input, .field select, .field textarea {
  width: 100%;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
}
.field input:focus, .field select:focus, .field textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 10px; }
table.data { border-collapse: collapse; width: 100%; background: var(--surface); font-size: 14px; }
table.data th, table.data td { text-align: left; padding: 10px 14px; border-bottom: 1px solid var(--border); }
table.data th { background: var(--surface-2); font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--text-muted); }
table.data tr:last-child td { border-bottom: none; }

/* ---------- Плиточный вид списков (Договоры/Письма/ЭТЛ/Проекты) ----------
   Альтернатива table-wrap, включается в admin/display-settings.php. */
.tile-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 14px;
}

.tile-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.tile-card .tile-title { font-weight: 700; font-size: 15px; }
.tile-card .tile-row { font-size: 13px; display: flex; flex-direction: column; gap: 2px; width: 100%; }
.tile-card .tile-row span:first-child { color: var(--text-muted); }
.tile-card .tile-row span:last-child { color: var(--text); text-align: left; width: 100%; }
.tile-card .tile-actions { margin-top: 6px; display: flex; gap: 10px; font-size: 13px; }

.chip { display: inline-block; font-size: 11px; font-weight: 700; padding: 3px 9px; border-radius: 20px; }
.chip-critical { background: var(--critical-soft); color: var(--critical); }
.chip-warning { background: var(--warning-soft); color: var(--warning); }
.chip-good { background: var(--good-soft); color: var(--good); }

.alert { padding: 12px 16px; border-radius: 8px; font-size: 14px; margin-bottom: 16px; }
.alert-error { background: var(--critical-soft); color: var(--critical); }
.alert-notice { background: var(--accent-soft); color: var(--accent); }

/* ---------- Главная: сетка разделов ---------- */

/* Дашборд-плитки: сетка 5×3 подгоняется под высоту экрана, чтобы все
   15 модулей помещались без прокрутки. Графика модуля — крупный «водяной
   знак» на всю плитку, текст поверх сверху. */
.module-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 14px;
  height: calc(100vh - 124px);
  min-height: 420px;
}

.module-card {
  --glow: color-mix(in srgb, var(--tile-hue, var(--brand-navy)) 20%, transparent);
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background:
    radial-gradient(130% 140% at 102% 108%, var(--glow), transparent 58%),
    var(--surface);
  border: 1px solid var(--border);
  border-top: 3px solid var(--tile-hue, var(--brand-navy));
  border-radius: 14px;
  padding: 16px 18px;
  text-decoration: none;
  color: var(--text);
  min-height: 0;
  transition: border-color .15s ease, transform .15s ease, box-shadow .15s ease;
}

.module-card:hover {
  border-color: color-mix(in srgb, var(--tile-hue, var(--accent)) 55%, var(--border));
  transform: translateY(-3px);
  box-shadow: 0 14px 28px -14px color-mix(in srgb, var(--tile-hue, var(--brand-navy)) 45%, transparent);
}

.module-card:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Текст всегда над графикой, прижат к верху плитки */
.module-text {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.module-card .module-title {
  font-weight: 700;
  font-size: clamp(15px, 1.15vw, 19px);
  letter-spacing: -0.01em;
  line-height: 1.15;
}

.module-card .module-desc {
  font-size: clamp(11px, 0.8vw, 13px);
  color: var(--text-muted);
  line-height: 1.35;
}

/* Крупная фирменная пиктограмма модуля — «дуотон»: мягкая заливка
   (fill-opacity внутри самого SVG) + чёткие акцентные линии поверх,
   цвет управляется целиком отсюда через currentColor. */
.module-icon {
  position: absolute;
  right: -8%;
  bottom: -12%;
  width: 58%;
  height: auto;
  color: var(--tile-hue, var(--accent));
  pointer-events: none;
  transition: transform .2s ease, filter .2s ease;
}

.module-card:hover .module-icon {
  transform: scale(1.06) rotate(-2deg);
  filter: brightness(1.08);
}

@media (max-width: 1100px) {
  .module-grid {
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(5, 1fr);
  }
}

@media (max-width: 680px) {
  .module-grid {
    grid-template-columns: repeat(2, 1fr);
    grid-auto-rows: 110px;
    grid-template-rows: none;
    height: auto;
  }
}

/* ---------- Auth screen (login.php) ---------- */

.auth-shell {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--sidebar-bg);
  padding: 20px;
}

.auth-card {
  background: var(--surface);
  border-radius: 14px;
  padding: 36px 32px;
  width: 100%;
  max-width: 360px;
}

.auth-card h1 { font-size: 20px; margin-bottom: 4px; }
.auth-card p.lede { color: var(--text-muted); font-size: 13px; margin: 0 0 20px; }
.auth-card .btn { width: 100%; justify-content: center; }
