:root {
  --bg: #f6f1eb;
  --bg-soft: #fbf8f4;
  --panel: #ffffff;
  --panel-2: #f3ece6;
  --text: #332b3f;
  --muted: #7f738b;
  --accent: #6c1e5c;
  --accent-2: #4a043b;
  --accent-soft: #efe6f7;
  --border: #e4d9ea;
  --user: #ede2f7;
  --assistant: #ffffff;
  --success: #6f9b78;
  --shadow: 0 10px 28px rgba(82, 58, 107, 0.08);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  overflow: hidden;
}

body {
  margin: 0;
  font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background:
    radial-gradient(circle at top left, rgba(164, 135, 200, 0.16), transparent 28%),
    linear-gradient(180deg, #fbf8f4 0%, #f6f1eb 100%);
  color: var(--text);
}

code {
  background: #f1e8f7;
  color: #5d447c;
  padding: 0.1rem 0.35rem;
  border-radius: 0.35rem;
}

.app-shell {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: 100vh;
  overflow: hidden;
}

.sidebar {
  border-right: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.72);
  backdrop-filter: blur(10px);
  overflow-y: auto;
  min-height: 0;
}

.sidebar-inner {
  padding: 24px;
}

.brand {
  display: flex;
  gap: 14px;
  align-items: center;
  margin-bottom: 20px;
}

.brand-mark {
  width: 52px;
  height: 52px;
  border-radius: 16px;
  display: grid;
  place-items: center;
  font-weight: 700;
  color: #ffffff;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 8px 20px rgba(127, 90, 166, 0.22);
}

.brand h1 {
  margin: 0;
  font-size: 1.3rem;
  color: #4a3a5f;
}

.brand p {
  margin: 0.35rem 0 0;
  color: var(--muted);
  font-size: 0.92rem;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}

.panel h2 {
  margin: 0 0 12px;
  font-size: 1rem;
  color: #5b4675;
}

.mode-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.radio-option {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: #fcfaf8;
  cursor: pointer;
  transition:
    border-color 0.15s ease,
    background 0.15s ease,
    box-shadow 0.15s ease;
}

.radio-option:hover {
  border-color: #d3c1e2;
  background: #faf6fc;
}

.radio-option input[type="radio"] {
  margin-top: 4px;
  accent-color: var(--accent);
  flex: 0 0 auto;
}

.radio-content {
  min-width: 0;
}

.radio-title {
  font-size: 0.95rem;
  font-weight: 700;
  color: #4a3a5f;
  margin-bottom: 4px;
}

.radio-desc {
  font-size: 0.86rem;
  line-height: 1.5;
  color: var(--muted);
}

.main {
  height: 100vh;
  min-height: 0;
  overflow: hidden;
  background: transparent;
}

.chat-layout {
  display: grid;
  grid-template-rows: 1fr auto;
  height: 100%;
  min-height: 0;
}

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

.message {
  max-width: 900px;
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 16px 18px;
  margin: 0 0 14px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.message.user {
  margin-left: auto;
  background: var(--user);
}

.message.assistant {
  background: var(--assistant);
}

.message-role {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 8px;
}

.message-body {
  white-space: normal;
  line-height: 1.62;
  color: var(--text);
  overflow-wrap: anywhere;
  word-break: break-word;
  width: 100%;
  max-width: 100%;
}

.message-body.has-html {
  overflow-wrap: anywhere;
}

.message-meta {
  margin-top: 10px;
  color: var(--muted);
  font-size: 0.84rem;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.composer {
  flex-shrink: 0;
  border-top: 1px solid var(--border);
  padding: 18px 24px 24px;
  background: rgba(251, 248, 244, 0.92);
  backdrop-filter: blur(10px);
}

#questionInput {
  width: 100%;
  resize: none;
  min-height: 88px;
  max-height: 220px;
  border-radius: 16px;
  border: 1px solid var(--border);
  background: #ffffff;
  color: var(--text);
  padding: 14px 16px;
  font: inherit;
  line-height: 1.5;
  box-shadow: inset 0 1px 2px rgba(70, 48, 95, 0.04);
}

#questionInput::placeholder {
  color: #a092ad;
}

