/* tglite — интерфейс панели.
 *
 * Без сборки и без внешних шрифтов: панель одного владельца на слабом
 * сервере, и всё, что ей нужно, — этот файл и app.js.
 *
 * Шаблоны почти не знают о дизайне: оформление навешивается на обычную
 * разметку (формы, таблицы, p.error …). Таблицы на узком экране становятся
 * карточками — подписи колонок в ячейки проставляет app.js.
 */

:root {
  color-scheme: light dark;
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #f1f3f7;
  --border: #e2e6ec;
  --text: #141821;
  --text-2: #5b6576;
  --accent: #2f6fed;
  --accent-text: #ffffff;
  --accent-soft: #e8effd;
  --ok: #17803d;
  --ok-soft: #e5f4ea;
  --warn: #8f5f00;
  --warn-soft: #fff3d6;
  --danger: #c62828;
  --danger-soft: #fdeaea;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 2px 8px rgba(16, 24, 40, .05);
  --sidebar: 248px;
  --focus: 0 0 0 3px color-mix(in srgb, var(--accent) 28%, transparent);
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1116;
    --surface: #161a22;
    --surface-2: #1d222c;
    --border: #2a303c;
    --text: #e8ebf1;
    --text-2: #9aa4b5;
    --accent: #6f9dff;
    --accent-text: #0b1020;
    --accent-soft: #1b2744;
    --ok: #5cc98a;
    --ok-soft: #13271b;
    --warn: #e7b653;
    --warn-soft: #2a2312;
    --danger: #ff7070;
    --danger-soft: #331a1b;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  }
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

h1 { font-size: 26px; line-height: 1.25; font-weight: 700; letter-spacing: -.015em; margin: 0 0 18px; }
h2 { font-size: 18px; font-weight: 650; margin: 32px 0 12px; }
p { margin: 0 0 12px; }
ul { padding-left: 20px; }
code { background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px;
       padding: 1px 6px; font-size: .88em; }

/* ---------- каркас ---------- */

.app { display: grid; grid-template-columns: var(--sidebar) minmax(0, 1fr); min-height: 100vh; }

.sidebar {
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
  display: flex; flex-direction: column;
  background: var(--surface); border-right: 1px solid var(--border);
  padding: 18px 12px 14px;
}

.brand { display: flex; align-items: center; gap: 10px; padding: 2px 10px 20px;
         font-weight: 750; font-size: 17px; color: var(--text); letter-spacing: -.01em; }
.brand:hover { text-decoration: none; }
.brand-logo { width: 32px; height: 32px; border-radius: 10px; display: grid; place-items: center;
              color: #fff; background: linear-gradient(135deg, #2f6fed 0%, #7b5cff 100%);
              box-shadow: 0 4px 12px rgba(47, 111, 237, .35); flex: none; }
.brand-logo svg { width: 18px; height: 18px; }

.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-section { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .08em;
               color: var(--text-2); padding: 14px 12px 6px; }
.nav a { display: flex; align-items: center; gap: 11px; padding: 9px 12px; border-radius: 10px;
         color: var(--text-2); font-weight: 550; font-size: 14.5px; }
.nav a:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.nav a[aria-current="page"] { background: var(--accent-soft); color: var(--accent); }
.nav svg { width: 18px; height: 18px; flex: none; }

.sidebar-foot { margin-top: auto; padding-top: 12px; border-top: 1px solid var(--border); }
.sidebar-foot form { margin: 0; }
.sidebar-foot button { width: 100%; justify-content: flex-start; gap: 11px;
                       background: transparent; color: var(--text-2); border-color: transparent;
                       font-weight: 550; box-shadow: none; }
.sidebar-foot button:hover { background: var(--surface-2); color: var(--danger); filter: none; }

.topbar { display: none; }
.scrim { display: none; }

.main { min-width: 0; padding: 30px 36px 56px; }
.content { max-width: 1240px; }

/* ---------- мелкие элементы ---------- */

.crumbs { font-size: 13px; color: var(--text-2); margin: 0 0 6px; }
.crumbs a { color: var(--text-2); }
.crumbs a:hover { color: var(--accent); text-decoration: none; }
.crumbs span[aria-current] { color: var(--text); }

.muted { color: var(--text-2); font-size: 13px; }
.hint { color: var(--text-2); font-size: 13px; }
.secret-set { color: var(--ok); font-size: 13px; font-weight: 650; }
.notice, .warn { color: var(--warn); }
.error { color: var(--danger); }

p.error, p.warn, p.notice, p.status-ok {
  display: block; padding: 11px 14px; border-radius: var(--radius-sm); font-size: 14px;
  border: 1px solid transparent;
}
p.error { background: var(--danger-soft); border-color: color-mix(in srgb, var(--danger) 25%, transparent); }
p.warn, p.notice { background: var(--warn-soft); border-color: color-mix(in srgb, var(--warn) 25%, transparent); }
p.status-ok { background: var(--ok-soft); color: var(--ok);
              border-color: color-mix(in srgb, var(--ok) 25%, transparent); font-weight: 600; }

.quote { white-space: pre-wrap; overflow-wrap: anywhere; border-left: 3px solid var(--border);
         padding: 2px 0 2px 10px; margin: 6px 0; color: var(--text); }

/* Состояния: плашка с точкой. «Сессия недействительна» обязана отличаться
   от «не запущено» с первого взгляда — на них разная реакция. */
span.status-ok, span.status-broken, span.status-idle, span.status-off,
td.status-ok, td.status-broken, td.status-idle, td.status-off { font-weight: 600; }
span.status-ok, span.status-broken, span.status-idle, span.status-off {
  display: inline-flex; align-items: center; gap: 6px; padding: 2px 10px; border-radius: 999px;
  font-size: 13px; line-height: 1.6;
}
span.status-ok::before, span.status-broken::before, span.status-idle::before, span.status-off::before,
td.status-ok::before, td.status-broken::before, td.status-idle::before, td.status-off::before {
  content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%;
  background: currentColor; margin-right: 7px; vertical-align: 1px;
}
.status-ok { color: var(--ok); }
span.status-ok { background: var(--ok-soft); }
.status-broken { color: var(--danger); }
span.status-broken { background: var(--danger-soft); }
.status-idle { color: var(--warn); }
span.status-idle { background: var(--warn-soft); }
.status-off { color: var(--text-2); }
span.status-off { background: var(--surface-2); }

/* ---------- кнопки ---------- */

button, .btn, .content > p > a[href$="/new"] {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  font: inherit; font-size: 14.5px; font-weight: 650; line-height: 1.2;
  min-height: 40px; padding: 9px 16px; border-radius: var(--radius-sm);
  border: 1px solid transparent; background: var(--accent); color: var(--accent-text);
  cursor: pointer; text-decoration: none; white-space: nowrap;
  box-shadow: 0 1px 2px rgba(16, 24, 40, .12);
  transition: filter .15s, background-color .15s, box-shadow .15s;
}
button:hover, .btn:hover, .content > p > a[href$="/new"]:hover { filter: brightness(1.07); text-decoration: none; }
button:focus-visible, .btn:focus-visible, a:focus-visible { outline: none; box-shadow: var(--focus); }
button:disabled { opacity: .55; cursor: not-allowed; }
button svg, .btn svg { width: 18px; height: 18px; flex: none; }


/* Второстепенные действия: отклонить, отменить, переключатели в таблицах. */
.btn-secondary,
button[value="reject"], button[value="fail"],
form[action$="/cancel"] button, form[action$="/login/start"] button.secondary,
.actions form[action$="/login/start"] button,
td form[action$="/monitor"] button, td form[action$="/store_all"] button,
form[hx-post] button {
  background: var(--surface); color: var(--text); border-color: var(--border); box-shadow: none;
}
.btn-secondary:hover, button[value="reject"]:hover, button[value="fail"]:hover,
form[action$="/cancel"] button:hover, .actions form[action$="/login/start"] button:hover,
td form button:hover, form[hx-post] button:hover { background: var(--surface-2); filter: none; }

/* Разрушительные — красные, чтобы их не нажимали на автомате. */
form[action$="/delete"] button {
  background: transparent; color: var(--danger);
  border-color: color-mix(in srgb, var(--danger) 35%, transparent); box-shadow: none;
}
form[action$="/delete"] button:hover { background: var(--danger-soft); filter: none; }

td form button { min-height: 32px; padding: 5px 12px; font-size: 13px; }

.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-top: 8px; }
a.tg-link:not(.btn) { font-weight: 600; }
a.btn.tg-link { background: #229ed9; color: #fff; }
.actions form { margin: 0; }

/* ---------- формы ---------- */

input, select, textarea {
  font: inherit; font-size: 14.5px; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 8px 12px; min-height: 40px; max-width: 100%;
  transition: border-color .15s, box-shadow .15s;
}
input::placeholder, textarea::placeholder { color: color-mix(in srgb, var(--text-2) 75%, transparent); }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: var(--focus); }
textarea { width: 100%; min-height: 100px; resize: vertical; line-height: 1.5; }
select {
  appearance: none; -webkit-appearance: none; padding-right: 36px; cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23808a9b' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center;
}
input[type="checkbox"], input[type="radio"] {
  min-height: 0; width: 18px; height: 18px; padding: 0; margin: 0 8px 0 0;
  accent-color: var(--accent); vertical-align: -3px; box-shadow: none;
}
input[type="date"] { min-width: 150px; }
label { cursor: pointer; }

