:root {
  --bg: #0f1115;
  --surface: #171a21;
  --surface-2: #1f242d;
  --line: #2a303b;
  --text: #e7e9ee;
  --muted: #98a1b3;
  --accent: #6ea8fe;
  --ok: #4ade80;
  --warn: #fbbf24;
  --danger: #f87171;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

.hidden { display: none !important; }

.screen {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

input, textarea, button { font: inherit; color: inherit; }

/* login */
#login { align-items: center; justify-content: center; }

.login-card {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: min(360px, 88vw);
  padding: 24px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
}

.login-card h1 { margin: 0 0 8px; font-size: 20px; }

input[type="password"], input[type="email"] {
  padding: 12px 14px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 12px;
}

.login-card button {
  padding: 12px;
  background: var(--accent);
  color: #0b1020;
  border: 0;
  border-radius: 12px;
  font-weight: 600;
}

.error { color: var(--danger); font-size: 14px; min-height: 18px; margin: 0; }

/* bars */
.bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.bar .title {
  flex: 1;
  font-weight: 600;
  font-size: 15px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ghost {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--muted);
  border-radius: 10px;
  padding: 6px 10px;
  font-size: 14px;
}

.ghost.accent { color: var(--accent); border-color: #2f4570; }
.ghost.danger { color: var(--danger); border-color: #4a2b2b; }
.icon-button { padding: 6px 9px; font-size: 15px; }

/* активные сессии — чип с живой точкой, виден в шапке на любом экране */
.chip-active {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border: 1px solid #2f4570;
  border-radius: 999px;
  background: rgba(110, 168, 254, 0.12);
  color: var(--accent);
  font-size: 13px;
  font-weight: 600;
}

.chip-active .live-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.1s ease-in-out infinite;
}

.chip-active.waiting { border-color: var(--warn); background: rgba(251, 191, 36, 0.14); color: var(--warn); }
.chip-active.waiting .live-dot { background: var(--warn); animation: blink 0.9s step-start infinite; }

/* lists */
.card-list { list-style: none; margin: 0; padding: 10px; overflow-y: auto; flex: 1; }

.card-list li {
  padding: 14px;
  margin-bottom: 10px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
}

.card-list .row { display: flex; align-items: center; gap: 10px; }
.card-list .name { font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-list .meta { display: flex; gap: 8px; margin-top: 6px; font-size: 12px; color: var(--muted); align-items: center; }

.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex-shrink: 0; }
.dot.running { background: var(--warn); animation: pulse 1.1s ease-in-out infinite; }
.dot.waiting { background: var(--accent); animation: blink 0.9s step-start infinite; }
.dot.idle { background: var(--muted); }

@keyframes pulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.6); opacity: 0.45; }
}

@keyframes blink {
  50% { opacity: 0.25; }
}

.spinner {
  width: 14px;
  height: 14px;
  border: 2px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}

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

.hint { color: var(--muted); font-size: 13px; text-align: center; padding: 16px; margin: 0; }

/* chat */
.chat-body { position: relative; flex: 1; min-height: 0; display: flex; }
.stream { flex: 1; overflow-y: auto; padding: 12px 12px 12px; display: flex; flex-direction: column; gap: 10px; }

