:root {
  color-scheme: dark;
  --accent: #7c4de3;
  --accent-rgb: 124 77 227;
  --page: #0f1014;
  --shell: #15161a;
  --panel: #1c1d22;
  --panel-2: #24252b;
  --field: #111216;
  --line: #30323a;
  --line-soft: #272932;
  --text: #f4f0e8;
  --muted: #aaa39a;
  --muted-2: #7f7a73;
  --danger: #ef6b5b;
  --shadow: 0 24px 70px rgba(0, 0, 0, 0.35);
  --radius: 16px;
  --font: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-family: var(--font);
}

:root[data-theme="light"] {
  color-scheme: light;
  --page: #f7f5f1;
  --shell: #ffffff;
  --panel: #fbfaf7;
  --panel-2: #f1eee8;
  --field: #ffffff;
  --line: #ddd7ce;
  --line-soft: #ebe5dc;
  --text: #25221f;
  --muted: #69615a;
  --muted-2: #8a8178;
  --shadow: 0 24px 70px rgba(33, 25, 12, 0.12);
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
[hidden] { display: none !important; }
body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(circle at top left, rgb(var(--accent-rgb) / 0.12), transparent 34rem),
    var(--page);
  color: var(--text);
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.55; }
h1, h2, p { margin: 0; }
h2 { font-size: 0.88rem; }

.noscript {
  margin: 12px auto;
  width: min(1380px, calc(100vw - 24px));
  padding: 0.8rem 1rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  color: var(--danger);
}

.app-shell {
  display: grid;
  grid-template-columns: 280px minmax(360px, 1fr) 360px;
  gap: 1px;
  width: min(1480px, 100vw);
  height: 100vh;
  margin: 0 auto;
  overflow: hidden;
  background: var(--line-soft);
  box-shadow: var(--shadow);
}

.sidebar, .main-chat, .settings-panel {
  min-width: 0;
  min-height: 0;
  background: var(--shell);
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px;
}

.sidebar-header, .section-heading, .chat-header, .chat-identity, .chat-header-actions, .modal-header, .modal-actions, .range-row {
  display: flex;
  align-items: center;
}

.sidebar-header, .section-heading, .chat-header, .modal-header, .modal-actions { justify-content: space-between; gap: 12px; }
.chat-identity, .chat-header-actions, .range-row { gap: 10px; }

.brand h1 { font-size: 1.05rem; letter-spacing: -0.02em; }
.brand p, .chat-title span, label, .status-box, .repository-meta, .empty-chat p, .sidebar-footer { color: var(--muted); }
.brand p { margin-top: 3px; font-size: 0.76rem; }

.sidebar-actions { display: grid; gap: 8px; }
.export-actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.sidebar-section { min-height: 0; }
.sidebar-section.grow { flex: 1; overflow: hidden; display: flex; flex-direction: column; }
.character-list, .chat-list, .repo-character-list { display: grid; gap: 6px; overflow: auto; padding-right: 2px; }
.character-list { min-height: 0; }

.sidebar-footer {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--line-soft);
  font-size: 0.74rem;
}

.main-chat {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
}

.chat-header {
  min-height: 70px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line-soft);
  background: color-mix(in srgb, var(--shell), var(--panel) 50%);
}

.chat-title h2 { font-size: 0.98rem; }
.chat-title span { display: inline-flex; align-items: center; gap: 6px; font-size: 0.76rem; }
.chat-title i {
  width: 7px;
  height: 7px;
  border-radius: 99px;
  background: #38c172;
}