/* Форма с полями — карточка. Одинокая кнопка (удалить, отправить код)
   остаётся просто кнопкой. */
.content > form[method="post"]:has(input:not([type="hidden"]), textarea, select),
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 22px 24px; margin: 0 0 20px; max-width: 860px;
}
.content > form[method="post"] > p { margin-bottom: 16px; }
.content > form[method="post"] > p > label > input:not([type="checkbox"]):not([type="radio"]),
.content > form[method="post"] > p > label > select,
.content > form[method="post"] > fieldset > p > label > input:not([type="checkbox"]):not([type="radio"]) {
  margin-top: 6px;
}
.content > form[method="post"] > button[type="submit"], .content > form[method="post"] > button:last-child {
  margin-top: 4px;
}

/* Фильтры списков — одной строкой с переносом. */
.content > form[method="get"] {
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px; margin: 0 0 18px;
}
.content > form[method="get"] input[name="q"] { flex: 1 1 260px; }
.content > form[method="get"] select { max-width: 260px; }

fieldset { border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 14px 16px 6px;
           margin: 18px 0; background: color-mix(in srgb, var(--surface-2) 55%, transparent); min-width: 0; }
legend { font-weight: 650; font-size: 14px; padding: 0 6px; color: var(--text); }

.field { margin: 0 0 16px; max-width: 46rem; }
.field > label { display: block; margin-bottom: 6px; font-weight: 600; font-size: 14px; }
.field .hint { display: block; margin-top: 6px; }
.field input:not([type="checkbox"]):not([type="radio"]), .field select { max-width: 100%; }
fieldset .field:last-child { margin-bottom: 10px; }

