:root {
  color-scheme: light;
  --bg: #f6f7f3;
  --panel: #ffffff;
  --panel-soft: #eef4f1;
  --ink: #17211d;
  --muted: #66736d;
  --line: #d9e0dc;
  --accent: #226c5f;
  --accent-dark: #174f45;
  --warn: #8c5311;
  --danger: #a3312a;
  --shadow: 0 18px 50px rgba(20, 35, 30, 0.12);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  overflow-x: hidden;
}

button,
input,
textarea {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
  touch-action: manipulation;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(280px, 360px) minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 22px;
  padding: 28px;
  background: #fbfcfa;
  border-right: 1px solid var(--line);
}

.brand-block h1,
.chat-header h2,
.empty-state h2 {
  margin: 0;
  letter-spacing: 0;
}

.brand-block h1 {
  font-size: clamp(2rem, 4vw, 3.35rem);
  line-height: 0.96;
  max-width: 260px;
}

.eyebrow {
  margin: 0 0 8px;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: uppercase;
}

.login-form,
.houses-panel,
.metadata-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.metadata-fields {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.login-form label,
.question-form label,
.metadata-form label {
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 700;
}

.login-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
}

input,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  outline: none;
}

input {
  min-height: 44px;
  padding: 0 12px;
}

textarea {
  min-height: 76px;
  max-height: 220px;
  resize: vertical;
  padding: 12px;
  line-height: 1.45;
}

input:focus,
textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(34, 108, 95, 0.14);
}

.login-row button,
.question-row button {
  min-height: 44px;
  padding: 0 18px;
  border-radius: 8px;
  background: var(--accent);
  color: white;
  font-weight: 800;
}

.login-row button:hover,
.question-row button:hover {
  background: var(--accent-dark);
}

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

.status-text {
  min-height: 20px;
  margin: 0;
  color: var(--danger);
  font-size: 0.88rem;
}

.panel-header,
.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-width: 0;
}

.panel-header h2 {
  margin: 0;
  font-size: 1rem;
}

.panel-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.icon-button {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  background: var(--panel-soft);
  color: var(--accent-dark);
  font-size: 1.12rem;
  font-weight: 900;
}

.houses-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.house-button {
  width: 100%;
  min-height: 66px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  text-align: left;
}

.house-button:hover,
.house-button[aria-selected="true"] {
  border-color: var(--accent);
  background: var(--panel-soft);
}

.house-name {
  display: block;
  overflow-wrap: anywhere;
  font-weight: 800;
}

.house-meta {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.82rem;
}

.chat-pane {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-width: 0;
  padding: 28px;
}

.chat-header {
  padding-bottom: 20px;
  border-bottom: 1px solid var(--line);
}

.chat-header h2 {
  font-size: clamp(1.5rem, 2.5vw, 2.35rem);
}

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

.mode-tabs {
  display: flex;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.mode-tab {
  min-height: 36px;
  padding: 0 12px;
  background: #fff;
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 800;
}

.mode-tab[aria-pressed="true"] {
  background: var(--panel-soft);
  color: var(--accent-dark);
}

.secondary-button {
  min-height: 38px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font-weight: 750;
  white-space: nowrap;
}

.secondary-button.compact {
  min-height: 32px;
  padding: 0 12px;
  font-size: 0.86rem;
}

.metadata-form {
  padding-top: 6px;
  border-top: 1px solid var(--line);
}

.metadata-form.is-collapsed .metadata-fields,
.metadata-form.is-collapsed #saveMetadata {
  display: none;
}

.metadata-form input {
  min-height: 38px;
}

.metadata-form textarea {
  min-height: 68px;
  font-size: 0.92rem;
}

.empty-state {
  align-self: center;
  justify-self: center;
  max-width: 480px;
  text-align: center;
}

.empty-state p {
  color: var(--muted);
}

.chat-log {
  display: flex;
  flex-direction: column;
  gap: 14px;
  overflow-y: auto;
  padding: 24px 0;
}

.message {
  max-width: min(760px, 100%);
  padding: 14px 16px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--panel);
  box-shadow: 0 8px 24px rgba(20, 35, 30, 0.06);
}

.message.user {
  align-self: flex-end;
  background: #e7f2ee;
}

.message.assistant {
  align-self: flex-start;
}

.message.system {
  align-self: center;
  max-width: 640px;
  background: #fff8ec;
  color: var(--warn);
}

.message-body {
  display: flex;
  flex-direction: column;
  gap: 10px;
  line-height: 1.55;
}

