/* ============================================
   AI Аналитик — AI Chat UI
   Themes: Warm Light (default) + Warm Dark
   ============================================ */

/* Respect user motion preferences (WCAG 2.3.3) */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* Отключаем ВСЕ transitions на момент смены темы — иначе 100+ элементов
   анимируют одновременно bg/color/border и браузер лагает */
html.theme-switching,
html.theme-switching *,
html.theme-switching *::before,
html.theme-switching *::after {
  transition: none !important;
}

:root,
[data-theme="light"] {
  /* Surfaces — холодный нейтрал, hue ~220, шаг L* ~4 */
  --bg-deep:       #EEF0F3;
  --bg-primary:    #F4F5F7;
  --bg-secondary:  #FFFFFF;
  --bg-tertiary:   #E9EBEF;
  --bg-elevated:   #FFFFFF;
  --bg-hover:      #E3E6EB;
  --bg-active:     #D9DCE2;

  /* Accent — Forest Green, 5.82:1 на bg-primary */
  --accent:         #0F7A4F;
  --accent-hover:   #0C6641;
  --accent-press:   #0A563A;
  --accent-dim:     #4A9E7C;
  --accent-glow:    rgba(15, 122, 79, 0.16);
  --accent-subtle:  rgba(15, 122, 79, 0.08);
  --accent-wash:    rgba(15, 122, 79, 0.04);

  /* Bubbles */
  --user-bubble:             rgba(15, 122, 79, 0.10);
  --user-bubble-border:      rgba(15, 122, 79, 0.22);
  --user-bubble-accent:      #0F7A4F;
  --user-bubble-text:        #0A3C28;
  --assistant-bubble:        #FFFFFF;
  --assistant-bubble-border: #E1E4EA;

  /* Text — графит с холодным оттенком */
  --text-primary:   #0E1217;
  --text-secondary: #3E4651;
  --text-muted:     #6B7280;
  --text-accent:    #0F7A4F;
  --text-link:      #1D4ED8;
  --text-on-accent: #FFFFFF;
  --text-inverse:   #F4F5F7;

  /* Borders */
  --border:         #D9DDE3;
  --border-light:   #E6E9EE;
  --border-strong:  #C3C8D1;
  --border-accent:  rgba(15, 122, 79, 0.32);

  /* Semantic */
  --success:        #0F7A4F;
  --success-bg:     rgba(15, 122, 79, 0.10);
  --warning:        #B45309;
  --warning-bg:     #FEF3C7;
  --error:          #B42318;
  --error-bg:       #FEE4E2;
  --info:           #1D4ED8;
  --info-bg:        #DBEAFE;

  /* Shadows — холодные, многослойные */
  --shadow-xs: 0 1px 1px rgba(14, 18, 23, 0.04);
  --shadow-sm: 0 1px 2px rgba(14, 18, 23, 0.06), 0 1px 1px rgba(14, 18, 23, 0.04);
  --shadow-md: 0 4px 8px rgba(14, 18, 23, 0.06), 0 2px 4px rgba(14, 18, 23, 0.05), 0 0 0 1px rgba(14, 18, 23, 0.02);
  --shadow-lg: 0 16px 32px rgba(14, 18, 23, 0.10), 0 4px 8px rgba(14, 18, 23, 0.06);
  --shadow-glow: 0 0 0 4px var(--accent-glow);
  --shadow-inset: inset 0 1px 0 rgba(255, 255, 255, 0.6);

  /* Gradients */
  --gradient-bg:     linear-gradient(180deg, #F4F5F7 0%, #EEF0F3 100%);
  --gradient-accent: linear-gradient(135deg, #0F7A4F 0%, #0C6641 100%);
  --gradient-card:   linear-gradient(180deg, #FFFFFF 0%, #FBFBFC 100%);

  /* Code */
  --code-bg:        #EEF0F3;
  --code-fg:        #0E1217;
  --code-block-bg:  #0E1217;
  --code-block-fg:  #E4E7EC;

  /* Structural (unchanged) */
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-xl: 20px;
  --radius-full: 9999px;

  --sidebar-width: 320px;
  --header-height: 60px;
  --input-height: 64px;

  --font-body: 'Manrope', -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: 'JetBrains Mono', 'Fira Code', monospace;

  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
  --ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);

  color-scheme: light;
}

[data-theme="dark"] {
  /* Surfaces — честный графит, hue ~225 */
  --bg-deep:       #0A0C10;
  --bg-primary:    #0F1216;
  --bg-secondary:  #161A20;
  --bg-tertiary:   #1C2028;
  --bg-elevated:   #1E2229;
  --bg-hover:      #242933;
  --bg-active:     #2C323D;

  /* Accent — приподнятый зелёный, 8.1:1 */
  --accent:         #34D399;
  --accent-hover:   #4ADE9F;
  --accent-press:   #22B57E;
  --accent-dim:     #1E8A63;
  --accent-glow:    rgba(52, 211, 153, 0.22);
  --accent-subtle:  rgba(52, 211, 153, 0.12);
  --accent-wash:    rgba(52, 211, 153, 0.06);

  /* Bubbles */
  --user-bubble:             rgba(52, 211, 153, 0.14);
  --user-bubble-border:      rgba(52, 211, 153, 0.30);
  --user-bubble-accent:      #34D399;
  --user-bubble-text:        #D1FAE5;
  --assistant-bubble:        #161A20;
  --assistant-bubble-border: #262B35;

  /* Text */
  --text-primary:   #E4E7EC;
  --text-secondary: #A1A9B5;
  --text-muted:     #8F897A;    /* AA-fix (5.1:1) сохранён */
  --text-accent:    #34D399;
  --text-link:      #60A5FA;
  --text-on-accent: #0A3C28;
  --text-inverse:   #0E1217;

  /* Borders */
  --border:         #262B35;
  --border-light:   #1E2229;
  --border-strong:  #353B47;
  --border-accent:  rgba(52, 211, 153, 0.35);

  /* Semantic */
  --success:        #34D399;
  --success-bg:     rgba(52, 211, 153, 0.12);
  --warning:        #F59E0B;
  --warning-bg:     rgba(245, 158, 11, 0.12);
  --error:          #F87171;
  --error-bg:       rgba(248, 113, 113, 0.12);
  --info:           #60A5FA;
  --info-bg:        rgba(96, 165, 250, 0.12);

  /* Shadows + inner-highlight для карточности в темноте */
  --shadow-xs: 0 1px 1px rgba(0, 0, 0, 0.3);
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4), 0 1px 1px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 8px rgba(0, 0, 0, 0.4), 0 2px 4px rgba(0, 0, 0, 0.3), inset 0 1px 0 rgba(255, 255, 255, 0.04);
  --shadow-lg: 0 16px 32px rgba(0, 0, 0, 0.5), 0 4px 8px rgba(0, 0, 0, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.05);
  --shadow-glow: 0 0 0 4px var(--accent-glow);
  --shadow-inset: inset 0 1px 0 rgba(255, 255, 255, 0.08);

  /* Gradients */
  --gradient-bg:     linear-gradient(180deg, #0F1216 0%, #0A0C10 100%);
  --gradient-accent: linear-gradient(135deg, #34D399 0%, #22B57E 100%);
  --gradient-card:   linear-gradient(180deg, #181C23 0%, #141820 100%);

  /* Code */
  --code-bg:        #1C2028;
  --code-fg:        #E4E7EC;
  --code-block-bg:  #0A0C10;
  --code-block-fg:  #E4E7EC;

  color-scheme: dark;
}

/* ============ RESET ============ */

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  height: 100dvh; /* dynamic viewport height — respects mobile keyboard + address bar */
  overflow: hidden;
}

body {
  font-family: var(--font-body);
  font-size: 14px;
  line-height: 1.5;
  color: var(--text-primary);
  background: var(--bg-deep);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

input, textarea, button {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
  border: none;
  outline: none;
  background: none;
}

button {
  cursor: pointer;
}

a {
  color: var(--text-link);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

.hidden {
  display: none !important;
}

/* ============ SCROLLBAR ============ */

::-webkit-scrollbar {
  width: 6px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: var(--bg-hover);
  border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover {
  background: var(--bg-active);
}

/* ============ LOGIN SCREEN ============ */

#login-screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-deep);
  z-index: 1000;
  animation: loginFadeIn 0.6s var(--ease-out);
}

.login-bg-grid {
  position: absolute;
  inset: 0;
  background-image:
    linear-gradient(var(--accent-wash) 1px, transparent 1px),
    linear-gradient(90deg, var(--accent-wash) 1px, transparent 1px);
  background-size: 60px 60px;
  mask-image: radial-gradient(ellipse 60% 60% at 50% 50%, black 20%, transparent 70%);
}

.login-card {
  position: relative;
  width: 380px;
  max-width: calc(100vw - 32px);
  padding: 48px 40px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg), var(--shadow-glow);
  animation: loginCardIn 0.8s var(--ease-out) 0.2s both;
}

.login-logo {
  text-align: center;
  margin-bottom: 36px;
}

.login-logo-icon {
  width: 64px;
  height: 64px;
  margin: 0 auto 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  font-weight: 800;
  letter-spacing: -0.02em;
  color: var(--text-on-accent);
  background: var(--accent);
  border-radius: var(--radius-lg);
  box-shadow: var(--accent-glow);
}

.login-logo h1 {
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.5px;
  color: var(--text-primary);
}

.login-logo p {
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 4px;
}

.input-group {
  margin-bottom: 20px;
}

.input-group label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.input-group input {
  width: 100%;
  padding: 12px 16px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 14px;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.input-group input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.input-group input::placeholder {
  color: var(--text-muted);
}

.login-error {
  font-size: 13px;
  color: var(--error);
  min-height: 20px;
  margin-bottom: 8px;
  text-align: center;
}

.login-btn {
  width: 100%;
  padding: 12px;
  background: var(--accent);
  color: var(--text-on-accent);
  font-weight: 700;
  font-size: 14px;
  border-radius: var(--radius-sm);
  transition: background 0.2s, transform 0.15s;
}

.login-btn:hover {
  background: var(--accent-hover);
  transform: translateY(-1px);
}

.login-btn:active {
  transform: translateY(0);
}

.login-footer {
  text-align: center;
  margin-top: 24px;
  font-size: 11px;
  color: var(--text-muted);
}

/* ============ APP LAYOUT ============ */

#app {
  display: flex;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

/* ============ SIDEBAR ============ */

#sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg-deep);
  border-right: 1px solid var(--border);
  z-index: 100;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 16px 12px;
  border-bottom: 1px solid var(--border);
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-icon {
  width: 38px;
  height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 800;
  letter-spacing: -0.02em;
  color: var(--text-on-accent);
  background: var(--accent);
  border-radius: var(--radius-sm);
}

.brand-text {
  display: flex;
  flex-direction: column;
}

.brand-name {
  font-size: 15px;
  font-weight: 700;
}

.brand-status {
  font-size: 11px;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: 5px;
}

.status-dot {
  width: 7px;
  height: 7px;
  background: var(--accent);
  border-radius: 50%;
  animation: pulse 2s infinite;
}

#new-chat-btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  border-radius: var(--radius-sm);
  transition: all 0.2s;
}

#new-chat-btn:hover {
  background: var(--bg-hover);
  color: var(--accent);
}

.sidebar-search {
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text-muted);
}

.sidebar-search input {
  flex: 1;
  padding: 8px 0;
  font-size: 13px;
  color: var(--text-primary);
}

.sidebar-search input::placeholder {
  color: var(--text-muted);
}

.session-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px;
}

.session-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s;
  position: relative;
}

