:root {
  color-scheme: light;
  --bg: #f3f5f7;
  --panel: #ffffff;
  --text: #17202a;
  --muted: #667386;
  --line: #dce3ea;
  --primary: #176b87;
  --primary-dark: #0f4f64;
  --soft: #eef7f8;
  --customer: #eef2f6;
  --ai: #dff3ec;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  overflow: auto;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

.chat-page {
  overflow: hidden;
}

.app-shell {
  width: min(1240px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 28px 0;
}

.chat-page .app-shell {
  height: 100vh;
}

.workspace {
  display: grid;
  grid-template-columns: 0.82fr 1.18fr;
  gap: 20px;
  align-items: stretch;
}

.chat-page .workspace {
  height: calc(100vh - 56px);
  min-height: 0;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 22px;
  min-width: 0;
  min-height: 0;
}

.input-panel {
  overflow-y: auto;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-start;
  margin-bottom: 18px;
}

.chat-header {
  align-items: center;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 26px;
  line-height: 1.2;
}

h2 {
  font-size: 22px;
  line-height: 1.2;
}

p,
.field span,
.status-dot {
  color: var(--muted);
  font-size: 14px;
}

.status-dot {
  background: var(--soft);
  color: var(--primary-dark);
  border: 1px solid #cce5e8;
  border-radius: 999px;
  padding: 7px 12px;
  white-space: nowrap;
}

.field {
  display: grid;
  gap: 8px;
  margin-bottom: 16px;
}

textarea,
select,
input[type="text"],
input[type="file"] {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  font: inherit;
}

select,
input[type="text"] {
  min-height: 42px;
  padding: 8px 10px;
}

textarea {
  min-height: 230px;
  padding: 14px;
  resize: vertical;
  line-height: 1.55;
}

.drop-zone {
  position: relative;
  display: grid;
  place-items: center;
  min-height: 118px;
  border: 1px dashed #9bb8c3;
  border-radius: 8px;
  background: #f8fbfc;
  text-align: center;
  transition: border-color 0.16s ease, background 0.16s ease;
}

.drop-zone input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.drop-zone strong {
  display: block;
  margin-bottom: 6px;
  color: var(--primary-dark);
  font-size: 16px;
}

.drop-zone p {
  max-width: min(520px, 80vw);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.drop-zone.is-dragging {
  border-color: var(--primary);
  background: var(--soft);
}

.actions,
.header-actions,
.top-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
}

button {
  border: 0;
  border-radius: 6px;
  background: var(--primary);
  color: #fff;
  cursor: pointer;
  font: inherit;
  font-weight: 700;
  padding: 11px 18px;
}

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

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

button.secondary {
  background: #eef2f5;
  color: var(--text);
  border: 1px solid var(--line);
}

button.secondary:hover {
  background: #e4ebf0;
}

button.small {
  padding: 8px 12px;
  white-space: nowrap;
}

.text-link {
  color: var(--primary-dark);
  font-size: 14px;
  font-weight: 700;
  text-decoration: none;
}

.text-link:hover {
  text-decoration: underline;
}

.chat-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.chat-thread {
  flex: 1;
  min-height: 0;
  height: 100%;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: linear-gradient(#f8fafb, #f6f8f9);
  padding: 18px;
}

.empty-state {
  display: grid;
  place-items: center;
  align-content: center;
  min-height: 430px;
  gap: 8px;
  text-align: center;
  color: var(--muted);
}

.empty-state strong {
  color: var(--text);
  font-size: 18px;
}

.message-row {
  display: flex;
  margin: 14px 0;
}

.from-customer {
  justify-content: flex-start;
}

.from-ai {
  justify-content: flex-end;
}

.message-bubble {
  max-width: min(78%, 560px);
  border: 1px solid rgba(18, 30, 41, 0.08);
  border-radius: 8px;
  padding: 11px 13px;
  box-shadow: 0 1px 2px rgba(22, 33, 44, 0.04);
}

.from-customer .message-bubble {
  background: var(--customer);
}

.from-ai .message-bubble {
  background: var(--ai);
}

.message-bubble.pending {
  opacity: 0.74;
}

.message-label {
  margin-bottom: 5px;
  color: var(--muted);
  font-size: 12px;
}

.image-note {
  margin-bottom: 8px;
  color: var(--primary-dark);
  font-size: 13px;
}

.message-content {
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.7;
  font-size: 15px;
}

.chat-image {
  display: block;
  width: min(260px, 100%);
  max-height: 320px;
  object-fit: cover;
  border-radius: 8px;
  margin: 8px 0;
  border: 1px solid rgba(18, 30, 41, 0.12);
}

.chat-image-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 8px;
  margin: 8px 0;
}

.chat-image-grid .chat-image {
  width: 100%;
  max-height: 260px;
  margin: 0;
}

.photo-preview-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 8px;
  margin-top: -8px;
}

.photo-preview-item {
  display: grid;
  gap: 6px;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 8px;
}

.photo-preview-item img {
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  border-radius: 6px;
}

.photo-preview-item span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
  font-size: 12px;
}

.knowledge-layout {
  display: grid;
  grid-template-columns: 0.95fr 1.05fr;
  gap: 20px;
  align-items: start;
  padding-bottom: 32px;
}

