/* ============================================================
   Ollama Local Chat — light theme, mobile-first
   Accent: #6366f1 → #a855f7 (indigo→violet), фон #f6f7fb
   ============================================================ */
:root {
  --accent: #6366f1;
  --accent-2: #a855f7;
  --gradient: linear-gradient(135deg, #6366f1 0%, #a855f7 100%);
  --bg: #f6f7fb;
  --surface: #ffffff;
  --border: #e5e7f0;
  --text: #1e1e2e;
  --text-dim: #6b7280;
  --danger: #dc2626;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(30, 30, 60, 0.06), 0 4px 16px rgba(30, 30, 60, 0.05);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

#app { height: 100dvh; display: flex; flex-direction: column; }

/* ---------- AUTH ---------- */
.auth-view {
  flex: 1; display: flex; align-items: center; justify-content: center;
  padding: 20px;
  background:
    radial-gradient(60% 50% at 20% 10%, rgba(99, 102, 241, 0.12), transparent),
    radial-gradient(50% 40% at 85% 90%, rgba(168, 85, 247, 0.12), transparent),
    var(--bg);
}
.auth-card {
  width: 100%; max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 28px 26px;
}
.brand { text-align: center; margin-bottom: 22px; }
.brand-logo {
  width: 54px; height: 54px; margin: 0 auto 12px;
  border-radius: 16px;
  background: var(--gradient);
  color: #fff; font-size: 30px;
  display: flex; align-items: center; justify-content: center;
}
.brand h1 { font-size: 22px; margin: 0 0 4px; letter-spacing: -0.02em; }
.brand-sub { margin: 0; color: var(--text-dim); font-size: 13px; }

.auth-tabs {
  display: grid; grid-template-columns: 1fr 1fr;
  background: #eef0f8; border-radius: 12px; padding: 4px; margin-bottom: 18px;
}
.auth-tab {
  border: 0; background: transparent; padding: 9px 0;
  border-radius: 9px; font-size: 14px; font-weight: 600;
  color: var(--text-dim); cursor: pointer;
}
.auth-tab.active { background: var(--gradient); color: #fff; }

.field { display: block; margin-bottom: 14px; }
.field span {
  display: block; font-size: 12.5px; font-weight: 600;
  color: var(--text-dim); margin-bottom: 6px;
}
.field input {
  width: 100%; padding: 11px 13px;
  border: 1.5px solid var(--border); border-radius: 11px;
  font-size: 15px; background: #fbfbfe; color: var(--text);
  outline: none; transition: border-color .15s;
}
.field input:focus { border-color: var(--accent); background: #fff; }

.auth-error {
  background: #fef2f2; color: var(--danger);
  border: 1px solid #fecaca; border-radius: 10px;
  padding: 10px 12px; font-size: 13.5px; margin-bottom: 14px;
}

/* ---------- generic buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 1.5px solid var(--border); background: var(--surface);
  color: var(--text); border-radius: 12px;
  padding: 10px 16px; font-size: 14.5px; font-weight: 600;
  cursor: pointer; transition: box-shadow .15s, transform .05s, border-color .15s;
}
.btn:hover { border-color: #cdd1e4; }
.btn:active { transform: translateY(1px); }
.btn-primary {
  background: var(--gradient); border: 0; color: #fff;
  box-shadow: 0 4px 14px rgba(99, 102, 241, 0.35);
}
.btn-ghost {
  border: 0; background: transparent; color: var(--text-dim);
  font-size: 13px; font-weight: 600; padding: 6px 10px; border-radius: 8px;
}
.btn-ghost:hover { color: var(--danger); background: #fef2f2; }
.icon-btn {
  border: 1.5px solid var(--border); background: var(--surface);
  width: 38px; height: 38px; border-radius: 11px;
  font-size: 16px; cursor: pointer; color: var(--text);
  display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn:hover { border-color: var(--accent); }
.icon-btn.regen-btn:hover { border-color: var(--accent-2); }
.icon-btn.edit-btn:hover { border-color: var(--accent); }

/* hidden utility */
.hidden { display: none !important; }

/* ============================================================
   MODAL DIALOG
   ============================================================ */
.modal {
  position: fixed; inset: 0; z-index: 100;
  display: flex; align-items: center; justify-content: center;
  background: rgba(15, 15, 25, 0.6);
  backdrop-filter: blur(4px);
}
.modal.hidden { display: none; }
.modal-content {
  width: min(90vw, 520px);
  max-height: 80vh;
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  display: flex; flex-direction: column;
  overflow: hidden;
}
.modal-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
}
.modal-header h3 {
  margin: 0; font-size: 16px; font-weight: 600; color: var(--text);
}
.modal-close {
  border: 0; background: transparent; cursor: pointer;
  font-size: 20px; color: var(--text-dim); padding: 4px;
  border-radius: 6px; line-height: 1;
}
.modal-close:hover { background: #f3f4fb; color: var(--danger); }
.modal-body {
  padding: 14px;
  flex: 1; overflow-y: auto;
}
.modal-body textarea {
  width: 100%; flex: 1; min-height: 160px;
  padding: 12px 14px; font: inherit;
  font-size: 15px; line-height: 1.55;
  border: 1.5px solid var(--border); border-radius: 10px;
  resize: vertical; outline: none; color: var(--text);
  background: #fbfbfe;
}
.modal-body textarea:focus {
  border-color: var(--accent); background: #fff;
}
.modal-footer {
  padding: 12px 14px;
  border-top: 1px solid var(--border);
  display: flex; gap: 10px; justify-content: flex-end;
}
.modal-footer .btn {
  padding: 8px 16px; font-size: 14px;
}
.modal-footer .btn-cancel {
  background: transparent; border-color: var(--border); color: var(--text-dim);
}
.modal-footer .btn-cancel:hover { background: #f3f4fb; color: var(--text); }
.modal-footer .btn-save {
  background: var(--gradient); border-color: var(--gradient); color: #fff;
}
.modal-footer .btn-save:hover { opacity: .9; }

/* swipe-left для перегенерации user-сообщения */

/* ============================================================
   CHAT LAYOUT: grid 280px | 1fr (desktop)
   ============================================================ */
.chat-view {
  flex: 1; min-height: 0;
  display: grid;
  grid-template-columns: 280px 1fr;   /* desktop */
}

/* ---------- sidebar ---------- */
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  min-height: 0;
}
.sidebar-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 14px 10px;
}
.brand-mini { display: flex; align-items: center; gap: 8px; min-width: 0; }
.brand-logo-sm {
  width: 30px; height: 30px; border-radius: 9px;
  background: var(--gradient); color: #fff; font-size: 16px;
  display: flex; align-items: center; justify-content: center; flex: 0 0 auto;
}
.brand-name { font-weight: 700; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#btn-close-drawer { display: none; }

.btn-new {
  margin: 4px 12px 12px;
  padding: 11px;
}
.btn-new .plus { font-size: 18px; line-height: 1; }

.session-list {
  flex: 1; overflow-y: auto; padding: 0 10px 10px;
  display: flex; flex-direction: column; gap: 4px;
}
.session-item {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 11px; border-radius: 11px;
  border: 1.5px solid transparent;
  background: transparent; cursor: pointer; text-align: left; width: 100%;
  font: inherit; color: var(--text);
}
.session-item:hover { background: #f3f4fb; }
.session-item.active {
  background: linear-gradient(135deg, rgba(99,102,241,.10), rgba(168,85,247,.10));
  border-color: rgba(99,102,241,.45);
}
.session-title {
  flex: 1; min-width: 0; font-size: 14px; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.session-model { font-size: 12px; color: var(--text-dim); flex: 0 0 auto; }
.session-del {
  border: 0; background: transparent; color: var(--text-dim);
  font-size: 14px; cursor: pointer; border-radius: 7px;
  width: 26px; height: 26px; flex: 0 0 auto;
}
.session-del:hover { color: var(--danger); background: #fef2f2; }

.sidebar-footer {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px; border-top: 1px solid var(--border);
  gap: 8px;
}
.user-chip {
  font-size: 13px; font-weight: 600; color: var(--text-dim);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.user-chip::before { content: "👤 "; }

/* ---------- main column ---------- */
.main {
  display: flex; flex-direction: column;
  min-width: 0; min-height: 0;
  background: var(--bg);
}
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
#btn-open-drawer { display: none; }

/* segment control: ⚡ Flash | ◉ Universal */
.segment {
  display: inline-flex; background: #eef0f8;
  border-radius: 999px; padding: 4px;
}
.seg-btn {
  border: 1.5px solid transparent; background: transparent;
  padding: 7px 16px; border-radius: 999px;
  font-size: 13.5px; font-weight: 700; color: var(--text-dim);
  cursor: pointer; white-space: nowrap;
}
.seg-btn.active {
  background: var(--gradient); color: #fff;
  border-color: transparent;
  box-shadow: 0 2px 10px rgba(99, 102, 241, 0.35);
}
.topbar-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }

/* search toggle */
.btn-icon {
  position: relative;
  border: 1.5px solid var(--border); background: var(--surface);
  width: 40px; height: 40px; border-radius: 999px;
  font-size: 16px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
}
.btn-icon[aria-pressed="true"] {
  background: var(--gradient); border-color: transparent; color: #fff;
}
.chip {
  position: absolute; top: -9px; right: -14px;
  font-size: 9.5px; font-weight: 800; letter-spacing: .03em;
  padding: 2.5px 7px; border-radius: 999px;
  text-transform: uppercase;
}
.chip-on  { background: #dcfce7; color: #15803d; border: 1px solid #bbf7d0; }
.chip-off { background: #f3f4f6; color: #6b7280; border: 1px solid #e5e7eb; }

/* ---------- messages ---------- */
.messages {
  flex: 1; overflow-y: auto;
  padding: 18px 16px 10px;
  display: flex; flex-direction: column; gap: 12px;
}
.empty-state {
  margin: auto; text-align: center; color: var(--text-dim);
  max-width: 420px;
}
.empty-logo {
  width: 64px; height: 64px; margin: 0 auto 14px;
  border-radius: 20px; background: var(--gradient); color: #fff;
  font-size: 34px; display: flex; align-items: center; justify-content: center;
}
.empty-state p { margin: 4px 0; font-size: 14.5px; }
.empty-hint { font-size: 12.5px !important; color: #9aa0b4 !important; }

/* карточки сообщения: user — правая, assistant — левая с accent border */
.msg {
  max-width: 82%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 11px 14px;
  font-size: 14.5px; line-height: 1.55;
  box-shadow: 0 1px 2px rgba(30, 30, 60, 0.04);
  overflow-wrap: anywhere;
}
.msg.user {
  align-self: flex-end;
  background: var(--gradient);
  border: 0; color: #fff;
  border-bottom-right-radius: 4px;
}
.msg.assistant {
  align-self: flex-start;
  border-left: 3px solid var(--accent);
  border-bottom-left-radius: 4px;
}
.msg .meta {
  font-size: 11px; color: var(--text-dim);
  margin-top: 6px; display: flex; gap: 8px;
}
.msg.user .meta { color: rgba(255, 255, 255, 0.75); }

/* rendering markdown внутри .msg */
.msg :is(p, ul, ol) { margin: 6px 0; }
.msg p:first-child { margin-top: 0; }
.msg p:last-child { margin-bottom: 0; }
.msg pre {
  background: #1e1e2e; color: #e5e7f0;
  padding: 12px; border-radius: 10px;
  overflow-x: auto; font-size: 13px;
}
.msg code {
  background: #eef0f8; padding: 1.5px 5px;
  border-radius: 5px; font-size: 13px;
}
.msg pre code { background: transparent; padding: 0; }
.msg a { color: var(--accent); }
.msg.user a { color: #fff; text-decoration: underline; }
.msg table { border-collapse: collapse; margin: 8px 0; }
.msg th, .msg td { border: 1px solid var(--border); padding: 5px 9px; font-size: 13.5px; }
.msg blockquote {
  margin: 6px 0; padding: 4px 12px;
  border-left: 3px solid var(--accent-2); color: var(--text-dim);
}

/* error message bubble */
.msg.error {
  align-self: stretch; max-width: 100%;
  background: #fef2f2; border: 1px solid #fecaca; color: #991b1b;
  font-size: 13.5px; padding: 10px 14px;
}

/* ---------- spinner (единственная анимация) ---------- */
.spinner {
  display: inline-block;
  width: 18px; height: 18px;
  border: 2px solid rgba(255, 255, 255, 0.35);
  border-top-color: #6366f1;
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
.spinner[hidden] { display: none; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- composer ---------- */
.composer {
  padding: 10px 14px calc(12px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.composer form {
  display: flex; align-items: flex-end; gap: 10px;
  background: var(--bg);
  border: 1.5px solid var(--border);
  border-radius: 16px;
  padding: 8px 8px 8px 14px;
}
.composer form:focus-within { border-color: var(--accent); background: #fff; }
.composer textarea {
  flex: 1; border: 0; background: transparent; resize: none;
  font: inherit; font-size: 15px; line-height: 1.45;
  max-height: 160px; outline: none; color: var(--text);
  padding: 6px 0;
}
.btn-send {
  flex: 0 0 auto;
  width: 44px; height: 44px; border-radius: 13px;
  border: 0; background: var(--gradient); color: #fff;
  font-size: 17px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  box-shadow: 0 4px 14px rgba(99, 102, 241, 0.35);
}
.btn-send:disabled { opacity: .55; cursor: default; }
.btn-send .send-icon { line-height: 1; }
.btn-send .spinner { border-color: rgba(255,255,255,.35); border-top-color: #fff; }

/* sending state */
.btn-send.sending .send-icon { display: none; }
.btn-send.sending .spinner { display: inline-block; }

/* ============================================================
   MOBILE < 720px: sidebar → drawer
   ============================================================ */
#drawer-backdrop {
  position: fixed; inset: 0; z-index: 20;
  background: rgba(20, 20, 40, 0.45);
}
@media (max-width: 719px) {
  .chat-view { grid-template-columns: 1fr; }

  .sidebar {
    position: fixed; z-index: 30;
    top: 0; left: 0; bottom: 0;
    width: min(300px, 84vw);
    transform: translateX(-100%);
    transition: transform .22s ease;
    box-shadow: 8px 0 30px rgba(20, 20, 40, 0.18);
  }
  .sidebar.open { transform: translateX(0); }
  #btn-close-drawer { display: inline-flex; }
  #btn-open-drawer { display: inline-flex; }

  .msg { max-width: 94%; }
  .segment { padding: 3px; }
  .seg-btn { padding: 6px 11px; font-size: 12.5px; }
  .topbar { padding: 8px 10px; gap: 8px; }
  .chip { right: -10px; }
  .messages { padding: 14px 10px 8px; }
}

/* scrollbars (subtle) */
.session-list::-webkit-scrollbar, .messages::-webkit-scrollbar { width: 8px; }
.session-list::-webkit-scrollbar-thumb, .messages::-webkit-scrollbar-thumb {
  background: #d6d9e8; border-radius: 8px;
}
/* swipe-left for regeneration (disabled per requirements) */
.msg.user {
  user-select: auto;  /* allow normal selection */
}
.msg.user.regen-busy {
  opacity: .55;
  filter: grayscale(.15);
}
.msg.assistant.regen-placeholder .content { min-height: 20px; }
.regen-hint { font-size: 12.5px; color: var(--text-dim); font-style: italic; }
