/* Палитра: тёплая бумага и чернила, один акцент (тёмная зелень) для главного действия и текущего раздела.
   Статусные цвета (зелёный, янтарный, красный) всегда идут с текстом. Шрифт системный: внешних шрифтов CSP не допускает. */

:root {
  color-scheme: light dark;
  --bg: #f5f3ee;
  --surface: #ffffff;
  --sunken: #ece9e2;
  --line: #d9d4c8;
  --control: #7a7468;
  --text: #1f1d19;
  --muted: #5b564c;
  --accent: #0e6655;
  --accent-hover: #0a5143;
  --on-accent: #ffffff;
  --danger: #9e1f16;
  --on-danger: #ffffff;
  --ok-bg: #dcefe8;
  --ok-text: #0b5546;
  --warn-bg: #fbebc8;
  --warn-text: #6e4700;
  --bad-bg: #fbe0dc;
  --bad-text: #9e1f16;
  --shadow: 0 8px 28px rgb(31 29 25 / 0.18);
  --radius: 10px;
  --radius-control: 8px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --nav-height: 60px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151512;
    --surface: #1f1e1a;
    --sunken: #2a2823;
    --line: #3a3832;
    --control: #8c877b;
    --text: #ece9e1;
    --muted: #a8a296;
    --accent: #4fc3a8;
    --accent-hover: #6ad3ba;
    --on-accent: #0a1f19;
    --danger: #ffa89f;
    --on-danger: #0a1f19;
    --ok-bg: #17382f;
    --ok-text: #7fdcc3;
    --warn-bg: #3e3114;
    --warn-text: #f2c46b;
    --bad-bg: #4a1e1a;
    --bad-text: #ffa89f;
    --shadow: 0 8px 28px rgb(0 0 0 / 0.5);
  }
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 var(--font);
  overflow-wrap: anywhere;
}

h1, h2, h3, p, ul, ol, dl, dd, pre, blockquote, figure { margin: 0; }
h1 { font-size: clamp(1.5rem, 1.2rem + 1.4vw, 1.875rem); line-height: 1.2; font-weight: 650; }
h2 { font-size: 1.125rem; line-height: 1.3; font-weight: 650; }
h3 { font-size: 1rem; font-weight: 650; }
a { color: var(--accent); text-underline-offset: 3px; }
code, pre, textarea.mono { font-family: var(--mono); font-size: 0.9em; }
small, .muted { color: var(--muted); }
.plain { padding: 16px; }

:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

/* ---------- Каркас ---------- */

.shell { min-height: 100dvh; }
.skip-link { position: fixed; z-index: 20; top: 8px; left: 8px; padding: 10px 16px; background: var(--surface); }
.skip-link:not(:focus) { clip-path: inset(50%); }
.session-bar { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 16px; color: var(--muted); }
.session-bar span { font-weight: 650; }
.nav {
  position: fixed;
  inset: auto 0 0 0;
  z-index: 10;
  display: flex;
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--line);
}
.nav .brand { display: none; }
.nav a {
  flex: 1;
  display: grid;
  place-items: center;
  min-height: var(--nav-height);
  padding: 0 8px;
  color: var(--muted);
  font-weight: 500;
  text-decoration: none;
  border-top: 3px solid transparent;
  transition: color 0.15s, background-color 0.15s;
}
.nav a[aria-current="page"] { color: var(--accent); font-weight: 650; border-top-color: var(--accent); }
.nav a:hover { background: var(--sunken); }

main {
  width: 100%;
  max-width: 980px;
  margin: 0 auto;
  padding: 20px 16px calc(var(--nav-height) + env(safe-area-inset-bottom) + 28px);
  animation: enter 0.18s ease-out;
}
main.bare { padding-bottom: 28px; max-width: 560px; }
main:focus { outline: none; }

@keyframes enter { from { opacity: 0; transform: translateY(4px); } }

@media (min-width: 600px) {
  main { padding-inline: 28px; }
}

@media (min-width: 840px) {
  .shell { display: grid; grid-template-columns: 232px minmax(0, 1fr); }
  .nav {
    position: sticky;
    top: 0;
    height: 100dvh;
    flex-direction: column;
    gap: 2px;
    padding: 24px 12px;
    border-top: 0;
    border-right: 1px solid var(--line);
  }
  .nav .brand { display: block; padding: 0 12px 16px; font-weight: 650; font-size: 1.125rem; }
  .nav a { flex: none; display: block; min-height: 44px; padding: 10px 12px; border: 0; border-radius: var(--radius-control); }
  .nav a[aria-current="page"] { background: var(--sunken); }
  main { padding: 24px 40px 48px; margin: 0 auto 0 0; }
  main.bare { margin: 0 auto; }
  .session-bar { justify-content: flex-end; }
  .session-bar span { display: none; }
}