.session-item:hover {
  background: var(--bg-hover);
}

.session-item.active {
  background: var(--accent-subtle);
  box-shadow: inset 3px 0 0 var(--accent);
  color: var(--text-primary);
}

.session-item.active .session-title {
  font-weight: 600;
  color: var(--text-primary);
}

.session-icon {
  width: 40px;
  height: 40px;
  min-width: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-tertiary);
  border-radius: var(--radius-sm);
  font-size: 18px;
}

.session-item.active .session-icon {
  background: var(--accent-subtle);
}

.session-info {
  flex: 1;
  min-width: 0;
}

.session-title {
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-preview {
  font-size: 12px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.session-time {
  font-size: 11px;
  color: var(--text-muted);
  align-self: flex-start;
  white-space: nowrap;
}

.session-empty {
  padding: 40px 20px;
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
  line-height: 1.6;
}

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}

.logout-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  width: 100%;
  font-size: 13px;
  color: var(--text-secondary);
  border-radius: var(--radius-sm);
  transition: all 0.2s;
}

.logout-btn:hover {
  background: var(--bg-hover);
  color: var(--error);
}

/* ============ CHAT AREA ============ */

#chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  position: relative;
  background: var(--bg-secondary);
}

.chat-header {
  height: var(--header-height);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  z-index: 10;
}

