:root {
  --bg: #0a0a0a;
  --surface: #141414;
  --surface2: #1e1e1e;
  --border: #2a2a2a;
  --accent: #7c6aff;
  --accent2: #a594ff;
  --text: #f0f0f0;
  --text2: #888;
  --text3: #555;
  --danger: #ff4f4f;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --radius: 18px;
}
* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; background: var(--bg); color: var(--text); font-family: -apple-system, system-ui, sans-serif; overflow: hidden; }
.hidden { display: none !important; }

/* Auth */
#auth-screen {
  height: 100dvh; display: flex; align-items: center; justify-content: center;
  background: var(--bg); padding: 24px;
}
#auth-box {
  width: 100%; max-width: 340px; text-align: center;
  display: flex; flex-direction: column; align-items: center; gap: 16px;
}
#auth-logo { margin-bottom: 8px; }
#auth-box h1 { font-size: 28px; font-weight: 700; }
#auth-box p { color: var(--text2); font-size: 15px; }
#google-signin-btn {
  display: flex; align-items: center; gap: 10px; justify-content: center;
  width: 100%; padding: 14px 20px; background: white; color: #333;
  border: none; border-radius: 14px; font-size: 16px; font-weight: 600;
  cursor: pointer; margin-top: 8px; transition: opacity 0.15s;
}
#google-signin-btn:active { opacity: 0.85; }
#auth-error { color: var(--danger); font-size: 13px; padding: 8px 12px; background: rgba(255,79,79,0.1); border-radius: 10px; }

/* App layout */
#app { display: flex; flex-direction: column; height: 100dvh; }
#main-app { display: flex; flex-direction: column; height: 100dvh; }

/* Header */
#header {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(var(--safe-top) + 12px) 16px 12px;
  background: var(--bg); border-bottom: 1px solid var(--border);
  flex-shrink: 0; gap: 10px;
}
#header-left { display: flex; align-items: center; gap: 8px; min-width: 0; }
#header-right { display: flex; align-items: center; gap: 4px; flex-shrink: 0; }
#header button { background: none; border: none; color: var(--text2); cursor: pointer; padding: 6px; border-radius: 10px; display: flex; align-items: center; }
#header button:active { background: var(--surface2); color: var(--text); }
#chat-title { font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 160px; }
#model-pill { font-size: 11px; color: var(--accent2); background: rgba(124,106,255,0.12); border-radius: 20px; padding: 2px 8px; white-space: nowrap; cursor: pointer; border: 1px solid rgba(124,106,255,0.2); flex-shrink: 0; }

