:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --panel: #ffffff;
  --line: #dfe4ea;
  --text: #20242a;
  --muted: #657180;
  --accent: #06c755;
  --accent-dark: #039a41;
  --danger: #b42318;
}

* {
  box-sizing: border-box;
}

html, body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

h1, h2, h3, p {
  margin: 0;
}

p {
  color: var(--muted);
  margin-top: 8px;
}

input,
select,
button {
  min-height: 42px;
  border-radius: 6px;
  font: inherit;
}

input,
select {
  border: 1px solid var(--line);
  padding: 8px 10px;
  background: #fff;
}

input[type="file"] {
  min-width: min(360px, 100%);
}

button {
  border: 0;
  background: var(--accent);
  color: #fff;
  padding: 0 18px;
  font-weight: 700;
  cursor: pointer;
}

button:hover {
  background: var(--accent-dark);
}

button:disabled {
  cursor: wait;
  opacity: 0.65;
}

button.secondary {
  background: #eef2f6;
  color: var(--text);
}

.notice {
  margin-top: 14px;
  border-radius: 6px;
  padding: 12px;
  background: #edfdf4;
  border: 1px solid #b7efce;
  color: #14532d;
}

.notice.error {
  background: #fff1f0;
  border-color: #ffccc7;
  color: var(--danger);
}

.muted {
  color: var(--muted);
  font-size: 12px;
}

.empty {
  color: var(--muted);
  text-align: center;
  padding: 36px 16px;
}

.file-badge {
  display: inline-block;
  margin-top: 6px;
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent-dark);
  color: #fff;
  text-decoration: none;
}

.file-badge.failed {
  background: none;
  color: #b91c1c;
  padding: 0;
}

/* App shell */

.app-shell {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: min(1180px, 100%);
  margin: 0 auto;
}

.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

.app-header h1 {
  font-size: 20px;
}

.icon-button {
  min-height: 38px;
  min-width: 38px;
  width: 38px;
  padding: 0;
  border-radius: 999px;
  background: #eef2f6;
  color: var(--text);
  font-size: 18px;
  display: grid;
  place-items: center;
}

.icon-button:hover {
  background: #e2e7ed;
}

/* Chat shell (two-pane) */

.chat-shell {
  flex: 1;
  display: grid;
  grid-template-columns: 320px 1fr;
  min-height: 0;
}

.chat-shell[hidden] {
  display: none;
}

.thread-list-pane {
  border-right: 1px solid var(--line);
  background: var(--panel);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.thread-search-bar {
  padding: 12px;
  border-bottom: 1px solid var(--line);
}

.thread-search-bar input {
  width: 100%;
}

.thread-rows {
  overflow-y: auto;
  flex: 1;
}

.thread-row {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  color: var(--text);
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  padding: 12px 16px;
  font-weight: 400;
  min-height: 0;
  cursor: pointer;
}

.thread-row:hover {
  background: #f4f6f8;
}

.thread-row.active {
  background: #eaf8ef;
}

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

.thread-row-preview {
  color: var(--muted);
  font-size: 13px;
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.thread-row-time {
  color: var(--muted);
  font-size: 11px;
  margin-top: 4px;
}

.thread-detail-pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: #f0f2f0;
}

.thread-detail-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

.thread-detail-header h2 {
  font-size: 16px;
}

.back-button {
  display: none;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.day-divider {
  display: flex;
  justify-content: center;
  margin: 14px 0 6px;
}

.day-divider span {
  background: #dde3e8;
  color: var(--muted);
  font-size: 12px;
  padding: 4px 12px;
  border-radius: 999px;
}

.chat-row {
  display: flex;
  flex-direction: column;
  max-width: 72%;
  margin-bottom: 6px;
}

.chat-row.user {
  align-self: flex-start;
  align-items: flex-start;
}

.chat-row.account {
  align-self: flex-end;
  align-items: flex-end;
}

.chat-bubble {
  border-radius: 16px;
  padding: 10px 14px;
  word-break: break-word;
}

.chat-row.user .chat-bubble {
  background: #ffffff;
  color: var(--text);
  border-top-left-radius: 4px;
}

.chat-row.account .chat-bubble {
  background: var(--accent);
  color: #fff;
  border-top-right-radius: 4px;
}

.bubble-text {
  white-space: pre-wrap;
}

.bubble-attachment {
  margin-top: 6px;
}

.chat-time {
  color: var(--muted);
  font-size: 11px;
  margin-top: 3px;
}

/* Settings view */

.settings-view {
  padding: 20px;
  overflow-y: auto;
}

.settings-view[hidden] {
  display: none;
}

.settings-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.settings-block {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 16px;
}

#uploadForm {
  display: flex;
  gap: 12px;
  margin-top: 16px;
  align-items: center;
  flex-wrap: wrap;
}

.db-path {
  display: grid;
  gap: 6px;
  margin-top: 16px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f9fafb;
}

.db-path span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.db-path code {
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  word-break: break-all;
}

.admin-table-wrap {
  overflow: auto;
  margin-top: 16px;
}

.admin-table-wrap table {
  width: 100%;
  min-width: 560px;
  border-collapse: collapse;
}

.admin-table-wrap th,
.admin-table-wrap td {
  border-bottom: 1px solid var(--line);
  padding: 10px 12px;
  text-align: left;
  font-size: 14px;
}

.admin-table-wrap th {
  background: #f9fafb;
  color: #3f4853;
  font-size: 13px;
}

#exportMessagesButton {
  margin-top: 16px;
}

@media (max-width: 860px) {
  .chat-shell {
    grid-template-columns: 1fr;
  }

  .chat-shell:not(.thread-open) .thread-detail-pane {
    display: none;
  }

  .chat-shell.thread-open .thread-list-pane {
    display: none;
  }

  .chat-shell.thread-open .back-button {
    display: grid;
  }

  .chat-row {
    max-width: 88%;
  }
}
