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

:root {
  color-scheme: light;
  --bg: #f4f2ed;
  --surface: #ffffff;
  --surface-muted: #f7f8f5;
  --ink: #232723;
  --muted: #68706a;
  --line: #d9ddd6;
  --accent: #28705a;
  --accent-soft: #e4f1eb;
  --danger: #a33a3a;
  --danger-soft: #fff0f0;
  --shadow: 0 12px 30px rgba(36, 43, 38, 0.08);
}

body {
  background: var(--bg);
  color: var(--ink);
  display: flex;
  flex-direction: column;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
    "Segoe UI", sans-serif;
  height: 100vh;
  overflow: hidden;
}

button,
input,
textarea {
  font: inherit;
}

button {
  border-radius: 8px;
}

.icon {
  fill: none;
  flex: 0 0 auto;
  height: 1rem;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
  width: 1rem;
}

.app-header {
  align-items: center;
  background: rgba(244, 242, 237, 0.92);
  border-bottom: 1px solid var(--line);
  display: flex;
  flex-shrink: 0;
  gap: 0.75rem;
  padding: 0.85rem 1.25rem;
}

.app-header h1 {
  flex: 1;
  font-size: 1.1rem;
  font-weight: 700;
  line-height: 1.2;
}

.chat-title-input {
  background: transparent;
  border: 0;
  border-bottom: 1px solid transparent;
  color: var(--ink);
  flex: 1;
  font-size: 1.1rem;
  font-weight: 700;
  line-height: 1.2;
  min-width: 0;
  padding: 0.35rem 0;
}

.chat-title-input:focus {
  border-bottom-color: var(--accent);
  outline: none;
}

.chat-title-input[hidden],
.back-button[hidden],
.chat-view[hidden],
.history-view[hidden] {
  display: none;
}

.back-button,
.new-chat-button,
.upload-form button,
.chat-input button,
.delete-chat-button {
  align-items: center;
  border: 1px solid transparent;
  cursor: pointer;
  display: inline-flex;
  gap: 0.45rem;
  justify-content: center;
  line-height: 1;
  min-height: 2.4rem;
  padding: 0.65rem 0.8rem;
}

.back-button,
.new-chat-button,
.upload-form button,
.chat-input button {
  background: var(--ink);
  color: #fff;
}

.back-button:hover,
.new-chat-button:hover,
.upload-form button:hover,
.chat-input button:hover {
  background: #111411;
}

.history-view {
  flex: 1;
  min-height: 0;
  padding: 1.25rem;
}

.history-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  height: 100%;
  overflow-y: auto;
  padding: 1rem;
}

.history-list {
  display: grid;
  gap: 0.75rem;
}

.history-item {
  background: var(--surface-muted);
  border: 1px solid transparent;
  border-radius: 8px;
  color: var(--ink);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  padding: 0.9rem;
  text-align: left;
  width: 100%;
}

.history-item:hover {
  background: #fff;
  border-color: var(--line);
}

.history-item-header {
  align-items: flex-start;
  display: flex;
  gap: 0.75rem;
  justify-content: space-between;
}

.history-item strong {
  font-size: 1rem;
  line-height: 1.3;
}

.delete-chat-button {
  background: var(--danger-soft);
  border-color: #efc6c6;
  color: var(--danger);
  flex: 0 0 auto;
  min-height: 2rem;
  padding: 0.45rem 0.55rem;
}

.delete-chat-button:hover {
  background: #ffe2e2;
}

.delete-chat-button:disabled,
.chat-input button:disabled,
.chat-input textarea:disabled,
.upload-form button:disabled,
.upload-form input:disabled {
  cursor: not-allowed;
  opacity: 0.65;
}

.history-item span,
.history-item p,
.empty-history {
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.4;
}