.chat-log {
  min-height: 0;
  overflow: auto;
  padding: 28px max(24px, 8vw);
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.empty-chat {
  margin: auto;
  max-width: 390px;
  text-align: center;
}
.empty-chat h2 { margin-bottom: 8px; font-size: 1.3rem; }
.empty-state {
  display: grid;
  gap: 8px;
  place-items: start;
  padding: 12px;
  border: 1px dashed var(--line);
  border-radius: 13px;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.35;
}
.empty-state strong {
  color: var(--text);
  font-size: 0.86rem;
}
.empty-chat.empty-state {
  place-items: center;
}

.composer {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto auto;
  gap: 8px;
  align-items: end;
  margin: 0 max(18px, 7vw) 18px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 22px;
  background: var(--panel);
}

textarea, input, select {
  width: 100%;
  border: 1px solid var(--line-soft);
  border-radius: 12px;
  background: var(--field);
  color: var(--text);
  outline: none;
}
textarea:focus, input:focus, select:focus { border-color: rgb(var(--accent-rgb) / 0.72); box-shadow: 0 0 0 3px rgb(var(--accent-rgb) / 0.14); }
textarea { resize: vertical; line-height: 1.4; }
.composer textarea { min-height: 42px; max-height: 150px; padding: 11px 4px; border: 0; background: transparent; box-shadow: none; resize: none; }
.stacked-form textarea, .stacked-form input, select, input[type="number"] { margin-top: 6px; padding: 10px 11px; }
input[type="color"] { height: 42px; padding: 4px; }
.range-row input[type="range"] { min-width: 0; }
.range-row output {
  min-width: 4.5rem;
  color: var(--muted);
  font-size: 0.78rem;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.settings-panel {
  overflow: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.panel-card {
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  background: var(--panel);
  padding: 13px;
}
.panel-card > summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  list-style: none;
  cursor: pointer;
  font-weight: 750;
}
.panel-card > summary::-webkit-details-marker { display: none; }
.panel-card[open] > summary { margin-bottom: 12px; }
.theme-card { display: grid; gap: 12px; }

.stacked-form, .model-actions { display: grid; gap: 10px; }
.advanced-fields { border-top: 1px solid var(--line-soft); padding-top: 8px; }
.advanced-fields > summary { cursor: pointer; color: var(--muted); }
.advanced-fields[open] { display: grid; gap: 10px; }

.primary-button, .ghost-button, .text-button, .icon-button, .avatar-button, .send-button, .spark-button, .small-action, .danger-text-button, .theme-mode-button {
  border: 1px solid var(--line-soft);
  border-radius: 12px;
  background: var(--panel-2);
  color: var(--text);
  font-weight: 720;
}
.primary-button, .ghost-button, .text-button { padding: 10px 12px; }
.primary-button, .text-button.primary, .send-button {
  border-color: rgb(var(--accent-rgb) / 0.45);
  background: var(--accent);
  color: white;
}
.ghost-button, .text-button.secondary, .text-button.subtle { background: transparent; }
.full { width: 100%; }
.icon-button, .avatar-button, .send-button, .spark-button, .small-action {
  display: inline-grid;
  place-items: center;
  width: 38px;
  height: 38px;
  padding: 0;
}
.avatar-button { border-radius: 99px; font-size: 0.72rem; }
.danger, .danger-text-button { color: var(--danger); }
.danger-text-button { border: 0; background: transparent; padding: 0; }
.theme-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.theme-mode-button { padding: 9px 8px; color: var(--muted); }
.theme-mode-button.active { border-color: rgb(var(--accent-rgb) / 0.6); background: rgb(var(--accent-rgb) / 0.16); color: var(--text); }
.theme-swatches { display: flex; gap: 8px; flex-wrap: wrap; }
.swatch { width: 28px; height: 28px; border: 2px solid var(--line); border-radius: 999px; background: var(--swatch); }
.swatch.active { outline: 3px solid rgb(var(--accent-rgb) / 0.24); border-color: var(--text); }

.character-card, .chat-item, .repo-character-card {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  min-height: 54px;
  padding: 8px;
  border: 1px solid transparent;
  border-radius: 13px;
  background: transparent;
  text-align: left;
}
.character-card:hover, .character-card[aria-pressed="true"], .chat-item:hover, .chat-item.active {
  border-color: rgb(var(--accent-rgb) / 0.24);
  background: rgb(var(--accent-rgb) / 0.12);
}
.character-summary, .chat-main { min-width: 0; display: grid; gap: 2px; }
.character-summary strong, .chat-title-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 0.86rem; }
.character-summary span, .chat-preview { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); font-size: 0.74rem; }
.chat-main { border: 0; background: transparent; padding: 0; text-align: left; }
.delete-chat-button { opacity: 0; border: 0; background: transparent; color: var(--danger); font-size: 1.1rem; }
.chat-item:hover .delete-chat-button { opacity: 1; }