.chat-header-info h2 {
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-subtitle {
  font-size: 12px;
  color: var(--text-secondary);
}

.icon-btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  border-radius: var(--radius-sm);
  transition: all 0.2s;
}

.icon-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.icon-btn.danger-hover:hover {
  background: var(--bg-hover);
  color: var(--error);
}

/* ============ MESSAGES ============ */

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  scroll-behavior: smooth;
}

.message {
  display: flex;
  animation: messageIn 0.35s var(--ease-out) both;
  max-width: 75%;
}

.message.user {
  align-self: flex-end;
}

.message.assistant {
  align-self: flex-start;
}

.message-bubble {
  padding: 10px 16px;
  border-radius: var(--radius-lg);
  line-height: 1.6;
  word-wrap: break-word;
  overflow-wrap: break-word;
  position: relative;
}

.message.user .message-bubble {
  background: var(--user-bubble);
  border: 1px solid var(--user-bubble-border);
  color: var(--user-bubble-text);
  border-bottom-right-radius: 4px;
  box-shadow: inset 2px 0 0 var(--user-bubble-accent);
}

.message.assistant .message-bubble {
  background: var(--assistant-bubble);
  border: 1px solid var(--assistant-bubble-border);
  border-bottom-left-radius: 4px;
  box-shadow: var(--shadow-sm);
}

