:root {
  --ai-bg: #f5f7fb;
  --ai-panel: #ffffff;
  --ai-panel-hover: #f1f5f9;
  --ai-panel-soft: #f8fafc;
  --ai-text: #111827;
  --ai-text-soft: #334155;
  --ai-text-muted: #64748b;
  --ai-border: #e2e8f0;
  --ai-primary: #667eea;
  --ai-primary-2: #764ba2;
  --ai-accent: #10a37f;
}

html,
body {
  height: 100%;
  overflow: hidden;
  background: var(--ai-bg);
}

body {
  display: flex;
  flex-direction: column;
  height: 100svh;
  height: 100dvh;
}

.header {
  flex: 0 0 auto;
}

.ai-home-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 38px;
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
}

.ai-home-link {
  border: 1px solid #dbe2ea;
  background: #ffffff;
  color: #334155;
  transition: border-color 0.2s, color 0.2s, box-shadow 0.2s;
}

.ai-home-link:hover {
  border-color: var(--primary);
  color: var(--primary);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.12);
}

.ai-page {
  flex: 1 1 auto;
  height: auto;
  min-height: 0;
  background: var(--ai-bg);
  padding: 0;
  overflow: hidden;
}

.ai-container {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  gap: 0;
  height: 100%;
  min-height: 0;
  max-width: none;
  padding: 0;
}

.ai-sidebar {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--ai-panel);
  border-right: 1px solid var(--ai-border);
  box-shadow: 10px 0 24px rgba(15, 23, 42, 0.03);
}

.ai-sidebar > div:first-child {
  padding: 18px 26px 8px;
  border-bottom: 0;
}

.ai-sidebar h1 {
  margin: 0;
  color: var(--ai-text);
  font-size: 20px;
  line-height: 1.3;
  font-weight: 700;
}

.ai-tabs {
  display: grid;
  gap: 6px;
  padding: 6px 18px 12px;
}

.ai-tab {
  width: 100%;
  min-height: 44px;
  border: 0;
  background: transparent;
  color: var(--ai-text-soft);
  border-radius: 8px;
  padding: 12px 8px;
  font-size: 14px;
  cursor: pointer;
  text-align: left;
  transition: background 0.2s, color 0.2s, transform 0.2s;
}

.ai-tab:hover {
  background: var(--ai-panel-hover);
  color: var(--ai-text);
}

.ai-tab.active {
  background: linear-gradient(135deg, var(--ai-primary) 0%, var(--ai-primary-2) 100%);
  color: #fff;
  font-weight: 700;
  box-shadow: 0 8px 20px rgba(102, 126, 234, 0.18);
}

.ai-composer label {
  color: var(--ai-text-muted);
  font-size: 12px;
  font-weight: 700;
}

#aiQuestion:focus {
  box-shadow: none;
}

.ai-sidebar-actions {
  display: grid;
  gap: 10px;
  margin-top: auto;
  padding: 14px 18px 18px;
  border-top: 1px solid var(--ai-border);
}

.ai-sidebar-actions[hidden] {
  display: none;
}

.ai-sidebar-actions .btn {
  min-height: 40px;
  border-color: var(--ai-border);
  background: #ffffff;
  color: var(--ai-text-soft);
  border-radius: 8px;
}

.ai-sidebar-actions .btn:hover {
  background: var(--ai-panel-hover);
  color: var(--ai-text);
}

.ai-chat-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background: var(--ai-panel);
}

.ai-chat-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 24px;
  border-bottom: 1px solid var(--ai-border);
  background: var(--ai-panel);
}

.ai-chat-head h2 {
  margin: 0;
  color: var(--ai-text);
  font-size: 18px;
  font-weight: 700;
}

.ai-messages {
  min-height: 0;
  overflow-y: auto;
  padding: 24px;
  background: #ffffff;
}

.ai-message {
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr);
  gap: 12px;
  max-width: 980px;
  margin: 0 auto 20px;
  animation: aiFadeIn 0.24s ease;
}

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

.ai-message.user {
  grid-template-columns: minmax(0, 1fr) 36px;
}

.ai-message.user .ai-avatar {
  grid-column: 2;
  background: linear-gradient(135deg, var(--ai-primary) 0%, var(--ai-primary-2) 100%);
}

.ai-message.user .ai-bubble {
  grid-column: 1;
  grid-row: 1;
  justify-self: end;
  max-width: min(72%, 720px);
  background: linear-gradient(135deg, var(--ai-primary) 0%, var(--ai-primary-2) 100%);
  color: #ffffff;
  box-shadow: 0 10px 22px rgba(102, 126, 234, 0.22);
}

