:root {
  --bg: #14161c;
  --surface: #20232d;
  --surface-2: #2a2e39;
  --border: #2a2e38;
  --text: #e4e6eb;
  --muted: #8b90a0;
  --accent: #e0a458;
  --accent-dim: #b8823f;
  --danger: #d97070;
  --success: #7fb896;
  --mono: ui-monospace, "SFMono-Regular", "JetBrains Mono", Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", sans-serif;
  --radius: 8px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
}

body {
  display: flex;
  flex-direction: column;
}

button {
  font-family: var(--sans);
  cursor: pointer;
}

input, select, textarea {
  font-family: var(--sans);
}

a { color: var(--accent); }

.hidden { display: none !important; }

/* ---------- Centered screens (gate / auth) ---------- */

.center-screen {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px;
  animation: card-in 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes card-in {
  from { opacity: 0; transform: scale(0.9); }
  to { opacity: 1; transform: scale(1); }
}

.card > * {
  animation: modal-content-in 0.3s ease both;
  animation-delay: 0.06s;
}

.card-eyebrow {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent);
  margin: 0 0 8px;
}

.card h1 {
  font-size: 20px;
  margin: 0 0 20px;
}

.field {
  margin-bottom: 14px;
}

.field label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 6px;
}

.field input {
  width: 100%;
  padding: 10px 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 14px;
  transition: border-color 0.15s ease;
}

.field input:focus {
  outline: none;
  border-color: var(--accent-dim);
}

.btn {
  width: 100%;
  padding: 10px 14px;
  border-radius: 6px;
  border: none;
  font-size: 14px;
  font-weight: 600;
  background: var(--accent);
  color: #1a1408;
  transition: background 0.15s ease, transform 0.1s ease, opacity 0.15s ease;
}

.btn:hover { background: var(--accent-dim); }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.5; cursor: default; }

.btn-secondary {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-secondary:hover { border-color: var(--accent-dim); }

.btn-danger { background: var(--danger); color: #2a0e0e; }

.link-row {
  margin-top: 16px;
  font-size: 13px;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
}

.error-msg {
  background: rgba(217, 112, 112, 0.12);
  border: 1px solid rgba(217, 112, 112, 0.4);
  color: var(--danger);
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 13px;
  margin-bottom: 14px;
  animation: shake-in 0.3s ease;
}

@keyframes shake-in {
  0% { opacity: 0; transform: translateX(0); }
  25% { opacity: 1; transform: translateX(-4px); }
  50% { transform: translateX(3px); }
  75% { transform: translateX(-2px); }
  100% { transform: translateX(0); }
}

/* ---------- Recovery phrase (signature moment) ---------- */

.phrase-card {
  background: var(--surface-2);
  border: 1px solid var(--accent-dim);
  border-radius: 6px;
  padding: 16px;
  margin: 14px 0;
}

.phrase-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 16px;
  font-family: var(--mono);
  font-size: 13px;
}

.phrase-word .idx { color: var(--muted); margin-right: 6px; }

.phrase-warning {
  font-size: 12px;
  color: var(--muted);
  margin-top: 10px;
  line-height: 1.5;
}

/* ---------- Chat app shell ---------- */

.app-shell {
  flex: 1;
  display: flex;
  min-height: 0;
}

.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  width: 240px;
  height: 100%;
  background: var(--surface);
  border-right: 1px solid var(--border);
  box-shadow: 2px 0 12px rgba(0, 0, 0, 0.25);
  display: flex;
  flex-direction: column;
  padding: 12px;
  gap: 8px;
  transform: translateX(0);
  transition: transform 0.3s ease;
  z-index: 30;
}

.sidebar-backdrop {
  display: none;
}