.message-time {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 4px;
  text-align: right;
}

.message.assistant .message-time {
  text-align: left;
}

/* Message content formatting */
.message-content h3 {
  font-size: 14px;
  font-weight: 700;
  margin: 8px 0 4px;
  color: var(--text-primary);
}

.message-content h4 {
  font-size: 13px;
  font-weight: 600;
  margin: 6px 0 2px;
}

.message-content p {
  margin: 4px 0;
}

.message-content strong {
  font-weight: 600;
  color: var(--text-primary);
}

.message-content em {
  font-style: italic;
  color: var(--text-secondary);
}

.message-content code {
  font-family: var(--font-mono);
  font-size: 12px;
  background: rgba(0,0,0,0.3);
  padding: 2px 6px;
  border-radius: 4px;
}

.message-content pre {
  margin: 8px 0;
  padding: 12px;
  background: rgba(0,0,0,0.3);
  border-radius: var(--radius-sm);
  overflow-x: auto;
}

.message-content pre code {
  background: none;
  padding: 0;
  font-size: 12px;
  line-height: 1.5;
}

.message-content ul, .message-content ol {
  margin: 6px 0;
  padding-left: 22px;
}

.message-content li {
  margin: 3px 0;
  padding-left: 2px;
}

/* Мягкий цвет маркеров — не отвлекает от содержания */
.message-content li::marker {
  color: var(--text-muted);
}

/* В пользовательском пузыре маркер в зелёном тоне — консистентно с акцентом */
.message.user .message-content li::marker {
  color: var(--user-bubble-accent);
}

.message-content table {
  border-collapse: collapse;
  margin: 8px 0;
  font-size: 12px;
  width: 100%;
}

.message-content th, .message-content td {
  padding: 6px 10px;
  border: 1px solid var(--border);
  text-align: left;
}

.message-content th {
  background: var(--bg-tertiary);
  font-weight: 600;
}

/* ============ STATUS MESSAGES ============ */

.message.status {
  align-self: flex-start;
  max-width: min(400px, 90vw);
}

.status-bubble {
  padding: 14px 18px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  font-size: 13px;
  line-height: 1.55;
  max-width: 100%;
}

.status-title {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 4px;
}

.status-desc {
  font-size: 12px;
  color: var(--text-secondary);
  line-height: 1.5;
}

/* ============ FILE CARDS ============ */

.file-card {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 8px 0;
  padding: 12px 16px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all 0.2s;
}

.file-card:hover {
  background: var(--bg-hover);
  border-color: var(--accent-dim);
}

.file-icon {
  width: 42px;
  height: 42px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #e74c3c, #c0392b);
  border-radius: var(--radius-sm);
  font-size: 11px;
  font-weight: 700;
  color: white;
  letter-spacing: 0.5px;
}

.file-info {
  flex: 1;
  min-width: 0;
}

