:root {
  color-scheme: light;
  --ink: #211f1b;
  --muted: #746e66;
  --line: #dfd6ca;
  --paper: #f5efe6;
  --surface: #fffaf2;
  --surface-2: #ffffff;
  --dark: #171512;
  --teal: #126b66;
  --teal-2: #0b4f4b;
  --plum: #644862;
  --gold: #b98b3c;
  --danger: #9f2f2b;
  --shadow: 0 24px 80px rgba(0, 0, 0, 0.2);
}

* {
  box-sizing: border-box;
}

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

button,
input,
select,
textarea {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

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

.chat-app {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  width: min(1200px, calc(100vw - 28px));
  height: 100vh;
  margin: 0 auto;
  padding: 18px 0;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  color: var(--surface);
  padding: 4px 2px 14px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-dot {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--gold);
  box-shadow: 0 0 0 8px rgba(185, 139, 60, 0.14);
}

.brand p,
.label,
.status-line,
.profile-state {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}

.app-header .brand p {
  color: rgba(255, 250, 242, 0.68);
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 24px;
  letter-spacing: 0;
}

h2 {
  font-size: 20px;
  letter-spacing: 0;
}

.profile-button {
  min-height: 38px;
  border: 1px solid rgba(255, 250, 242, 0.22);
  border-radius: 999px;
  padding: 0 15px;
  color: var(--surface);
  background: rgba(255, 250, 242, 0.08);
}

.workspace {
  position: relative;
  display: grid;
  min-height: 0;
}

.chat-surface {
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto auto auto;
  min-height: 0;
  border: 1px solid rgba(223, 214, 202, 0.86);
  border-radius: 8px;
  background: var(--paper);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.chat-thread {
  display: grid;
  align-content: start;
  gap: 18px;
  min-height: 0;
  overflow-y: auto;
  padding: 26px clamp(16px, 4vw, 72px);
}

.message {
  display: grid;
  grid-template-columns: 38px minmax(0, 1fr);
  gap: 12px;
  max-width: 860px;
}

.message.user {
  justify-self: end;
  grid-template-columns: minmax(0, 1fr) 38px;
}

.message.user .avatar {
  grid-column: 2;
  grid-row: 1;
}

.message.user .bubble {
  grid-column: 1;
  grid-row: 1;
  background: #e9f3f1;
  border-color: #c9ddda;
}

.avatar {
  display: grid;
  width: 38px;
  height: 38px;
  place-items: center;
  border-radius: 50%;
  color: white;
  background: var(--teal);
  font-size: 13px;
  font-weight: 700;
}

.message.user .avatar {
  background: var(--plum);
}

.bubble {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-2);
  padding: 14px 16px;
  line-height: 1.72;
}

.message-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.message-meta strong {
  font-size: 13px;
}

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

.message-body p + p {
  margin-top: 12px;
}

.prompt-strip {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 0 clamp(16px, 4vw, 72px) 12px;
}

.prompt-strip button {
  flex: 0 0 auto;
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0 14px;
  color: var(--teal-2);
  background: var(--surface);
}

.composer {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 116px;
  gap: 10px;
  border-top: 1px solid var(--line);
  padding: 14px clamp(16px, 4vw, 72px);
  background: var(--surface);
}

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

textarea {
  min-height: 50px;
  max-height: 160px;
  padding: 12px 13px;
  resize: vertical;
  line-height: 1.5;
}

input,
select {
  min-height: 40px;
  padding: 8px 10px;
}

textarea:focus,
input:focus,
select:focus {
  border-color: var(--teal);
  box-shadow: 0 0 0 3px rgba(18, 107, 102, 0.14);
}

.composer button,
.save-profile {
  min-height: 44px;
  border-radius: 8px;
  color: white;
  background: var(--teal);
}

.composer button:hover,
.save-profile:hover {
  background: var(--teal-2);
}

.status-line {
  padding: 0 clamp(16px, 4vw, 72px) 14px;
}

.profile-panel {
  position: absolute;
  top: 16px;
  right: 16px;
  z-index: 4;
  width: min(360px, calc(100vw - 48px));
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
  padding: 16px;
  transform: translateX(calc(100% + 40px));
  transition: transform 180ms ease;
}

.profile-panel.open {
  transform: translateX(0);
}

.panel-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.panel-top button {
  min-height: 32px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0 12px;
  color: var(--teal-2);
  background: var(--surface-2);
}

.birth-form {
  display: grid;
  gap: 12px;
}

label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
}

.profile-state {
  margin-top: 12px;
}

.memory-box {
  display: grid;
  gap: 12px;
  margin-top: 18px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.memory-title {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.memory-title h2 {
  font-size: 16px;
}

.memory-title button,
.memory-item button {
  min-height: 30px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0 10px;
  color: var(--teal-2);
  background: var(--surface-2);
  font-size: 12px;
}

.memory-list {
  display: grid;
  gap: 10px;
}

.memory-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: start;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-2);
  padding: 10px;
}

.memory-item span {
  color: var(--gold);
  font-size: 12px;
}

.memory-item p {
  margin-top: 4px;
  color: var(--ink);
  font-size: 13px;
  line-height: 1.45;
}

.admin-app {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  width: min(1180px, calc(100vw - 28px));
  min-height: 100vh;
  margin: 0 auto;
  padding: 18px 0;
}

.admin-link {
  display: inline-grid;
  place-items: center;
  text-decoration: none;
}

.admin-shell {
  display: grid;
  grid-template-columns: minmax(0, 1.25fr) minmax(280px, 0.75fr);
  gap: 14px;
  align-content: start;
}

