:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-alt: #eceef2;
  --border: #e1e3e9;
  --text: #1e2230;
  --text-muted: #6b7280;
  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --danger: #dc2626;
  --sidebar-bg: #1b1f2b;
  --sidebar-text: #e7e9f0;
  --sidebar-muted: #9096a8;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 20, 30, .04), 0 6px 20px rgba(16, 20, 30, .06);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Apple SD Gothic Neo', 'Malgun Gothic', sans-serif;
  background: var(--bg);
  color: var(--text);
}

.hidden { display: none !important; }

button {
  font-family: inherit;
}
button:disabled {
  opacity: .6;
  cursor: default;
}

.brand {
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: -.01em;
}

/* --- Loading / spinner --- */
.loading {
  position: relative;
  color: transparent !important;
}
.loading::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 15px;
  height: 15px;
  margin: -7.5px 0 0 -7.5px;
  border: 2px solid rgba(255, 255, 255, .5);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin .6s linear infinite;
}
.icon-text-btn.loading::after,
.conversation-delete.loading::after,
.memory-delete.loading::after {
  border-color: rgba(0, 0, 0, .15);
  border-top-color: var(--text-muted);
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

.primary-btn {
  padding: .75rem 1.1rem;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: #fff;
  font-size: .95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s ease;
}
.primary-btn:hover:not(:disabled) { background: var(--accent-hover); }

/* --- App shell --- */
.app {
  display: flex;
  height: 100vh;
}

.sidebar {
  width: 240px;
  flex-shrink: 0;
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: .6rem;
}

.sidebar-top {
  display: flex;
  flex-direction: column;
  gap: .2rem;
  padding-bottom: .6rem;
  margin-bottom: .2rem;
  border-bottom: 1px solid rgba(255, 255, 255, .08);
}

.device-badge {
  font-size: .8rem;
  color: var(--sidebar-muted);
  min-height: 1.1em;
}

.new-chat-btn {
  padding: .65rem;
  border: 1px solid rgba(255, 255, 255, .14);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, .06);
  color: var(--sidebar-text);
  font-weight: 600;
  font-size: .9rem;
  cursor: pointer;
  transition: background .15s ease;
}
.new-chat-btn:hover:not(:disabled) { background: rgba(255, 255, 255, .12); }

.conversation-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: .15rem;
}

.conversation-item {
  display: flex;
  align-items: center;
  border-radius: 8px;
}
.conversation-item:hover { background: rgba(255, 255, 255, .06); }