.file-name {
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.file-size {
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 1px;
}

.file-download {
  color: var(--accent);
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  transition: background 0.2s;
  background: none;
  border: 1px solid var(--accent);
  cursor: pointer;
}

.file-download:hover {
  background: var(--accent-glow);
  text-decoration: none;
}

.file-download:disabled {
  opacity: 0.5;
  cursor: wait;
}

/* ============ TYPING INDICATOR ============ */

.typing-indicator {
  align-self: flex-start;
  animation: messageIn 0.3s var(--ease-out);
}

.typing-bubble {
  display: flex;
  gap: 5px;
  padding: 14px 20px;
  background: var(--assistant-bubble);
  border: 1px solid var(--assistant-bubble-border);
  border-radius: var(--radius-lg);
  border-bottom-left-radius: 4px;
}

.typing-dot {
  width: 8px;
  height: 8px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: typingBounce 1.4s infinite ease-in-out;
}

.typing-dot:nth-child(2) { animation-delay: 0.16s; }
.typing-dot:nth-child(3) { animation-delay: 0.32s; }

/* ============ EMPTY STATE ============ */

.empty-state {
  position: absolute;
  inset: var(--header-height) 0 0 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px;
  text-align: center;
  animation: fadeIn 0.5s var(--ease-out);
}

/* Hide input area when empty state is visible */
.empty-state:not(.hidden) ~ .input-area {
  display: none;
}

.empty-logo {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 44px;
  font-weight: 800;
  letter-spacing: -0.03em;
  color: var(--accent);
  background: none;
  border: none;
  box-shadow: none;
  margin-bottom: 16px;
  filter: drop-shadow(0 4px 12px var(--accent-glow));
  line-height: 1;
}

.empty-state h2 {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: var(--text-primary);
  margin-bottom: 6px;
}

.empty-state p {
  color: var(--text-secondary);
  font-size: 14px;
  margin-bottom: 28px;
}

.empty-actions {
  display: flex;
  gap: 12px;
  margin-bottom: 32px;
}

.action-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 13px;
  transition: all 0.2s;
}

.action-btn.primary {
  background: var(--accent);
  color: var(--text-on-accent);
}

.action-btn.primary:hover {
  background: var(--accent-hover);
  transform: translateY(-1px);
  box-shadow: var(--shadow-glow);
}

.action-btn.secondary {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border: 1px solid var(--border);
}

.action-btn.secondary:hover {
  background: var(--bg-hover);
  border-color: var(--text-muted);
}

/* (empty-hints styles moved to HINT PILLS block below) */

/* ============ INPUT AREA ============ */

.input-area {
  padding: 12px 24px 16px;
  padding-bottom: max(16px, env(safe-area-inset-bottom, 16px));
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  width: 100%;
  box-sizing: border-box;
}

.input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 4px 4px 4px 16px;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.input-wrapper:focus-within {
  border-color: var(--accent);
  box-shadow: var(--shadow-glow);
}

.input-wrapper textarea {
  flex: 1;
  resize: none;
  padding: 10px 0;
  max-height: 140px;
  line-height: 1.5;
  color: var(--text-primary);
  background: transparent;
  font-size: 14px;
}

.input-wrapper textarea::placeholder {
  color: var(--text-muted);
}

.send-btn {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--gradient-accent);
  color: #FFFFFF;
  border-radius: var(--radius-sm);
  transition: all 0.2s;
  flex-shrink: 0;
}

.send-btn:disabled {
  background: var(--bg-hover);
  color: var(--text-muted);
  cursor: default;
}

.send-btn:not(:disabled):hover {
  background: var(--accent-hover);
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

.input-footer {
  display: flex;
  justify-content: space-between;
  padding: 6px 4px 0;
  font-size: 11px;
  color: var(--text-muted);
}

.char-count.warning {
  color: var(--warning);
}

.char-count.danger {
  color: var(--error);
}

/* ============ MOBILE ============ */

.sidebar-toggle {
  display: none;
  position: fixed;
  top: calc((var(--header-height) - 44px) / 2);
  left: 12px;
  z-index: 200;
  width: 44px;
  height: 44px;
  align-items: center;
  justify-content: center;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  z-index: 90;
  opacity: 0;
  transition: opacity 0.3s;
}

.sidebar-overlay.visible {
  display: block;
  opacity: 1;
}

@media (max-width: 768px) {
  .sidebar-toggle {
    display: flex;
  }

  #sidebar {
    position: fixed;
    left: 0;
    top: 0;
    height: 100vh;
    height: 100dvh;
    transform: translateX(-100%);
    transition: transform 0.3s var(--ease-out);
    box-shadow: var(--shadow-lg);
  }

  #sidebar.open {
    transform: translateX(0);
  }

  .message {
    max-width: 88%;
  }

  /* Chat header: leave room for sidebar toggle */
  .chat-header {
    padding-left: 64px;
    padding-right: 12px;
    padding-top: env(safe-area-inset-top, 0);
  }

  .chat-header-info {
    min-width: 0;
    flex: 1;
  }

  /* Empty state: fit on mobile screen */
  .empty-state {
    padding: 24px 16px;
  }

  .empty-actions {
    flex-direction: column;
    width: 100%;
  }

  .empty-actions button {
    width: 100%;
  }

  /* Touch targets: 44px minimum */
  .icon-btn, #new-chat-btn {
    width: 44px;
    height: 44px;
  }

  .logout-btn {
    padding: 12px 16px;
  }

  .file-download {
    padding: 10px 16px;
    min-height: 44px;
  }

  /* Input area: prevent overflow */
  .input-area {
    padding: 8px 12px 12px;
    padding-bottom: max(12px, env(safe-area-inset-bottom, 12px));
  }

  .input-wrapper {
    padding: 4px 4px 4px 12px;
  }

  /* Sidebar toggle: vertically center with header, accounting for safe area */
  .sidebar-toggle {
    top: calc(env(safe-area-inset-top, 0px) + (var(--header-height) - 44px) / 2);
  }
}

