* { box-sizing: border-box; }

:root {
  --bg: #f5f5f7;
  --surface: #fff;
  --border: #e5e5e7;
  --text: #1d1d1f;
  --muted: #86868b;
  --accent: #0071e3;
  --accent-hover: #0077ed;
  --user-bg: #0071e3;
  --user-fg: #fff;
  --bot-bg: #f0f0f3;
  --bot-fg: #1d1d1f;
  --error: #d12c2c;
  --error-bg: #fee;
  --code-bg: #1d1d1f;
  --code-fg: #f5f5f7;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1c1e;
    --surface: #2c2c2e;
    --border: #3a3a3c;
    --text: #f5f5f7;
    --muted: #98989d;
    --user-bg: #0a84ff;
    --bot-bg: #3a3a3c;
    --bot-fg: #f5f5f7;
    --error-bg: #3a1f1f;
  }
}

html, body {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  margin: 0;
  font: 15px/1.45 -apple-system, "Segoe UI", "SF Pro Text", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
}

body.centered {
  min-height: 100vh;
  display: grid;
  place-items: center;
}

.card {
  background: var(--surface);
  border-radius: 12px;
  padding: 32px 28px;
  width: min(320px, 88vw);
  box-shadow: 0 4px 20px rgba(0,0,0,.08);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

@media (max-width: 480px) {
  .card {
    padding: 28px 20px;
    border-radius: 16px;
    width: 90vw;
  }
  .card h1 { font-size: 32px; }
  .card input, .card button { font-size: 16px; padding: 12px 16px; }
}

.card h1 {
  margin: 0 0 8px;
  font-size: 28px;
  text-align: center;
  font-weight: 600;
}

input, button, textarea {
  font: inherit;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  outline: none;
  background: var(--surface);
  color: var(--text);
}

input:focus, textarea:focus { border-color: var(--accent); }

button {
  background: var(--accent);
  color: #fff;
  border: none;
  cursor: pointer;
  padding: 10px 18px;
  font-weight: 500;
  transition: background .15s;
}

button:hover:not(:disabled) { background: var(--accent-hover); }
button:disabled { opacity: .5; cursor: default; }

.error {
  color: var(--error);
  font-size: 13px;
  text-align: center;
}

/* ── App layout ── */

.app {
  display: grid;
  grid-template-columns: 260px 1fr;
  height: 100vh;
  height: 100dvh;
  height: var(--app-h, 100dvh);
  background: var(--surface);
  overflow: hidden;
}

.sidebar {
  background: var(--bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  padding: 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sidebar-header h1 {
  margin: 0;
  font-size: 18px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}
.brand-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
}
.mobile-avatar {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  flex-shrink: 0;
}

.sidebar-header .new-btn {
  padding: 8px 12px;
  font-size: 13px;
  width: 100%;
}

.sessions-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.session-item {
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: background .1s;
  border: none;
  background: transparent;
  color: var(--text);
  text-align: left;
  width: 100%;
}

.session-item:hover { background: var(--border); }
.session-item.active { background: var(--accent); color: #fff; }
.session-item .meta { font-size: 11px; opacity: .7; }
.session-item .delete {
  visibility: hidden;
  background: transparent;
  color: var(--muted);
  border: none;
  padding: 2px 6px;
  font-size: 14px;
  cursor: pointer;
}
.session-item:hover .delete { visibility: visible; }
.session-item.active .delete { color: rgba(255,255,255,.7); }

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
}

.sidebar-footer .username { color: var(--muted); }
.sidebar-footer form button,
.sidebar-footer .footer-actions button {
  background: transparent;
  color: var(--muted);
  padding: 4px 8px;
  font-size: 13px;
  border: none;
  cursor: pointer;
}
.sidebar-footer form button:hover,
.sidebar-footer .footer-actions button:hover { color: var(--text); background: transparent; }
.sidebar-footer .footer-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}
.sidebar-footer .footer-actions button { font-size: 15px; }

.main {
  display: grid;
  grid-template-rows: 1fr auto;
  overflow: hidden;
}

#messages {
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-height: 0;
}

.msg {
  max-width: 75%;
  padding: 10px 14px;
  border-radius: 14px;
  line-height: 1.5;
  word-wrap: break-word;
}