.sidebar-chats {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.chat-item {
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 13px;
  color: var(--muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: background 0.15s ease, color 0.15s ease;
}

.chat-item:hover, .chat-item.active {
  background: var(--surface-2);
  color: var(--text);
}

.chat-title {
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.delete-chat-btn {
  flex: 0 0 auto;
  width: 22px;
  height: 22px;
  display: none;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  border-radius: 4px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
  transition: color 0.15s ease, background 0.15s ease, transform 0.1s ease;
}

.chat-item:hover .delete-chat-btn,
.chat-item.active .delete-chat-btn {
  display: flex;
}

.delete-chat-btn:hover {
  color: var(--danger);
  background: rgba(217, 112, 112, 0.12);
}

.delete-chat-btn:active { transform: scale(0.9); }

@media (max-width: 720px) {
  /* No hover on touch devices, so always show the delete button rather
     than hiding it behind a hover state the user can never trigger. */
  .delete-chat-btn { display: flex; }
}

.main-col {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  margin-left: 240px;
}

.top-bar {
  position: relative;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
}

.top-bar-title {
  font-size: 14px;
}

.top-bar-title .model-name {
  font-family: var(--mono);
  color: var(--accent);
}

.empty-state {
  text-align: center;
}

.empty-state-title {
  font-size: 22px;
  font-weight: 600;
  color: var(--text);
}

.empty-state-greeting {
  font-size: 14px;
  color: var(--muted);
  margin-top: 6px;
}

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

.icon-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 13px;
  display: inline-flex;
  align-items: center;
  text-decoration: none;
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}
.icon-btn:hover { color: var(--text); border-color: var(--accent-dim); }
.icon-btn:active { transform: scale(0.96); }

select.model-picker {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 13px;
  transition: border-color 0.15s ease;
}

select.model-picker:focus {
  outline: none;
  border-color: var(--accent-dim);
}


.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.messages.empty {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  flex: 1;
}

.msg {
  max-width: 70%;
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 14px;
  line-height: 1.5;
  word-wrap: break-word;
  animation: msg-in 0.2s ease;
}

@keyframes msg-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.msg.user {
  align-self: flex-end;
  background: var(--accent);
  color: #1a1408;
  white-space: pre-wrap;
}

.msg.assistant {
  align-self: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
}

.msg.error {
  align-self: flex-start;
  background: rgba(217, 112, 112, 0.12);
  border: 1px solid rgba(217, 112, 112, 0.4);
  color: var(--danger);
  white-space: pre-wrap;
}

/* ---------- Markdown content inside assistant messages ---------- */

.msg.assistant > *:first-child { margin-top: 0; }
.msg.assistant > *:last-child { margin-bottom: 0; }

.msg.assistant p {
  margin: 0 0 10px;
}

.msg.assistant h1,
.msg.assistant h2,
.msg.assistant h3,
.msg.assistant h4,
.msg.assistant h5,
.msg.assistant h6 {
  margin: 16px 0 8px;
  line-height: 1.3;
  font-weight: 600;
  color: var(--text);
}

.msg.assistant h1 { font-size: 1.35em; }
.msg.assistant h2 { font-size: 1.2em; }
.msg.assistant h3 { font-size: 1.08em; }
.msg.assistant h4,
.msg.assistant h5,
.msg.assistant h6 { font-size: 1em; }

.msg.assistant strong { font-weight: 700; color: var(--text); }
.msg.assistant em { font-style: italic; }
.msg.assistant u { text-decoration: underline; }
.msg.assistant s, .msg.assistant del { text-decoration: line-through; }

.msg.assistant code {
  font-family: var(--mono);
  font-size: 0.9em;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
}

.msg.assistant pre {
  margin: 10px 0;
  padding: 12px 14px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow-x: auto;
}

.msg.assistant pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: 0.85em;
  line-height: 1.5;
  white-space: pre;
  color: var(--text);
}

.msg.assistant ul,
.msg.assistant ol {
  margin: 0 0 10px;
  padding-left: 22px;
}

.msg.assistant li { margin: 3px 0; }
.msg.assistant li > ul,
.msg.assistant li > ol { margin: 3px 0; }

.msg.assistant blockquote {
  margin: 10px 0;
  padding: 4px 12px;
  border-left: 3px solid var(--accent-dim);
  color: var(--muted);
}

.msg.assistant a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.msg.assistant hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 14px 0;
}

.msg.assistant table {
  width: 100%;
  border-collapse: collapse;
  margin: 10px 0;
  font-size: 0.9em;
}

.msg.assistant th,
.msg.assistant td {
  text-align: left;
  padding: 6px 10px;
  border: 1px solid var(--border);
}

.msg.assistant th {
  background: var(--surface-2);
  font-weight: 600;
}

.msg.assistant img {
  max-width: 100%;
  border-radius: 6px;
}

