:root {
  --bg: #f7f7f7;
  --bg-surface: #fff;
  --text: #1a1a1a;
  --text-secondary: #666;
  --text-muted: #888;
  --text-history: #444;
  --border: #e0e0e0;
  --border-input: #d0d0d0;
  --primary: #2d6a4f;
  --primary-hover: #245a42;
  --hover: #f0f0f0;
  --active: #e8f5e9;
  --code-bg: #f0f0f0;
  --delete: #999;
  --delete-hover: #c62828;
  --link: #1976d2;
  --link-hover: #0d47a1;
  --link-visited: #0a2f6b;
  --bg-overlay: rgba(247,247,247,0.92);
  --chat-bg: url('hempli-day.png');
}

[data-theme="dark"] {
  --bg: #1a1a1a;
  --bg-surface: #242424;
  --text: #e0e0e0;
  --text-secondary: #aaa;
  --text-muted: #888;
  --text-history: #ccc;
  --border: #333;
  --border-input: #444;
  --primary: #40916c;
  --primary-hover: #52a37e;
  --hover: #2a2a2a;
  --active: #1a3328;
  --code-bg: #2a2a2a;
  --delete: #777;
  --delete-hover: #ef5350;
  --link: #7fe0a7;
  --link-hover: #b1f0c8;
  --link-visited: #4fa87a;
  --bg-overlay: rgba(26,26,26,0.92);
  --chat-bg: url('hempli-night.png');
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  font-family: "Hack", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  height: 100dvh;
  display: flex;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

/* --- Sidebar --- */

#sidebar {
  width: 260px;
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
  transition: margin-left 0.2s;
}

#sidebar.hidden { margin-left: -260px; }

#new-chat {
  margin: 12px;
  padding: 8px 12px;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  text-align: left;
}

#new-chat:hover { background: var(--primary-hover); }

#history {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px 12px;
}

.history-item {
  display: flex;
  align-items: center;
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text-history);
  border: none;
  background: none;
  width: 100%;
  text-align: left;
  gap: 6px;
}

.history-item:hover { background: var(--hover); }
.history-item.active { background: var(--active); color: var(--text); }

.history-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-action {
  display: none;
  background: none;
  border: none;
  color: var(--delete);
  cursor: pointer;
  font-size: 14px;
  padding: 0 2px;
  flex-shrink: 0;
}

.history-item:hover .history-action { display: block; }
.history-save:hover { color: var(--text); }
.history-delete:hover { color: var(--delete-hover); }

#sidebar-footer {
  padding: 8px 12px 12px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex-shrink: 0;
}

#sidebar-footer button,
#sidebar-footer a,
#sidebar-footer #version {
  display: block;
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 12px;
  color: var(--text-secondary);
  text-decoration: none;
  border: none;
  background: none;
  cursor: pointer;
  text-align: left;
}

#sidebar-footer button:hover,
#sidebar-footer a:hover { background: var(--hover); color: var(--text); }

/* --- Main area --- */

#main-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

header {
  padding: 10px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--bg-surface);
  flex-shrink: 0;
}

#sidebar-toggle,
#theme-toggle {
  background: none;
  border: none;
  font-size: 18px;
  cursor: pointer;
  color: var(--text-secondary);
  padding: 2px 4px;
}

#sidebar-toggle:hover,
#theme-toggle:hover { color: var(--text); }

header h1 {
  font-size: 18px;
  color: var(--primary);
  font-weight: 700;
}

#model-info {
  font-size: 12px;
  color: var(--text-muted);
  flex: 1;
}

main {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  background:
    linear-gradient(var(--bg-overlay), var(--bg-overlay)),
    var(--chat-bg) center center / contain no-repeat fixed;
}

.messages {
  max-width: 700px;
  margin: 0 auto;
}

.msg { margin-bottom: 16px; line-height: 1.6; }

.msg.user { text-align: right; }

.bubble {
  display: inline-block;
  padding: 10px 14px;
  border-radius: 12px;
  max-width: 85%;
  text-align: left;
  word-break: break-word;
  font-size: 14px;
}

.msg.user .bubble { white-space: pre-wrap; }

.msg.assistant .bubble h1,
.msg.assistant .bubble h2,
.msg.assistant .bubble h3 { margin: 12px 0 4px; font-size: 15px; }
.msg.assistant .bubble h1 { font-size: 17px; }
.msg.assistant .bubble p { margin: 4px 0; }
.msg.assistant .bubble ul, .msg.assistant .bubble ol { margin: 4px 0 4px 20px; }
.msg.assistant .bubble code { background: var(--code-bg); padding: 1px 4px; border-radius: 3px; font-size: 13px; }
.msg.assistant .bubble pre { background: var(--code-bg); padding: 8px; border-radius: 6px; overflow-x: auto; margin: 6px 0; }
.msg.assistant .bubble pre code { background: none; padding: 0; }
.msg.assistant .bubble strong { font-weight: 600; }
.msg.assistant .bubble a { color: var(--link); text-decoration: underline; }
.msg.assistant .bubble a:visited { color: var(--link-visited); }
.msg.assistant .bubble a:hover { color: var(--link-hover); }

.msg.user .bubble {
  background: var(--primary);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.msg.assistant .bubble {
  background: transparent;
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

footer {
  padding: 10px 16px 12px;
  border-top: 1px solid var(--border);
  background: var(--bg-surface);
  flex-shrink: 0;
}

#form {
  max-width: 700px;
  margin: 0 auto;
  display: flex;
  gap: 8px;
  align-items: flex-end;
}

#input {
  flex: 1;
  padding: 10px 12px;
  border: 1px solid var(--border-input);
  border-radius: 10px;
  font-size: 14px;
  font-family: inherit;
  resize: none;
  outline: none;
  background: var(--bg-surface);
  color: var(--text);
  max-height: 120px;
  line-height: 1.4;
}

#input:focus { border-color: var(--primary); }

#send {
  width: 38px;
  height: 38px;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 10px;
  font-size: 18px;
  cursor: pointer;
  flex-shrink: 0;
}

#send:hover { background: var(--primary-hover); }
#send:disabled { opacity: 0.4; cursor: not-allowed; }

/* --- Mobile --- */

@media (max-width: 640px) {
  #sidebar { position: fixed; z-index: 10; height: 100dvh; }
  #sidebar.hidden { margin-left: -260px; }
}
