:root {
  --rust: #375AA2;
  --rust-dark: #375AA2;
  --rust-light: #375AA2;
  --navy: #112844;
  --navy-light: #1b3a63;
  --navy-lighter: #26547f;
  --white: #FFFFFF;
  --paper: #f7f5f2;
  --line: #e4e0da;
  --ink: #1c2430;
  --ink-soft: #5b6472;
  --radius-sm: 6px;
  --radius-md: 10px;
  --font-ui: 'Ollama Sans', 'Söhne', -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
  --font-mono: 'IBM Plex Mono', 'SFMono-Regular', Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  font-family: var(--font-ui);
  color: var(--ink);
  background: var(--paper);
  -webkit-font-smoothing: antialiased;
}

button {
  font-family: inherit;
  cursor: pointer;
}

::selection { background: var(--rust-light); color: var(--white); }

/* ---------- App shell ---------- */
#app {
  display: grid;
  grid-template-columns: 268px 1fr;
  height: 100vh;
  overflow-y: hidden;
}

/* ---------- Sidebar ---------- */
#sidebar {
  background: var(--navy);
  color: var(--white);
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 18px 16px 14px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
}

.brand-mark {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--rust) 0%, var(--rust-dark) 100%);
  flex-shrink: 0;
  position: relative;
}
.brand-mark::after {
  content: "";
  position: absolute;
  inset: 8px;
  border-radius: 3px;
  background: var(--white);
  opacity: .9;
}

.brand-name {
  font-weight: 600;
  font-size: 15px;
  letter-spacing: 0.2px;
}
.brand-sub {
  font-size: 11px;
  color: rgba(255,255,255,0.5);
  margin-top: 1px;
}

.new-chat-btn {
  margin: 14px 14px 8px;
  padding: 10px 14px;
  background: var(--rust);
  color: var(--white);
  border: none;
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: background .15s ease;
}
.new-chat-btn:hover { background: var(--rust-dark); }
.new-chat-btn svg { flex-shrink: 0; }

.sidebar-section-label {
  padding: 14px 16px 6px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: rgba(255,255,255,0.4);
}

#chat-history {
  flex: 1;
  max-height: 50vh;
  overflow-y: auto;
  padding: 0 8px;
}

.history-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  color: rgba(255,255,255,0.82);
  font-size: 13.5px;
  cursor: pointer;
  margin-bottom: 2px;
  position: relative;
}
.history-item:hover { background: rgba(255,255,255,0.06); }
.history-item.active {
  background: var(--navy-light);
  color: var(--white);
}
.history-item.active::before {
  content: "";
  position: absolute;
  left: -8px;
  top: 8px;
  bottom: 8px;
  width: 3px;
  background: var(--rust);
  border-radius: 2px;
}
.history-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.history-delete {
  opacity: 0;
  background: none;
  border: none;
  color: rgba(255,255,255,0.5);
  padding: 2px 4px;
  border-radius: 4px;
}
.history-item:hover .history-delete { opacity: 1; }
.history-delete:hover { color: var(--white); background: rgba(255,255,255,0.12); }

.history-empty {
  padding: 20px 12px;
  color: rgba(255,255,255,0.35);
  font-size: 13px;
  text-align: center;
}

#sidebar-tools {
  border-top: 1px solid rgba(255,255,255,0.08);
  padding: 8px;
  position: sticky;
  bottom: 0;  
}

.tool-btn {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 10px;
  background: none;
  border: none;
  color: rgba(255,255,255,0.85);
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  text-align: left;
}
.tool-btn:hover { background: rgba(255,255,255,0.07); }
.tool-btn svg { flex-shrink: 0; color: var(--rust-light); }

/* ---------- Main column ---------- */
#main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--white);
}

#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 24px;
  border-bottom: 1px solid var(--line);
}

#chat-title-wrap { display: flex; flex-direction: column; min-width: 0; }
#chat-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--navy);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 60vw;
}
#chat-title[contenteditable="true"] {
  padding: 0 4px;
}
#chat-meta {
  font-size: 11.5px;
  color: var(--ink-soft);
  font-family: var(--font-mono);
  margin-top: 2px;
}

.topbar-actions { display: flex; gap: 8px; }
.icon-btn {
  border: 1px solid var(--line);
  background: var(--white);
  color: var(--ink-soft);
  width: 34px; height: 34px;
  border-radius: var(--radius-sm);
  display: flex; align-items: center; justify-content: center;
}
.icon-btn:hover { border-color: var(--rust); color: var(--rust); }

/* ---------- Messages ---------- */
#messages {
  flex: 1;
  max-height: 100vh;
  overflow-y: auto;
  padding: 24px 0 12px;
}
#messages-inner {
  max-width: 760px;
  margin: 0 auto;
  padding: 0 24px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.msg-row { display: flex; gap: 12px; }
