:root {
  --bg: #f4ede0;
  --card: #fbf6ec;
  --ink: #2a1f17;
  --muted: #6b5b48;
  --accent: #7a1f1f;
  --accent-soft: #e9d8d0;
  --line: #d8c9af;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  font-size: 17px;
  line-height: 1.55;
}

.modal {
  position: fixed; inset: 0;
  background: rgba(42, 31, 23, 0.55);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
.login-card {
  background: var(--card);
  padding: 2.4rem 2.2rem;
  border: 1px solid var(--line);
  width: min(420px, 90vw);
  display: flex; flex-direction: column; gap: 0.9rem;
  box-shadow: 0 30px 80px rgba(42, 31, 23, 0.25);
}
.login-card h1 {
  margin: 0; font-size: 1.6rem; font-weight: 500; letter-spacing: 0.02em;
}
.login-card .sub {
  margin: -0.2rem 0 0.6rem; color: var(--muted); font-style: italic;
}
label {
  display: flex; flex-direction: column; gap: 0.3rem;
  font-size: 0.85rem; color: var(--muted);
}
input, textarea {
  font-family: inherit; font-size: 1rem;
  padding: 0.55rem 0.7rem;
  background: #fff; color: var(--ink);
  border: 1px solid var(--line);
}
input:focus, textarea:focus {
  outline: 2px solid var(--accent-soft);
  border-color: var(--accent);
}
button {
  font-family: inherit; font-size: 1rem;
  padding: 0.6rem 1rem;
  background: var(--accent); color: var(--card);
  border: none; cursor: pointer;
  letter-spacing: 0.04em;
}
button:hover { background: #5e1717; }
.error { color: var(--accent); margin: 0; font-style: italic; font-size: 0.9rem; }

#layout {
  display: flex;
  height: 100vh;
  height: 100dvh;
}

#sidebar-backdrop { display: none; }
#menu-toggle { display: none; }

#sidebar {
  width: 240px;
  flex: 0 0 240px;
  border-right: 1px solid var(--line);
  background: var(--card);
  display: flex; flex-direction: column;
  overflow: hidden;
}
.sidebar-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 0.6rem;
  padding: 1rem 1rem 0.7rem;
  border-bottom: 1px solid var(--line);
}
.sidebar-head h2 {
  margin: 0; font-weight: 500; font-size: 0.95rem;
  letter-spacing: 0.18em; text-transform: uppercase; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  min-width: 0;
}
.sidebar-head button {
  background: transparent; color: var(--accent);
  border: 1px solid var(--line); padding: 0.3rem 0.65rem;
  font-size: 0.8rem;
  white-space: nowrap; flex-shrink: 0;
  letter-spacing: 0.02em;
}
.sidebar-head button:hover { background: var(--accent-soft); }

#conv-list {
  list-style: none; margin: 0; padding: 0.4rem 0;
  overflow-y: auto; flex: 1;
}
#conv-list li {
  display: flex; align-items: center; gap: 0.4rem;
  padding: 0.55rem 0.9rem;
  cursor: pointer;
  border-left: 3px solid transparent;
  font-size: 0.95rem;
}
#conv-list li:hover { background: var(--accent-soft); }
#conv-list li.active {
  background: var(--accent-soft);
  border-left-color: var(--accent);
}
#conv-list .title {
  flex: 1;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
#conv-list .title.untitled { color: var(--muted); font-style: italic; }
#conv-list .delete {
  background: transparent; color: var(--muted);
  border: none; padding: 0 0.3rem;
  font-size: 1.1rem; line-height: 1;
  opacity: 0; transition: opacity 0.1s;
}
#conv-list li:hover .delete { opacity: 1; }
#conv-list .delete:hover { color: var(--accent); background: transparent; }

#chat {
  flex: 1;
  display: flex; flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 760px;
  margin: 0 auto;
  padding: 0 1rem;
  min-width: 0;
}