.conversation-label {
  flex: 1;
  text-align: left;
  padding: .55rem .6rem;
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: .85rem;
  color: var(--sidebar-text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation-delete {
  flex-shrink: 0;
  padding: .3rem .5rem;
  margin-right: .3rem;
  border: none;
  background: transparent;
  color: var(--sidebar-muted);
  cursor: pointer;
  border-radius: 6px;
  font-size: .8rem;
}
.conversation-delete:hover:not(:disabled) { background: rgba(220, 38, 38, .15); color: #fca5a5; }

.legacy-badge {
  display: inline-block;
  margin-left: .4rem;
  padding: .1rem .4rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, .1);
  color: var(--sidebar-muted);
  font-size: .68rem;
  vertical-align: middle;
}

.sidebar-footer {
  display: flex;
  flex-direction: column;
  gap: .3rem;
  padding-top: .6rem;
  border-top: 1px solid rgba(255, 255, 255, .08);
}

.icon-text-btn {
  text-align: left;
  padding: .55rem .6rem;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--sidebar-text);
  font-size: .85rem;
  cursor: pointer;
}
.icon-text-btn:hover:not(:disabled) { background: rgba(255, 255, 255, .06); }

.chat {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.chat-header {
  padding: 1rem 1.4rem;
  font-size: 1.05rem;
  font-weight: 700;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1.2rem 1.4rem;
  display: flex;
  flex-direction: column;
  gap: .6rem;
}

.message {
  max-width: 65%;
  padding: .65rem 1rem;
  border-radius: var(--radius);
  white-space: pre-wrap;
  line-height: 1.5;
  font-size: .92rem;
}

.message.user {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 3px;
}

.message.model {
  align-self: flex-start;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 3px;
}

.message.thinking {
  opacity: .55;
  font-style: italic;
}

.cooldown-banner {
  margin: 0 1.4rem .6rem;
  padding: .7rem 1rem;
  border-radius: var(--radius);
  background: #fff7ed;
  border: 1px solid #fed7aa;
  color: #9a3412;
  font-size: .85rem;
}

.chat-form {
  display: flex;
  gap: .6rem;
  padding: 1rem 1.4rem;
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.chat-form input {
  flex: 1;
  padding: .75rem 1rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  font-size: .95rem;
  background: var(--bg);
}
.chat-form input:disabled { opacity: .6; }
.chat-form input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.chat-form button {
  padding: .75rem 1.2rem;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}
.chat-form button:hover:not(:disabled) { background: var(--accent-hover); }

/* --- Onboarding --- */
.onboarding {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  padding: 1.5rem;
}

.onboarding-card {
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 2rem;
  width: min(92vw, 420px);
  text-align: center;
}

.onboarding-lead {
  margin: .8rem 0 1.2rem;
  color: var(--text-muted);
  font-size: .92rem;
  line-height: 1.5;
}

.onboarding-card textarea {
  width: 100%;
  padding: .8rem 1rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  font-size: .9rem;
  font-family: inherit;
  resize: vertical;
  margin-bottom: 1rem;
  background: var(--bg);
}
.onboarding-card .primary-btn { width: 100%; }

/* --- Modals --- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 22, 30, .45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
}

.modal {
  background: var(--surface);
  border-radius: 14px;
  box-shadow: var(--shadow);
  width: min(92vw, 440px);
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.1rem 1.3rem;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 { margin: 0; font-size: 1rem; }

.modal-close {
  border: none;
  background: transparent;
  font-size: 1.2rem;
  line-height: 1;
  cursor: pointer;
  color: var(--text-muted);
}

.modal-body {
  padding: 1.1rem 1.3rem;
  overflow-y: auto;
}

.modal-body label {
  display: block;
  font-size: .85rem;
  color: var(--text-muted);
  margin-bottom: .4rem;
}

.modal-body select {
  width: 100%;
  padding: .65rem .8rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  font-size: .9rem;
  margin-bottom: .6rem;
  background: var(--surface);
}

.hint {
  font-size: .78rem;
  color: var(--text-muted);
  margin: 0 0 1rem;
}

.memory-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: .6rem;
  padding: .6rem 0;
  border-bottom: 1px solid var(--border);
  font-size: .88rem;
  line-height: 1.5;
}
.memory-row:last-child { border-bottom: none; }

.memory-delete, .conversation-delete {
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: .85rem;
  color: var(--text-muted);
  border-radius: 6px;
  padding: .2rem .4rem;
  flex-shrink: 0;
}
.memory-delete:hover:not(:disabled) { background: rgba(220, 38, 38, .1); color: var(--danger); }

/* --- Login page --- */
.login-body {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-card {
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 2.2rem 2rem;
  text-align: center;
  width: min(90vw, 320px);
}
.login-card .brand { font-size: 1.4rem; margin-bottom: .3rem; }
.login-lead { margin: 0 0 1.4rem; color: var(--text-muted); font-size: .9rem; }
.login-card input {
  width: 100%;
  box-sizing: border-box;
  padding: .75rem 1rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  font-size: .95rem;
  margin-bottom: .8rem;
  background: var(--bg);
}
.login-card .primary-btn { width: 100%; }
.error { color: var(--danger); font-size: .82rem; min-height: 1.2em; margin-bottom: .4rem; }