.msg.user {
  align-self: flex-end;
  background: var(--user-bg);
  color: var(--user-fg);
  border-bottom-right-radius: 4px;
  white-space: pre-wrap;
}

.msg.bot {
  align-self: flex-start;
  background: var(--bot-bg);
  color: var(--bot-fg);
  border-bottom-left-radius: 4px;
  max-width: 85%;
}

.msg.system {
  align-self: center;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  font-style: italic;
}

.msg.error {
  align-self: center;
  background: var(--error-bg);
  color: var(--error);
  font-size: 13px;
}

/* Markdown styling inside .msg.bot */
.msg.bot > *:first-child { margin-top: 0; }
.msg.bot > *:last-child { margin-bottom: 0; }
.msg.bot p { margin: 8px 0; }
.msg.bot h1, .msg.bot h2, .msg.bot h3 {
  margin: 14px 0 6px;
  font-weight: 600;
}
.msg.bot h1 { font-size: 1.3em; }
.msg.bot h2 { font-size: 1.15em; }
.msg.bot h3 { font-size: 1.05em; }
.msg.bot ul, .msg.bot ol {
  margin: 6px 0;
  padding-left: 24px;
}
.msg.bot li { margin: 3px 0; }
.msg.bot blockquote {
  border-left: 3px solid var(--muted);
  margin: 6px 0;
  padding: 4px 12px;
  color: var(--muted);
}
.msg.bot a { color: var(--accent); }
.msg.bot table {
  border-collapse: collapse;
  margin: 8px 0;
  font-size: 13px;
}
.msg.bot th, .msg.bot td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}
.msg.bot th { background: rgba(0,0,0,.05); }
.msg.bot pre {
  background: var(--code-bg);
  color: var(--code-fg);
  padding: 12px 14px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 8px 0;
  font-size: 13px;
  line-height: 1.45;
}
.msg.bot pre code {
  background: transparent;
  padding: 0;
  border-radius: 0;
  font-size: inherit;
}
.msg.bot code {
  background: rgba(0,0,0,.08);
  padding: 1px 5px;
  border-radius: 4px;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 90%;
}

@media (prefers-color-scheme: dark) {
  .msg.bot code { background: rgba(255,255,255,.1); }
}

/* Footer / input */

footer {
  border-top: 1px solid var(--border);
  padding: 10px 14px;
  padding-bottom: max(10px, env(safe-area-inset-bottom, 10px));
  display: flex;
  gap: 8px;
  align-items: flex-end;
  background: var(--surface);
}

#input {
  flex: 1 1 auto;
  resize: none;
  min-height: 40px;
  max-height: 200px;
  border-radius: 20px;
  padding: 10px 14px;
  min-width: 0;
}

footer button {
  flex: 0 0 auto;
}

footer .attach-btn,
footer .mic-btn,
footer #send {
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  line-height: 1;
}

footer #send {
  font-size: 22px;
  background: var(--accent);
  color: #fff;
}

@media (max-width: 720px) {
  footer { padding: 8px 10px; padding-bottom: max(8px, env(safe-area-inset-bottom, 8px)); gap: 6px; }
  footer .attach-btn,
  footer .mic-btn,
  footer #send { width: 38px; height: 38px; }
  #input { min-height: 38px; font-size: 16px; padding: 9px 14px; }
}

/* Typing indicator */
.typing {
  display: inline-flex;
  gap: 4px;
  align-items: center;
  padding: 6px 4px;
}
.typing span {
  width: 6px; height: 6px;
  background: var(--muted);
  border-radius: 50%;
  animation: typing 1.2s infinite;
}
.typing span:nth-child(2) { animation-delay: .15s; }
.typing span:nth-child(3) { animation-delay: .3s; }
@keyframes typing {
  0%, 60%, 100% { transform: translateY(0); opacity: .4; }
  30% { transform: translateY(-4px); opacity: 1; }
}

/* Mobile */
@media (max-width: 720px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: 280px;
    transform: translateX(-100%);
    transition: transform .25s;
    z-index: 10;
    box-shadow: 2px 0 16px rgba(0,0,0,.15);
  }
  .sidebar.open { transform: translateX(0); }
  .hamburger {
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
    gap: 6px;
    background: var(--bot-bg);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 6px 12px;
    font-size: 18px;
    cursor: pointer;
    font-weight: 500;
  }
  .hamburger::after { content: "Чаты"; font-size: 14px; }
  .msg { max-width: 90%; }
}
.hamburger { display: none; }