@media (max-width: 700px) {
  #sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: min(280px, 82vw); flex: none;
    z-index: 50;
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    box-shadow: 2px 0 18px rgba(42, 31, 23, 0.25);
  }
  body.sidebar-open #sidebar { transform: translateX(0); }
  #sidebar-backdrop {
    display: block;
    position: fixed; inset: 0;
    background: rgba(42, 31, 23, 0.5);
    z-index: 40;
    opacity: 0; pointer-events: none;
    transition: opacity 0.22s ease;
  }
  body.sidebar-open #sidebar-backdrop { opacity: 1; pointer-events: auto; }

  #chat {
    padding: 0 0.85rem;
    max-width: none;
  }
  #chat header {
    gap: 0.4rem;
    padding: 0.8rem 0 0.55rem;
  }
  #chat header h1 {
    font-size: clamp(0.85rem, 3.6vw, 1.15rem);
    flex: 1;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    letter-spacing: 0;
  }
  #menu-toggle {
    display: inline-flex; align-items: center; justify-content: center;
    background: transparent; color: var(--muted);
    border: 1px solid var(--line);
    width: 2rem; height: 2rem; padding: 0;
    font-size: 1.05rem; line-height: 1;
    flex-shrink: 0;
  }
  #menu-toggle:hover { background: var(--accent-soft); color: var(--ink); }
  #chat header button#logout {
    padding: 0.3rem 0.55rem; font-size: 0.8rem;
  }
  #composer { padding: 0.7rem 0 1rem; }
  .msg { max-width: 95%; }
}
#chat header {
  display: flex; align-items: baseline; justify-content: space-between;
  padding: 1.2rem 0 0.6rem;
  border-bottom: 1px solid var(--line);
}
#chat header h1 {
  margin: 0; font-weight: 500; font-size: 1.4rem; letter-spacing: 0.02em;
}
#chat header button {
  background: transparent; color: var(--muted);
  border: 1px solid var(--line); padding: 0.3rem 0.7rem;
  font-size: 0.85rem;
}
#chat header button:hover { background: var(--accent-soft); color: var(--ink); }

#messages {
  flex: 1; overflow-y: auto;
  padding: 1.2rem 0;
  display: flex; flex-direction: column; gap: 1.2rem;
}
.msg { max-width: 88%; }
.msg .who {
  font-size: 0.7rem; text-transform: uppercase;
  letter-spacing: 0.12em; color: var(--muted);
  margin-bottom: 0.25rem;
}
.msg.user { align-self: flex-end; text-align: right; }
.msg.user .bubble {
  background: var(--accent-soft);
  padding: 0.7rem 0.95rem;
  border: 1px solid var(--line);
  display: inline-block;
  white-space: pre-wrap;
  text-align: left;
}
.msg.assistant .bubble > *:first-child { margin-top: 0; }
.msg.assistant .bubble > *:last-child { margin-bottom: 0; }
.msg.assistant .bubble p { margin: 0.5em 0; }
.msg.assistant .bubble ul, .msg.assistant .bubble ol { margin: 0.5em 0; padding-left: 1.4em; }
.msg.assistant .bubble li { margin: 0.2em 0; }
.msg.assistant .bubble strong { color: var(--ink); }
.msg.assistant .bubble em { color: var(--muted); }
.msg.assistant .bubble a { color: var(--accent); word-break: break-all; }
.msg.assistant .bubble code {
  background: var(--accent-soft); padding: 0.05em 0.35em;
  font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 0.92em;
}
.msg.assistant .bubble blockquote {
  margin: 0.5em 0; padding-left: 0.9em;
  border-left: 2px solid var(--line); color: var(--muted); font-style: italic;
}

#composer {
  display: flex; gap: 0.5rem;
  padding: 0.8rem 0 1.4rem;
  border-top: 1px solid var(--line);
}
#composer textarea {
  flex: 1; resize: none;
  min-height: 2.4rem; max-height: 200px;
}
#composer button { padding: 0 1.2rem; }
