/* App dashboard */

.app-shell {
  display: flex;
  height: 100vh;
  background: var(--surface-2);
  overflow: hidden;
}

/* Themed scrollbars */
.chat-messages,
.sidebar-history,
.settings-view {
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.chat-messages::-webkit-scrollbar,
.sidebar-history::-webkit-scrollbar,
.settings-view::-webkit-scrollbar {
  width: 8px;
}

.chat-messages::-webkit-scrollbar-track,
.sidebar-history::-webkit-scrollbar-track,
.settings-view::-webkit-scrollbar-track {
  background: transparent;
}

.chat-messages::-webkit-scrollbar-thumb,
.sidebar-history::-webkit-scrollbar-thumb,
.settings-view::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 99px;
  border: 2px solid transparent;
  background-clip: padding-box;
}

.chat-messages:hover::-webkit-scrollbar-thumb,
.sidebar-history:hover::-webkit-scrollbar-thumb,
.settings-view:hover::-webkit-scrollbar-thumb {
  background: rgba(124, 108, 255, 0.45);
  background-clip: padding-box;
}

/* Sidebar */
.app-sidebar {
  width: 260px;
  flex-shrink: 0;
  background: var(--card-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: width 0.2s, margin 0.2s;
}

.app-sidebar.collapsed {
  width: 68px;
}

.sidebar-top {
  padding: 16px 14px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.sidebar-toggle {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--ink-muted);
  cursor: pointer;
  display: grid;
  place-items: center;
}

.sidebar-toggle:hover { background: var(--surface-3); }

.sidebar-nav {
  padding: 0 10px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border: none;
  border-radius: 10px;
  background: transparent;
  font-family: inherit;
  font-size: 14px;
  font-weight: 500;
  color: var(--ink-muted);
  cursor: pointer;
  text-align: left;
  width: 100%;
}

.nav-item svg { width: 18px; height: 18px; flex-shrink: 0; }
.nav-item:hover { background: var(--surface-3); color: var(--ink); }
.nav-item.active { background: var(--brand-light); color: var(--brand); font-weight: 600; }

.app-sidebar.collapsed .nav-item span,
.app-sidebar.collapsed .sidebar-brand span,
.app-sidebar.collapsed .history-label,
.app-sidebar.collapsed .account-info { display: none; }

.sidebar-history {
  flex: 1;
  overflow-y: auto;
  padding: 8px 10px;
  margin-top: 8px;
}

.history-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-soft);
  padding: 8px 12px 6px;
}

.history-item {
  display: block;
  width: 100%;
  padding: 9px 12px;
  border: none;
  border-radius: 8px;
  background: transparent;
  font-family: inherit;
  font-size: 13px;
  color: var(--ink-muted);
  text-align: left;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-item:hover { background: var(--surface-3); color: var(--ink); }
.history-item.active { background: var(--surface-3); color: var(--ink); font-weight: 500; }

.sidebar-footer {
  padding: 12px 10px 14px;
  border-top: 1px solid var(--border);
}

.account-btn {
  display: flex;
  align-items: center;
  gap: 11px;
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-2);
  cursor: pointer;
  font-family: inherit;
  text-align: left;
  transition: background 0.15s, border-color 0.15s;
}

.account-btn:hover {
  background: var(--surface-3);
  border-color: rgba(124, 108, 255, 0.3);
}

.account-avatar {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--brand), var(--brand-dark));
  color: #fff;
  font-size: 14px;
  font-weight: 700;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  overflow: hidden;
}

.account-avatar img { width: 100%; height: 100%; object-fit: cover; }

.account-info { min-width: 0; flex: 1; }
.account-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.account-plan { font-size: 11px; color: var(--ink-soft); margin-top: 1px; }

.account-chevron {
  width: 16px;
  height: 16px;
  color: var(--ink-soft);
  flex-shrink: 0;
}

.app-sidebar.collapsed .account-chevron { display: none; }

/* Main */
.app-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  position: relative;
}

.app-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 24px;
  background: var(--nav-bg);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border-soft);
}

.topbar-title { font-size: 15px; font-weight: 700; display: flex; align-items: center; gap: 10px; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }

.extension-banner {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  margin: 12px 24px 0;
  background: var(--brand-light);
  border: 1px solid rgba(124, 108, 255, 0.2);
  border-radius: 12px;
  font-size: 13px;
  color: var(--ink-muted);
}

