/* Тёмная тема по умолчанию, светлая по системе. Цвета в OKLCH.
   Плотно, тихо, подсказки клавиш чипами. Без теней и рамок, где хватает тона. */

:root {
  color-scheme: dark light;

  --bg: oklch(0.165 0.004 260);
  --surface: oklch(0.205 0.005 260);
  --surface-2: oklch(0.255 0.006 260);
  --line: oklch(0.31 0.006 260);
  --ink: oklch(0.93 0.004 95);
  --ink-soft: oklch(0.7 0.008 260);
  --ink-faint: oklch(0.55 0.008 260);
  --accent: oklch(0.8 0.1 195);
  --accent-ink: oklch(0.17 0.01 260);
  --danger: oklch(0.72 0.17 25);
  --shadow: oklch(0 0 0 / 0.5);

  --panel-width: 17.5rem;
  --column: 47rem;
  --radius: 0.55rem;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: oklch(0.985 0.003 95);
    --surface: oklch(0.96 0.005 95);
    --surface-2: oklch(0.93 0.006 95);
    --line: oklch(0.88 0.007 95);
    --ink: oklch(0.24 0.012 260);
    --ink-soft: oklch(0.48 0.012 260);
    --ink-faint: oklch(0.62 0.01 260);
    --accent: oklch(0.52 0.12 195);
    --accent-ink: oklch(0.99 0.003 95);
    --danger: oklch(0.55 0.18 25);
    --shadow: oklch(0.2 0.02 260 / 0.18);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--ink);
  font: 0.97rem/1.6 var(--font);
  -webkit-text-size-adjust: 100%;
}

button, textarea, input { font: inherit; color: inherit; }
button {
  cursor: pointer;
  border: 0;
  border-radius: var(--radius);
  background: var(--surface-2);
  padding: 0.4rem 0.8rem;
}
button:hover { background: var(--line); }
button:focus-visible, textarea:focus-visible, input:focus-visible, summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-ink); }
button.danger { background: var(--danger); color: var(--accent-ink); }
button.quiet { background: transparent; color: var(--ink-soft); padding: 0.15rem 0.5rem; }
button.quiet:hover { color: var(--ink); background: var(--surface-2); }

[hidden] { display: none !important; }

kbd {
  display: inline-block;
  min-width: 1.35rem;
  margin-left: 0.2rem;
  padding: 0 0.35rem;
  border-radius: 0.3rem;
  background: var(--surface-2);
  color: var(--ink-soft);
  font: 0.72rem/1.35rem var(--font);
  text-align: center;
}
kbd:first-child { margin-left: 0; }

svg { width: 1.15rem; height: 1.15rem; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* Какой из трёх видов на экране. */
body[data-view="loading"] .login,
body[data-view="loading"] .app,
body[data-view="login"] .app,
body[data-view="app"] .login { display: none; }

/* Вход */
.login { min-height: 100%; display: grid; place-items: center; padding: 1.5rem; }
.login form { width: min(24rem, 100%); display: grid; gap: 1rem; }
.login h1 { margin: 0; font-size: 1.1rem; font-weight: 600; letter-spacing: 0.02em; }
.login input { border: 0; border-radius: var(--radius); background: var(--surface); padding: 0.75rem 0.95rem; }
.login button { padding: 0.7rem; background: var(--accent); color: var(--accent-ink); }
#login-error { margin: 0; min-height: 1.5rem; color: var(--danger); }

/* Каркас: беседа слева, панель разговоров справа, как в pi-ui */
.app {
  height: 100%;
  height: 100dvh;
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--panel-width);
}
body[data-panel="closed"] .app { grid-template-columns: minmax(0, 1fr); }
body[data-panel="closed"] .panel { display: none; }

.chat { position: relative; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.scroller { flex: 1; overflow-y: auto; overflow-anchor: auto; }
.column { width: 100%; max-width: var(--column); min-height: 100%; margin: 0 auto; padding: 1.6rem 1.2rem 1rem; display: flex; flex-direction: column; }

.round {
  display: inline-grid;
  place-items: center;
  width: 2.2rem;
  height: 2.2rem;
  padding: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--ink-soft);
}
.round:hover { background: var(--surface-2); color: var(--ink); }
.round.primary { background: var(--accent); color: var(--accent-ink); }
.round.primary:hover { background: var(--accent); filter: brightness(1.1); }
.floating { position: absolute; top: 0.6rem; left: 0.6rem; z-index: 5; display: none; }
body[data-panel="closed"] .floating { display: inline-grid; }

#send .icon-stop, #send[data-state="busy"] .icon-send { display: none; }
#send[data-state="busy"] .icon-stop { display: block; fill: currentColor; stroke: none; }

