/*
 * Дизайн-система бэкофиса Polylo: токены, шрифты, база, кнопки, поля, вход.
 *
 * Файл публичный — его загружает и страница входа, и закрытая часть. Здесь нет ничего об
 * экранах бэкофиса: каркас, таблицы и окна — в adminui-app/app.css.
 *
 * Стиль — строгий минимализм для рабочего инструмента: плотная сетка, ясная иерархия текста,
 * один акцентный цвет (зелёный — действие и «всё в порядке»). Цвета задаются только токенами;
 * у тёмной темы свои оттенки, а не инверсия светлой.
 */

/* ---------- Шрифты ---------- */

/* Fira Sans и Fira Code (лицензия SIL OFL), свои копии: бэкофис не обращается к чужим адресам. */
@font-face { font-family: "Fira Sans"; font-style: normal; font-weight: 400; font-display: swap;
  src: url("/admin/ui/fonts/fira-sans-400-cyrillic.woff2") format("woff2");
  unicode-range: U+0301, U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116; }
@font-face { font-family: "Fira Sans"; font-style: normal; font-weight: 400; font-display: swap;
  src: url("/admin/ui/fonts/fira-sans-400-latin.woff2") format("woff2");
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; }
@font-face { font-family: "Fira Sans"; font-style: normal; font-weight: 500; font-display: swap;
  src: url("/admin/ui/fonts/fira-sans-500-cyrillic.woff2") format("woff2");
  unicode-range: U+0301, U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116; }
@font-face { font-family: "Fira Sans"; font-style: normal; font-weight: 500; font-display: swap;
  src: url("/admin/ui/fonts/fira-sans-500-latin.woff2") format("woff2");
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; }
@font-face { font-family: "Fira Sans"; font-style: normal; font-weight: 600; font-display: swap;
  src: url("/admin/ui/fonts/fira-sans-600-cyrillic.woff2") format("woff2");
  unicode-range: U+0301, U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116; }
@font-face { font-family: "Fira Sans"; font-style: normal; font-weight: 600; font-display: swap;
  src: url("/admin/ui/fonts/fira-sans-600-latin.woff2") format("woff2");
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; }
@font-face { font-family: "Fira Code"; font-style: normal; font-weight: 400 500; font-display: swap;
  src: url("/admin/ui/fonts/fira-code-cyrillic.woff2") format("woff2");
  unicode-range: U+0301, U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116; }
@font-face { font-family: "Fira Code"; font-style: normal; font-weight: 400 500; font-display: swap;
  src: url("/admin/ui/fonts/fira-code-latin.woff2") format("woff2");
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; }

/* ---------- Токены ---------- */

:root {
  color-scheme: light;

  --font-sans: "Fira Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-mono: "Fira Code", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;

  /* Поверхности и текст. Контраст основного и приглушённого текста — не ниже 4.5:1. */
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-2: #f1f5f9;
  --surface-hover: #f1f5f9;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --text-muted: #475569;
  --text-subtle: #64748b;

  /* Акцент — основное действие и выбранное (индиго, белый текст 6.3:1). Успех и прогресс — зелёный. */
  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --on-accent: #ffffff;
  --accent-soft: #eef2ff;
  --accent-soft-text: #3730a3;
  --success: #15803d;
  --success-soft: #dcfce7;
  --success-soft-text: #14532d;

  --danger: #b91c1c;
  --danger-hover: #991b1b;
  --on-danger: #ffffff;
  --danger-soft: #fee2e2;
  --danger-soft-text: #7f1d1d;
  --warning: #d97706;
  --warning-soft: #fef3c7;
  --warning-soft-text: #78350f;
  --info-soft: #dbeafe;
  --info-soft-text: #1e3a8a;
  --neutral-soft: #e2e8f0;
  --neutral-soft-text: #334155;

  --focus: #4f46e5;
  --scrim: rgb(15 23 42 / 0.45);
  --qr-bg: #ffffff;

  --shadow-sm: 0 1px 2px rgb(15 23 42 / 0.06);
  --shadow: 0 1px 2px rgb(15 23 42 / 0.06), 0 2px 6px rgb(15 23 42 / 0.06);
  --shadow-lg: 0 12px 32px rgb(15 23 42 / 0.18);

  /* Размеры. Плотная шкала рабочего инструмента: шаг 4px. */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --space-8: 32px;
  --space-10: 40px;

  --text-xs: 12px;
  --text-sm: 13px;
  --text-base: 14px;
  --text-md: 16px;
  --text-lg: 18px;
  --text-xl: 22px;
  --leading: 1.5;

  --radius-sm: 6px;
  --radius: 8px;
  --radius-lg: 12px;
  --radius-full: 999px;

  --control-h: 36px;
  --icon: 16px;
  --icon-lg: 20px;
  --content-max: 1120px;
  --login-max: 400px;

  --dur-fast: 120ms;
  --dur: 180ms;
  --ease: cubic-bezier(0.2, 0.7, 0.3, 1);
}