.msg { padding: 12px 14px; border-radius: 14px; line-height: 1.45; white-space: pre-wrap; word-break: break-word; }
.msg.user { background: var(--accent); color: #0b1020; align-self: flex-end; max-width: 85%; }
.msg.assistant { background: var(--surface); border: 1px solid var(--line); }

.tool {
  border: 1px solid var(--line);
  background: var(--surface-2);
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 13px;
}

.tool .head { display: flex; align-items: center; gap: 8px; }
.tool .name { color: var(--accent); font-weight: 600; }
.tool .summary { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.tool .status { font-size: 12px; }
.tool .status.ok { color: var(--ok); }
.tool .status.fail { color: var(--danger); }
.tool pre { margin: 8px 0 0; white-space: pre-wrap; word-break: break-word; color: var(--muted); font-size: 12px; max-height: 40vh; overflow: auto; }

.system { color: var(--muted); font-size: 12px; text-align: center; }

/* floating usage button — живёт в пространстве чата, не в блоке ввода */
.fab {
  position: absolute;
  right: 14px;
  bottom: 16px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: rgba(31, 36, 45, 0.92);
  color: var(--muted);
  font-size: 18px;
  backdrop-filter: blur(6px);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
}

/* permission */
.permission { border-top: 1px solid var(--line); background: var(--surface); padding: 12px 14px; }
.permission-body { display: flex; flex-direction: column; gap: 6px; margin-bottom: 10px; }
.permission-tool { font-weight: 600; }
.permission-summary { color: var(--warn); font-size: 13px; word-break: break-all; }
.permission-actions { display: flex; gap: 8px; }
.permission-actions button { flex: 1; padding: 12px 8px; border: 0; border-radius: 12px; font-weight: 600; font-size: 14px; }
.allow { background: var(--ok); color: #06210f; }
.allow-session { background: var(--surface-2); color: var(--text); border: 1px solid var(--line) !important; }
.deny { background: var(--danger); color: #2b0b0b; }

/* строка текущей модели — светло-серая, ненавязчивая, всегда видно что работает */
.model-line {
  padding: 6px 14px;
  font-size: 12px;
  color: var(--muted);
  background: var(--surface);
  border-top: 1px solid var(--line);
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* composer */
.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  /* В standalone-PWA env(safe-area-inset-bottom) уже большой, поэтому базовый
     отступ минимальный — иначе под строкой ввода зияет пустота. */
  padding: 10px 12px max(10px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--surface);
}

.stop { background: var(--danger) !important; color: #2b0b0b !important; }

.composer textarea {
  flex: 1;
  resize: none;
  max-height: 30vh;
  padding: 10px 12px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 14px;
}

.mic, .send {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--surface-2);
  font-size: 18px;
  flex-shrink: 0;
}

.mic.recording { background: var(--danger); border-color: var(--danger); color: #2b0b0b; }
.send { background: var(--accent); color: #0b1020; border: 0; font-weight: 700; }

/* sheet */
.sheet { position: fixed; inset: 0; z-index: 20; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.55); animation: fade 0.18s ease-out; }

.sheet-card {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 82dvh;
  overflow-y: auto;
  padding: 10px 18px calc(24px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--line);
  border-radius: 18px 18px 0 0;
  animation: slide-up 0.24s cubic-bezier(0.22, 1, 0.36, 1);
}

@keyframes slide-up { from { transform: translateY(100%); } to { transform: translateY(0); } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.sheet-handle { width: 42px; height: 4px; border-radius: 2px; background: var(--line); margin: 4px auto 14px; }
.sheet-card h2 { margin: 0 0 14px; font-size: 17px; }
.sheet-close { width: 100%; margin-top: 16px; padding: 12px; border: 1px solid var(--line); background: var(--surface-2); border-radius: 12px; }

.group-title { font-size: 13px; color: var(--accent); font-weight: 600; margin: 14px 0 2px; }
.group-title:first-child { margin-top: 0; }
.stat { display: flex; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.stat:last-of-type { border-bottom: 0; }
.stat .label { color: var(--muted); }
.stat .value { font-weight: 600; text-align: right; }

.meter { height: 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; margin: 6px 0 14px; }
.meter span { display: block; height: 100%; background: var(--accent); transition: width 0.3s ease; }
.meter.warn span { background: var(--warn); }
.meter.danger span { background: var(--danger); }

/* Настройки модели — ряды чипов вместо select: на мобилке select даёт мелкий
   нечитаемый текст и системную вёрстку, чипы выглядят одинаково везде. */
.field { margin-bottom: 18px; }
.field .field-label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 8px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  padding: 9px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 14px;
}
.chip.selected { background: var(--accent); color: #0b1020; border-color: var(--accent); font-weight: 600; }

/* тумблер зелёный/серый */
.switch { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 0; font-size: 15px; }
.toggle {
  position: relative;
  width: 50px;
  height: 30px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  flex-shrink: 0;
  transition: background 0.2s ease;
}
.toggle::after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--muted);
  transition: transform 0.2s ease, background 0.2s ease;
}
.toggle.on { background: rgba(74, 222, 128, 0.25); border-color: var(--ok); }
.toggle.on::after { transform: translateX(20px); background: var(--ok); }