.mobile-header {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}
@media (max-width: 720px) {
  .mobile-header { display: flex; }
  .main { grid-template-rows: auto 1fr auto; }
}
.mobile-header .title { font-weight: 600; }

.main {
  min-height: 0;
}
footer {
  flex-shrink: 0;
}

.backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 9;
}
.backdrop.show { display: block; }

/* ── Attachments ── */

.attach-btn,
.mic-btn {
  background: transparent;
  color: var(--muted);
  padding: 8px 10px;
  font-size: 18px;
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.attach-btn:hover,
.mic-btn:hover { background: var(--bot-bg); color: var(--text); }

.mic-btn.recording {
  background: #ff453a;
  color: #fff;
  border-color: #ff453a;
  animation: pulse 1s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 69, 58, .5); }
  50% { box-shadow: 0 0 0 8px rgba(255, 69, 58, 0); }
}

#attachments-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 0 20px;
}
#attachments-bar:empty { display: none; }
#attachments-bar + footer { padding-top: 8px; }

.file-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px;
  background: var(--bot-bg);
  border-radius: 8px;
  font-size: 12px;
  color: var(--text);
  text-decoration: none;
  max-width: 280px;
}
.file-chip .ico { font-size: 14px; }
.file-chip .fname {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}
.file-chip.pending { opacity: .7; }
.file-chip.uploaded { opacity: 1; }
.file-chip.error { background: var(--error-bg); color: var(--error); }
.file-chip .rm {
  background: transparent;
  color: var(--muted);
  border: none;
  padding: 0 2px;
  cursor: pointer;
  font-size: 14px;
}

.attachments-in-msg {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}
.msg.user .attachments-in-msg .file-chip {
  background: rgba(255,255,255,.2);
  color: #fff;
}
.msg.bot .attachments-in-msg .file-chip {
  background: var(--surface);
  border: 1px solid var(--border);
  cursor: pointer;
}
.msg.bot .attachments-in-msg .file-chip:hover { border-color: var(--accent); }

/* Drop overlay */
.drop-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 113, 227, .3);
  z-index: 100;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}
.drop-overlay.show { display: flex; }
.drop-inner {
  background: rgba(0,0,0,.85);
  color: #fff;
  padding: 24px 40px;
  border-radius: 12px;
  font-size: 18px;
  font-weight: 500;
}

/* Library modal */
.modal {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.5);
  z-index: 50;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.modal.show { display: flex; }
.modal-content {
  background: var(--surface);
  border-radius: 14px;
  width: 100%;
  max-width: 600px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 { margin: 0; font-size: 17px; }
.modal-close {
  background: transparent;
  color: var(--muted);
  border: none;
  font-size: 28px;
  cursor: pointer;
  padding: 0 8px;
  line-height: 1;
}
.modal-close:hover { color: var(--text); background: transparent; }
.modal-body {
  overflow-y: auto;
  padding: 14px 18px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.lib-section h3 {
  margin: 0 0 8px;
  font-size: 13px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .5px;
  font-weight: 600;
}
.lib-list { display: flex; flex-direction: column; gap: 6px; }
.lib-empty {
  color: var(--muted);
  font-size: 13px;
  padding: 12px 0;
  text-align: center;
}

.lib-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  background: var(--bg);
  border-radius: 8px;
}
.lib-icon { font-size: 20px; flex-shrink: 0; }
.lib-info { flex: 1; min-width: 0; }
.lib-name {
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.lib-meta { font-size: 11px; color: var(--muted); }
.lib-actions { display: flex; gap: 4px; flex-shrink: 0; }
.lib-act {
  background: transparent;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 6px 8px;
  border-radius: 6px;
  font-size: 16px;
  text-decoration: none;
  line-height: 1;
  display: inline-flex;
  align-items: center;
}
.lib-act:hover { background: var(--border); color: var(--text); }
.lib-act.download { color: var(--accent); }
.lib-act.delete:hover { color: var(--error); }

.sidebar-header .lib-btn {
  background: transparent;
  color: var(--text);
  padding: 8px 12px;
  font-size: 13px;
  width: 100%;
  border: 1px solid var(--border);
}
.sidebar-header .lib-btn:hover { background: var(--border); }