/* Пустое состояние */
.welcome { margin: auto 0; padding: 12vh 0 4vh; }
.welcome h1 { margin: 0 0 0.7rem; font-size: 1.15rem; font-weight: 600; text-align: center; }
.hint { margin: 0 0 2.4rem; display: flex; flex-wrap: wrap; gap: 0.4rem 1.1rem; justify-content: center; color: var(--ink-soft); font-size: 0.85rem; }
.welcome h2 { margin: 0 0 0.4rem; font-size: 0.72rem; font-weight: 500; letter-spacing: 0.08em; text-transform: uppercase; color: var(--ink-faint); }

/* Панель разговоров */
.panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-left: 1px solid var(--line);
  background: var(--bg);
}
.panel header { display: flex; justify-content: space-between; align-items: center; padding: 0.9rem 1rem 0.5rem; color: var(--ink-soft); font-size: 0.85rem; }
.panel .new { margin: 0 0.6rem 0.4rem; display: flex; justify-content: space-between; align-items: center; background: var(--surface); color: var(--ink); }
.chips { display: inline-flex; }
#conversation-list { flex: 1; overflow-y: auto; padding: 0 0.4rem 0.5rem; }
.panel footer { display: flex; justify-content: space-between; padding: 0.5rem 0.6rem; border-top: 1px solid var(--line); }

.group { margin: 0.9rem 0.6rem 0.25rem; font-size: 0.72rem; letter-spacing: 0.06em; text-transform: uppercase; color: var(--ink-faint); }

.conversation {
  position: relative;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  border-radius: var(--radius);
  padding: 0.1rem 0.2rem 0.1rem 0;
}
.conversation:hover, .conversation:focus-within { background: var(--surface); }
.conversation[aria-current="true"] { background: var(--surface-2); }
.conversation .open {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.6rem;
  padding: 0.4rem 0.6rem;
  background: transparent;
  text-align: left;
}
.conversation .open:hover { background: transparent; }
.conversation .title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conversation .when { flex: none; color: var(--ink-faint); font-size: 0.75rem; font-variant-numeric: tabular-nums; }
.conversation .remove { flex: none; opacity: 0; padding: 0.2rem 0.45rem; }
.conversation:hover .remove, .conversation:focus-within .remove { opacity: 1; }
.conversation:hover .when, .conversation:focus-within .when { display: none; }
@media (hover: none) { .conversation .remove { opacity: 0.7; } .conversation .when { display: none; } }

/* Недавние на пустом экране */
#recent .conversation { margin: 0; }

.scrim { display: none; }