#questionInput:focus {
  outline: none;
  border-color: rgba(127, 90, 166, 0.45);
  box-shadow:
    0 0 0 4px rgba(127, 90, 166, 0.12),
    inset 0 1px 2px rgba(70, 48, 95, 0.04);
}

.composer-actions {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  margin-top: 12px;
}

button {
  border: 0;
  border-radius: 999px;
  padding: 12px 18px;
  font: inherit;
  font-weight: 700;
  color: #ffffff;
  background: #8a6aad;
  cursor: pointer;
  box-shadow: 0 10px 22px rgba(127, 90, 166, 0.14);
  transition:
    transform 0.15s ease,
    box-shadow 0.15s ease,
    opacity 0.15s ease,
    background 0.15s ease;
}

button:hover:not(:disabled) {
  transform: translateY(-1px);
  background: #78589d;
  box-shadow: 0 14px 26px rgba(127, 90, 166, 0.18);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.65;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 0.25rem 0.55rem;
  border-radius: 999px;
  font-size: 0.8rem;
  background: #eef7f0;
  color: #4f7a59;
  border: 1px solid #d5e7d8;
}

.badge.warning {
  background: #fbf3df;
  color: #9a7a25;
  border-color: #efdfb0;
}

.message.thinking {
  background: #f8f3fb;
  border-style: dashed;
  opacity: 0.95;
}

.message.thinking .message-role {
  color: #8a6aad;
}

.message.thinking .message-body {
  color: #7f738b;
  font-style: italic;
}

.message.thinking .message-meta {
  color: #a092ad;
}

.structured-content {
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  width: 100%;
  max-width: 100%;
  line-height: 1.58;
}

.structured-content h1,
.structured-content h2,
.structured-content h3 {
  width: 100%;
  max-width: 100%;
  margin: 14px 0 4px;
  color: #4d3b63;
  line-height: 1.32;
}

.structured-content h1:first-child,
.structured-content h2:first-child,
.structured-content h3:first-child {
  margin-top: 2px;
}

.structured-content h1 {
  font-size: 1.22rem;
}

.structured-content h2 {
  font-size: 1.12rem;
}

.structured-content h3 {
  font-size: 1.02rem;
}

.structured-content p {
  margin: 0;
  width: 100%;
  max-width: 100%;
}

.structured-content ul {
  margin: 2px 0 4px 20px;
  padding-right: 8px;
  width: calc(100% - 20px);
  max-width: calc(100% - 20px);
}

.structured-content li {
  margin: 4px 0;
  line-height: 1.55;
}

.structured-content strong {
  font-weight: 800;
}

.content-link,
.message-link {
  color: var(--accent);
  font-weight: 700;
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
  overflow-wrap: anywhere;
}

.content-link:hover,
.message-link:hover {
  color: var(--accent-2);
}

.message-link-row {
  margin: 4px 0;
}

.message-link-card {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  max-width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #fcfaf8;
  color: var(--accent);
  line-height: 1.35;
  text-decoration: none;
  box-shadow: 0 6px 16px rgba(82, 58, 107, 0.05);
  transition:
    background 0.15s ease,
    border-color 0.15s ease,
    transform 0.15s ease,
    box-shadow 0.15s ease;
}

.message-link-card:hover {
  border-color: #d3c1e2;
  background: #faf6fc;
  color: var(--accent-2);
  text-decoration: none;
  transform: translateY(-1px);
  box-shadow: 0 9px 20px rgba(82, 58, 107, 0.08);
}

.message-link-arrow {
  flex: 0 0 auto;
  font-size: 0.9rem;
}

.message-figure {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 100%;
  margin: 8px 0 12px;
  padding: 0;
  line-height: normal;
  text-align: center;
}