.admin-panel {
  display: grid;
  gap: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
  padding: 16px;
}

.train-panel {
  grid-row: span 2;
}

.studio-panel {
  align-content: start;
  grid-row: span 2;
}

.section-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.section-head h2 {
  font-size: 18px;
}

.section-head button,
.admin-form button {
  min-height: 36px;
  border-radius: 8px;
  padding: 0 14px;
  color: white;
  background: var(--teal);
}

.section-head button:hover,
.admin-form button:hover {
  background: var(--teal-2);
}

.admin-pill {
  min-height: 30px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 10px;
  color: var(--teal-2);
  background: var(--surface-2);
  font-size: 12px;
}

.admin-form {
  display: grid;
  gap: 12px;
}

.admin-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.admin-form .secondary-action {
  border: 1px solid rgba(18, 107, 102, 0.28);
  color: var(--teal-2);
  background: #e9f3f1;
}

.admin-form .secondary-action:hover {
  color: white;
  background: var(--teal-2);
}

.form-hint {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
}

.training-chat-thread {
  display: grid;
  gap: 10px;
  max-height: 280px;
  min-height: 170px;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-2);
  padding: 12px;
}

.training-message {
  max-width: 92%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 10px 12px;
}

.training-message.operator {
  justify-self: end;
  border-color: #c9ddda;
  background: #e9f3f1;
}

.training-message strong {
  display: block;
  color: var(--teal-2);
  font-size: 12px;
}

.training-message p {
  margin-top: 4px;
  color: var(--ink);
  font-size: 13px;
  line-height: 1.55;
}

.training-chat-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 86px;
  gap: 10px;
}

.training-chat-form button {
  min-height: 46px;
  border-radius: 8px;
  color: white;
  background: var(--teal);
}

.upload-zone {
  position: relative;
  display: grid;
  place-items: center;
  min-height: 150px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--muted);
  text-align: center;
  padding: 18px;
}

.upload-zone.dragging {
  border-color: var(--teal);
  background: #e9f3f1;
}

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

.upload-zone strong {
  color: var(--ink);
  font-size: 18px;
}

.upload-zone p {
  margin-top: 6px;
  font-size: 13px;
}

.github-import-row {
  display: grid;
  grid-template-columns: minmax(180px, 0.7fr) minmax(220px, 1fr) 128px;
  align-items: center;
  gap: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: rgba(255, 255, 255, 0.72);
}

.github-import-row strong {
  color: var(--ink);
  font-size: 14px;
}

.github-import-row p {
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

.github-import-row input {
  min-height: 42px;
}

.github-import-row button {
  min-height: 42px;
  color: white;
  background: var(--teal);
}

.inline-check {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--ink);
}

.inline-check input {
  width: auto;
  min-height: auto;
}

.admin-result {
  min-height: 82px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  padding: 12px;
  background: var(--surface-2);
  color: var(--muted);
  line-height: 1.55;
}

.chunk-preview {
  margin-top: 10px;
  border-top: 1px solid var(--line);
  padding-top: 10px;
}

.chunk-preview strong {
  color: var(--ink);
  font-size: 13px;
}

.chunk-preview p {
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
  max-height: 72px;
  overflow: hidden;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.metric-item {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-2);
  padding: 12px;
}

.metric-item span {
  display: block;
  color: var(--muted);
  font-size: 12px;
}

.metric-item strong {
  display: block;
  margin-top: 4px;
  color: var(--ink);
  font-size: 24px;
}

.admin-list {
  display: grid;
  gap: 10px;
}

.admin-list-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: start;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-2);
  padding: 12px;
}

.admin-list-item strong {
  font-size: 14px;
}

.admin-list-item p {
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
  overflow-wrap: anywhere;
}

.source-check {
  display: flex;
  align-items: center;
  gap: 8px;
}

.source-check input {
  width: auto;
  min-height: auto;
}

.admin-list-item span {
  border: 1px solid rgba(18, 107, 102, 0.2);
  border-radius: 999px;
  padding: 4px 8px;
  color: var(--teal-2);
  background: #e9f3f1;
  font-size: 12px;
}

.source-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.source-delete-button {
  border: 1px solid rgba(159, 47, 43, 0.28);
  border-radius: 999px;
  padding: 4px 8px;
  color: var(--danger);
  background: #fff8f6;
  font-size: 12px;
  min-height: auto;
}

.source-delete-button:hover {
  border-color: rgba(159, 47, 43, 0.48);
  background: #fff0ed;
}

@media (max-width: 720px) {
  .chat-app {
    width: 100vw;
    height: 100vh;
    padding: 12px;
  }

  .app-header {
    padding-left: 2px;
    padding-right: 2px;
  }

  .chat-surface {
    border-radius: 8px;
  }

  .message,
  .message.user {
    grid-template-columns: 32px minmax(0, 1fr);
    justify-self: stretch;
  }

  .message.user .avatar,
  .message.user .bubble {
    grid-column: auto;
    grid-row: auto;
  }

  .avatar {
    width: 32px;
    height: 32px;
  }

  .composer {
    grid-template-columns: 1fr;
  }

  .profile-panel {
    top: 8px;
    right: 8px;
    width: calc(100vw - 32px);
  }

  .admin-app {
    width: 100vw;
    padding: 12px;
  }

  .admin-shell {
    grid-template-columns: 1fr;
  }

  .train-panel {
    grid-row: auto;
  }

  .metric-grid {
    grid-template-columns: 1fr;
  }

  .github-import-row,
  .training-chat-form {
    grid-template-columns: 1fr;
  }
}
