:root {
  --paper: #f4efe3;
  --paper-2: #ece5d5;
  --ink: #23201b;
  --ink-soft: #4a463d;
  --oxblood: #7c2d28;
  --oxblood-2: #5f211d;
  --gold: #a9852f;
  --line: #cdc3ab;
  --shadow: rgba(35, 32, 27, 0.14);
  --serif: Georgia, "Times New Roman", serif;
  --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
}
body {
  background-image:
    radial-gradient(circle at 20% 0%, rgba(169, 133, 47, 0.05), transparent 55%),
    radial-gradient(circle at 100% 100%, rgba(124, 45, 40, 0.05), transparent 55%);
  min-height: 100vh;
}

.wrap { width: 100%; max-width: 820px; margin: 0 auto; padding: 0 20px; }

/* Masthead */
.masthead {
  position: relative;
  border-bottom: 3px double var(--oxblood);
  padding: 30px 0 22px;
  background: linear-gradient(180deg, var(--paper-2), var(--paper));
}
.brand { display: flex; align-items: center; gap: 16px; justify-content: center; }
.brand .rule { flex: 1; height: 1px; background: var(--line); max-width: 140px; }
.masthead h1 {
  font-family: var(--serif); font-weight: 700;
  font-size: clamp(2rem, 6vw, 3rem); letter-spacing: 0.04em;
  margin: 0; color: var(--ink); text-align: center;
}
.tagline {
  text-align: center; font-family: var(--serif); font-style: italic;
  color: var(--ink-soft); margin: 12px auto 0; max-width: 640px; line-height: 1.5;
}
.tagline strong { font-style: normal; color: var(--oxblood); }

/* Account control (top-right) */
.account { position: absolute; top: 14px; right: 16px; z-index: 5; text-align: right; }
.account-btn, .account-chip {
  font-family: var(--serif); font-size: 0.85rem; letter-spacing: 0.03em;
  padding: 6px 14px; border: 1px solid var(--line); background: #fbf7ee;
  color: var(--ink-soft); border-radius: 2px; cursor: pointer;
}
.account-btn:hover { border-color: var(--oxblood); color: var(--oxblood); }
.account-menu {
  display: inline-flex; align-items: center; gap: 12px;
  background: #fbf7ee; border: 1px solid var(--line); border-radius: 2px;
  padding: 6px 12px;
}
.account-email { font-size: 0.82rem; color: var(--ink-soft); font-family: var(--serif); }
.account-link {
  font-family: var(--serif); font-size: 0.82rem; background: none; border: none;
  color: var(--oxblood); cursor: pointer; padding: 0;
}
.account-link:hover { text-decoration: underline; }

/* Shell layout */
.shell {
  display: flex;
  max-width: 1120px;
  margin: 0 auto;
  min-height: 60vh;
}