.history-item p {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.empty-history {
  padding: 1rem;
}

.container {
  display: grid;
  flex: 1;
  gap: 1rem;
  grid-template-columns: minmax(15rem, 18rem) minmax(25rem, 1fr) minmax(20rem, 25rem);
  min-height: 0;
  padding: 1rem 1.25rem 1.25rem;
}

.main-section,
aside {
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  min-height: 0;
}

.upload {
  background: var(--surface);
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding: 1rem;
}

.upload-form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.upload-dropzone {
  align-items: flex-start;
  background: var(--accent-soft);
  border: 1px dashed #8cb7a8;
  border-radius: 8px;
  color: #173c32;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  justify-content: center;
  min-height: 7rem;
  padding: 1rem;
}

.upload-dropzone:hover {
  background: #dcece4;
}

.upload-title,
.document-summary {
  font-weight: 700;
}

.upload-subtitle,
.upload-status {
  font-size: 0.85rem;
  line-height: 1.4;
}

.upload-form input {
  color: var(--muted);
  font-size: 0.82rem;
}

.upload-status {
  color: var(--muted);
  min-height: 2.5rem;
}

.upload-status.success {
  color: var(--accent);
}

.upload-status.error {
  color: var(--danger);
}

.document-panel {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 0.75rem;
  min-height: 0;
}

.document-summary {
  color: var(--ink);
  font-size: 0.9rem;
}

.document-list {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  min-height: 0;
  overflow-y: auto;
}

.document-card {
  align-items: flex-start;
  background: var(--surface-muted);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  display: flex;
  gap: 0.65rem;
  padding: 0.75rem;
}

.document-icon {
  color: #9a4b43;
  height: 1.15rem;
  margin-top: 0.1rem;
  width: 1.15rem;
}

.document-card-body {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  min-width: 0;
}

.document-card strong {
  font-size: 0.9rem;
  line-height: 1.3;
  overflow-wrap: anywhere;
}

.document-card span,
.empty-documents {
  color: var(--muted);
  font-size: 0.8rem;
  line-height: 1.4;
}

.chat {
  background: var(--surface);
  display: flex;
  flex-direction: column;
}

.chat-area {
  background:
    linear-gradient(rgba(255, 255, 255, 0.78), rgba(255, 255, 255, 0.78)),
    radial-gradient(circle at top left, #e4f1eb, transparent 32rem);
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

.chat-container {
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
  padding: 1rem;
}

.chat-bubble {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 4px 14px rgba(36, 43, 38, 0.06);
  max-width: 82%;
  padding: 0.65rem 0.8rem;
  white-space: pre-wrap;
  width: fit-content;
}

.chat-bubble.answer {
  align-self: flex-start;
  line-height: 1.5;
  max-width: 88%;
  white-space: normal;
}

.chat-bubble.question {
  align-self: flex-end;
  background: #20362f;
  border-color: #20362f;
  color: #fff;
}

.chat-bubble.suggested-question {
  align-self: flex-start;
  background: var(--accent-soft);
  border-color: #9fc8b9;
  color: #173c32;
  cursor: pointer;
  font: inherit;
  text-align: left;
}

.chat-bubble.suggested-question:hover {
  background: #d8eadf;
}

.chat-bubble.answer h3,
.chat-bubble.answer h4,
.chat-bubble.answer h5,
.chat-bubble.answer h6,
.chat-bubble.answer p,
.chat-bubble.answer ul,
.chat-bubble.answer ol,
.chat-bubble.answer pre {
  margin: 0 0 0.65rem;
}

.chat-bubble.answer h3,
.chat-bubble.answer h4,
.chat-bubble.answer h5,
.chat-bubble.answer h6 {
  font-size: 1rem;
}

.chat-bubble.answer ul,
.chat-bubble.answer ol {
  padding-left: 1.25rem;
}

.chat-bubble.answer li + li {
  margin-top: 0.25rem;
}

.chat-bubble.answer code {
  background: #edf0ec;
  border-radius: 4px;
  padding: 0.1rem 0.25rem;
}

.citation-link {
  background: var(--accent-soft);
  border: 0;
  border-radius: 6px;
  color: #174426;
  cursor: pointer;
  font: inherit;
  padding: 0.05rem 0.3rem;
}

.citation-link:hover {
  background: #cbe4d1;
}

.citation-link:disabled {
  cursor: default;
  opacity: 0.65;
}

.reference-link {
  display: inline-block;
  line-height: 1.4;
  text-align: left;
}

.chat-bubble.answer pre {
  background: #edf0ec;
  border-radius: 8px;
  overflow-x: auto;
  padding: 0.75rem;
}

.chat-bubble.answer pre code {
  background: transparent;
  padding: 0;
}

.chat-bubble.answer > :last-child {
  margin-bottom: 0;
}

.chat-bubble.error {
  background: var(--danger-soft);
  border-color: #efc6c6;
  color: var(--danger);
}

.chat-input {
  background: #fff;
  border-top: 1px solid var(--line);
  display: flex;
  gap: 0.75rem;
  padding: 0.75rem;
}

.chat-input textarea {
  background: var(--surface-muted);
  border: 1px solid var(--line);
  border-radius: 8px;
  flex: 1;
  height: 3.2rem;
  max-height: 3.2rem;
  min-height: 3.2rem;
  padding: 0.8rem 0.9rem;
  resize: none;
}

.chat-input textarea:focus {
  border-color: var(--accent);
  outline: none;
}

aside {
  background: #45524b;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  padding: 1rem;
}

.sources {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.source-placeholder {
  color: #f1f5f1;
  font-size: 0.9rem;
  line-height: 1.4;
  margin: 0;
}

.document-viewer {
  background: #fff;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  min-height: 32rem;
  padding: 0.75rem;
}

.document-viewer strong {
  color: var(--ink);
  line-height: 1.3;
}

.document-viewer span {
  color: var(--muted);
  font-size: 0.8rem;
}

.document-frame {
  border: 1px solid var(--line);
  border-radius: 6px;
  flex: 1;
  min-height: 28rem;
  width: 100%;
}

.source-item {
  background: #fff;
  border-radius: 8px;
  padding: 0.75rem;
}

.source-header {
  align-items: center;
  display: flex;
  gap: 0.75rem;
  justify-content: space-between;
  margin-bottom: 0.4rem;
}

.source-name {
  color: var(--muted);
  font-size: 0.8rem;
  line-height: 1.4;
  margin-bottom: 0.5rem;
}

.source-content {
  color: var(--ink);
  font-size: 0.9rem;
  line-height: 1.45;
}

@media (max-width: 1050px) {
  .container {
    grid-template-columns: minmax(14rem, 17rem) minmax(22rem, 1fr);
  }

  aside {
    grid-column: 1 / -1;
    min-height: 22rem;
  }
}

@media (max-width: 800px) {
  body {
    overflow: auto;
  }

  .app-header {
    padding: 0.75rem;
  }

  .container {
    display: flex;
    flex-direction: column;
    overflow: visible;
    padding: 0.75rem;
  }

  .history-view {
    padding: 0.75rem;
  }

  .chat {
    min-height: 28rem;
  }

  .chat-bubble {
    max-width: 92%;
  }

  .chat-input {
    gap: 0.5rem;
  }

  .chat-input button span,
  .delete-chat-button span {
    display: none;
  }
}