.message-image-link {
  display: inline-block;
  max-width: 100%;
  margin-left: auto;
  margin-right: auto;
  color: inherit;
  text-decoration: none;
  cursor: pointer;
  line-height: 0;
}

.message-image-frame {
  position: relative;
  display: inline-block;
  width: auto;
  max-width: 100%;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 0;
  background: #fcfaf8;
  box-shadow: 0 8px 22px rgba(82, 58, 107, 0.08);
  line-height: 0;
  transition:
    border-color 0.15s ease,
    box-shadow 0.15s ease,
    transform 0.15s ease,
    background 0.15s ease;
}

.message-image {
  display: block;
  width: auto;
  max-width: 100%;
  height: auto;
  max-height: 420px;
  object-fit: contain;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
  transition: filter 0.15s ease;
}

.message-image.is-clickable,
.message-image.is-gallery-image {
  cursor: pointer;
}

.message-image-link:hover .message-image-frame {
  border-color: #d3c1e2;
  background: #faf6fc;
  transform: translateY(-1px);
  box-shadow: 0 12px 28px rgba(82, 58, 107, 0.12);
}

.message-image-link:hover .message-image {
  filter: brightness(0.985);
}

.message-image-open {
  position: absolute;
  right: 10px;
  bottom: 10px;
  padding: 5px 10px;
  border-radius: 999px;
  background: rgba(74, 4, 59, 0.82);
  color: #ffffff;
  font-size: 0.78rem;
  font-weight: 700;
  line-height: 1;
  opacity: 0;
  transform: translateY(4px);
  pointer-events: none;
  transition:
    opacity 0.15s ease,
    transform 0.15s ease;
}

.message-image-link:hover .message-image-open {
  opacity: 1;
  transform: translateY(0);
}

.message-image-gallery-button {
  display: inline-block;
  width: auto;
  max-width: 100%;
  margin: 0 auto;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: inherit;
  line-height: 0;
  box-shadow: none;
  cursor: zoom-in;
}

.message-image-gallery-button:hover,
.message-image-gallery-button:focus {
  background: transparent;
  box-shadow: none;
  transform: none;
}

.message-image-gallery-button:focus-visible {
  outline: 3px solid rgba(108, 30, 92, 0.28);
  outline-offset: 3px;
}

.message-image-gallery-button:hover .message-image-frame {
  border-color: #d3c1e2;
  background: #faf6fc;
  transform: translateY(-1px);
  box-shadow: 0 12px 28px rgba(82, 58, 107, 0.12);
}

.message-image-gallery-button:hover .message-image {
  filter: brightness(0.985);
}

.message-image-gallery-button:hover .message-image-open {
  opacity: 1;
  transform: translateY(0);
}

body.is-gallery-open {
  overflow: hidden;
}

.image-gallery-modal {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: grid;
  place-items: center;
  padding: 48px 72px;
  background: rgba(20, 14, 27, 0.82);
}

.image-gallery-modal[hidden] {
  display: none;
}

.image-gallery-content {
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
}

.image-gallery-full-image {
  display: block;
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: 0;
  background: #ffffff;
  box-shadow: 0 22px 70px rgba(0, 0, 0, 0.35);
}

.image-gallery-close,
.image-gallery-nav {
  position: fixed;
  z-index: 10000;
  display: flex;
  align-items: center;
  justify-content: center;
  width: auto;
  margin: 0;
  border: 0;
  color: #ffffff;
  background: rgba(74, 4, 59, 0.86);
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.22);
  font-family: Arial, Helvetica, sans-serif;
  font-weight: 400;
  text-align: center;
  transition:
    background 0.15s ease,
    box-shadow 0.15s ease;
}

.image-gallery-close {
  top: 22px;
  right: 22px;
  width: 44px;
  height: 44px;
  padding: 0 0 3px;
  border-radius: 999px;
  font-size: 2rem;
  line-height: 1;
  transform: none;
}