/* Сообщения */
.message { margin: 0 0 1.5rem; }
.message[data-role="user"] .body { background: var(--surface); border-radius: var(--radius); padding: 0.6rem 0.9rem; }
.plain { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.plain + .file, .file + .plain, .file + .file { margin-top: 0.6rem; }

.file { border-radius: var(--radius); background: var(--bg); }
.file summary { cursor: pointer; padding: 0.3rem 0.7rem; color: var(--ink-soft); font-size: 0.88rem; }
.file pre { margin: 0; padding: 0.6rem 0.8rem; max-height: 24rem; overflow: auto; border-top: 1px solid var(--line); }

.thinking { margin: 0 0 0.5rem; color: var(--ink-soft); font-size: 0.88rem; }
.thinking summary { cursor: pointer; font-style: italic; }
.thinking .text { margin: 0.4rem 0 0; padding-left: 0.8rem; border-left: 2px solid var(--line); white-space: pre-wrap; overflow-wrap: anywhere; }

.prose { overflow-wrap: anywhere; }
.prose > :first-child { margin-top: 0; }
.prose > :last-child { margin-bottom: 0; }
.prose p, .prose ul, .prose ol, .prose pre, .prose blockquote, .prose table { margin: 0.8em 0; }
.prose h1, .prose h2, .prose h3, .prose h4 { line-height: 1.3; margin: 1.4em 0 0.5em; }
.prose h1 { font-size: 1.35rem; } .prose h2 { font-size: 1.15rem; } .prose h3, .prose h4 { font-size: 1.02rem; }
.prose a { color: var(--accent); }
.prose blockquote { padding-left: 0.9rem; border-left: 3px solid var(--line); color: var(--ink-soft); }
.prose hr { border: 0; border-top: 1px solid var(--line); margin: 1.4rem 0; }
.prose code { font-family: var(--mono); font-size: 0.88em; background: var(--surface); border-radius: 0.3rem; padding: 0.1em 0.35em; }
.prose pre { position: relative; background: var(--surface); border-radius: var(--radius); padding: 0.8rem 1rem; overflow-x: auto; }
.prose pre code { background: none; padding: 0; font-size: 0.86rem; }
.prose table { border-collapse: collapse; display: block; overflow-x: auto; }
.prose th, .prose td { border: 1px solid var(--line); padding: 0.35rem 0.7rem; }
.prose th { background: var(--surface); }
.prose .copy { position: absolute; top: 0.4rem; right: 0.4rem; font-size: 0.78rem; }

.status { margin: 0.5rem 0 0; color: var(--ink-soft); font-size: 0.88rem; }
.status[data-tone="problem"] { color: var(--danger); }

.actions { display: flex; flex-wrap: wrap; gap: 0.1rem; align-items: center; margin-top: 0.25rem; color: var(--ink-faint); font-size: 0.82rem; opacity: 0; transition: opacity 0.12s; }
.message:hover .actions, .message:focus-within .actions, .actions:has(.branch) { opacity: 1; }
@media (hover: none) { .actions { opacity: 1; } }
.actions .branch { padding: 0 0.3rem; font-variant-numeric: tabular-nums; }
.actions .usage { margin-left: 0.5rem; }

.system-card { margin: 0 0 1.5rem; }
.system-card summary { cursor: pointer; color: var(--ink-faint); font-size: 0.85rem; }
.system-card .plain { margin-top: 0.4rem; padding-left: 0.8rem; border-left: 2px solid var(--line); color: var(--ink-soft); }

/* Поле ввода: плашка снизу и тонкая строка под ней */
.composer { width: 100%; max-width: var(--column); margin: 0 auto; padding: 0.3rem 1.2rem calc(0.6rem + env(safe-area-inset-bottom)); }
.field { display: flex; align-items: flex-end; gap: 0.2rem; border-radius: 1.1rem; background: var(--surface); padding: 0.4rem 0.45rem 0.4rem 0.9rem; border: 1px solid transparent; }
.field:focus-within { border-color: var(--line); }
.field textarea { flex: 1; min-height: 2.2rem; max-height: 40vh; resize: none; border: 0; background: transparent; padding: 0.45rem 0; line-height: 1.5; }
.field textarea:focus-visible { outline: none; }
.field textarea::placeholder { color: var(--ink-faint); }
.status-line { display: flex; justify-content: space-between; gap: 1rem; margin: 0.35rem 0.9rem 0; color: var(--ink-faint); font-size: 0.78rem; }
.status-line span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#model { flex: none; }

.attachments { display: flex; flex-wrap: wrap; gap: 0.4rem; margin-bottom: 0.4rem; }
.attachments:empty { display: none; }
.chip { display: inline-flex; align-items: center; gap: 0.3rem; border-radius: 999px; padding: 0.1rem 0.3rem 0.1rem 0.7rem; font-size: 0.85rem; background: var(--surface-2); }
.editing, .notice { margin: 0 0 0.4rem; font-size: 0.88rem; color: var(--ink-soft); display: flex; gap: 0.6rem; align-items: center; justify-content: space-between; }
.notice { color: var(--danger); justify-content: flex-start; }

.drop {
  position: fixed; inset: 0; z-index: 30; display: grid; place-items: center;
  background: oklch(0.1 0.01 260 / 0.75); color: oklch(0.98 0 0); font-size: 1.15rem;
  border: 3px dashed var(--accent); pointer-events: none;
}

/* Диалоги */
dialog {
  width: min(40rem, calc(100% - 2rem));
  border: 0;
  border-radius: 1rem;
  background: var(--surface);
  color: var(--ink);
  padding: 1.2rem;
  box-shadow: 0 1rem 3rem var(--shadow);
}
dialog::backdrop { background: oklch(0.05 0.01 260 / 0.6); }
dialog h2 { margin: 0 0 0.4rem; font-size: 1.05rem; }
dialog p { margin: 0 0 0.8rem; color: var(--ink-soft); font-size: 0.9rem; }
dialog textarea {
  width: 100%; border: 0; border-radius: var(--radius); background: var(--bg);
  padding: 0.6rem 0.8rem; font-family: var(--mono); font-size: 0.88rem; line-height: 1.5; resize: vertical;
}
#confirm-dialog { width: min(26rem, calc(100% - 2rem)); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 0.6rem; }
.dialog-actions kbd { background: oklch(0 0 0 / 0.18); color: inherit; }
#system-status { min-height: 1.4rem; margin: 0.4rem 0; }

/* Узкий экран: панель выезжает справа поверх */
@media (max-width: 900px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  body[data-panel="closed"] .panel { display: flex; }
  .panel {
    position: fixed; inset: 0 0 0 auto; width: min(20rem, 88vw); z-index: 20;
    transform: translateX(100%); transition: transform 0.18s ease; box-shadow: 0 0 3rem var(--shadow);
  }
  body[data-panel="open"] .panel { transform: none; }
  body[data-panel="open"] .scrim { display: block; position: fixed; inset: 0; z-index: 15; background: oklch(0.05 0.01 260 / 0.5); }
  .floating { display: inline-grid; left: auto; right: 0.6rem; }
  body[data-panel="open"] .floating { display: none; }
  .column { padding-top: 3rem; }
  .hint { display: none; }
}
@media (prefers-reduced-motion: reduce) { .panel { transition: none; } .actions { transition: none; } }