/* Тёмная тема: по настройке системы, если тема не выбрана явно, и по явному выбору. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0f172a;
    --surface: #1b2336;
    --surface-2: #232c40;
    --surface-hover: #272f42;
    --border: #334155;
    --border-strong: #475569;
    --text: #f8fafc;
    --text-muted: #cbd5e1;
    --text-subtle: #94a3b8;
    --accent: #a5b4fc;
    --accent-hover: #c7d2fe;
    --on-accent: #1e1b4b;
    --accent-soft: rgb(129 140 248 / 0.18);
    --accent-soft-text: #c7d2fe;
    --success: #4ade80;
    --success-soft: rgb(34 197 94 / 0.16);
    --success-soft-text: #86efac;
    --danger: #f87171;
    --danger-hover: #fca5a5;
    --on-danger: #0f172a;
    --danger-soft: rgb(248 113 113 / 0.16);
    --danger-soft-text: #fca5a5;
    --warning: #fbbf24;
    --warning-soft: rgb(251 191 36 / 0.16);
    --warning-soft-text: #fcd34d;
    --info-soft: rgb(96 165 250 / 0.16);
    --info-soft-text: #93c5fd;
    --neutral-soft: #334155;
    --neutral-soft-text: #e2e8f0;
    --focus: #a5b4fc;
    --scrim: rgb(2 6 23 / 0.6);
    --shadow-sm: 0 1px 2px rgb(0 0 0 / 0.3);
    --shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 2px 8px rgb(0 0 0 / 0.25);
    --shadow-lg: 0 12px 32px rgb(0 0 0 / 0.5);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f172a;
  --surface: #1b2336;
  --surface-2: #232c40;
  --surface-hover: #272f42;
  --border: #334155;
  --border-strong: #475569;
  --text: #f8fafc;
  --text-muted: #cbd5e1;
  --text-subtle: #94a3b8;
  --accent: #a5b4fc;
  --accent-hover: #c7d2fe;
  --on-accent: #1e1b4b;
  --accent-soft: rgb(129 140 248 / 0.18);
  --accent-soft-text: #c7d2fe;
  --success: #4ade80;
  --success-soft: rgb(34 197 94 / 0.16);
  --success-soft-text: #86efac;
  --danger: #f87171;
  --danger-hover: #fca5a5;
  --on-danger: #0f172a;
  --danger-soft: rgb(248 113 113 / 0.16);
  --danger-soft-text: #fca5a5;
  --warning: #fbbf24;
    --warning-soft: rgb(251 191 36 / 0.16);
  --warning-soft-text: #fcd34d;
  --info-soft: rgb(96 165 250 / 0.16);
  --info-soft-text: #93c5fd;
  --neutral-soft: #334155;
  --neutral-soft-text: #e2e8f0;
  --focus: #a5b4fc;
  --scrim: rgb(2 6 23 / 0.6);
  --shadow-sm: 0 1px 2px rgb(0 0 0 / 0.3);
  --shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 2px 8px rgb(0 0 0 / 0.25);
  --shadow-lg: 0 12px 32px rgb(0 0 0 / 0.5);
}

/* На сенсорных экранах элементы управления крупнее: зона нажатия не меньше 44px. */
@media (pointer: coarse) {
  :root { --control-h: 44px; }
}

/* ---------- База ---------- */

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: var(--leading);
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; line-height: 1.25; font-weight: 600; }
h1 { font-size: var(--text-xl); }
h2 { font-size: var(--text-lg); }
h3 { font-size: var(--text-md); }
p { margin: 0; }

a { color: var(--accent); text-underline-offset: 2px; }
a:hover { color: var(--accent-hover); }

code, kbd, .mono { font-family: var(--font-mono); font-size: 0.95em; }

:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.icon {
  width: var(--icon);
  height: var(--icon);
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.icon-lg { width: var(--icon-lg); height: var(--icon-lg); }

.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }

