
  /* =========================================================
     1. ПЕРЕМЕННЫЕ (Design Tokens)
     ========================================================= */
  :root {
    /* --- Базовая палитра: Linear-style, светлая тема по умолчанию --- */
    --bg: #f6f7f9;
    --text: #16171a;
    --muted: #6b7077;
    --panel: #ffffff;
    --panel-border: #e6e7ec;
    --sidebar-bg: #16171a;
    --sidebar-fg: #c9ccd3;
    --primary: #5e6ad2;
    --primary-rgb: 94,106,210;
    --primary-text: #ffffff;
    --accent: #5e6ad2;

    /* --- Элементы ввода --- */
    --input-bg: #ffffff;
    --input-text: #16171a;
    --focus-ring: rgba(94,106,210,0.32);

    /* --- Типографика --- */
    --font-main: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --font-weight-base: 400; /* Normal */
    --font-style-base: normal; /* Normal/Italic */

    /* --- Размеры --- */
    --header-h: 64px;
    --sidebar-w: 280px;
    --radius: 10px;
    --shadow: 0 1px 2px rgba(0,0,0,0.04), 0 6px 16px rgba(0,0,0,0.06);

    /* --- Адаптивный шрифт --- */
    --fs-base: clamp(14px, 1.2vw + 10px, 18px);
    --fs-h2: clamp(20px, 2vw + 12px, 32px);
    --fs-h3: clamp(16px, 1.5vw + 10px, 24px);
    --fs-muted: clamp(12px, 0.8vw + 8px, 14px);

    --transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  }

  /* --- ТЕМНАЯ ТЕМА (Системная) — Linear graphite --- */
  @media (prefers-color-scheme: dark) {
    :root {
      --bg: #0b0c0e;
      --text: #edeef0;
      --muted: #8a8f98;
      --panel: #161719;
      --panel-border: #26272b;
      --sidebar-bg: #0e0f11;
      --sidebar-fg: #d2d4d9;
      --primary: #7c82e8;
      --primary-rgb: 124,130,232;
      --input-bg: #1c1d20;
      --input-text: #edeef0;
      --focus-ring: rgba(124,130,232,0.40);
      --shadow: 0 1px 2px rgba(0,0,0,0.3), 0 8px 24px rgba(0,0,0,0.45);
    }
  }

  /* --- ПРИНУДИТЕЛЬНЫЕ ТЕМЫ (перебивают системные) --- */
  html[data-theme="light"] {
    --bg: #f6f7f9; --text: #16171a; --muted: #6b7077; --panel: #ffffff; --panel-border: #e6e7ec;
    --sidebar-bg: #16171a; --sidebar-fg: #c9ccd3; --primary: #5e6ad2; --primary-rgb: 94,106,210; --primary-text: #ffffff;
    --input-bg: #ffffff; --input-text: #16171a; --focus-ring: rgba(94,106,210,0.32);
    --shadow: 0 1px 2px rgba(0,0,0,0.04), 0 6px 16px rgba(0,0,0,0.06);
  }

  html[data-theme="dark"] {
    --bg: #0b0c0e; --text: #edeef0; --muted: #8a8f98; --panel: #161719; --panel-border: #26272b;
    --sidebar-bg: #0e0f11; --sidebar-fg: #d2d4d9; --primary: #7c82e8; --primary-rgb: 124,130,232; --primary-text: #ffffff;
    --input-bg: #1c1d20; --input-text: #edeef0; --focus-ring: rgba(124,130,232,0.40);
    --shadow: 0 1px 2px rgba(0,0,0,0.3), 0 8px 24px rgba(0,0,0,0.45);
  }

  /* Масштаб */
  body[data-scale="small"]  { font-size: 0.85em; }
  body[data-scale="normal"] { font-size: 1em; }
  body[data-scale="large"]  { font-size: 1.15em; }
  body[data-scale="xl"]     { font-size: 1.3em; }

  /* Скрытие Живого Персонажа по настройке */
  body.hide-live-character .live-buddy-wrap,
  body.hide-live-character #liveBuddy,
  body.hide-live-character #buddyBubble,
  body.hide-live-character #buddyImg,
  body.hide-live-character .buddy-bubble,
  body.hide-live-character .buddy-img,
  body.hide-live-character [data-live-character],
  body.hide-live-character [data-widget="live-character"],
  body.hide-live-character [data-role="live-character"],
  body.hide-live-character #buddy,
  body.hide-live-character .buddy,
  body.hide-live-character [data-buddy],
  body.hide-live-character .floating-buddy,
  body.hide-live-character .assistant-widget,
  body.hide-live-character .persona-widget,
  body.hide-live-character .live-persona,
  body.hide-live-character .character-widget {
    display: none !important;
    visibility: hidden !important;
    opacity: 0 !important;
    pointer-events: none !important;
  }

  html, body {
    margin: 0; padding: 0;
    font-family: var(--font-main); /* Применяем шрифт */
    font-weight: var(--font-weight-base); /* Жирность */
    font-style: var(--font-style-base); /* Курсив */
    background: var(--bg);
    color: var(--text);
    height: 100%;
    overflow: hidden;
    -webkit-font-smoothing: antialiased;
  }

  /* Сетка */
  .layout-grid {
    display: grid;
    grid-template-columns: var(--sidebar-w) 1fr;
    height: calc(100vh - var(--header-h));
    overflow: hidden;
    position: relative;
  }

  /* Шапка */
  header.app-header {
    height: var(--header-h);
    background: var(--panel);
    border-bottom: 1px solid var(--panel-border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    box-sizing: border-box;
    z-index: 1000;
    position: relative;
    box-shadow: 0 2px 8px rgba(0,0,0,0.02);
  }

  .header-left { display: flex; align-items: center; gap: 15px; }
  .logo-icon { font-size: 24px; }
  header h1 { font-size: 1.15rem; margin: 0; font-weight: 800; letter-spacing: -0.02em; }

  /* Кнопка "Гамбургер" для мобильных */
  .menu-toggle {
    display: none;
    background: none;
    border: none;
    padding: 8px;
    cursor: pointer;
    color: var(--text);
    border-radius: 8px;
    transition: background 0.2s;
  }
  .menu-toggle:hover { background: rgba(128,128,128,0.1); }

  /* Сайдбар */
  aside.sidebar {
    background: var(--sidebar-bg);
    border-right: 1px solid var(--panel-border);
    overflow: hidden;
    height: 100%;
    transition: transform var(--transition);
    z-index: 500;
  }

  /* Контент */
  main.app-main {
    height: 100%;
    overflow-y: auto;
    padding: clamp(16px, 3vw, 32px);
    box-sizing: border-box;
    scroll-behavior: smooth;
    background: var(--bg);
    -webkit-overflow-scrolling: touch;
  }

  /* ✅ ИСПРАВЛЕНИЕ ЗДЕСЬ: Убрали ограничение в 1100px. Теперь контент на 100% ширины */
  .page-wrapper {
    width: 100%;
    max-width: 100%;
    margin: 0;
    box-sizing: border-box;
  }

  /* =========================================================
     3. МОБИЛЬНАЯ АДАПТАЦИЯ (Шторка)
     ========================================================= */
  @media (max-width: 980px) {
    .layout-grid {
      grid-template-columns: 1fr; /* Сайдбар уходит из сетки */
    }

    aside.sidebar {
      position: fixed;
      top: var(--header-h);
      left: 0;
      width: min(300px, 80vw);
      height: calc(100vh - var(--header-h));
      transform: translateX(-100%); /* Прячем влево */
      box-shadow: 15px 0 40px rgba(0,0,0,0.15);
    }

    /* Состояние открытого мобильного меню */
    body.menu-open aside.sidebar {
      transform: translateX(0);
    }

    .menu-toggle { display: block; }

    /* Затемнение фона при открытом меню */
    .sidebar-overlay {
      display: none;
      position: fixed;
      inset: var(--header-h) 0 0 0;
      background: rgba(0,0,0,0.4);
      backdrop-filter: blur(3px);
      z-index: 400;
    }
    body.menu-open .sidebar-overlay { display: block; }
  }

  /* =========================================================
     4. UI КОМПОНЕНТЫ И ТИПОГРАФИКА
     ========================================================= */
  .card { 
    background: var(--panel); 
    border-radius: var(--radius); 
    padding: clamp(18px, 2.5vw, 24px); 
    margin-bottom: 20px; 
    border: 1px solid var(--panel-border);
    box-shadow: var(--shadow);
  }

  /* Липкая шапка таблиц на всех страницах */
  .page-wrapper table {
    border-collapse: separate;
    border-spacing: 0;
  }

  .page-wrapper table thead th,
  .page-wrapper table thead td,
  .page-wrapper table.jw-sticky-table thead th,
  .page-wrapper table.jw-sticky-table thead td,
  .page-wrapper table .jw-sticky-th {
    position: sticky !important;
    top: 0;
    z-index: 20;
    background: var(--sidebar-bg) !important;
    color: var(--primary-text) !important;
    background-clip: padding-box;
    box-shadow: 0 1px 0 var(--panel-border);
  }

  .page-wrapper table thead tr {
    position: sticky;
    top: 0;
    z-index: 19;
  }

  .jw-resizable-table {
    table-layout: fixed;
  }

  .jw-resizable-table th {
    position: sticky !important;
    overflow: visible !important;
  }

  .jw-resizable-table th .col-resizer {
    position: absolute;
    top: 0;
    right: -3px;
    width: 7px;
    height: 100%;
    cursor: col-resize;
    user-select: none;
    touch-action: none;
    z-index: 35;
  }

  .jw-resizable-table th .col-resizer::after {
    content: '';
    position: absolute;
    top: 18%;
    bottom: 18%;
    left: 3px;
    width: 1px;
    background: rgba(255,255,255,0.38);
    opacity: 0;
    transition: opacity 0.15s ease;
  }

  .jw-resizable-table th:hover .col-resizer::after,
  .jw-resizable-table th.resizing .col-resizer::after {
    opacity: 1;
  }

  .jw-resizable-table.jw-is-resizing,
  .jw-resizable-table.jw-is-resizing * {
    cursor: col-resize !important;
    user-select: none !important;
  }

  @media (max-width: 760px), (pointer: coarse) {
    .jw-resizable-table th .col-resizer {
      display: none;
    }
  }

  h2 { font-size: var(--fs-h2); line-height: 1.15; font-weight: 800; margin-top: 0; margin-bottom: 10px; }
  h3 { font-size: var(--fs-h3); line-height: 1.25; font-weight: 700; margin-top: 0; margin-bottom: 10px; }
  p { font-size: var(--fs-base); line-height: 1.6; opacity: 0.95; }
  .muted { color: var(--muted, #8e8e93); font-size: var(--fs-muted); }

  /* Кнопки (Linear-style: компактные, плоские, чёткий фокус) */
  .btn, button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: var(--primary);
    color: var(--primary-text);
    border: 1px solid transparent;
    padding: 9px 16px;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.15s ease, border-color 0.15s ease, filter 0.15s ease, opacity 0.15s ease;
    text-decoration: none;
    font-size: var(--fs-base);
    font-family: inherit;
    gap: 8px;
  }
  .btn:hover { filter: brightness(1.08); }
  .btn:active, button:active { filter: brightness(0.96); }
  .btn:disabled, button:disabled { opacity: 0.45; cursor: not-allowed; filter: none; }
  .btn:focus-visible, button:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
  .btn.secondary { background: var(--input-bg); color: var(--text); border: 1px solid var(--panel-border); }
  .btn.secondary:hover { border-color: var(--primary); background: rgba(var(--primary-rgb),0.06); filter: none; }

  /* Поля ввода (с настраиваемой подсветкой) */
  input, select, textarea {
    background: var(--input-bg);
    color: var(--input-text);
    border: 1px solid var(--panel-border);
    border-radius: 8px;
    padding: 11px 12px;
    font-size: var(--fs-base);
    font-family: inherit;
    box-sizing: border-box;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
  }

  /* ПОДСВЕТКА ФОКУСА (Настраиваемая) */
  input:focus, select:focus, textarea:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 4px var(--focus-ring); /* Использует переменную из настроек */
  }
  input:hover:not(:focus):not(:disabled),
  select:hover:not(:focus):not(:disabled),
  textarea:hover:not(:focus):not(:disabled) { border-color: var(--primary); }
  input::placeholder, textarea::placeholder { color: #9a9aa0; opacity: 1; }
  input:disabled, select:disabled, textarea:disabled { opacity: 0.6; cursor: not-allowed; }

  /* Современные мелочи, применяются ко всем страницам */
  :root { accent-color: var(--primary); }
  ::selection { background: var(--focus-ring); }
  html { scroll-behavior: smooth; }

  /* Справочники и выбор из каталогов */
  .jw-dict-field {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
    width: 100%;
  }
  .jw-dict-field > input,
  .jw-dict-field > select,
  .jw-dict-field > textarea {
    flex: 1 1 auto;
    min-width: 0;
  }
  .jw-dict-btn {
    flex: 0 0 auto;
    min-width: 42px;
    align-self: flex-end;
    padding: 0 10px;
    border-radius: 10px;
    border: 1px solid var(--panel-border);
    background: var(--input-bg);
    color: var(--text);
    font-size: 18px;
    line-height: 1;
    cursor: pointer;
    box-shadow: none;
  }
  .jw-dict-btn:hover {
    border-color: var(--primary);
    transform: translateY(-1px);
  }
  .jw-dict-panel {
    width: min(980px, 100%);
    max-height: 88vh;
    background: var(--panel);
    border: 1px solid var(--panel-border);
    border-radius: 24px;
    box-shadow: 0 24px 70px rgba(0,0,0,0.28);
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }
  .jw-dict-top {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    padding: 18px 20px 12px;
    border-bottom: 1px solid var(--panel-border);
  }
  .jw-dict-title {
    font-size: 20px;
    font-weight: 900;
  }
  .jw-dict-meta {
    margin-top: 4px;
  }
  .jw-dict-search {
    margin: 14px 20px 10px;
    width: calc(100% - 40px);
  }
  .jw-dict-tabs {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 0 20px 12px;
    border-bottom: 1px solid var(--panel-border);
  }
  .jw-dict-tab.active {
    background: var(--primary) !important;
    color: #fff !important;
    border-color: var(--primary) !important;
  }
  .jw-dict-list {
    overflow: auto;
    padding: 12px 20px 18px;
    display: grid;
    gap: 10px;
  }
  .jw-dict-item {
    border: 1px solid var(--panel-border);
    border-radius: 16px;
    padding: 14px;
    background: var(--sidebar-bg);
    display: flex;
    gap: 12px;
    justify-content: space-between;
    align-items: flex-start;
  }
  .jw-dict-item:hover {
    border-color: var(--primary);
  }
  .jw-dict-item-main {
    min-width: 0;
  }
  .jw-dict-item-title {
    font-weight: 800;
    word-break: break-word;
  }
  .jw-dict-item-sub {
    margin-top: 4px;
    color: var(--muted);
    font-size: 12px;
    line-height: 1.45;
  }
  .jw-dict-chips {
    margin-top: 8px;
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
  }
  .jw-dict-chip {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 8px;
    border-radius: 999px;
    background: rgba(var(--primary-rgb),0.12);
    color: var(--primary);
    font-size: 11px;
    font-weight: 700;
  }
  .jw-dict-actions {
    display: flex;
    gap: 8px;
    flex-shrink: 0;
  }

  /* =========================================================
     5. ТАБЛИЦЫ И РЕСАЙЗ КОЛОНОК
     ========================================================= */
  .table-wrap { overflow-x: auto; border-radius: var(--radius); border: 1px solid var(--panel-border); }
  table { width: 100%; border-collapse: collapse; background: var(--panel); }
  
  th { 
    position: relative; 
    text-align: left; 
    padding: 14px 12px; 
    font-size: 11px; 
    text-transform: uppercase; 
    color: #888; 
    background: var(--sidebar-bg); 
    border-bottom: 2px solid var(--panel-border); 
    user-select: none;
  }
  
  td { padding: 12px; border-bottom: 1px solid var(--panel-border); font-size: var(--fs-base); vertical-align: top; }

  /* Инструмент изменения ширины (Scripts.html) */
  .col-resizer {
    position: absolute; top: 0; right: 0; width: 6px; height: 100%;
    cursor: col-resize; z-index: 10; user-select: none;
  }
  .col-resizer:hover { background: var(--primary); opacity: 0.5; }

  th.jw-sortable {
    cursor: pointer;
    padding-right: 30px;
  }
  th.jw-sortable::after {
    content: "↕";
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 12px;
    opacity: 0.45;
    pointer-events: none;
  }
  th.jw-sortable:hover::after,
  th.jw-sortable:focus-visible::after {
    opacity: 0.9;
  }
  th.jw-sortable:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: -2px;
  }
  th.jw-sort-asc::after { content: "↑"; opacity: 1; }
  th.jw-sort-desc::after { content: "↓"; opacity: 1; }

  /* Кастомизация скроллбара */
  main.app-main::-webkit-scrollbar { width: 8px; }
  main.app-main::-webkit-scrollbar-track { background: transparent; }
  main.app-main::-webkit-scrollbar-thumb { background: rgba(0,0,0,0.1); border-radius: 10px; }
  body.theme-dark main.app-main::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.1); }

  @media (max-width: 768px) {
    :root {
      --header-h: 58px;
      --radius: 12px;
      --fs-base: 16px;
    }

    header.app-header {
      padding: 0 12px;
    }

    header h1 {
      font-size: 0.98rem;
      max-width: 46vw;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    .logo-icon {
      font-size: 20px;
    }

    .scale-wrap {
      display: none;
    }

    main.app-main {
      padding: 12px 10px 92px;
    }

    .card {
      padding: 14px;
      margin-bottom: 12px;
      border-radius: 14px;
      box-shadow: 0 2px 8px rgba(0,0,0,0.06);
    }

    .btn, button {
      min-height: 44px;
      padding: 10px 12px;
      border-radius: 12px;
      touch-action: manipulation;
    }

    .btn.small, button.small,
    .btn.tiny, button.tiny {
      min-height: 38px;
    }

    .jw-dict-panel {
      border-radius: 20px;
      max-height: 84vh;
    }
    .jw-dict-top {
      padding: 16px;
    }
    .jw-dict-search {
      margin: 12px 16px 10px;
      width: calc(100% - 32px);
    }
    .jw-dict-tabs,
    .jw-dict-list {
      padding-left: 16px;
      padding-right: 16px;
    }

    input, select, textarea {
      font-size: 16px;
      min-height: 44px;
    }

    textarea {
      line-height: 1.45;
    }

    .form-grid,
    .output-grid,
    .admin-grid,
    .metric-grid {
      grid-template-columns: 1fr !important;
    }

    .table-wrap,
    .table-container,
    .table-wrapper,
    [style*="overflow-x:auto"],
    [style*="overflow-x: auto"] {
      -webkit-overflow-scrolling: touch;
      scroll-snap-type: x proximity;
      border-radius: 14px;
    }

    table {
      min-width: 680px;
    }

    th, td {
      padding: 10px 8px;
    }

    th.jw-sortable::after {
      right: 6px;
    }

    .jw-textarea-expand-btn {
      width: 100%;
    }
  }

  /* Учет безопасных зон iOS */
  @supports (padding: env(safe-area-inset-top)) {
    header.app-header {
      padding-top: env(safe-area-inset-top);
      height: calc(var(--header-h) + env(safe-area-inset-top));
    }
    aside.sidebar {
      padding-bottom: env(safe-area-inset-bottom);
    }
  }