.extension-banner strong { color: var(--ink); }
.extension-banner .btn { padding: 8px 14px; font-size: 12px; margin-left: auto; flex-shrink: 0; }

/* Chat view */
.chat-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
}

.chat-empty {
  max-width: 720px;
  margin: 0 auto;
  text-align: center;
  padding: 48px 20px 32px;
}

.chat-empty h2 {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--ink-muted);
  margin-bottom: 28px;
}

.prompt-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  max-width: 640px;
  margin: 0 auto;
}

.prompt-card {
  padding: 16px 18px;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 14px;
  text-align: left;
  font-family: inherit;
  font-size: 13px;
  line-height: 1.5;
  color: var(--ink-muted);
  cursor: pointer;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.prompt-card:hover {
  border-color: rgba(124, 108, 255, 0.4);
  box-shadow: 0 4px 16px -8px var(--brand-glow);
  color: var(--ink);
}

.msg {
  max-width: 720px;
  margin: 0 auto 20px;
  display: flex;
  gap: 12px;
}

.msg.user { flex-direction: row-reverse; }

.msg-avatar {
  width: 32px;
  height: 32px;
  border-radius: 10px;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  font-size: 12px;
  font-weight: 700;
}

.msg.user .msg-avatar {
  background: linear-gradient(135deg, var(--brand), var(--brand-dark));
  color: #fff;
}

.msg.assistant .msg-avatar {
  background: var(--brand-light);
  color: var(--brand);
}

.msg-body {
  max-width: 85%;
  padding: 14px 16px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.65;
}

.msg.user .msg-body {
  background: linear-gradient(135deg, var(--brand), var(--brand-dark));
  color: #fff;
  border-bottom-right-radius: 4px;
}

.msg.assistant .msg-body {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
  color: var(--ink);
}

.msg-body p { margin-bottom: 8px; }
.msg-body p:last-child { margin-bottom: 0; }

/* Rendered markdown */
.msg-body h1, .msg-body h2, .msg-body h3, .msg-body h4 {
  font-weight: 700;
  line-height: 1.3;
  margin: 14px 0 8px;
  color: var(--ink);
}
.msg-body h1 { font-size: 1.25em; }
.msg-body h2 { font-size: 1.15em; }
.msg-body h3 { font-size: 1.05em; }
.msg-body h4 { font-size: 1em; }
.msg-body h1:first-child, .msg-body h2:first-child, .msg-body h3:first-child { margin-top: 0; }

.msg-body strong { font-weight: 700; }
.msg-body em { font-style: italic; }

.msg-body ul, .msg-body ol {
  margin: 8px 0 10px;
  padding-left: 1.4em;
}
.msg-body li { margin: 4px 0; }
.msg-body li > p { margin: 0; }

.msg-body code {
  font-family: ui-monospace, 'Cascadia Code', 'Consolas', monospace;
  font-size: 0.9em;
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 5px;
}

.msg-body pre {
  margin: 10px 0;
  padding: 12px 14px;
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow-x: auto;
}
.msg-body pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: 12px;
  line-height: 1.5;
}

.msg-body blockquote {
  margin: 10px 0;
  padding: 8px 12px;
  border-left: 3px solid var(--brand);
  background: var(--surface-2);
  border-radius: 0 8px 8px 0;
  color: var(--ink-muted);
}

.msg-body a {
  color: var(--brand);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.msg-body hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 14px 0;
}

.msg-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}

.msg-attach-img {
  max-width: 200px;
  max-height: 140px;
  border-radius: 10px;
  border: 1px solid var(--border);
  object-fit: cover;
}

.msg-attach-file {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  background: rgba(255,255,255,0.12);
  border-radius: 8px;
  font-size: 12px;
}

.msg.user .msg-attach-file { background: rgba(255,255,255,0.15); }

/* Attach preview strip */
.attach-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px 14px 0;
}

.attach-preview:not(.hidden) + .chat-textarea { padding-top: 8px; }

.attach-item {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 12px;
  color: var(--ink-muted);
  max-width: 180px;
}

.attach-item img {
  width: 40px;
  height: 40px;
  border-radius: 6px;
  object-fit: cover;
  flex-shrink: 0;
}

.attach-item .attach-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
  min-width: 0;
}