.chat-picker { max-height: 17rem; overflow-y: auto; background: var(--surface);
               border: 1px solid var(--border); border-radius: var(--radius-sm);
               padding: 8px 12px; margin-bottom: 12px; line-height: 2; }

.emoji-picker { margin: 4px 0 16px; max-width: 46rem; }
.emoji-group { margin-bottom: 4px; }
.emoji-picker button.emoji { font-size: 19px; line-height: 1; min-height: 36px; min-width: 36px;
                             padding: 4px; margin: 2px; background: var(--surface); color: inherit;
                             border: 1px solid var(--border); box-shadow: none; font-weight: 400; }
.emoji-picker button.emoji:hover { background: var(--surface-2); filter: none; }
.emoji-picker .hint { display: block; margin-top: 6px; }

/* ---------- таблицы ---------- */

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border);
              border-radius: var(--radius); box-shadow: var(--shadow); margin: 0 0 18px; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 12px 14px; text-align: left; vertical-align: top; font-size: 14px;
         border-bottom: 1px solid var(--border); }
th { font-size: 12px; font-weight: 650; text-transform: uppercase; letter-spacing: .04em;
     color: var(--text-2); background: var(--surface-2); white-space: nowrap; }
tr:last-child > td { border-bottom: 0; }
tbody tr:hover > td, table.cards tr:not(.head):hover > td {
  background: color-mix(in srgb, var(--accent-soft) 45%, transparent);
}
td form { margin: 0; }
td > div + div { margin-top: 2px; }
/* Таблица «ключ — значение» (Служебное): заголовки в строках. */
tr > th + td { width: 100%; }
tr > th:first-child:not(:only-child) { width: 1%; }