/* Sidebar */
.sidebar {
  width: 240px;
  flex-shrink: 0;
  border-right: 1px solid var(--line);
  padding: 18px 14px;
  display: none; /* shown when logged in */
  flex-direction: column;
  gap: 10px;
}
.sidebar.show { display: flex; }
.new-conv {
  font-family: var(--serif); font-size: 0.9rem; letter-spacing: 0.02em;
  padding: 9px 12px; border: 1px solid var(--oxblood); background: transparent;
  color: var(--oxblood); border-radius: 2px; cursor: pointer; width: 100%;
}
.new-conv:hover { background: var(--oxblood); color: #f6efe2; }
.conv-list { display: flex; flex-direction: column; gap: 4px; overflow-y: auto; }
.conv-item {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 10px; border-radius: 3px; cursor: pointer;
  border: 1px solid transparent; transition: background 0.12s ease;
}
.conv-item:hover { background: var(--paper-2); }
.conv-item.active { background: #fbf7ee; border-color: var(--line); border-left: 3px solid var(--oxblood); }
.conv-title {
  flex: 1; font-size: 0.88rem; color: var(--ink); line-height: 1.3;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-title[contenteditable="true"] { white-space: normal; outline: 1px solid var(--gold); border-radius: 2px; padding: 1px 3px; }
.conv-del, .conv-edit {
  background: none; border: none; cursor: pointer; color: var(--ink-soft);
  font-size: 0.85rem; opacity: 0; padding: 2px 4px; line-height: 1;
}
.conv-item:hover .conv-del, .conv-item:hover .conv-edit { opacity: 0.6; }
.conv-del:hover { color: var(--oxblood); opacity: 1; }
.conv-edit:hover { color: var(--gold); opacity: 1; }
.sidebar-hint {
  font-size: 0.82rem; color: var(--ink-soft); font-family: var(--serif);
  font-style: italic; line-height: 1.5; margin: 6px 2px 0;
}

/* Main column */
.main {
  flex: 1;
  min-width: 0;
  max-width: 820px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  flex-direction: column;
}

/* Modes */
.modes { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin: 22px 0 8px; }
.mode {
  font-family: var(--serif); font-size: 0.92rem; letter-spacing: 0.03em;
  padding: 7px 16px; border: 1px solid var(--line); background: transparent;
  color: var(--ink-soft); border-radius: 2px; cursor: pointer; transition: all 0.15s ease;
}
.mode:hover { border-color: var(--oxblood); color: var(--oxblood); }
.mode.is-active { background: var(--oxblood); border-color: var(--oxblood); color: #f6efe2; box-shadow: 0 1px 4px var(--shadow); }

/* Log */
.log { flex: 1; margin: 16px 0; min-height: 36vh; display: flex; flex-direction: column; gap: 18px; }
.msg { display: flex; flex-direction: column; max-width: 100%; }
.msg .who {
  font-family: var(--serif); font-size: 0.72rem; letter-spacing: 0.14em;
  text-transform: uppercase; color: var(--gold); margin-bottom: 5px;
}
.msg.user .who { color: var(--ink-soft); text-align: right; }
.bubble { padding: 14px 18px; border-radius: 3px; line-height: 1.62; box-shadow: 0 1px 5px var(--shadow); }
.msg.counsel .bubble { background: #fbf7ee; border: 1px solid var(--line); border-left: 3px solid var(--oxblood); }
.msg.user { align-items: flex-end; }
.msg.user .bubble { background: var(--ink); color: #f1ece0; max-width: 85%; border-left: 3px solid var(--gold); }

.bubble p { margin: 0 0 0.7em; }
.bubble p:last-child { margin-bottom: 0; }
.bubble h3, .bubble h4 { font-family: var(--serif); color: var(--oxblood-2); margin: 0.9em 0 0.4em; font-size: 1.05rem; }
.bubble ul, .bubble ol { margin: 0.3em 0 0.8em; padding-left: 1.4em; }
.bubble li { margin: 0.28em 0; }
.bubble strong { color: var(--oxblood-2); }
.bubble em { color: var(--ink-soft); }
.bubble code { font-family: ui-monospace, "SFMono-Regular", Menlo, monospace; background: var(--paper-2); padding: 1px 5px; border-radius: 3px; font-size: 0.9em; }
.bubble blockquote { margin: 0.6em 0; padding: 4px 0 4px 14px; border-left: 2px solid var(--gold); color: var(--ink-soft); font-family: var(--serif); font-style: italic; }

.greeting .bubble { background: #fbf7ee; }
.greeting em { display: block; margin-top: 8px; color: var(--ink-soft); font-size: 0.95rem; }

/* Composer */
.composer { display: flex; gap: 10px; align-items: flex-end; border-top: 1px solid var(--line); padding-top: 14px; }
#input {
  flex: 1; font-family: var(--sans); font-size: 1rem; line-height: 1.5; color: var(--ink);
  background: #fbf7ee; border: 1px solid var(--line); border-radius: 3px; padding: 12px 14px; resize: none; max-height: 200px;
}
#input:focus { outline: none; border-color: var(--oxblood); box-shadow: 0 0 0 2px rgba(124, 45, 40, 0.12); }
#send {
  font-family: var(--serif); letter-spacing: 0.05em; font-size: 0.98rem; padding: 12px 22px;
  background: var(--oxblood); color: #f6efe2; border: none; border-radius: 3px; cursor: pointer; transition: background 0.15s ease;
}
#send:hover { background: var(--oxblood-2); }
#send:disabled { opacity: 0.5; cursor: default; }

.disclaimer { font-size: 0.78rem; color: var(--ink-soft); text-align: center; margin: 12px 0 30px; line-height: 1.5; }

/* Save nudge */
.save-nudge { font-family: var(--serif); font-style: italic; font-size: 0.86rem; color: var(--ink-soft); text-align: center; margin: -4px 0 4px; }
.save-nudge button { background: none; border: none; color: var(--oxblood); cursor: pointer; font: inherit; text-decoration: underline; padding: 0; }

/* Thinking indicator */
.dots { display: inline-flex; gap: 5px; align-items: center; height: 1.2em; }
.dots span { width: 6px; height: 6px; border-radius: 50%; background: var(--oxblood); animation: pulse 1.2s infinite ease-in-out both; }
.dots span:nth-child(2) { animation-delay: 0.2s; }
.dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes pulse { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }

.error .bubble { border-left-color: #b23a34; background: #fbeee9; color: #7a231d; }

/* Auth modal */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(35, 32, 27, 0.5);
  display: flex; align-items: center; justify-content: center; padding: 20px; z-index: 50;
}
.modal-backdrop[hidden] { display: none; }
.modal {
  position: relative; background: var(--paper); border: 1px solid var(--line);
  border-top: 4px solid var(--oxblood); border-radius: 4px; padding: 30px 28px 24px;
  width: 100%; max-width: 380px; box-shadow: 0 12px 40px rgba(35, 32, 27, 0.3);
}
.modal-close { position: absolute; top: 10px; right: 14px; background: none; border: none; font-size: 1.5rem; color: var(--ink-soft); cursor: pointer; line-height: 1; }
.modal-title { font-family: var(--serif); color: var(--ink); margin: 0 0 6px; font-size: 1.5rem; }
.modal-sub { color: var(--ink-soft); font-size: 0.9rem; margin: 0 0 18px; line-height: 1.5; }
.auth-form { display: flex; flex-direction: column; gap: 12px; }
.auth-form label { font-family: var(--serif); font-size: 0.82rem; color: var(--ink-soft); display: flex; flex-direction: column; gap: 5px; }
.auth-form input {
  font-family: var(--sans); font-size: 1rem; padding: 10px 12px; border: 1px solid var(--line);
  border-radius: 3px; background: #fbf7ee; color: var(--ink);
}
.auth-form input:focus { outline: none; border-color: var(--oxblood); box-shadow: 0 0 0 2px rgba(124, 45, 40, 0.12); }
.auth-error { color: #b23a34; font-size: 0.85rem; margin: 2px 0 0; }
.auth-submit {
  font-family: var(--serif); letter-spacing: 0.04em; font-size: 1rem; padding: 11px; margin-top: 4px;
  background: var(--oxblood); color: #f6efe2; border: none; border-radius: 3px; cursor: pointer;
}
.auth-submit:hover { background: var(--oxblood-2); }
.auth-submit:disabled { opacity: 0.6; cursor: default; }
.auth-toggle { text-align: center; font-size: 0.85rem; color: var(--ink-soft); margin: 16px 0 0; }
.link-btn { background: none; border: none; color: var(--oxblood); cursor: pointer; font: inherit; text-decoration: underline; padding: 0; }

@media (max-width: 760px) {
  .shell { flex-direction: column; }
  .sidebar { width: auto; border-right: none; border-bottom: 1px solid var(--line); max-height: 200px; }
  .conv-list { max-height: 130px; }
}
@media (max-width: 560px) {
  .masthead { padding: 40px 0 16px; }
  .brand .rule { max-width: 40px; }
  .account { top: 10px; right: 10px; }
}