.msg-row.user { flex-direction: row-reverse; }

.avatar {
  width: 30px; height: 30px;
  border-radius: 8px;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 700; color: var(--white);
}
.avatar.assistant { background: var(--navy); }
.avatar.user { background: var(--rust); }

.bubble {
  max-width: 78%;
  padding: 11px 15px;
  border-radius: var(--radius-md);
  font-size: 14.5px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-wrap: break-word;
}
.msg-row.assistant .bubble {
  background: var(--paper);
  border: 1px solid var(--line);
  color: var(--ink);
  border-top-left-radius: 3px;
}
.msg-row.user .bubble {
  background: var(--rust);
  color: var(--white);
  border-top-right-radius: 3px;
}
.bubble .attach-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  margin-top: 6px;
  font-size: 12px;
  opacity: 0.85;
}

.msg-tokens {
  font-family: var(--font-mono);
  font-size: 10.5px;
  color: var(--ink-soft);
  margin-top: 4px;
}
.msg-row.user .msg-tokens { text-align: right; color: rgba(255,255,255,0.6); }

.thinking .bubble {
  display: flex;
  gap: 4px;
  align-items: center;
}
.dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--rust);
  animation: bounce 1.1s infinite ease-in-out;
}
.dot:nth-child(2) { animation-delay: .15s; }
.dot:nth-child(3) { animation-delay: .3s; }
@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: .5; }
  40% { transform: translateY(-4px); opacity: 1; }
}

.empty-state {
  max-width: 480px;
  margin: 10vh auto;
  text-align: center;
  color: var(--ink-soft);
}
.empty-state .mark {
  width: 46px; height: 46px; border-radius: 12px;
  background: linear-gradient(135deg, var(--rust), var(--navy));
  margin: 0 auto 16px;
}
.empty-state h2 { color: var(--navy); font-size: 19px; margin: 0 0 6px; }
.empty-state p { font-size: 13.5px; margin: 0; }

/* ---------- Composer ---------- */
#composer-wrap {
  padding: 12px 24px 18px;
  position: sticky;
  bottom: 0;
}
#composer {
  max-width: 760px;
  margin: 0 auto;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  background: var(--white);
  transition: border-color .15s ease;
}
#composer:focus-within { border-color: var(--rust); }

#composer-toprow {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px 0;
  flex-wrap: wrap;
}

.select-pill {
  display: flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--line);
  background: var(--paper);
  border-radius: 999px;
  padding: 4px 10px 4px 8px;
  font-size: 12px;
  color: var(--navy);
}
.select-pill select {
  border: none;
  background: transparent;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--navy);
  outline: none;
  max-width: 160px;
}

.attach-btn {
  border: 1px solid var(--line);
  background: var(--paper);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 12px;
  color: var(--navy);
  display: flex; align-items: center; gap: 6px;
}
.attach-btn:hover { border-color: var(--rust); color: var(--rust); }

.file-chip {
  display: flex; align-items: center; gap: 6px;
  background: var(--rust);
  color: var(--white);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 12px;
}
.file-chip button {
  background: none; border: none; color: var(--white);
  opacity: .8; font-size: 13px; line-height: 1; padding: 0;
}
.file-chip button:hover { opacity: 1; }

#composer-textrow {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 8px 8px 14px;
}
#msg-input {
  flex: 1;
  border: none;
  outline: none;
  resize: none;
  font-family: inherit;
  font-size: 14.5px;
  line-height: 1.5;
  max-height: 200px;
  padding: 6px 0;
  color: var(--ink);
  background: transparent;
}
#msg-input::placeholder { color: #9aa2ad; }

#send-btn {
  width: 36px; height: 36px;
  border-radius: 10px;
  border: none;
  background: var(--rust);
  color: var(--white);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  transition: background .15s ease, opacity .15s;
}
#send-btn:hover { background: var(--rust-dark); }
#send-btn:disabled { opacity: .4; cursor: default; }

.composer-hint {
  text-align: center;
  font-size: 11px;
  color: var(--ink-soft);
  margin-top: 8px;
}

/* ---------- Modal / overlay panels ---------- */
.overlay {
  position: fixed; inset: 0;
  background: rgba(17, 40, 68, 0.55);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 50;
}
.overlay.open { display: flex; }

.panel {
  background: var(--white);
  width: min(760px, 92vw);
  max-height: 84vh;
  border-radius: var(--radius-md);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 60px rgba(17,40,68,0.35);
}
.panel-header {
  background: var(--navy);
  color: var(--white);
  padding: 16px 22px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.panel-header h3 { margin: 0; font-size: 16px; font-weight: 600; }
.panel-close {
  background: none; border: none; color: rgba(255,255,255,0.7);
  font-size: 20px; line-height: 1;
}
.panel-close:hover { color: var(--white); }
.panel-body {
  padding: 20px 22px;
  overflow-y: auto;
}

/* Usage dashboard */
.stat-cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin-bottom: 22px;
}
.stat-card {
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  padding: 14px 16px;
  background: var(--paper);
}
.stat-card .num {
  font-family: var(--font-mono);
  font-size: 22px;
  font-weight: 700;
  color: var(--navy);
}
.stat-card .label {
  font-size: 12px;
  color: var(--ink-soft);
  margin-top: 2px;
}
.stat-card.accent .num { color: var(--rust); }