.character-thumb, .repo-thumb, .active-avatar {
  display: grid;
  place-items: center;
  overflow: hidden;
  width: 36px;
  height: 36px;
  border-radius: 12px;
  background: rgb(var(--accent-rgb) / 0.18);
  color: var(--text);
  font-weight: 900;
}
.active-avatar { width: 42px; height: 42px; border-radius: 14px; }
.character-thumb img, .repo-thumb img, .active-avatar img { width: 100%; height: 100%; object-fit: cover; }

.message { max-width: 780px; display: grid; gap: 6px; }
.message.user { align-self: flex-end; max-width: 620px; }
.message.assistant { align-self: flex-start; }
.message-meta { color: var(--muted-2); font-size: 0.72rem; font-weight: 700; }
.message-bubble {
  padding: 12px 14px;
  border: 1px solid var(--line-soft);
  border-radius: 17px;
  background: var(--panel);
  line-height: 1.5;
  white-space: pre-wrap;
}
.message.user .message-bubble { background: rgb(var(--accent-rgb) / 0.16); border-color: rgb(var(--accent-rgb) / 0.22); }

.shard-picker-grid { display: grid; grid-template-columns: 1fr; gap: 8px; }
.file-picker {
  display: grid;
  gap: 4px;
  padding: 9px;
  border: 1px dashed var(--line);
  border-radius: 12px;
  background: color-mix(in srgb, var(--panel), var(--field) 42%);
}
.file-picker input { font-size: 0.74rem; }
.shard-file-name { color: var(--muted); font-size: 0.72rem; overflow-wrap: anywhere; }
.model-download-links { display: flex; flex-wrap: wrap; gap: 8px; }
.model-download-links a { color: var(--accent); font-weight: 750; text-decoration: none; }
.status-box {
  min-height: 40px;
  padding: 10px;
  border: 1px solid var(--line-soft);
  border-radius: 12px;
  background: var(--field);
  font-size: 0.78rem;
  line-height: 1.35;
}
progress { width: 100%; height: 8px; accent-color: var(--accent); }
pre { max-height: 170px; overflow: auto; white-space: pre-wrap; color: var(--muted); }
.check-row { display: flex; align-items: center; gap: 8px; }
.check-row input { width: auto; }

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(0, 0, 0, 0.45);
}
.profile-modal {
  width: min(540px, 100%);
  max-height: min(760px, 94vh);
  overflow: auto;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--shell);
  box-shadow: var(--shadow);
}
.modal-header { margin-bottom: 14px; }
.disclaimer-modal {
  display: grid;
  gap: 14px;
}
.disclaimer-copy {
  display: grid;
  gap: 10px;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.5;
}

.mobile-tabbar {
  display: none;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}

@media (max-width: 1120px) {
  .app-shell { grid-template-columns: 250px minmax(0, 1fr); }
  .settings-panel { position: fixed; inset: 12px 12px 12px auto; width: min(380px, calc(100vw - 24px)); border: 1px solid var(--line); border-radius: 18px; box-shadow: var(--shadow); }
}

@media (max-width: 760px) {
  body { padding-bottom: 72px; }
  .app-shell { grid-template-columns: 1fr; height: auto; min-height: 100vh; }
  .sidebar, .settings-panel { position: static; width: auto; max-height: none; }
  .main-chat { min-height: 72vh; }
  .chat-log { padding: 20px 16px; }
  .composer { margin: 0 12px 12px; }
  .sidebar, .main-chat, .settings-panel, #explorePanel { scroll-margin-top: 12px; scroll-margin-bottom: 84px; }
  .mobile-tabbar {
    position: fixed;
    left: 10px;
    right: 10px;
    bottom: 10px;
    z-index: 30;
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 6px;
    padding: 6px;
    border: 1px solid var(--line);
    border-radius: 18px;
    background: color-mix(in srgb, var(--shell), transparent 4%);
    box-shadow: var(--shadow);
  }
  .mobile-tabbar button {
    display: grid;
    place-items: center;
    gap: 2px;
    min-height: 48px;
    border: 1px solid transparent;
    border-radius: 13px;
    background: transparent;
    color: var(--muted);
    font-size: 0.68rem;
    font-weight: 750;
  }
  .mobile-tabbar button.active {
    border-color: rgb(var(--accent-rgb) / 0.32);
    background: rgb(var(--accent-rgb) / 0.14);
    color: var(--text);
  }
  .nav-icon {
    display: block;
    font-size: 1.05rem;
    line-height: 1;
  }
}