.attach-remove {
  width: 20px;
  height: 20px;
  border: none;
  border-radius: 50%;
  background: var(--surface-3);
  color: var(--ink-muted);
  cursor: pointer;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  font-size: 14px;
  line-height: 1;
}
.attach-remove:hover { background: rgba(239,68,68,0.2); color: #f87171; }

.tool-icon-btn {
  width: 34px;
  height: 34px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--ink-muted);
  cursor: pointer;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.tool-icon-btn:hover {
  background: var(--surface-3);
  color: var(--ink);
  border-color: rgba(124, 108, 255, 0.35);
}
.tool-icon-btn svg { width: 16px; height: 16px; }

.msg-loading {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--ink-muted);
  font-size: 14px;
}

.msg-spin {
  width: 16px;
  height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--brand);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* Input bar */
.chat-input-wrap {
  padding: 16px 24px 24px;
  background: linear-gradient(180deg, transparent, var(--surface-2) 30%);
}

.chat-input-box {
  max-width: 760px;
  margin: 0 auto;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}

.chat-input-box:focus-within {
  border-color: rgba(124, 108, 255, 0.5);
  box-shadow: 0 0 0 4px rgba(124, 108, 255, 0.12);
}

.chat-textarea {
  width: 100%;
  min-height: 56px;
  max-height: 160px;
  padding: 16px 18px 8px;
  border: none;
  outline: none;
  resize: none;
  font-family: inherit;
  font-size: 15px;
  line-height: 1.5;
  background: transparent;
  color: var(--ink);
}

.chat-textarea::placeholder { color: var(--ink-soft); }

.chat-input-tools {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px 12px;
  gap: 8px;
  flex-wrap: wrap;
}

.tool-group { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

.tool-select {
  appearance: none;
  padding: 7px 28px 7px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23808090' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E") no-repeat right 8px center;
  font-family: inherit;
  font-size: 12px;
  font-weight: 500;
  color: var(--ink-muted);
  cursor: pointer;
}

.tool-select option { background: var(--card-bg); color: var(--ink); }

.tool-chip {
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  font-family: inherit;
  font-size: 12px;
  font-weight: 500;
  color: var(--ink-muted);
  cursor: pointer;
}

.tool-chip.active {
  background: var(--brand-light);
  border-color: rgba(124, 108, 255, 0.35);
  color: var(--brand);
}

.btn-send {
  width: 38px;
  height: 38px;
  border: none;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--brand), var(--brand-dark));
  color: #fff;
  cursor: pointer;
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

.btn-send:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-send svg { width: 18px; height: 18px; }

/* Settings view */
.settings-view {
  flex: 1;
  overflow-y: auto;
  padding: 32px 24px;
}

.settings-inner {
  max-width: 640px;
  margin: 0 auto;
}

.settings-inner h1 {
  font-size: 26px;
  font-weight: 800;
  letter-spacing: -0.02em;
  margin-bottom: 24px;
}

.settings-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 24px;
  margin-bottom: 16px;
}

.settings-card h2 {
  font-size: 15px;
  font-weight: 700;
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border-soft);
  font-size: 14px;
}

.settings-row:last-child { border-bottom: none; padding-bottom: 0; }
.settings-row .label { color: var(--ink-muted); }
.settings-row .value { font-weight: 600; text-align: right; }

.usage-bar-wrap {
  margin-top: 12px;
  height: 8px;
  background: var(--surface-3);
  border-radius: 99px;
  overflow: hidden;
}

.usage-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--brand), #8b5cf6);
  border-radius: 99px;
  transition: width 0.3s;
}

.settings-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 16px;
}

.btn-danger {
  background: rgba(239, 68, 68, 0.1);
  color: #f87171;
  border: 1px solid rgba(239, 68, 68, 0.3);
  padding: 10px 16px;
  border-radius: 10px;
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.btn-danger:hover { background: rgba(239, 68, 68, 0.18); }

html[data-theme="light"] .btn-danger {
  background: #fef2f2;
  color: #b91c1c;
  border-color: #fecaca;
}

html[data-theme="light"] .btn-danger:hover { background: #fee2e2; }

.security-note {
  margin-top: 12px;
  padding: 12px 14px;
  background: var(--surface-2);
  border-radius: 10px;
  font-size: 13px;
  color: var(--ink-muted);
  line-height: 1.55;
}

.hidden { display: none !important; }

@media (max-width: 768px) {
  .app-sidebar { position: fixed; z-index: 50; height: 100%; left: 0; transform: translateX(-100%); }
  .app-sidebar.open { transform: translateX(0); }
  .prompt-grid { grid-template-columns: 1fr; }
}