.ai-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  background: linear-gradient(135deg, #10b981 0%, #3b82f6 100%);
  color: #ffffff;
  font-size: 12px;
  font-weight: 800;
}

.ai-bubble {
  max-width: min(78%, 780px);
  width: fit-content;
  min-width: 120px;
  border-radius: 12px;
  padding: 12px 16px;
  background: #ffffff;
  color: #333333;
  line-height: 1.65;
  word-break: break-word;
  border: 1px solid #e6ebf2;
  box-shadow: 0 8px 22px rgba(15, 23, 42, 0.05);
}

.ai-message.error .ai-bubble {
  border: 1px solid #fecaca;
  background: #fff5f5;
  color: #991b1b;
}

.ai-bubble pre {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  font: inherit;
  line-height: 1.7;
}

.ai-answer-content {
  display: grid;
  gap: 9px;
  color: #333333;
  font-size: 14px;
  line-height: 1.72;
}

.ai-answer-content h3 {
  margin: 12px 0 0;
  color: #111827;
  font-size: 16px;
  line-height: 1.4;
}

.ai-answer-content h3:first-child {
  margin-top: 0;
}

.ai-answer-content p {
  margin: 0;
}

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

.ai-answer-content li {
  margin: 4px 0;
}

.ai-answer-content strong {
  color: #111827;
  font-weight: 700;
}

.ai-answer-content code {
  border-radius: 5px;
  padding: 2px 5px;
  background: #e9eef5;
  color: #0f172a;
  font-family: Consolas, Monaco, monospace;
  font-size: 0.92em;
}

.ai-math {
  width: fit-content;
  max-width: 100%;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  padding: 8px 11px;
  background: #ffffff;
  color: #111827;
  font-family: Consolas, Monaco, monospace;
  line-height: 1.6;
  white-space: pre-wrap;
}

.ai-message-image {
  display: block;
  width: min(280px, 100%);
  max-height: 260px;
  object-fit: contain;
  border-radius: 10px;
  margin-bottom: 10px;
  background: #ffffff;
}

.ai-message-meta {
  margin-top: 8px;
  font-size: 12px;
  color: rgba(51, 51, 51, 0.55);
}

.ai-message.user .ai-message-meta {
  color: rgba(255, 255, 255, 0.76);
}

.ai-thinking {
  display: inline-flex;
  gap: 5px;
  align-items: center;
  min-height: 24px;
}

.ai-thinking span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--ai-primary);
  animation: aiPulse 1s infinite ease-in-out;
}

.ai-thinking span:nth-child(2) {
  animation-delay: 0.14s;
}

.ai-thinking span:nth-child(3) {
  animation-delay: 0.28s;
}

@keyframes aiPulse {
  0%, 80%, 100% {
    transform: translateY(0);
    opacity: 0.35;
  }
  40% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

.ai-resource-links {
  display: grid;
  gap: 8px;
  margin-top: 12px;
}

.ai-resource {
  display: grid;
  gap: 4px;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  padding: 10px 12px;
  background: #ffffff;
  color: #111827;
}

.ai-resource:hover {
  border-color: var(--ai-primary);
}

.ai-resource strong {
  font-size: 14px;
  line-height: 1.45;
}

.ai-resource span {
  color: #6b7280;
  font-size: 12px;
}

.ai-composer {
  display: grid;
  grid-template-columns: 38px minmax(0, 1fr) 38px;
  grid-auto-rows: auto;
  gap: 8px;
  align-items: center;
  width: min(900px, calc(100% - 48px));
  margin: 0 auto 10px;
  padding: 7px 10px;
  border: 1px solid #dbe3ef;
  border-radius: 16px;
  background: #ffffff;
  box-shadow: 0 12px 28px rgba(15, 23, 42, 0.08);
  transition: border-color 0.18s, box-shadow 0.18s, background 0.18s;
}

.ai-composer:focus-within {
  border-color: rgba(102, 126, 234, 0.65);
  box-shadow: 0 14px 34px rgba(102, 126, 234, 0.16);
}

.ai-composer label {
  display: none;
}

#aiQuestion {
  grid-column: 2;
  grid-row: 1;
  align-self: center;
  width: 100%;
  height: 40px;
  min-height: 40px;
  max-height: 144px;
  resize: none;
  border: 0;
  border-radius: 10px;
  padding: 8px 4px;
  background: transparent;
  color: #111827;
  font-size: 14px;
  line-height: 24px;
  outline: none;
  overflow-y: hidden;
}

#aiQuestion::placeholder {
  color: #64748b;
}

.ai-composer-actions {
  display: contents;
}

.ai-icon-btn,
.ai-send-btn {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid transparent;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  transition: background 0.18s, border-color 0.18s, transform 0.18s, opacity 0.18s;
}