.hidden { display: none !important; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.stack { display: flex; flex-direction: column; gap: var(--space-4); }
.stack-sm { display: flex; flex-direction: column; gap: var(--space-2); }
.row { display: flex; align-items: center; gap: var(--space-2); flex-wrap: wrap; }
.muted { color: var(--text-muted); }
.subtle { color: var(--text-subtle); }
.text-sm { font-size: var(--text-sm); }
.text-danger { color: var(--danger); }

/* ---------- Кнопки ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: var(--control-h);
  padding: 0 var(--space-4);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 500;
  line-height: 1;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
  transition: background-color var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease),
    color var(--dur-fast) var(--ease);
}
.btn:hover { background: var(--surface-hover); color: var(--text); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }

.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); color: var(--on-accent); }

.btn-danger { color: var(--danger); }
.btn-danger:hover { background: var(--danger-soft); color: var(--danger); }
.btn-danger-solid { background: var(--danger); border-color: var(--danger); color: var(--on-danger); }
.btn-danger-solid:hover { background: var(--danger-hover); border-color: var(--danger-hover); color: var(--on-danger); }

.btn-ghost { background: transparent; border-color: transparent; }
.btn-ghost:hover { background: var(--surface-hover); }

.btn-block { width: 100%; }

/* Кнопка-иконка: квадрат не меньше высоты поля, подпись — в aria-label. */
.btn-icon { width: var(--control-h); padding: 0; }

.btn-link {
  min-height: auto;
  padding: 0;
  border: 0;
  background: none;
  color: var(--accent);
  font-weight: 500;
}
.btn-link:hover { background: none; color: var(--accent-hover); text-decoration: underline; }

/* Идёт запрос: кнопка заблокирована, вместо текста — индикатор, ширина не прыгает. */
.btn.is-loading { position: relative; color: transparent; pointer-events: none; }
.btn.is-loading > * { visibility: hidden; }
.btn.is-loading::after {
  content: "";
  position: absolute;
  width: 16px;
  height: 16px;
  border: 2px solid currentColor;
  border-color: var(--text-subtle) transparent var(--text-subtle) transparent;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
.btn-primary.is-loading::after, .btn-danger-solid.is-loading::after {
  border-color: var(--on-accent) transparent var(--on-accent) transparent;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Поля ---------- */

.field { display: flex; flex-direction: column; gap: var(--space-1); }
.field-label { font-weight: 500; color: var(--text); }

input, select, textarea {
  width: 100%;
  min-height: var(--control-h);
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  /* 16px: меньший шрифт в поле заставляет iOS приближать страницу при фокусе. */
  font: inherit;
  font-size: var(--text-md);
  line-height: 1.3;
  transition: border-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--focus);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 25%, transparent);
}
input[aria-invalid="true"] { border-color: var(--danger); }

input[type="radio"], input[type="checkbox"] {
  width: 18px;
  height: 18px;
  min-height: 0;
  margin: 0;
  padding: 0;
  accent-color: var(--accent);
  box-shadow: none;
}

.field-code {
  font-family: var(--font-mono);
  font-size: var(--text-lg);
  letter-spacing: 0.15em;
  text-align: center;
}

.hint { font-size: var(--text-sm); color: var(--text-subtle); }
.hint.text-danger { color: var(--danger); }

.check { display: flex; align-items: center; gap: var(--space-2); min-height: var(--control-h); cursor: pointer; }

/* Сообщение об ошибке у формы: цвет вместе с иконкой и текстом. */
.form-error {
  display: flex;
  gap: var(--space-2);
  align-items: flex-start;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius);
  background: var(--danger-soft);
  color: var(--danger-soft-text);
  font-size: var(--text-sm);
}

/* ---------- Резервные коды ---------- */

.codes {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--space-2) var(--space-4);
  margin: 0;
  padding: var(--space-4);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface-2);
  list-style: none;
  font-family: var(--font-mono);
  font-size: var(--text-md);
  text-align: center;
}

.qr {
  width: 200px;
  height: 200px;
  margin: 0 auto;
  padding: var(--space-2);
  border-radius: var(--radius);
  /* Камера читает код только на светлом фоне — и в тёмной теме тоже. */
  background: var(--qr-bg);
}
.qr svg { display: block; width: 100%; height: 100%; }

.secret {
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius);
  background: var(--surface-2);
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  word-break: break-all;
  user-select: all;
}

/* ---------- Вход ---------- */

.login {
  display: grid;
  place-items: center;
  min-height: 100vh;
  padding: var(--space-6) var(--space-4);
}

.login-card {
  width: 100%;
  max-width: var(--login-max);
  padding: var(--space-8) var(--space-6);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.brand { display: flex; align-items: center; gap: var(--space-2); font-weight: 600; font-size: var(--text-md); }
.brand-mark {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: var(--on-accent);
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  font-weight: 500;
}
.brand-sub { color: var(--text-subtle); font-weight: 400; }

.login-head { display: flex; flex-direction: column; gap: var(--space-2); margin-bottom: var(--space-6); }
.login-title { font-size: var(--text-lg); }
.login-steps { margin: 0; padding-left: var(--space-5); color: var(--text-muted); }
.login-steps li + li { margin-top: var(--space-1); }

@media (max-width: 480px) {
  .login { padding: var(--space-4); align-items: start; }
  .login-card { padding: var(--space-6) var(--space-4); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0ms !important; animation-duration: 0ms !important; }
  .btn.is-loading::after { animation: spin 1.6s linear infinite !important; }
}

/* Сенсорные экраны: зона нажатия кнопок и полей — не меньше 44 px (мышь — плотнее). */
@media (pointer: coarse) {
  .btn, input:not([type=checkbox]):not([type=radio]), select, textarea { min-height: 44px; }
}