.chat-lost > td { background: var(--danger-soft); }

/* Лента намерений: одно срабатывание — одна строка; ширины заданы, иначе
   текст сообщения съедает колонку, в которой принимают решение. */
.plans { table-layout: fixed; min-width: 900px; }
.plans td { overflow-wrap: anywhere; }
.plans th:nth-child(1) { width: 6.5rem; }
.plans th:nth-child(2) { width: 15%; }
.plans th:nth-child(3) { width: 27%; }
.plans th:nth-child(4) { width: 30%; }
.plans .nowrap { white-space: nowrap; }
.plans .plan + .plan { margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--border); }
.plans textarea { margin-top: 6px; min-height: 80px; }

/* ---------- постраничный вывод ---------- */

.pager { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin: 4px 0 18px; }
.pager .pg-btn { display: inline-flex; align-items: center; min-height: 34px; padding: 5px 12px;
                 border-radius: 999px; border: 1px solid var(--border); background: var(--surface);
                 color: var(--text); font-size: 13.5px; font-weight: 550; }
.pager a.pg-btn:hover { border-color: var(--accent); color: var(--accent); text-decoration: none; }
.pager .pg-btn.disabled { color: var(--text-2); opacity: .55; }
.pager .pg-info { font-size: 13.5px; padding: 0 6px; }

/* ---------- страница входа ---------- */

.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px;
        background:
          radial-gradient(1200px 600px at 10% -10%, color-mix(in srgb, var(--accent) 16%, transparent), transparent 60%),
          radial-gradient(900px 500px at 110% 110%, color-mix(in srgb, #7b5cff 14%, transparent), transparent 60%),
          var(--bg); }
.auth-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border);
             border-radius: 18px; box-shadow: 0 10px 40px rgba(16, 24, 40, .12); padding: 30px 28px; }
.auth-card .brand { padding: 0 0 22px; font-size: 20px; }
.auth-card h1 { font-size: 20px; margin-bottom: 4px; }
.auth-card label { display: block; font-weight: 600; font-size: 14px; margin: 18px 0 6px; }
.auth-card input, .auth-card button { width: 100%; }
.auth-card button { margin-top: 16px; min-height: 44px; }
.auth-card p.error { margin: 16px 0 0; }

/* ---------- мобильные ---------- */