.knowledge-layout.is-side-expanded {
  grid-template-columns: 1fr;
}

.knowledge-layout.is-side-expanded > .panel:first-child {
  display: none;
}

.knowledge-layout.is-side-expanded .knowledge-panel {
  height: calc(100vh - 56px);
  max-height: calc(100vh - 56px);
}

.admin-page .app-shell {
  min-height: 100vh;
  height: auto;
  padding-bottom: 40px;
}

.admin-page .panel {
  overflow: visible;
}

.drop-zone.compact {
  min-height: 92px;
}

.knowledge-textarea {
  min-height: 360px;
}

.compact-textarea {
  min-height: 170px;
}

.knowledge-items {
  display: grid;
  gap: 12px;
}

.knowledge-browser {
  display: grid;
  gap: 14px;
}

.knowledge-panel {
  height: calc(100vh - 56px);
  max-height: calc(100vh - 56px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.knowledge-panel .panel-header {
  flex: 0 0 auto;
}

.knowledge-panel > #expandListBtn {
  align-self: flex-start;
  margin: -4px 0 12px;
}

.knowledge-accordion {
  display: grid;
  gap: 10px;
  flex: 1 1 auto;
  align-content: start;
  overflow-y: auto;
  padding-right: 4px;
  min-height: 0;
}

.knowledge-accordion-item {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fafb;
  overflow: hidden;
}

.knowledge-accordion-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  width: 100%;
  background: transparent;
  padding: 13px 14px;
}

.knowledge-accordion-title:hover,
.knowledge-accordion-item.is-open .knowledge-accordion-title {
  background: var(--soft);
}

.knowledge-title-main {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  min-width: 0;
  flex: 1 1 auto;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: var(--text);
  text-align: left;
  padding: 0;
}

.knowledge-title-main:hover {
  background: transparent;
}

.knowledge-title-main span {
  display: grid;
  gap: 5px;
  min-width: 0;
}

.knowledge-title-main strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 16px;
  line-height: 1.35;
}

.knowledge-title-main em {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
  font-size: 13px;
  font-style: normal;
  font-weight: 400;
}

.knowledge-title-main b {
  flex: 0 0 auto;
  color: var(--primary-dark);
  font-size: 13px;
}

.knowledge-actions {
  display: flex;
  gap: 8px;
  flex: 0 0 auto;
}

.danger-action {
  color: #9f2f2f !important;
  border-color: #e4baba !important;
  background: #fff3f3 !important;
}

.knowledge-accordion-body {
  border-top: 1px solid var(--line);
  max-height: 460px;
  overflow-y: auto;
  padding: 14px;
  background: #fff;
}

.knowledge-accordion-item:not(.is-open) .knowledge-accordion-body {
  display: none;
}

.knowledge-accordion-item.is-open .knowledge-accordion-body {
  display: block;
}

.knowledge-accordion-item.is-open .knowledge-accordion-body {
  min-height: min(420px, 48vh);
  max-height: min(560px, 58vh);
}

.knowledge-title-list {
  display: grid;
  gap: 8px;
  max-height: 240px;
  overflow-y: auto;
  padding-right: 4px;
}

.knowledge-title-item {
  display: grid;
  gap: 5px;
  width: 100%;
  text-align: left;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fafb;
  color: var(--text);
  padding: 12px 13px;
}

.knowledge-title-item:hover,
.knowledge-title-item.is-active {
  background: var(--soft);
  border-color: #a8cdd6;
}

.knowledge-title-item strong {
  font-size: 16px;
  line-height: 1.35;
}

.knowledge-title-item span {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.35;
}

.knowledge-detail {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fafb;
  min-height: 320px;
  max-height: 620px;
  overflow-y: auto;
  padding: 16px;
}

.knowledge-detail-header {
  margin-bottom: 14px;
}

.knowledge-detail-header h3 {
  margin: 0 0 6px;
  font-size: 18px;
  line-height: 1.35;
}

.knowledge-detail-header p,
.detail-loading,
.empty-list {
  color: var(--muted);
  font-size: 14px;
}

.knowledge-full-content {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.7;
  font-family: inherit;
  color: var(--text);
}

.knowledge-item {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fafb;
  padding: 14px;
}

.knowledge-item h3 {
  margin: 0 0 6px;
  font-size: 17px;
}

.knowledge-item p {
  margin-bottom: 10px;
}

.knowledge-item div {
  color: var(--text);
  line-height: 1.6;
  white-space: pre-wrap;
}

.compact-empty {
  min-height: 220px;
}

@media (max-width: 900px) {
  .chat-page {
    overflow: auto;
  }

  .workspace,
  .knowledge-layout {
    grid-template-columns: 1fr;
    height: auto;
  }

  .app-shell {
    width: min(100vw - 20px, 1240px);
    padding: 10px 0;
    height: auto;
  }

  .panel {
    padding: 16px;
  }

  textarea {
    min-height: 190px;
  }

  .chat-thread {
    height: 60vh;
    min-height: 360px;
  }

  .knowledge-panel {
    height: 72vh;
    max-height: 72vh;
  }

  .knowledge-accordion {
    max-height: none;
  }

  .message-bubble {
    max-width: 88%;
  }
}