.page-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px 16px; margin-bottom: 20px; }
.page-head p { flex-basis: 100%; color: var(--muted); }
.back { display: inline-flex; align-items: center; min-height: 44px; margin-bottom: 4px; }
.stack, .stack-lg { display: grid; grid-template-columns: minmax(0, 1fr); }
.stack { gap: 16px; }
.stack-lg { gap: 28px; }
ol.stack { padding-left: 1.4em; }
.row-actions { display: flex; flex-wrap: wrap; gap: 8px; }

/* ---------- Вкладки раздела ---------- */

.tabs {
  display: flex;
  gap: 4px;
  margin: 0 -16px 20px;
  padding: 0 16px;
  overflow-x: auto;
  scrollbar-width: none;
  border-bottom: 1px solid var(--line);
}
.tabs::-webkit-scrollbar { display: none; }
.tabs a {
  flex: none;
  display: grid;
  place-items: center;
  min-height: 44px;
  padding: 0 14px;
  color: var(--muted);
  font-weight: 500;
  text-decoration: none;
  border-bottom: 3px solid transparent;
  transition: color 0.15s;
}
.tabs a:hover { color: var(--text); }
.tabs a[aria-current="page"] { color: var(--accent); font-weight: 650; border-bottom-color: var(--accent); }
@media (min-width: 600px) { .tabs { margin-inline: 0; padding-inline: 0; } }

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

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 16px;
  font: inherit;
  font-weight: 600;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--control);
  border-radius: var(--radius-control);
  cursor: pointer;
  text-decoration: none;
  transition: background-color 0.15s, border-color 0.15s, color 0.15s;
}
.btn:hover:not(:disabled) { background: var(--sunken); }
.btn.primary { color: var(--on-accent); background: var(--accent); border-color: var(--accent); }
.btn.primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.danger { color: var(--danger); }
.btn.danger-solid { color: var(--on-danger); background: var(--danger); border-color: var(--danger); }
.btn.danger-solid:hover:not(:disabled) { background: var(--danger); filter: brightness(0.92); }
.btn.quiet { border-color: transparent; background: transparent; }
.btn.quiet:hover:not(:disabled) { background: var(--sunken); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn[aria-busy="true"] { cursor: progress; opacity: 0.85; }
.btn[aria-busy="true"]::before { content: ""; width: 1em; height: 1em; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin 0.8s linear infinite; }

.spinner { display: inline-block; width: 1.1em; height: 1.1em; border: 2px solid var(--control); border-right-color: transparent; border-radius: 50%; animation: spin 0.8s linear infinite; vertical-align: -0.2em; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Карточки и строки ---------- */

.card {
  display: grid;
  gap: 14px;
  padding: 18px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.card-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 12px; }
.cards { display: grid; gap: 16px; align-items: start; grid-template-columns: repeat(auto-fill, minmax(min(100%, 340px), 1fr)); }
.contents { display: contents; }

.list { display: grid; gap: 10px; padding: 0; list-style: none; }
.item {
  display: grid;
  gap: 6px;
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
a.item { color: inherit; text-decoration: none; min-height: 44px; transition: border-color 0.15s, background-color 0.15s; }
a.item:hover { border-color: var(--control); background: var(--sunken); }
.item.faded { color: var(--muted); }
.item-title { font-weight: 650; }
.item-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 12px; color: var(--muted); font-size: 0.9rem; }
.item-text { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.item-split { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 16px; }

.kv { display: grid; grid-template-columns: max-content 1fr; gap: 4px 16px; }
.kv dt { color: var(--muted); }

.badge {
  display: inline-block;
  padding: 2px 10px;
  font-size: 0.85rem;
  font-weight: 600;
  line-height: 1.5;
  color: var(--text);
  background: var(--sunken);
  border-radius: 6px;
  white-space: nowrap;
}
.badge.ok { color: var(--ok-text); background: var(--ok-bg); }
.badge.warn { color: var(--warn-text); background: var(--warn-bg); }
.badge.bad { color: var(--bad-text); background: var(--bad-bg); }
.badges { display: flex; flex-wrap: wrap; gap: 6px; }

.notice { padding: 12px 14px; border-radius: var(--radius-control); background: var(--sunken); white-space: pre-line; }
.notice.warn { color: var(--warn-text); background: var(--warn-bg); }
.notice.bad { color: var(--bad-text); background: var(--bad-bg); }
.notice.ok { color: var(--ok-text); background: var(--ok-bg); }

.empty { display: grid; gap: 8px; justify-items: start; padding: 24px 20px; background: var(--surface); border: 1px dashed var(--control); border-radius: var(--radius); }
.empty p { color: var(--muted); max-width: 60ch; }
.empty .row-actions { margin-top: 8px; }
.loading { display: flex; align-items: center; gap: 10px; padding: 20px 4px; color: var(--muted); }

/* ---------- Формы ---------- */

.field { display: grid; gap: 6px; }
.field > label { font-weight: 600; }
.field .hint { color: var(--muted); font-size: 0.9rem; }
.field-error { color: var(--bad-text); font-weight: 500; white-space: pre-line; }
.field-error::before { content: "Помилка: "; font-weight: 650; }
input, select, textarea {
  width: 100%;
  min-height: 44px;
  padding: 9px 12px;
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--control);
  border-radius: var(--radius-control);
  transition: border-color 0.15s;
}
textarea { min-height: 120px; line-height: 1.45; resize: vertical; }
input[aria-invalid="true"], textarea[aria-invalid="true"], select[aria-invalid="true"] { border-color: var(--danger); border-width: 2px; }
input[readonly], textarea[readonly] { background: var(--sunken); }
input[type="checkbox"], input[type="radio"] { width: 22px; height: 22px; min-height: 0; padding: 0; accent-color: var(--accent); flex: none; }
.form-grid { display: grid; gap: 14px; }
@media (min-width: 600px) { .form-grid.two { grid-template-columns: 1fr 1fr; } }

.choice {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  min-height: 44px;
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-control);
  cursor: pointer;
}
.choice:has(input:checked) { border-color: var(--accent); background: var(--sunken); }
.choice span { display: grid; gap: 2px; }

/* ---------- Проверка Telegram ---------- */

.checks { display: grid; gap: 10px; padding: 0; list-style: none; }
.checks li { display: grid; grid-template-columns: 28px 1fr; gap: 8px; align-items: start; }
.checks .mark { display: grid; place-items: center; width: 28px; height: 28px; font-weight: 700; border-radius: 50%; color: var(--ok-text); background: var(--ok-bg); }
.checks .fail .mark { color: var(--bad-text); background: var(--bad-bg); }
.checks .advice .mark { color: var(--warn-text); background: var(--warn-bg); }
.checks .hint { margin-top: 2px; color: var(--muted); font-size: 0.9rem; }
.checks .tag { margin-left: 6px; font-size: 0.85rem; font-weight: 600; }

/* ---------- Чат ---------- */

.chat { display: grid; grid-template-columns: minmax(0, 1fr); gap: 14px; }
.msg { display: grid; grid-template-columns: minmax(0, 1fr); gap: 4px; max-width: min(100%, 720px); }
.msg-meta { font-size: 0.85rem; color: var(--muted); }
.msg-body { padding: 12px 14px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); white-space: pre-wrap; }
.msg.user { justify-self: end; }
.msg.user .msg-body { background: var(--sunken); }
.msg.assistant .msg-body { white-space: normal; }