.ai-icon-btn {
  grid-column: 1;
  grid-row: 1;
  border-color: transparent;
  background: transparent;
  color: #64748b;
}

.ai-icon-btn:hover {
  background: #eef2ff;
  border-color: transparent;
  color: #4f46e5;
}

.ai-send-btn {
  grid-column: 3;
  grid-row: 1;
  border: 0;
  background: #111827;
  color: #ffffff;
}

.ai-send-btn:hover {
  transform: translateY(-1px);
  background: #1f2937;
}

.ai-send-btn:disabled,
.ai-icon-btn:disabled,
.ai-tab:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  transform: none;
}

.ai-image-preview {
  grid-column: 1 / -1;
  grid-row: 1;
  display: grid;
  grid-template-columns: 72px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  border: 1px dashed var(--ai-border);
  border-radius: 14px;
  padding: 10px;
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.08), rgba(16, 185, 129, 0.08));
}

.ai-composer:has(.ai-image-preview:not([hidden])) #aiQuestion,
.ai-composer:has(.ai-image-preview:not([hidden])) .ai-icon-btn,
.ai-composer:has(.ai-image-preview:not([hidden])) .ai-send-btn {
  grid-row: 2;
}

.ai-image-preview[hidden] {
  display: none;
}

.ai-image-preview img {
  width: 72px;
  height: 72px;
  object-fit: cover;
  border-radius: 10px;
  background: #fff;
}

.ai-image-preview strong {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: #111827;
  font-size: 13px;
}

.ai-image-tip {
  margin: 4px 0 0;
  color: #64748b;
  font-size: 12px;
  line-height: 1.45;
}

.ai-image-preview button {
  margin-top: 6px;
  border: 0;
  background: transparent;
  color: #fb7185;
  font-size: 13px;
  cursor: pointer;
}

.ai-messages::-webkit-scrollbar {
  width: 6px;
}

.ai-messages::-webkit-scrollbar-track {
  background: #f8fafc;
}

.ai-messages::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border-radius: 999px;
}

.ai-messages::-webkit-scrollbar-thumb:hover {
  background: #94a3b8;
}

@media screen and (max-width: 900px) {
  .ai-page {
    height: auto;
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
  }

  .ai-container {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr);
    min-height: 0;
  }

  .ai-sidebar {
    border-right: 0;
    border-bottom: 1px solid var(--ai-border);
  }

  .ai-tabs,
  .ai-sidebar-actions {
    grid-template-columns: 1fr 1fr;
  }

  .ai-chat-panel {
    min-height: 0;
  }
}

@media screen and (max-width: 640px) {
  html,
  body {
    height: 100svh;
  }

  body {
    overflow: hidden;
  }

  .ai-page {
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
  }

  .ai-container {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0;
  }

  .ai-sidebar {
    flex: 0 0 auto;
  }

  .ai-chat-panel {
    display: flex;
    flex: 1 1 auto;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
  }

  .ai-sidebar > div:first-child,
  .ai-chat-head,
  .ai-composer {
    padding-left: 16px;
    padding-right: 16px;
  }

  .ai-sidebar > div:first-child {
    display: none;
  }

  .ai-tabs {
    padding: 8px 12px;
  }

  .ai-tabs,
  .ai-sidebar-actions {
    grid-template-columns: 1fr;
  }

  .ai-messages {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    padding: 14px 12px 10px;
  }

  .ai-chat-head {
    padding-top: 10px;
    padding-bottom: 10px;
  }

  .ai-composer {
    flex: 0 0 auto;
    grid-template-columns: 34px minmax(0, 1fr) 34px;
    width: calc(100% - 20px);
    margin: 0 auto max(28px, calc(env(safe-area-inset-bottom) + 22px));
    padding: 6px 8px;
    border-radius: 14px;
  }

  #aiQuestion {
    height: 38px;
    min-height: 38px;
    max-height: 112px;
    padding: 7px 2px;
    line-height: 22px;
  }

  .ai-icon-btn,
  .ai-send-btn {
    width: 34px;
    height: 34px;
    min-height: 34px;
  }

  .ai-message,
  .ai-message.user {
    grid-template-columns: 30px minmax(0, 1fr);
    gap: 9px;
  }

  .ai-message.user .ai-avatar {
    grid-column: 1;
  }

  .ai-message.user .ai-bubble {
    grid-column: 2;
    justify-self: stretch;
    max-width: 100%;
  }

  .ai-bubble {
    max-width: 100%;
  }

  .ai-avatar {
    width: 30px;
    height: 30px;
  }

  .header-actions {
    gap: 8px;
  }

  .header-actions .btn {
    padding: 7px 10px;
    font-size: 13px;
  }
}
