/* ── Базовые переменные ─────────────────────────────────────────────────── */
:root {
  --bg: #0f0f1a;
  --bg2: #1a1a2e;
  --bg3: #252540;
  --text: #e8e8f0;
  --text2: #9090b0;
  --accent: #6C5CE7;
  --accent2: #00CEC9;
  --danger: #ff6b6b;
  --success: #00b894;
  --border: #2a2a45;
  --msg-user: #1e1e3a;
  --msg-ai: transparent;
  --radius: 12px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

[data-theme="light"] {
  --bg: #f5f5fa;
  --bg2: #eeeef5;
  --bg3: #dddde8;
  --text: #1a1a2e;
  --text2: #606080;
  --border: #d0d0e0;
  --msg-user: #e8e8f8;
}

@media (prefers-color-scheme: light) {
  [data-theme="auto"] {
    --bg: #f5f5fa;
    --bg2: #eeeef5;
    --bg3: #dddde8;
    --text: #1a1a2e;
    --text2: #606080;
    --border: #d0d0e0;
    --msg-user: #e8e8f8;
  }
}

/* ── Сброс ──────────────────────────────────────────────────────────────── */
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

.hidden { display: none !important; }

/* ── Экраны ─────────────────────────────────────────────────────────────── */
.screen {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}

/* ── Вход ───────────────────────────────────────────────────────────────── */
.login {
  justify-content: center;
  align-items: center;
  background: var(--bg);
}
.login-box {
  width: 90%;
  max-width: 340px;
  text-align: center;
}
.brand {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 8px;
}
.brand-icon { flex-shrink: 0; }
.hint {
  color: var(--text2);
  font-size: 13px;
  margin-bottom: 24px;
}
.pw-row {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}
.pw-row input {
  flex: 1;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg2);
  color: var(--text);
  font-size: 16px;
  outline: none;
}
.pw-row input:focus { border-color: var(--accent); }
.eye {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text2);
  font-size: 18px;
  padding: 0 12px;
  cursor: pointer;
}
.primary {
  width: 100%;
  padding: 14px;
  border: none;
  border-radius: var(--radius);
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}
.primary:active { opacity: 0.85; }
.err {
  color: var(--danger);
  font-size: 13px;
  margin-top: 10px;
  min-height: 18px;
}

/* ── Шапка ──────────────────────────────────────────────────────────────── */
.top {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  min-height: 52px;
  flex-shrink: 0;
}
.top .title {
  flex: 1;
  font-weight: 600;
  font-size: 16px;
  display: flex;
  align-items: center;
  gap: 6px;
  overflow: hidden;
}
.top .title .sub {
  font-size: 11px;
  color: var(--text2);
  font-weight: 400;
}
.hdr-icon { flex-shrink: 0; }
.ghost {
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 14px;
  padding: 6px 12px;
  cursor: pointer;
  white-space: nowrap;
}
.ghost:active { background: var(--bg3); }
.ghost.danger { color: var(--danger); border-color: var(--danger); }

/* ── Список диалогов ────────────────────────────────────────────────────── */
.searchbar {
  padding: 8px 14px;
  background: var(--bg2);
  flex-shrink: 0;
}
.searchbar input {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  outline: none;
}
.searchbar input:focus { border-color: var(--accent); }

.sessions {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px 0;
}
.session-item {
  display: flex;
  align-items: center;
  padding: 14px 16px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  gap: 12px;
}
.session-item:active { background: var(--bg3); }
.session-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  flex-shrink: 0;
}
.session-dot.active { background: var(--success); animation: pulse 1.5s infinite; }
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}
.session-info { flex: 1; min-width: 0; }
.session-title {
  font-size: 15px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.session-date {
  font-size: 12px;
  color: var(--text2);
  margin-top: 2px;
}
.session-empty {
  text-align: center;
  color: var(--text2);
  padding: 40px 20px;
  font-size: 14px;
}

/* ── Чат ────────────────────────────────────────────────────────────────── */
.messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.msg {
  max-width: 92%;
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 15px;
  line-height: 1.5;
  word-wrap: break-word;
  overflow-wrap: break-word;
}
.msg.user {
  align-self: flex-end;
  background: var(--msg-user);
  border: 1px solid var(--border);
}
.msg.assistant {
  align-self: flex-start;
  background: var(--msg-ai);
}
.msg.system {
  align-self: center;
  color: var(--text2);
  font-size: 12px;
  background: var(--bg3);
  padding: 6px 12px;
  border-radius: 8px;
}
.msg.tool {
  align-self: flex-start;
  background: var(--bg3);
  font-size: 13px;
  color: var(--text2);
  border-left: 3px solid var(--accent);
  padding: 8px 12px;
}
.msg.error {
  align-self: center;
  color: var(--danger);
  background: rgba(255,107,107,0.1);
  font-size: 13px;
}

/* Markdown внутри ответов */
.msg.assistant p { margin-bottom: 8px; }
.msg.assistant p:last-child { margin-bottom: 0; }
.msg.assistant pre {
  background: #131311;
  color: #e8e8f0;
  padding: 10px 12px;
  border-radius: 8px;
  overflow-x: auto;
  font-size: 13px;
  margin: 8px 0;
}
.msg.assistant code {
  background: var(--bg3);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 13px;
}
.msg.assistant pre code {
  background: none;
  padding: 0;
}
.msg.assistant ul, .msg.assistant ol {
  padding-left: 20px;
  margin: 6px 0;
}
.msg.assistant a {
  color: var(--accent2);
  text-decoration: underline;
}
.msg.assistant h1, .msg.assistant h2, .msg.assistant h3 {
  margin: 12px 0 6px;
  font-size: 16px;
  font-weight: 600;
}
.msg.assistant blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 12px;
  color: var(--text2);
  margin: 8px 0;
}
.msg.assistant table {
  border-collapse: collapse;
  margin: 8px 0;
  font-size: 13px;
  width: 100%;
}
.msg.assistant th, .msg.assistant td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}
.msg.assistant th { background: var(--bg3); font-weight: 600; }

/* Индикатор печати */
.typing-indicator {
  display: inline-flex;
  gap: 4px;
  padding: 8px 14px;
}
.typing-indicator span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text2);
  animation: typing 1.2s infinite;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing {
  0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-4px); }
}

/* ── Панель разрешений ──────────────────────────────────────────────────── */
.permission-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: var(--bg3);
  border-top: 1px solid var(--accent);
  flex-shrink: 0;
}
.perm-icon { font-size: 20px; flex-shrink: 0; }
.perm-text {
  flex: 1;
  font-size: 13px;
  color: var(--text);
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.perm-btns { display: flex; gap: 6px; flex-shrink: 0; }
.btn-allow, .btn-deny {
  padding: 6px 14px;
  border: none;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.btn-allow { background: var(--success); color: #fff; }
.btn-deny { background: var(--danger); color: #fff; }

/* ── Поле ввода ─────────────────────────────────────────────────────────── */
.input-bar {
  padding: 8px 14px;
  background: var(--bg2);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}
.input-row textarea {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  font-family: inherit;
  resize: none;
  outline: none;
  max-height: 120px;
  line-height: 1.4;
}
.input-row textarea:focus { border-color: var(--accent); }
.send-btn {
  width: 42px;
  height: 42px;
  border: none;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
}
.send-btn:active { opacity: 0.8; }
.send-btn:disabled { opacity: 0.4; cursor: default; }

/* ── Скроллбар ──────────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