.md > * + * { margin-top: 0.7em; }
.md h1, .md h2, .md h3 { font-size: 1.05rem; }
.md ul, .md ol { padding-left: 1.4em; }
.md li + li { margin-top: 0.25em; }
.md pre { padding: 10px 12px; overflow-x: auto; background: var(--sunken); border-radius: var(--radius-control); white-space: pre; }
.md code { padding: 1px 5px; background: var(--sunken); border-radius: 4px; }
.md pre code { padding: 0; background: none; }
.md blockquote { padding-left: 12px; color: var(--muted); border-left: 2px solid var(--line); }
.md .table-wrap { overflow-x: auto; }
.md table { border-collapse: collapse; }
.md th, .md td { padding: 6px 10px; text-align: left; border: 1px solid var(--line); }

/* ---------- Прочее ---------- */

pre.prompt { padding: 12px 14px; max-height: 320px; overflow: auto; white-space: pre-wrap; background: var(--sunken); border-radius: var(--radius-control); font-family: var(--font); font-size: 0.95rem; }
.repo-list { max-height: 44dvh; overflow-y: auto; }
.mono-name { font-family: var(--mono); font-weight: 650; }

dialog {
  width: min(560px, calc(100vw - 24px));
  max-height: min(86dvh, 720px);
  padding: 0;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: auto;
}
dialog[open] { animation: enter 0.15s ease-out; }
dialog::backdrop { background: rgb(0 0 0 / 0.45); }
.dialog-body { display: grid; gap: 16px; padding: 20px; }
.dialog-body > h2 { font-size: 1.25rem; }
.dialog-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; }
@media (max-width: 480px) { .dialog-actions .btn { flex: 1 1 100%; } }

.toasts {
  position: fixed;
  z-index: 30;
  inset: auto 12px calc(var(--nav-height) + env(safe-area-inset-bottom) + 12px) 12px;
  display: grid;
  gap: 8px;
  justify-items: center;
  pointer-events: none;
}
.toast {
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: 520px;
  padding: 12px 16px;
  color: var(--on-accent);
  background: var(--accent);
  border-radius: var(--radius-control);
  box-shadow: var(--shadow);
  animation: enter 0.18s ease-out;
}
.toast.error { color: var(--on-danger); background: var(--danger); pointer-events: auto; }
.toast .btn { min-height: 44px; color: inherit; background: transparent; border-color: currentColor; }
@media (min-width: 840px) { .toasts { inset: auto 24px 24px auto; justify-items: end; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}