.message-body p {
  margin: 0;
  white-space: pre-wrap;
}

.message-body h3 {
  margin: 2px 0 0;
  font-size: 1rem;
  line-height: 1.35;
}

.message-body ul,
.message-body ol {
  margin: 0;
  padding-left: 1.2rem;
}

.message-body li {
  margin: 4px 0;
}

.message-body code {
  padding: 1px 4px;
  border-radius: 4px;
  background: var(--panel-soft);
  font-size: 0.92em;
}

.message-body pre {
  margin: 0;
  max-width: 100%;
  overflow-x: auto;
  padding: 10px;
  border-radius: 8px;
  background: #f1f5f3;
  font-size: 0.9rem;
  line-height: 1.45;
}

.message-body a {
  color: var(--accent-dark);
  font-weight: 700;
}

.table-wrap {
  max-width: 100%;
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.table-wrap table {
  width: 100%;
  min-width: 640px;
  border-collapse: collapse;
  background: #fff;
}

.table-wrap th,
.table-wrap td {
  padding: 9px 10px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
  font-size: 0.9rem;
}

.table-wrap th {
  background: var(--panel-soft);
  color: var(--accent-dark);
  font-weight: 850;
}

.table-wrap tr:last-child td {
  border-bottom: 0;
}

.citation-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 12px;
}

.citation-list span {
  max-width: 100%;
  padding: 4px 8px;
  border-radius: 999px;
  background: var(--panel-soft);
  color: var(--accent-dark);
  font-size: 0.78rem;
  font-weight: 700;
  overflow-wrap: anywhere;
}

.question-form {
  padding-top: 18px;
  border-top: 1px solid var(--line);
}

.question-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  margin-top: 8px;
}

.question-row button {
  align-self: end;
  min-width: 82px;
}

@media (max-width: 820px) {
  .app-shell {
    display: flex;
    flex-direction: column;
    min-height: 100svh;
  }

  .sidebar {
    gap: 14px;
    padding: 18px;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .brand-block h1 {
    max-width: none;
    font-size: 1.65rem;
    line-height: 1;
  }

  .houses-list {
    flex-direction: row;
    gap: 10px;
    margin: 0 -18px;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    padding: 0 18px 4px;
    scroll-snap-type: x proximity;
  }

  .house-button {
    flex: 0 0 min(76vw, 280px);
    min-height: 58px;
    scroll-snap-align: start;
  }

  .metadata-form {
    gap: 10px;
  }

  .chat-pane {
    flex: 1;
    min-height: 62svh;
    padding: 18px;
  }

  .chat-log {
    padding: 18px 0 132px;
    scroll-padding-bottom: 132px;
  }

  .question-form {
    position: sticky;
    z-index: 5;
    bottom: 0;
    margin: 0 -18px;
    padding: 12px 18px calc(12px + env(safe-area-inset-bottom));
    background: rgba(246, 247, 243, 0.96);
    backdrop-filter: blur(10px);
    box-shadow: 0 -10px 30px rgba(20, 35, 30, 0.08);
  }

  .question-row {
    grid-template-columns: minmax(0, 1fr) 76px;
  }

  .question-row button {
    width: auto;
    min-width: 76px;
  }

  textarea {
    min-height: 58px;
    max-height: 132px;
  }
}

@media (max-width: 560px) {
  .sidebar,
  .chat-pane {
    padding: 14px;
  }

  .chat-header {
    align-items: flex-start;
    flex-direction: column;
    gap: 12px;
    padding-bottom: 14px;
  }

  .chat-header h2 {
    font-size: 1.35rem;
    overflow-wrap: anywhere;
  }

  .top-actions {
    width: 100%;
    justify-content: space-between;
  }

  .mode-tabs {
    flex: 1;
  }

  .mode-tab {
    flex: 1;
    min-height: 38px;
  }

  .login-row {
    grid-template-columns: minmax(0, 1fr) auto;
  }

  .panel-header {
    align-items: flex-start;
    flex-wrap: wrap;
  }

  .panel-actions {
    flex-wrap: wrap;
    justify-content: flex-end;
  }

  .houses-panel .panel-actions {
    justify-content: flex-start;
    width: 100%;
  }

  .message {
    max-width: 100%;
    padding: 12px;
  }

  .table-wrap table {
    min-width: 560px;
  }

  .question-form {
    margin: 0 -14px;
    padding-right: 14px;
    padding-left: 14px;
  }
}
