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

:root {
  --bg: oklch(0.13 0 0);
  --bg2: oklch(0.17 0 0);
  --bg3: oklch(0.21 0 0);
  --surface: oklch(0.19 0.01 272);
  --border: oklch(0.27 0.01 272);
  --text: oklch(0.92 0 0);
  --text2: oklch(0.65 0 0);
  --accent: oklch(0.55 0.18 272);
  --accent-hover: oklch(0.5 0.2 272);
  --danger: oklch(0.6 0.22 25);
  --msg-user: oklch(0.22 0.04 272);
  --msg-assistant: oklch(0.18 0 0);
  --radius: 8px;
  --radius-sm: 4px;
  --shadow: 0 1px 3px rgba(0,0,0,0.3);
  --font: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'JetBrains Mono', 'Fira Code', monospace;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.6;
  overflow: hidden;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
  font-size: inherit;
}

input, textarea, select {
  font-family: inherit;
  font-size: inherit;
  color: var(--text);
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  outline: none;
  transition: border-color 0.2s;
}
input:focus, textarea:focus, select:focus {
  border-color: var(--accent);
}

textarea {
  resize: vertical;
}

select {
  cursor: pointer;
}

::-webkit-scrollbar {
  width: 6px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

#app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ===== Sidebar ===== */
#sidebar {
  width: 240px;
  min-width: 240px;
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  animation: slideIn 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes slideIn {
  from { transform: translateX(-100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid var(--border);
}
.sidebar-header h1 {
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

#char-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.char-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.15s;
  margin-bottom: 4px;
}
.char-item:hover {
  background: var(--bg3);
}
.char-item.active {
  background: var(--accent);
  color: white;
}
.char-item .avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  flex-shrink: 0;
}
.char-item .cname {
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-footer {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}
.sidebar-footer button {
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  transition: background 0.15s;
}
.sidebar-footer button:first-child {
  flex: 1;
  background: var(--accent);
  color: white;
}
.sidebar-footer button:first-child:hover {
  background: var(--accent-hover);
}
#settings-btn {
  font-size: 18px;
  padding: 8px 10px;
}

/* ===== Main ===== */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

#chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
}

#char-info {
  display: flex;
  align-items: center;
  gap: 10px;
}
#char-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
}
#char-name {
  font-weight: 600;
  font-size: 16px;
}

#header-controls {
  display: flex;
  align-items: center;
  gap: 12px;
}
#model-select {
  padding: 6px 10px;
  font-size: 13px;
  min-width: 140px;
}

/* ===== Messages ===== */
#messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.msg {
  max-width: 80%;
  padding: 12px 16px;
  border-radius: var(--radius);
  line-height: 1.6;
  animation: fadeIn 0.2s ease;
  white-space: pre-wrap;
  word-wrap: break-word;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.msg.user {
  align-self: flex-end;
  background: var(--msg-user);
  border-bottom-right-radius: var(--radius-sm);
}

.msg.assistant {
  align-self: flex-start;
  background: var(--msg-assistant);
  border-bottom-left-radius: var(--radius-sm);
}

.msg.system {
  align-self: center;
  background: transparent;
  color: var(--text2);
  font-size: 12px;
  padding: 4px 12px;
}

.msg .sender {
  font-size: 12px;
  color: var(--text2);
  margin-bottom: 4px;
  font-weight: 500;
}

.msg .content {
  line-height: 1.6;
}

.msg.typing .content::after {
  content: '▊';
  animation: blink 0.8s step-end infinite;
}

@keyframes blink {
  50% { opacity: 0; }
}

.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text2);
  gap: 8px;
}
.empty-state .icon {
  font-size: 48px;
  opacity: 0.3;
}
.empty-state .hint {
  font-size: 14px;
}

/* ===== Input Bar ===== */
#input-bar {
  display: flex;
  gap: 10px;
  padding: 16px 20px;
  background: var(--bg2);
  border-top: 1px solid var(--border);
  align-items: flex-end;
}

#message-input {
  flex: 1;
  resize: none;
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 14px;
  line-height: 1.5;
  max-height: 200px;
  min-height: 44px;
}

#send-btn {
  padding: 10px 24px;
  background: var(--accent);
  color: white;
  border-radius: var(--radius);
  font-weight: 500;
  transition: background 0.15s, transform 0.1s;
  height: 44px;
}
#send-btn:hover { background: var(--accent-hover); }
#send-btn:active { transform: scale(0.97); }
#send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

/* ===== Modal ===== */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: fadeIn 0.15s ease;
}
.modal.hidden { display: none; }

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.6);
}

.modal-content {
  position: relative;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 12px;
  width: 520px;
  max-width: 90vw;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  animation: scaleIn 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes scaleIn {
  from { transform: scale(0.95); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 {
  font-size: 18px;
  font-weight: 600;
}
.modal-close {
  font-size: 24px;
  color: var(--text2);
  transition: color 0.15s;
}
.modal-close:hover { color: var(--text); }

.modal-body {
  padding: 20px;
  overflow-y: auto;
  flex: 1;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 16px 20px;
  border-top: 1px solid var(--border);
}
.modal-footer button {
  padding: 8px 20px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  transition: background 0.15s;
}
.btn-primary {
  background: var(--accent);
  color: white;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-danger {
  background: transparent;
  color: var(--danger);
  border: 1px solid var(--danger);
  margin-right: auto;
}
.btn-danger:hover { background: rgba(255, 68, 68, 0.1); }

.form-group {
  margin-bottom: 16px;
}
.form-group label {
  display: block;
  font-size: 13px;
  color: var(--text2);
  margin-bottom: 6px;
  font-weight: 500;
}
.form-group input[type="text"],
.form-group input[type="password"],
.form-group input[type="number"],
.form-group textarea,
.form-group select {
  width: 100%;
}
.form-group input[type="range"] {
  width: calc(100% - 50px);
  display: inline-block;
  vertical-align: middle;
}
.form-group input[type="range"] + span {
  display: inline-block;
  width: 40px;
  text-align: center;
  font-size: 14px;
  color: var(--text2);
  vertical-align: middle;
}

/* ===== Responsive ===== */
@media (max-width: 640px) {
  #sidebar {
    width: 60px;
    min-width: 60px;
  }
  .sidebar-header h1 { display: none; }
  .char-item .cname { display: none; }
  .sidebar-footer button:first-child { padding: 8px; }
  .msg { max-width: 90%; }
  #model-select { min-width: 100px; font-size: 12px; }
}