.composer {
  border-top: 1px solid var(--border);
  padding: 14px 20px;
  display: flex;
  gap: 10px;
}

.composer textarea {
  flex: 1;
  resize: none;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 10px 12px;
  font-size: 14px;
  font-family: var(--sans);
  min-height: 42px;
  max-height: 160px;
  transition: border-color 0.15s ease;
}

.composer textarea:focus { outline: none; border-color: var(--accent-dim); }

/* ---------- Modal ---------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 20px;
  opacity: 1;
  transition: opacity 0.2s ease;
}

/* Override the generic .hidden { display: none !important } for modals
   specifically, so open/close can transition instead of hard-cutting.
   Kept display:flex (via the modal-backdrop rule above) at all times;
   visibility + pointer-events handle actually removing it from
   interaction/screen-reader flow once faded out. */
.modal-backdrop.hidden {
  display: flex !important;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.2s ease, visibility 0s linear 0.2s;
}

.modal {
  width: 100%;
  max-width: 480px;
  max-height: 85vh;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  transform-origin: center center;
  transform: scale(1);
  opacity: 1;
  transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.2s ease;
}

.modal-backdrop.hidden .modal {
  transform: scale(0.85);
  opacity: 0;
}

/* Content fades in slightly after the box starts expanding, rather than
   both animating in lockstep — gives the "border expands, content
   catches up" feel instead of the whole card just scaling as one flat
   image. */
.modal > *:not(.modal-close) {
  animation: modal-content-in 0.3s ease both;
  animation-delay: 0.05s;
}

@keyframes modal-content-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

.modal h2 {
  margin: 0 0 16px;
  font-size: 16px;
}

.modal-close {
  float: right;
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 18px;
  transition: color 0.15s ease, transform 0.15s ease;
}

.modal-close:hover {
  color: var(--text);
  transform: rotate(90deg);
}

#byok-section {
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

#byok-section .section-title:first-child { margin-top: 0; }

.tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--border);
}

.tab {
  padding: 8px 12px;
  font-size: 13px;
  color: var(--muted);
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.2s ease;
}

.tab:hover { color: var(--text); }

.tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  margin-bottom: 10px;
}

.section-title {
  font-family: var(--mono);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin: 18px 0 8px;
}

.section-title:first-child { margin-top: 0; }

.admin-pane {
  animation: pane-in 0.2s ease;
}

@keyframes pane-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.admin-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  margin-bottom: 10px;
}

.admin-table th, .admin-table td {
  text-align: left;
  padding: 6px 8px;
  border-bottom: 1px solid var(--border);
}

.admin-table th { color: var(--muted); font-weight: 500; font-size: 11px; text-transform: uppercase; }

.admin-table tbody tr {
  transition: background 0.12s ease;
}

.admin-table tbody tr:hover {
  background: var(--surface-2);
}

.row-actions { display: flex; gap: 6px; }

.small-btn {
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}
.small-btn:hover { color: var(--text); border-color: var(--accent-dim); }
.small-btn:active { transform: scale(0.95); }
.small-btn.danger:hover { color: var(--danger); border-color: var(--danger); }

.inline-form {
  display: flex;
  gap: 6px;
  margin-bottom: 12px;
}

.inline-form input, .inline-form select {
  flex: 1;
  padding: 6px 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  font-size: 12px;
}

.callout {
  font-size: 12px;
  color: var(--muted);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 12px;
  margin-bottom: 12px;
}

.callout.accent { border-color: var(--accent-dim); color: var(--text); }

/* ---------- Light theme override ---------- */

body[data-theme="light"] {
  --bg: #f4f1ea;
  --surface: #ffffff;
  --surface-2: #f0ede4;
  --border: #ddd7c8;
  --text: #1e1c17;
  --muted: #7a7368;
}

body[data-theme="light"] .msg.user { color: #1a1408; }

/* ---------- Sidebar toggle / responsive ---------- */

#sidebar-toggle { display: none; }

@media (max-width: 720px) {
  #sidebar-toggle { display: block; }
  .msg { max-width: 88%; }

  .sidebar { transform: translateX(-100%); }
  .sidebar-open .sidebar { transform: translateX(0); }
  .main-col { margin-left: 0; }

  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 25;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
  }
  .sidebar-open .sidebar-backdrop {
    opacity: 1;
    pointer-events: auto;
  }
}

