/* ============================================================
   Волна 2 — UX и юзабилити. Реальные стили, привязанные к ds-*
   DOM из base.html и страниц портала. Глобально задачи 301–600.
   Светлая/тёмная тема через [data-theme]; всё остальное —
   доступность, состояния, командная палитра, адаптив, микро-UI.
   ============================================================ */

/* --- 0. Брейкпоинты-токены (задача 206) --- */
:root {
  --bp-sm: 600px; --bp-md: 768px; --bp-lg: 1024px; --bp-xl: 1280px;
  --ux-z-toast: 1200; --ux-z-palette: 1300; --ux-z-modal: 1100;
  --ux-ease: cubic-bezier(.2,.7,.3,1);
}

/* --- 1. Доступность: видимый фокус-ринг (задачи 126, 167) --- */
a:focus-visible, button:focus-visible, input:focus-visible,
select:focus-visible, textarea:focus-visible, [tabindex]:focus-visible,
.ds-nav-link:focus-visible, [role="tab"]:focus-visible,
[role="option"]:focus-visible, [role="menuitem"]:focus-visible {
  outline: 2px solid var(--ds-accent, #3b82f6);
  outline-offset: 2px;
  border-radius: 6px;
}
.ds-table tbody tr:focus-visible { outline: 2px solid var(--ds-accent, #3b82f6); outline-offset: -2px; }

/* sr-only (задача 161) */
.sr-only, .ds-sr-only {
  position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.ds-sr-only-focusable:focus, .ds-sr-only-focusable:focus-within {
  position: static !important; width: auto; height: auto; margin: 0; clip: auto; white-space: normal;
}

/* минимальная цель клика 44px на тач (задачи 163, 215) */
@media (hover: none) and (pointer: coarse) {
  .ds-btn, .icon-btn, .ds-nav-link, [role="tab"], .ds-tab,
  input[type="checkbox"], input[type="radio"] { min-height: 44px; min-width: 44px; }
}
/* нет залипания hover на тач (задача 226) */
@media (hover: none) { .ds-btn:hover, .ds-nav-link:hover { transform: none; } }

/* prefers-reduced-motion (задача 147) */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important; animation-iteration-count: 1 !important;
    transition-duration: .001ms !important; scroll-behavior: auto !important;
  }
}

/* forced-colors / Windows HCM (задача 156) */
@media (forced-colors: active) {
  .ds-btn, .ds-card, .ds-modal, .ux-palette, .ux-toast, .ds-nav-link { border: 1px solid CanvasText; }
  .ds-nav-link.is-active { forced-color-adjust: none; outline: 2px solid Highlight; }
}

/* --- 2. Командная палитра (задачи 4,5,6,15,25,28,37,42,162,182) --- */
.ux-palette-backdrop {
  position: fixed; inset: 0; background: rgba(8,10,14,.55); backdrop-filter: blur(3px);
  z-index: var(--ux-z-palette); display: none;
}
.ux-palette-backdrop.is-open { display: block; }
.ux-palette {
  position: fixed; left: 50%; top: 12vh; transform: translateX(-50%);
  width: min(640px, 92vw); z-index: calc(var(--ux-z-palette) + 1);
  background: var(--ds-surface, #14181f); color: var(--ds-text, #e7ecf3);
  border: 1px solid var(--ds-border, rgba(255,255,255,.12)); border-radius: 14px;
  box-shadow: 0 24px 60px rgba(0,0,0,.5); overflow: hidden; display: none;
}
.ux-palette.is-open { display: block; animation: ux-pop .16s var(--ux-ease); }
.ux-palette-input {
  width: 100%; box-sizing: border-box; border: 0; border-bottom: 1px solid var(--ds-border, rgba(255,255,255,.1));
  background: transparent; color: inherit; font-size: 16px; padding: 16px 18px; outline: none;
}
.ux-palette-list { max-height: 52vh; overflow: auto; margin: 0; padding: 6px; list-style: none; }
.ux-palette-group { font-size: 11px; text-transform: uppercase; letter-spacing: .06em;
  color: var(--ds-muted, #93a1b3); padding: 10px 12px 4px; }
.ux-palette-item {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 9px;
  cursor: pointer; color: inherit; text-decoration: none;
}
.ux-palette-item .ux-pal-hint { margin-left: auto; font-size: 11px; color: var(--ds-muted, #93a1b3);
  border: 1px solid var(--ds-border, rgba(255,255,255,.16)); border-radius: 5px; padding: 1px 6px; }
.ux-palette-item[aria-selected="true"], .ux-palette-item:hover {
  background: color-mix(in srgb, var(--ds-accent, #3b82f6) 18%, transparent);
}
.ux-palette-item mark { background: color-mix(in srgb, var(--ds-accent,#3b82f6) 40%, transparent); color: inherit; border-radius: 3px; }
.ux-palette-empty { padding: 28px 16px; text-align: center; color: var(--ds-muted, #93a1b3); }
@keyframes ux-pop { from { opacity: 0; transform: translateX(-50%) translateY(-6px); } to { opacity: 1; } }
@media (max-width: 600px) { .ux-palette { top: 0; width: 100vw; border-radius: 0; } .ux-palette-list { max-height: 70vh; } }

/* --- 3. Кнопка «Наверх» (задача 11) --- */
.ux-to-top {
  position: fixed; right: 18px; bottom: 18px; z-index: 60; width: 44px; height: 44px;
  border-radius: 50%; border: 1px solid var(--ds-border, rgba(255,255,255,.14));
  background: var(--ds-surface, #14181f); color: var(--ds-text, #e7ecf3); cursor: pointer;
  display: grid; place-items: center; opacity: 0; pointer-events: none;
  transform: translateY(8px); transition: opacity .2s, transform .2s; box-shadow: 0 8px 24px rgba(0,0,0,.35);
}
.ux-to-top.is-visible { opacity: 1; pointer-events: auto; transform: none; }
@media (max-width: 600px) { .ux-to-top { bottom: calc(18px + env(safe-area-inset-bottom)); } }

/* --- 4. Тосты + undo (задачи 241,242,243,255,260,125) --- */
.ds-toast-stack, .ux-toast-stack {
  position: fixed; right: 16px; bottom: 16px; z-index: var(--ux-z-toast);
  display: flex; flex-direction: column; gap: 8px; max-width: min(380px, 92vw);
}
.ux-toast {
  display: flex; gap: 10px; align-items: flex-start; padding: 12px 14px; border-radius: 11px;
  background: var(--ds-surface, #14181f); color: var(--ds-text, #e7ecf3);
  border: 1px solid var(--ds-border, rgba(255,255,255,.12)); border-left: 3px solid var(--ds-muted, #7c8aa0);
  box-shadow: 0 12px 30px rgba(0,0,0,.4); animation: ux-toast-in .2s var(--ux-ease);
}
.ux-toast.is-leaving { opacity: 0; transform: translateY(8px); transition: .18s; }
.ux-toast-success { border-left-color: #22c55e; }
.ux-toast-danger { border-left-color: #ef4444; }
.ux-toast-warn { border-left-color: #f59e0b; }
.ux-toast-info { border-left-color: var(--ds-accent, #3b82f6); }
.ux-toast-body { flex: 1; min-width: 0; }
.ux-toast-title { font-weight: 700; font-size: 13px; }
.ux-toast-msg { font-size: 12.5px; color: var(--ds-muted, #93a1b3); margin-top: 2px; }
.ux-toast-action { background: none; border: 0; color: var(--ds-accent, #3b82f6); cursor: pointer; font-weight: 700; padding: 0; }
.ux-toast-close { background: none; border: 0; color: var(--ds-muted, #93a1b3); cursor: pointer; font-size: 16px; line-height: 1; }
@media (max-width: 600px) { .ds-toast-stack, .ux-toast-stack { left: 12px; right: 12px; bottom: calc(64px + env(safe-area-inset-bottom)); max-width: none; } }
@keyframes ux-toast-in { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; } }

/* --- 5. Состояния: скелетоны, empty, error (задачи 91,95,104,116,117,118) --- */
.ux-skel { position: relative; overflow: hidden; background: color-mix(in srgb, var(--ds-text,#e7ecf3) 8%, transparent);
  border-radius: 8px; height: 14px; }
.ux-skel::after { content: ""; position: absolute; inset: 0;
  background: linear-gradient(90deg, transparent, color-mix(in srgb, var(--ds-text,#fff) 10%, transparent), transparent);
  transform: translateX(-100%); animation: ux-shimmer 1.2s infinite; }
.ux-skel-row { height: 38px; margin: 6px 0; }
.ux-skel-card { height: 92px; border-radius: 12px; }
@keyframes ux-shimmer { to { transform: translateX(100%); } }
@media (prefers-reduced-motion: reduce) { .ux-skel::after { animation: none; } }

.ux-empty { display: grid; place-items: center; gap: 10px; text-align: center; padding: 42px 20px;
  color: var(--ds-muted, #93a1b3); }
.ux-empty .ux-empty-icon { width: 46px; height: 46px; opacity: .7; }
.ux-empty-title { font-weight: 800; color: var(--ds-text, #e7ecf3); font-size: 15px; }
.ux-empty-desc { max-width: 42ch; font-size: 13px; }

.ux-error-banner { display: flex; gap: 10px; align-items: flex-start; padding: 12px 14px; border-radius: 11px;
  background: color-mix(in srgb, #ef4444 12%, transparent); border: 1px solid color-mix(in srgb, #ef4444 40%, transparent);
  color: var(--ds-text, #e7ecf3); margin: 10px 0; }
.ux-error-banner.is-warn { background: color-mix(in srgb, #f59e0b 12%, transparent); border-color: color-mix(in srgb, #f59e0b 40%, transparent); }
.ux-error-banner .ux-error-action { margin-left: auto; }

/* offline / network banner (задача 106) */
.ux-offline-banner { position: fixed; left: 50%; top: 0; transform: translateX(-50%); z-index: 1400;
  background: #ef4444; color: #fff; padding: 6px 16px; border-radius: 0 0 10px 10px; font-size: 13px;
  font-weight: 700; display: none; }
body.is-offline .ux-offline-banner { display: block; }

/* --- 6. Массовые действия — action-bar (задачи 43,79,230) --- */
.ux-actionbar { position: sticky; bottom: 0; z-index: 40; display: none; gap: 10px; align-items: center;
  padding: 10px 14px; margin-top: 8px; border-radius: 12px;
  background: var(--ds-surface, #14181f); border: 1px solid var(--ds-border, rgba(255,255,255,.14));
  box-shadow: 0 -6px 24px rgba(0,0,0,.3); }
.ux-actionbar.is-active { display: flex; }
.ux-actionbar-count { font-weight: 800; }
@media (max-width: 600px) { .ux-actionbar { position: fixed; left: 8px; right: 8px; bottom: calc(8px + env(safe-area-inset-bottom)); flex-wrap: wrap; } }

/* --- 7. Формы: счётчик символов, статус сохранения, ошибки (задачи 49,51,61,133) --- */
.ux-char-count { font-size: 11px; color: var(--ds-muted, #93a1b3); text-align: right; }
.ux-char-count.is-over { color: #ef4444; font-weight: 700; }
.ux-save-state { font-size: 12px; color: var(--ds-muted, #93a1b3); }
.ux-save-state.is-dirty { color: #f59e0b; }
.ux-save-state.is-saved { color: #22c55e; }
[aria-invalid="true"] { border-color: #ef4444 !important; box-shadow: 0 0 0 3px color-mix(in srgb, #ef4444 22%, transparent); }
.ux-field-error { color: #ef4444; font-size: 12px; margin-top: 3px; }
.ux-error-summary { border: 1px solid color-mix(in srgb, #ef4444 40%, transparent);
  background: color-mix(in srgb, #ef4444 10%, transparent); border-radius: 10px; padding: 10px 14px; margin: 0 0 12px; }
.ux-error-summary ul { margin: 6px 0 0; padding-left: 18px; }
.ux-pw-strength { height: 5px; border-radius: 3px; background: var(--ds-border, #2a2f3a); margin-top: 6px; overflow: hidden; }
.ux-pw-strength > i { display: block; height: 100%; width: 0; transition: width .2s, background .2s; }

/* dropzone (задача 57) */
.ux-dropzone { border: 2px dashed var(--ds-border, rgba(255,255,255,.2)); border-radius: 12px; padding: 24px;
  text-align: center; color: var(--ds-muted, #93a1b3); transition: border-color .15s, background .15s; }
.ux-dropzone.is-drag { border-color: var(--ds-accent, #3b82f6); background: color-mix(in srgb, var(--ds-accent,#3b82f6) 10%, transparent); }

/* diff-строки импорта (задача 55) */
.ux-diff-new { background: color-mix(in srgb, #22c55e 14%, transparent); }
.ux-diff-chg { background: color-mix(in srgb, #f59e0b 14%, transparent); }
.ux-diff-same { opacity: .6; }

/* --- 8. Онбординг: модал, коачмарки, шорткат-оверлей (задачи 171,172,187,191,195) --- */
.ux-onb-backdrop { position: fixed; inset: 0; background: rgba(8,10,14,.6); z-index: var(--ux-z-modal); display: none; }
.ux-onb-backdrop.is-open { display: block; }
.ux-onb-modal { position: fixed; left: 50%; top: 50%; transform: translate(-50%,-50%); z-index: calc(var(--ux-z-modal)+1);
  width: min(460px, 92vw); background: var(--ds-surface,#14181f); color: var(--ds-text,#e7ecf3);
  border: 1px solid var(--ds-border, rgba(255,255,255,.12)); border-radius: 16px; padding: 22px; display: none;
  box-shadow: 0 24px 60px rgba(0,0,0,.5); }
.ux-onb-modal.is-open { display: block; }
.ux-coach { position: absolute; z-index: calc(var(--ux-z-modal)+2); width: min(280px,86vw);
  background: var(--ds-surface,#14181f); color: var(--ds-text,#e7ecf3); border-radius: 12px; padding: 14px;
  border: 1px solid var(--ds-accent,#3b82f6); box-shadow: 0 12px 32px rgba(0,0,0,.45); }
.ux-coach-target { position: relative; z-index: calc(var(--ux-z-modal)+1); box-shadow: 0 0 0 3px var(--ds-accent,#3b82f6), 0 0 0 9999px rgba(8,10,14,.6); border-radius: 8px; }
.ux-shortcuts { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 24px; }
.ux-shortcuts kbd { background: var(--ds-border,#2a2f3a); border-radius: 5px; padding: 1px 7px; font-size: 12px; }
.ux-hint-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--ds-accent,#3b82f6); margin-left: 6px; vertical-align: middle; }

/* --- 9. Тёмная/светлая тема через [data-theme] (задачи 271,272,284,294) --- */
:root, html[data-theme="dark"] {
  --ds-bg: #0f1115; --ds-surface: #14181f; --ds-text: #e7ecf3; --ds-muted: #93a1b3;
  --ds-border: rgba(255,255,255,.12); --ds-accent: #3b82f6;
}
html[data-theme="light"] {
  --ds-bg: #f6f8fb; --ds-surface: #ffffff; --ds-text: #16202b; --ds-muted: #5b6b7d;
  --ds-border: rgba(16,32,48,.14); --ds-accent: #2563eb;
  color-scheme: light;
}
html[data-theme="light"] body { background: var(--ds-bg); color: var(--ds-text); }
html[data-contrast="high"] { --ds-border: rgba(255,255,255,.4); --ds-muted: #cbd5e1; }
html[data-theme="light"][data-contrast="high"] { --ds-border: #16202b; --ds-muted: #16202b; }
html[data-ui-scale="s"] { font-size: 14px; } html[data-ui-scale="l"] { font-size: 18px; }
.ux-theme-toggle[aria-checked="true"] .ux-theme-on { display: inline; }
.ux-theme-toggle .ux-theme-on { display: none; }

/* --- 10. Адаптив: off-canvas, карточный вид таблиц, 320px (задачи 207,208,224,229,235) --- */
.ux-container { width: 100%; max-width: var(--bp-xl); margin-inline: auto; padding-inline: clamp(12px, 3vw, 28px); }
@media (max-width: 600px) {
  .ds-table.ux-cardize thead { position: absolute; left: -9999px; }
  .ds-table.ux-cardize tr { display: block; margin-bottom: 10px; border: 1px solid var(--ds-border, rgba(255,255,255,.12)); border-radius: 10px; padding: 6px 10px; }
  .ds-table.ux-cardize td { display: flex; justify-content: space-between; gap: 12px; border: 0; padding: 5px 0; }
  .ds-table.ux-cardize td::before { content: attr(data-label); font-weight: 700; color: var(--ds-muted, #93a1b3); }
}
.ux-table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.ux-table-scroll.is-overflow { box-shadow: inset -12px 0 12px -12px rgba(0,0,0,.5); }
/* длинные URL не ломают вёрстку (задача 234) */
.ux-break { overflow-wrap: anywhere; word-break: break-word; }
/* 100dvh для оверлеев (задача 240) */
.ux-palette, .ux-onb-backdrop, .ux-onb-modal { max-height: 100dvh; }

/* адаптивная типографика (задача 217) */
h1, .tb-title { font-size: clamp(18px, 2.2vw, 24px); }

/* микро hover/active кнопок (задача 249) */
.ds-btn { transition: transform .12s var(--ux-ease), box-shadow .12s; }
.ds-btn:active { transform: translateY(1px); }

/* подсветка инлайн-сохранения ячейки (задача 269) */
.ux-cell-saved { animation: ux-cell-flash 1s var(--ux-ease); }
@keyframes ux-cell-flash { 0% { background: color-mix(in srgb, #22c55e 35%, transparent); } 100% { background: transparent; } }

/* индикатор внешней ссылки (задача 27) */
a[data-external]::after { content: "↗"; font-size: .8em; margin-left: 3px; opacity: .7; }

/* бейдж окружения (задача 33) */
.ux-env-badge { font-size: 10px; font-weight: 800; text-transform: uppercase; padding: 2px 7px; border-radius: 6px; }
.ux-env-badge[data-env="dev"] { background: #f59e0b; color: #111; }
.ux-env-badge[data-env="stage"] { background: #8b5cf6; color: #fff; }
.ux-env-badge[data-env="prod"] { background: #22c55e; color: #06210f; }