.image-gallery-nav {
  top: 50%;
  width: 48px;
  height: 64px;
  padding: 0 0 6px;
  border-radius: 999px;
  font-size: 3rem;
  line-height: 1;
  transform: translateY(-50%);
}

.image-gallery-modal .image-gallery-close:hover:not(:disabled),
.image-gallery-modal .image-gallery-close:focus:not(:disabled) {
  background: rgba(74, 4, 59, 0.96);
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.22);
  transform: none;
}

.image-gallery-modal .image-gallery-nav:hover:not(:disabled),
.image-gallery-modal .image-gallery-nav:focus:not(:disabled) {
  background: rgba(74, 4, 59, 0.96);
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.22);
  transform: translateY(-50%);
}

.image-gallery-prev {
  left: 22px;
}

.image-gallery-next {
  right: 22px;
}

.image-gallery-counter {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%);
  padding: 6px 12px;
  border-radius: 999px;
  background: rgba(74, 4, 59, 0.86);
  color: #ffffff;
  font-size: 0.86rem;
  font-weight: 700;
}

@media (max-width: 960px) {
  html,
  body {
    overflow: auto;
  }

  .app-shell {
    grid-template-columns: 1fr;
    height: auto;
    min-height: 100vh;
    overflow: visible;
  }

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

  .main {
    height: auto;
    overflow: visible;
  }

  .chat-layout {
    height: auto;
  }

  .messages {
    overflow: visible;
    padding: 18px;
  }

  button {
    width: 100%;
  }

  .message {
    max-width: 100%;
    border-radius: 18px;
  }

  .message-figure {
    margin: 8px 0 12px;
  }

  .message-image {
    max-height: 300px;
    border-radius: 0;
  }

  .message-image-frame {
    border-radius: 0;
  }

  .message-image-open {
    opacity: 1;
    transform: none;
  }

  .message-image-gallery-button,
  .image-gallery-modal button {
    width: auto;
  }

  .image-gallery-modal {
    padding: 54px 18px;
  }

  .image-gallery-close {
    top: 12px;
    right: 12px;
    width: 42px;
    height: 42px;
    padding: 0 0 3px;
    line-height: 1;
    transform: none;
  }

  .image-gallery-nav {
    width: 42px;
    height: 54px;
    padding: 0 0 5px;
    font-size: 2.4rem;
    line-height: 1;
    transform: translateY(-50%);
  }

  .image-gallery-modal .image-gallery-close:hover:not(:disabled),
  .image-gallery-modal .image-gallery-close:focus:not(:disabled) {
    background: rgba(74, 4, 59, 0.96);
    transform: none;
  }

  .image-gallery-modal .image-gallery-nav:hover:not(:disabled),
  .image-gallery-modal .image-gallery-nav:focus:not(:disabled) {
    background: rgba(74, 4, 59, 0.96);
    transform: translateY(-50%);
  }

  .image-gallery-prev {
    left: 10px;
  }

  .image-gallery-next {
    right: 10px;
  }

  .image-gallery-counter {
    bottom: 12px;
  }
}

@media (max-width: 640px) {
  .sidebar-inner {
    padding: 18px;
  }

  .brand-mark {
    width: 46px;
    height: 46px;
    border-radius: 14px;
  }

  .brand h1 {
    font-size: 1.14rem;
  }

  .panel {
    border-radius: 16px;
  }

  .messages {
    padding: 14px;
  }

  .message {
    padding: 14px;
    margin-bottom: 12px;
  }

  .composer {
    padding: 14px;
  }

  #questionInput {
    min-height: 82px;
  }

  .structured-content {
    gap: 0.7rem;
  }

  .structured-content h1 {
    font-size: 1.14rem;
  }

  .structured-content h2 {
    font-size: 1.07rem;
  }

  .structured-content h3 {
    font-size: 1rem;
  }

  .message-image {
    max-height: 240px;
    border-radius: 0;
  }

  .message-image-frame {
    border-radius: 0;
  }

  .message-link-card {
    width: 100%;
    justify-content: space-between;
  }
}