/* Small phones (≤480px) */
@media (max-width: 480px) {
  .login-card {
    padding: 32px 24px;
  }

  .login-logo h1 {
    font-size: 22px;
  }

  .login-logo-icon {
    width: 52px;
    height: 52px;
    font-size: 22px;
  }

  .empty-state h2 {
    font-size: 18px;
  }

  .empty-state p {
    font-size: 13px;
  }

  .empty-logo {
    width: 56px;
    height: 56px;
    font-size: 24px;
  }

  .empty-hints {
    gap: 6px;
  }

  .hint-pill {
    font-size: 11px;
    padding: 6px 10px;
  }

  .input-footer {
    display: none;
  }

  .message-content table {
    font-size: 11px;
    display: block;
    overflow-x: auto;
  }

  .message-content pre {
    overflow-x: auto;
    max-width: calc(100vw - 80px);
  }

  .typing-dot {
    width: 10px;
    height: 10px;
  }

  ::-webkit-scrollbar {
    width: 2px;
  }

  /* Compact messages area padding */
  #messages {
    padding: 12px 8px;
  }

  .message-bubble {
    padding: 8px 12px;
  }
}

/* ============ ANIMATIONS ============ */

@keyframes loginFadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes loginCardIn {
  from {
    opacity: 0;
    transform: translateY(20px) scale(0.97);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes messageIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes statusLineIn {
  from {
    opacity: 0;
    transform: translateX(-8px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes fadeOut {
  from { opacity: 1; }
  to { opacity: 0; }
}

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

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

@keyframes typingBounce {
  0%, 80%, 100% {
    transform: scale(0.7);
    opacity: 0.4;
  }
  40% {
    transform: scale(1);
    opacity: 1;
  }
}

/* removed: unused @keyframes checkIn, slideUp */

/* ============ CHAT HEADER ACTIONS ============ */
.chat-header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* ============ CONTEXT INDICATOR ============ */
.context-indicator {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 6px 12px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 12px; color: var(--text-secondary);
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  cursor: pointer;
}
.context-indicator:hover { background: var(--bg-hover); color: var(--text-primary); }
.context-indicator .ctx-label { font-weight: 500; }
.context-indicator .ctx-value { font-variant-numeric: tabular-nums; font-weight: 600; color: var(--text-primary); }
.context-indicator .ctx-bar {
  width: 80px; height: 4px;
  background: var(--border); border-radius: 2px;
  overflow: hidden; position: relative;
}
.context-indicator .ctx-bar-fill {
  display: block; height: 100%;
  background: var(--accent); border-radius: 2px;
  transition: width 0.4s ease, background 0.2s;
}
.context-indicator[data-level="warn"] { color: var(--warning); border-color: rgba(198, 138, 30, 0.4); }
.context-indicator[data-level="warn"] .ctx-value { color: var(--warning); }
.context-indicator[data-level="warn"] .ctx-bar-fill { background: var(--warning); }
.context-indicator[data-level="danger"] {
  color: var(--error); border-color: rgba(196, 54, 46, 0.4); background: var(--error-bg);
}
.context-indicator[data-level="danger"] .ctx-value { color: var(--error); }
.context-indicator[data-level="danger"] .ctx-bar-fill { background: var(--error); }

@media (max-width: 768px) {
  .context-indicator .ctx-label { display: none; }
  .context-indicator .ctx-bar { width: 40px; }
  .context-indicator { padding: 6px 10px; }
}
@media (max-width: 480px) {
  .context-indicator .ctx-bar { display: none; }
}

/* ============ TOAST ============ */
.toast-container {
  position: fixed; bottom: 24px; right: 24px;
  z-index: 2000; display: flex; flex-direction: column; gap: 10px;
  max-width: calc(100vw - 48px); pointer-events: none;
}
.toast {
  display: flex; align-items: flex-start; gap: 12px;
  padding: 14px 16px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow-lg);
  max-width: 380px; pointer-events: auto;
  animation: toastIn 0.3s ease;
}
.toast.warn { border-left: 3px solid var(--warning); }
.toast.danger { border-left: 3px solid var(--error); }
.toast-body { flex: 1; }
.toast-title { font-weight: 600; font-size: 13px; margin-bottom: 2px; color: var(--text-primary); }
.toast-desc { font-size: 12px; color: var(--text-secondary); line-height: 1.5; }
.toast-actions { display: flex; gap: 8px; margin-top: 8px; }
.toast-actions button { font-size: 12px; font-weight: 600; padding: 6px 10px; border-radius: 6px; }
.toast-action-primary { background: var(--accent); color: var(--text-on-accent); }
.toast-action-secondary { background: transparent; color: var(--text-secondary); }
.toast-close { color: var(--text-muted); padding: 2px; border-radius: 4px; }
@keyframes toastIn {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}
@media (max-width: 480px) {
  .toast-container { bottom: 16px; right: 12px; left: 12px; }
  .toast { max-width: none; }
}

/* ============ MODAL ============ */
.modal {
  position: fixed; inset: 0; z-index: 1500;
  display: none; align-items: center; justify-content: center;
  padding: 32px 16px;
}
.modal.open { display: flex; }
.modal-overlay {
  position: absolute; inset: 0;
  background: rgba(31, 29, 24, 0.45);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  animation: fadeIn 0.2s ease;
}
[data-theme="dark"] .modal-overlay { background: rgba(0, 0, 0, 0.65); }
.modal-window {
  position: relative; width: 100%; max-width: 960px;
  max-height: min(calc(100dvh - 64px), 920px);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow-lg);
  display: grid;
  grid-template-rows: auto 1fr;
  grid-template-columns: 180px 1fr;
  grid-template-areas: "header header" "nav body";
  overflow: hidden;
  animation: modalIn 0.28s ease;
}
.modal-header {
  grid-area: header;
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elevated);
}
.modal-header h1 { font-size: 16px; font-weight: 700; letter-spacing: -0.01em; color: var(--text-primary); }
.modal-close {
  width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-secondary); border-radius: 6px;
  transition: background 0.15s, color 0.15s;
}
.modal-close:hover { background: var(--bg-hover); color: var(--text-primary); }
.modal-nav {
  grid-area: nav;
  padding: 16px 8px 16px 16px;
  border-right: 1px solid var(--border);
  background: var(--bg-tertiary);
  display: flex; flex-direction: column; gap: 2px;
  overflow-y: auto;
}
.modal-nav-item {
  padding: 8px 12px; font-size: 13px;
  color: var(--text-secondary);
  border-radius: 6px; text-decoration: none;
  transition: background 0.15s, color 0.15s;
}
.modal-nav-item:hover { background: var(--bg-hover); color: var(--text-primary); text-decoration: none; }
.modal-nav-item.active { background: var(--accent-subtle); color: var(--accent); font-weight: 600; }
.modal-body {
  grid-area: body;
  overflow-y: auto;
  padding: 24px 28px 32px;
  scroll-behavior: smooth;
}
.settings-section { margin-bottom: 12px; }
.settings-section + .settings-section { margin-top: 8px; }
.section-divider { border: none; border-top: 1px solid var(--border); margin: 20px 0 20px; }
.section-head { margin-bottom: 16px; }
.section-head h2 { font-size: 15px; font-weight: 700; letter-spacing: -0.01em; margin-bottom: 4px; color: var(--text-primary); }
.section-desc { font-size: 12px; color: var(--text-secondary); line-height: 1.5; }
.setting-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; padding: 12px 0;
}
.setting-label { display: flex; flex-direction: column; gap: 2px; }
.setting-name { font-size: 13px; font-weight: 500; color: var(--text-primary); }
.setting-hint { font-size: 12px; color: var(--text-muted); }
.theme-switcher { display: flex; gap: 8px; }
.theme-option {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 10px 14px 8px;
  background: var(--bg-tertiary);
  border: 1.5px solid var(--border);
  border-radius: 8px;
  font-size: 11px; font-weight: 500;
  color: var(--text-secondary);
  transition: border-color 0.15s, color 0.15s, background 0.15s;
}
.theme-option:hover { border-color: var(--border-strong); color: var(--text-primary); }
.theme-option[aria-checked="true"] {
  border-color: var(--accent); color: var(--text-primary); background: var(--accent-subtle);
}
.theme-preview { width: 42px; height: 28px; border-radius: 5px; border: 1px solid var(--border); }
.theme-preview-light  { background: linear-gradient(135deg, #F3F1EC 50%, #FFFFFF 50%); border-color: #D4CEC0; }
.theme-preview-dark   { background: linear-gradient(135deg, #14110E 50%, #1A1713 50%); }
.theme-preview-system { background: linear-gradient(135deg, #FBFAF7 50%, #14110E 50%); }

/* Instruction cards */
.instruction-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 12px;
  overflow: hidden;
  transition: border-color 0.15s;
}
.instruction-card:focus-within { border-color: var(--border-strong); }
.instruction-head {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 12px;
  padding: 14px 16px 10px;
}
.instruction-head h3 { font-size: 13px; font-weight: 600; color: var(--text-primary); }
.instruction-meta { font-size: 11px; color: var(--text-muted); margin-top: 2px; }
.instruction-saved { font-size: 11px; color: var(--text-muted); font-style: italic; white-space: nowrap; }
.instruction-saved.dirty { color: var(--warning); font-style: normal; font-weight: 600; }
.instruction-editor {
  width: 100%; padding: 14px 16px;
  border: none;
  border-top: 1px solid var(--border-light);
  border-bottom: 1px solid var(--border-light);
  background: var(--bg-primary);
  color: var(--text-primary);
  font-family: 'Manrope', sans-serif;
  font-size: 13px; line-height: 1.6;
  resize: vertical; min-height: 280px; max-height: 560px;
  box-sizing: border-box;
}
@media (max-width: 640px) {
  .instruction-editor { min-height: 220px; max-height: 50dvh; }
}
.instruction-editor:focus { background: var(--bg-secondary); outline: none; }
.instruction-foot {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px;
}
.char-counter { font-size: 11px; color: var(--text-muted); font-variant-numeric: tabular-nums; }
.btn-primary-sm {
  padding: 6px 14px; background: var(--accent); color: var(--text-on-accent);
  font-size: 12px; font-weight: 600;
  border-radius: 6px; border: none; cursor: pointer;
  transition: background 0.15s;
}
.btn-primary-sm:hover { background: var(--accent-hover); }
.btn-primary-sm:disabled { background: var(--bg-hover); color: var(--text-muted); cursor: default; }

/* About list */
.about-list { display: flex; flex-direction: column; gap: 10px; }
.about-row { display: flex; justify-content: space-between; gap: 16px; font-size: 13px; }
.about-row dt { color: var(--text-secondary); }
.about-row dd { color: var(--text-primary); font-weight: 500; }
.about-row code {
  font-family: 'JetBrains Mono', monospace; font-size: 12px;
  background: var(--code-bg); padding: 2px 6px; border-radius: 4px;
}
.muted { color: var(--text-muted); }

/* Modal mobile */
@media (max-width: 640px) {
  .modal { padding: 0; align-items: stretch; }
  .modal-window {
    max-width: none; max-height: 100dvh; border-radius: 0;
    grid-template-columns: 1fr;
    grid-template-areas: "header" "nav" "body";
  }
  .modal-nav {
    flex-direction: row; overflow-x: auto;
    padding: 8px 12px;
    border-right: none; border-bottom: 1px solid var(--border);
    gap: 4px;
  }
  .modal-nav-item { white-space: nowrap; padding: 8px 14px; }
  .modal-body { padding: 16px; }
  .setting-row { flex-direction: column; align-items: stretch; }
  .theme-switcher { justify-content: space-between; }
  .theme-option { flex: 1; }
}
@keyframes modalIn {
  from { opacity: 0; transform: translateY(12px) scale(0.98); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

/* ============ HINT PILLS ============ */
.empty-hints { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }
.hint-pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 14px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 12px;
  color: var(--text-secondary);
  transition: all 0.15s;
  cursor: pointer;
}
.hint-pill:hover {
  background: var(--accent-subtle);
  border-color: var(--accent);
  color: var(--text-primary);
  transform: translateY(-1px);
}
.hint-icon { font-size: 14px; }