.section-title {
  font-size: 12.5px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-soft);
  margin: 22px 0 10px;
}
.section-title:first-of-type { margin-top: 0; }

.bar-row { display: flex; align-items: center; gap: 10px; margin-bottom: 9px; }
.bar-label {
  width: 130px;
  flex-shrink: 0;
  font-size: 12px;
  font-family: var(--font-mono);
  color: var(--navy);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.bar-track {
  flex: 1;
  height: 10px;
  background: var(--paper);
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid var(--line);
}
.bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--rust), var(--rust-light));
  border-radius: 6px;
}
.bar-value {
  width: 64px;
  text-align: right;
  font-family: var(--font-mono);
  font-size: 11.5px;
  color: var(--ink-soft);
  flex-shrink: 0;
}

.daybars {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  height: 90px;
  padding: 0 2px;
}
.daybar {
  flex: 1;
  background: var(--navy);
  border-radius: 3px 3px 0 0;
  min-height: 3px;
  position: relative;
}
.daybar:hover { background: var(--rust); }
.daybar-labels {
  display: flex;
  gap: 6px;
  padding: 0 2px;
  margin-top: 4px;
}
.daybar-labels span {
  flex: 1;
  text-align: center;
  font-size: 9.5px;
  color: var(--ink-soft);
  font-family: var(--font-mono);
}

table.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
table.data-table th {
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--ink-soft);
  padding: 6px 8px;
  border-bottom: 1px solid var(--line);
}
table.data-table td {
  padding: 8px;
  border-bottom: 1px solid var(--line);
  color: var(--ink);
}
table.data-table tr:last-child td { border-bottom: none; }
.tok { font-family: var(--font-mono); color: var(--navy); }

/* Artifacts */
.artifact-upload-row {
  border: 1.5px dashed var(--line);
  border-radius: var(--radius-md);
  padding: 18px;
  text-align: center;
  color: var(--ink-soft);
  font-size: 13px;
  margin-bottom: 18px;
  cursor: pointer;
  transition: border-color .15s, background .15s;
}
.artifact-upload-row:hover, .artifact-upload-row.drag {
  border-color: var(--rust);
  background: #fdf3ee;
}

.artifact-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 8px;
  border-bottom: 1px solid var(--line);
}
.artifact-item:last-child { border-bottom: none; }
.artifact-icon {
  width: 34px; height: 34px;
  border-radius: 8px;
  background: var(--navy);
  color: var(--white);
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 700;
  font-family: var(--font-mono);
  flex-shrink: 0;
}
.artifact-info { flex: 1; min-width: 0; }
.artifact-name {
  font-size: 13.5px; color: var(--navy); font-weight: 600;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.artifact-sub {
  font-size: 11.5px; color: var(--ink-soft);
  font-family: var(--font-mono);
}
.artifact-actions { display: flex; gap: 6px; }
.artifact-actions button {
  border: 1px solid var(--line);
  background: var(--white);
  border-radius: 6px;
  width: 30px; height: 30px;
  display: flex; align-items: center; justify-content: center;
  color: var(--ink-soft);
}
.artifact-actions button:hover { border-color: var(--rust); color: var(--rust); }
.empty-note { color: var(--ink-soft); font-size: 13px; text-align: center; padding: 30px 0; }

/* Settings panel */
.field-group { margin-bottom: 16px; }
.field-group label {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--navy);
  margin-bottom: 6px;
}
.field-group input[type=text] {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  font-family: var(--font-mono);
  outline: none;
}
.field-group input[type=text]:focus { border-color: var(--rust); }
.field-hint { font-size: 11.5px; color: var(--ink-soft); margin-top: 5px; }
.save-btn {
  background: var(--rust);
  color: var(--white);
  border: none;
  padding: 10px 18px;
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  font-weight: 600;
}
.save-btn:hover { background: var(--rust-dark); }

.toast {
  position: fixed;
  bottom: 20px; left: 50%;
  transform: translateX(-50%);
  background: var(--navy);
  color: var(--white);
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 13px;
  z-index: 100;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(-4px); }

@media (max-width: 760px) {
  #app { grid-template-columns: 1fr; }
  #sidebar { position: fixed; inset: 0 30% 0 0; z-index: 40; transform: translateX(-100%); transition: transform .2s; }
  #sidebar.open { transform: translateX(0); }
  .stat-cards { grid-template-columns: 1fr; }
}