/* Messages */
#messages { flex: 1; overflow-y: auto; padding: 16px 12px; display: flex; flex-direction: column; gap: 8px; scroll-behavior: smooth; }
#empty-state { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; color: var(--text3); text-align: center; padding: 40px; margin: auto; }
#empty-state p { font-size: 15px; font-weight: 500; color: var(--text2); }
#empty-state small { font-size: 13px; }
.msg { display: flex; flex-direction: column; max-width: 82%; animation: fadeUp 0.2s ease; }
@keyframes fadeUp { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.assistant { align-self: flex-start; align-items: flex-start; }
.msg.system-msg { align-self: center; align-items: center; max-width: 90%; }
.bubble { padding: 10px 14px; border-radius: var(--radius); font-size: 15px; line-height: 1.5; word-break: break-word; white-space: pre-wrap; }
.msg.user .bubble { background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.msg.assistant .bubble { background: var(--surface2); color: var(--text); border-bottom-left-radius: 4px; border: 1px solid var(--border); }
.msg.system-msg .bubble { background: transparent; color: var(--text3); font-size: 12px; text-align: center; }
.msg-meta { font-size: 11px; color: var(--text3); margin-top: 3px; padding: 0 4px; }
.typing-indicator { display: flex; gap: 4px; align-items: center; padding: 12px 14px; }
.typing-indicator span { width: 7px; height: 7px; border-radius: 50%; background: var(--text3); animation: bounce 1.2s infinite; }
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce { 0%,60%,100% { transform: translateY(0); } 30% { transform: translateY(-6px); } }

/* Input */
#input-bar { display: flex; align-items: flex-end; gap: 8px; padding: 10px 12px calc(var(--safe-bottom) + 10px); background: var(--bg); border-top: 1px solid var(--border); flex-shrink: 0; }
#msg-input { flex: 1; background: var(--surface); border: 1px solid var(--border); border-radius: 22px; padding: 10px 16px; color: var(--text); font-size: 15px; font-family: inherit; resize: none; outline: none; max-height: 120px; line-height: 1.4; transition: border-color 0.15s; }
#msg-input:focus { border-color: var(--accent); }
#msg-input::placeholder { color: var(--text3); }
#send-btn { width: 38px; height: 38px; border-radius: 50%; background: var(--accent); border: none; cursor: pointer; display: flex; align-items: center; justify-content: center; flex-shrink: 0; color: white; transition: transform 0.1s; }
#send-btn:active { transform: scale(0.92); }
#send-btn:disabled { background: var(--surface2); cursor: default; }
#send-btn:disabled svg { opacity: 0.4; }

/* Sidebar */
#sidebar-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.6); z-index: 100; opacity: 0; pointer-events: none; transition: opacity 0.2s; }
#sidebar-overlay.open { opacity: 1; pointer-events: all; }
#sidebar { position: fixed; left: 0; top: 0; bottom: 0; width: 80vw; max-width: 300px; background: var(--surface); z-index: 101; transform: translateX(-100%); transition: transform 0.25s cubic-bezier(0.4,0,0.2,1); display: flex; flex-direction: column; padding-top: calc(var(--safe-top)); }
#sidebar.open { transform: translateX(0); }
#sidebar-header { display: flex; align-items: center; justify-content: space-between; padding: 16px; border-bottom: 1px solid var(--border); }
#sidebar-header span { font-size: 16px; font-weight: 700; }
#sidebar-header button { background: none; border: none; color: var(--text2); cursor: pointer; padding: 4px; }
#convo-list { flex: 1; overflow-y: auto; padding: 8px; }
.convo-item { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px; border-radius: 12px; cursor: pointer; margin-bottom: 2px; transition: background 0.15s; }
.convo-item:hover, .convo-item.active { background: var(--surface2); }
.convo-item-text { flex: 1; min-width: 0; }
.convo-item-title { font-size: 14px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.convo-item-meta { font-size: 11px; color: var(--text2); margin-top: 2px; }
.convo-delete { background: none; border: none; color: var(--text3); cursor: pointer; padding: 4px 6px; border-radius: 8px; flex-shrink: 0; }
.convo-delete:active { color: var(--danger); }
#sidebar-footer { padding: 12px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 6px; padding-bottom: calc(12px + var(--safe-bottom)); }
.sidebar-action { width: 100%; padding: 10px; background: var(--surface2); border: 1px solid var(--border); border-radius: 12px; color: var(--text2); font-size: 13px; cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 6px; font-family: inherit; transition: background 0.15s; }
.sidebar-action:active { background: var(--border); }
.sidebar-action.danger { color: var(--danger); }

/* Sheet */
.sheet-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.7); z-index: 200; display: none; align-items: flex-end; }
.sheet-overlay.open { display: flex; }
.sheet { width: 100%; background: var(--surface); border-radius: 20px 20px 0 0; padding: 0 0 calc(var(--safe-bottom) + 16px); max-height: 92dvh; display: flex; flex-direction: column; animation: slideUp 0.28s cubic-bezier(0.4,0,0.2,1); }
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
.sheet-handle { width: 36px; height: 4px; background: var(--border); border-radius: 2px; margin: 12px auto 0; flex-shrink: 0; }
.sheet-header { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px 12px; flex-shrink: 0; }
.sheet-header h2 { font-size: 17px; font-weight: 700; }
.sheet-header button { background: none; border: none; color: var(--accent2); cursor: pointer; font-size: 15px; font-weight: 600; font-family: inherit; }
.sheet-body { overflow-y: auto; padding: 0 16px; flex: 1; }

/* Settings */
.section-label { font-size: 12px; font-weight: 600; color: var(--text2); text-transform: uppercase; letter-spacing: 0.06em; padding: 16px 4px 6px; }
.card { background: var(--surface2); border-radius: 14px; overflow: hidden; margin-bottom: 4px; }
.row { display: flex; align-items: center; gap: 12px; padding: 13px 14px; border-bottom: 1px solid var(--border); }
.row:last-child { border-bottom: none; }
.row-label { font-size: 15px; flex: 1; }
.row select { background: none; border: none; color: var(--accent2); font-size: 14px; outline: none; cursor: pointer; font-family: inherit; -webkit-appearance: none; text-align: right; }
.row select option { background: var(--surface2); color: var(--text); }
.profile-card { background: var(--surface2); border-radius: 14px; padding: 14px; margin-bottom: 8px; border: 1px solid var(--border); }
.profile-card-header { display: flex; align-items: flex-start; justify-content: space-between; }
.profile-name { font-size: 15px; font-weight: 600; }
.profile-detail { font-size: 13px; color: var(--text2); margin-top: 3px; }
.profile-actions { display: flex; gap: 8px; }
.icon-btn { background: none; border: none; color: var(--text3); cursor: pointer; padding: 4px; border-radius: 6px; display: flex; }
.icon-btn.del { color: var(--danger); }
.add-btn { width: 100%; padding: 12px; background: transparent; border: 1px dashed var(--border); border-radius: 14px; color: var(--accent); font-size: 14px; cursor: pointer; font-family: inherit; transition: background 0.15s; }
.add-btn:active { background: var(--surface2); }
.action-btn { width: 100%; padding: 13px; background: var(--surface2); border: none; border-radius: 14px; color: var(--text); font-size: 15px; cursor: pointer; font-family: inherit; margin-bottom: 8px; text-align: left; display: flex; align-items: center; gap: 10px; }
.action-btn.danger { color: var(--danger); }

/* Memory */
#memories-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.7); z-index: 200; display: none; align-items: flex-end; }
#memories-overlay.open { display: flex; }
#memories-panel { width: 100%; background: var(--surface); border-radius: 20px 20px 0 0; max-height: 85dvh; display: flex; flex-direction: column; animation: slideUp 0.28s cubic-bezier(0.4,0,0.2,1); padding-bottom: calc(var(--safe-bottom) + 16px); }
#memories-list { flex: 1; overflow-y: auto; padding: 8px 16px; }
.memory-item { background: var(--surface2); border-radius: 12px; padding: 12px; margin-bottom: 8px; display: flex; gap: 10px; align-items: flex-start; }
.memory-content { flex: 1; font-size: 14px; line-height: 1.4; }
.memory-meta { font-size: 11px; color: var(--text2); margin-top: 4px; }
.memory-priority { font-size: 10px; padding: 2px 6px; border-radius: 10px; font-weight: 600; }
.memory-priority.high { background: rgba(255,79,79,0.15); color: #ff6b6b; }
.memory-priority.medium { background: rgba(124,106,255,0.15); color: var(--accent2); }
.memory-priority.low { background: rgba(136,136,136,0.15); color: var(--text2); }

/* Modal */
#modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.8); z-index: 300; display: none; align-items: center; justify-content: center; padding: 24px; }
#modal-overlay.open { display: flex; }
#modal { background: var(--surface); border-radius: 18px; padding: 24px; width: 100%; max-width: 340px; animation: fadeUp 0.2s ease; }
#modal h3 { font-size: 17px; font-weight: 700; margin-bottom: 16px; }
.modal-field { margin-bottom: 12px; }
.modal-field label { font-size: 12px; color: var(--text2); display: block; margin-bottom: 5px; font-weight: 500; text-transform: uppercase; letter-spacing: 0.05em; }
.modal-field input, .modal-field select, .modal-field textarea { width: 100%; background: var(--surface2); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; color: var(--text); font-size: 15px; font-family: inherit; outline: none; transition: border-color 0.15s; }
.modal-field input:focus, .modal-field select:focus, .modal-field textarea:focus { border-color: var(--accent); }
.modal-field select option { background: var(--surface2); }
.modal-field textarea { resize: vertical; min-height: 70px; line-height: 1.4; }
.modal-btns { display: flex; gap: 8px; margin-top: 20px; }
.modal-btns button { flex: 1; padding: 12px; border-radius: 12px; font-size: 15px; font-weight: 600; cursor: pointer; font-family: inherit; border: none; }
.btn-cancel { background: var(--surface2); color: var(--text2); }
.btn-save { background: var(--accent); color: white; }

/* Toast */
#toast { position: fixed; bottom: calc(var(--safe-bottom) + 90px); left: 50%; transform: translateX(-50%) translateY(20px); background: var(--surface2); border: 1px solid var(--border); color: var(--text); padding: 10px 18px; border-radius: 20px; font-size: 14px; z-index: 400; opacity: 0; transition: opacity 0.2s, transform 0.2s; pointer-events: none; white-space: nowrap; }
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

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