@media (max-width: 960px) {
  .app { grid-template-columns: minmax(0, 1fr); }

  .topbar {
    display: flex; position: sticky; top: 0; z-index: 30; height: 56px; align-items: center; gap: 10px;
    padding: 0 12px; padding-top: env(safe-area-inset-top);
    background: color-mix(in srgb, var(--surface) 88%, transparent);
    backdrop-filter: saturate(160%) blur(10px); -webkit-backdrop-filter: saturate(160%) blur(10px);
    border-bottom: 1px solid var(--border);
  }
  .topbar-title { font-weight: 650; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .icon-btn { width: 42px; height: 42px; min-height: 42px; padding: 0; border-radius: 12px; flex: none;
              background: transparent; color: var(--text); border: 0; box-shadow: none; }
  .icon-btn:hover { background: var(--surface-2); filter: none; }
  .icon-btn svg { width: 22px; height: 22px; }

  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 50; width: min(84vw, 300px); height: 100dvh;
             transform: translateX(-102%); transition: transform .22s ease; box-shadow: 0 0 40px rgba(0, 0, 0, .25);
             padding-top: calc(18px + env(safe-area-inset-top)); }
  body.nav-open .sidebar { transform: none; }
  .scrim { position: fixed; inset: 0; z-index: 40; background: rgba(8, 10, 15, .45);
           opacity: 0; pointer-events: none; transition: opacity .2s; display: block; }
  body.nav-open .scrim { opacity: 1; pointer-events: auto; }
  body.nav-open { overflow: hidden; }
  .nav a { padding: 12px 12px; font-size: 15.5px; }

  .main { padding: 18px 14px 48px; }
  h1 { font-size: 22px; margin-bottom: 14px; }
  .crumbs { display: none; }

  /* 16px — иначе iOS увеличивает страницу при фокусе на поле. */
  input, select, textarea, button { font-size: 16px; }
  input:not([type="checkbox"]):not([type="radio"]), select, button { min-height: 44px; }
  .content > form[method="post"]:has(input:not([type="hidden"]), textarea, select), .card { padding: 16px; }
  .content > form[method="post"] input:not([type="checkbox"]):not([type="radio"]):not([size="5"]):not([size="10"]),
  .content > form[method="post"] select { width: 100%; }
  .content > form[method="get"] > * { flex: 1 1 100%; }
  .content > form[method="get"] select { max-width: none; }
  .content > form[method="get"] > button { flex: 1 1 100%; }
  fieldset { padding: 12px 12px 4px; }

  .plans { min-width: 0; table-layout: auto; }
}

@media (max-width: 720px) {
  /* Таблицы — карточками: строка становится карточкой, ячейка — парой
     «подпись — значение». Подписи из заголовка таблицы ставит app.js. */
  .table-wrap:has(> table.cards) { background: transparent; border: 0; box-shadow: none; overflow: visible; }
  table.cards, table.cards tbody, table.cards tr, table.cards td { display: block; width: 100%; }
  table.cards thead, table.cards tr.head { display: none; }
  table.cards tr { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
                   box-shadow: var(--shadow); margin: 0 0 10px; padding: 8px 0; overflow: hidden; }
  table.cards tr.chat-lost { border-color: color-mix(in srgb, var(--danger) 45%, transparent); }
  /* Подпись — абсолютом в левой трети, всё содержимое ячейки (и элементы,
     и голый текст) — в правой части. Сетка здесь не годится: текстовый узел
     после вложенного блока уезжал бы в колонку подписей. */
  table.cards td { position: relative; border: 0; padding: 7px 14px 7px calc(34% + 10px);
                   min-height: 2.4em; font-size: 14.5px; overflow-wrap: anywhere; }
  table.cards td::before { content: attr(data-label); position: absolute; left: 14px; top: 9px;
                           width: calc(34% - 18px); color: var(--text-2); font-size: 11px; font-weight: 650;
                           line-height: 1.35; text-transform: uppercase; letter-spacing: .03em; }
  table.cards td[data-label=""], table.cards td[colspan] { padding-left: 14px; }
  table.cards td[data-label=""]::before, table.cards td[colspan]::before { display: none; }
  /* Ячейки с крупным содержимым (лента ответов) — подпись сверху, текст на всю ширину. */
  table.cards.plans td { padding-left: 14px; }
  table.cards.plans td::before { position: static; display: block; width: auto; margin: 2px 0 4px; }
  table.cards td.status-ok::before, table.cards td.status-broken::before,
  table.cards td.status-idle::before, table.cards td.status-off::before {
    content: attr(data-label); display: block; width: calc(34% - 18px); height: auto; border-radius: 0;
    background: none; margin: 0; color: var(--text-2); vertical-align: baseline;
  }
  table.cards tbody tr:hover > td, table.cards tr:not(.head):hover > td { background: transparent; }
  table.cards .chat-lost > td, table.cards tr.chat-lost > td { background: var(--danger-soft); }
  /* Ячейка «Решение» и одиночные кнопки — во всю ширину под палец. */
  table.cards td form button { width: 100%; min-height: 40px; font-size: 15px; }
  .plans td.nowrap { white-space: normal; }

  tr > th:first-child:not(:only-child) { width: 40%; white-space: normal; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; }
}
