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

:root {
  --bg: #0a0a0a;
  --surface: #141414;
  --surface-2: #1e1e1e;
  --border: #2a2a2a;
  --text: #e8e8e8;
  --text-dim: #888;
  --accent: #c4a0ff;
  --accent-dim: #8b5cf6;
  --user-bg: #1a1a2e;
  --assistant-bg: #141414;
  --error: #ef4444;
  --radius: 12px;
  --app-width: 100%;
  --app-height: 100vh;
  --app-offset-x: 0px;
  --app-offset-y: 0px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@supports (height: 100dvh) {
  :root {
    --app-height: 100dvh;
  }
}

html, body {
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
  background: var(--bg);
  overflow: hidden;
  -webkit-text-size-adjust: 100%;
}

body {
  position: relative;
  overflow: hidden;
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.5;
}

#viewport {
  position: fixed;
  top: 0;
  left: 0;
  width: var(--app-width);
  max-width: 100%;
  height: var(--app-height);
  min-height: var(--app-height);
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  background: var(--bg);
  transform: translate3d(var(--app-offset-x), var(--app-offset-y), 0);
  will-change: transform, width, height;
}

/* ---- Setup Screen ---- */
#setup {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  padding: 24px;
  gap: 20px;
  touch-action: manipulation;
}

#setup h1 {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.5px;
}

#setup p {
  color: var(--text-dim);
  text-align: center;
  max-width: 320px;
}

#setup input {
  width: 100%;
  max-width: 360px;
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 16px;
  outline: none;
  transition: border-color 0.2s;
}

#setup input:focus { border-color: var(--accent-dim); }

#setup input::placeholder { color: var(--text-dim); }

#setup button {
  width: 100%;
  max-width: 360px;
  padding: 14px;
  background: var(--accent-dim);
  color: white;
  border: none;
  border-radius: var(--radius);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s;
}

#setup button:active { opacity: 0.8; }

/* ---- App Layout ---- */
#app {
  display: none;
  flex-direction: column;
  width: 100%;
  min-width: 0;
  height: 100%;
  overflow: hidden;
}

#app.active { display: flex; }
#setup.hidden { display: none; }

/* ---- Header ---- */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  padding-top: calc(12px + env(safe-area-inset-top, 0px));
  padding-left: calc(16px + env(safe-area-inset-left, 0px));
  padding-right: calc(16px + env(safe-area-inset-right, 0px));
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  min-height: calc(52px + env(safe-area-inset-top, 0px));
  flex-shrink: 0;
}

header h2 {
  font-size: 17px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  text-align: center;
  padding: 0 8px;
}

.header-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 22px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 8px;
  transition: background 0.15s, color 0.15s;
  flex-shrink: 0;
}

.header-btn:hover { background: var(--surface-2); color: var(--text); }

/* ---- Sidebar ---- */
#sidebar {
  position: fixed;
  top: 0; left: 0; bottom: 0;
  width: 280px;
  max-width: 80vw;
  background: var(--surface);
  border-right: 1px solid var(--border);
  transform: translateX(-100%);
  transition: transform 0.25s ease;
  z-index: 100;
  display: flex;
  flex-direction: column;
  will-change: transform;
}

#sidebar.open { transform: translateX(0); }

#sidebar-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 99;
  display: none;
}

#sidebar-overlay.show { display: block; }

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.sidebar-header h3 { font-size: 15px; font-weight: 600; }

#new-chat-btn {
  background: var(--accent-dim);
  color: white;
  border: none;
  padding: 6px 14px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

#conv-list {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 8px;
  touch-action: pan-y;
  overscroll-behavior: contain;
}

.conv-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  color: var(--text-dim);
  transition: background 0.15s;
}

.conv-item:hover { background: var(--surface-2); }
.conv-item.active { background: var(--surface-2); color: var(--text); }

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

.conv-item .delete-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 16px;
  cursor: pointer;
  padding: 2px 6px;
  opacity: 0;
  transition: opacity 0.15s;
}

.conv-item:hover .delete-btn { opacity: 1; }
.conv-item .delete-btn:hover { color: var(--error); }

/* ---- Messages ---- */
#messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  overscroll-behavior: contain;
}

.msg {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 15px;
  line-height: 1.55;
  word-break: break-word;
  white-space: pre-wrap;
}

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

.msg.assistant {
  align-self: flex-start;
  background: var(--assistant-bg);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
  max-width: 92%;
}

.msg.error {
  align-self: flex-start;
  background: rgba(239, 68, 68, 0.1);
  border: 1px solid rgba(239, 68, 68, 0.3);
  color: var(--error);
}

.tool-block {
  font-size: 12px;
  font-family: "SF Mono", SFMono-Regular, Menlo, monospace;
  margin: 4px 0;
  padding: 6px 10px;
  background: var(--surface-2);
  border-radius: 8px;
  border-left: 2px solid var(--accent-dim);
  color: var(--text-dim);
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 120px;
  overflow-y: auto;
}

.tool-block .tool-name {
  color: var(--accent);
  font-weight: 600;
  font-size: 12px;
}

.tool-block.tool-error {
  border-left-color: var(--error);
}

.msg.streaming::after {
  content: "";
  display: inline-block;
  width: 6px;
  height: 16px;
  background: var(--accent);
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: blink 0.8s step-end infinite;
}

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

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  color: var(--text-dim);
  gap: 8px;
  padding: 40px;
  text-align: center;
}

.empty-state span { font-size: 40px; }

/* ---- Input Bar ---- */
#input-bar {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 16px;
  padding-bottom: calc(12px + var(--safe-bottom));
  padding-left: calc(16px + env(safe-area-inset-left, 0px));
  padding-right: calc(16px + env(safe-area-inset-right, 0px));
  border-top: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
  touch-action: manipulation;
}

#msg-input {
  flex: 1;
  min-width: 0;
  padding: 10px 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 20px;
  color: var(--text);
  font-size: 16px;
  font-family: inherit;
  line-height: 1.4;
  resize: none;
  max-height: 120px;
  outline: none;
  transition: border-color 0.2s;
}

#msg-input:focus { border-color: var(--accent-dim); }
#msg-input::placeholder { color: var(--text-dim); }

.input-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 18px;
  flex-shrink: 0;
  transition: opacity 0.15s, background 0.15s;
}

#send-btn {
  background: var(--accent-dim);
  color: white;
}

#send-btn:disabled { opacity: 0.4; cursor: not-allowed; }

#voice-btn {
  background: var(--surface-2);
  color: var(--text-dim);
  border: 1px solid var(--border);
}

#voice-btn.recording {
  background: var(--error);
  color: white;
  border-color: var(--error);
  animation: pulse 1.5s ease infinite;
}

#tts-btn {
  background: var(--surface-2);
  color: var(--text-dim);
  border: 1px solid var(--border);
  font-size: 16px;
}

#tts-btn.active { color: var(--accent); border-color: var(--accent-dim); }

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.08); }
}

/* ---- Settings Modal ---- */
#settings-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 200;
  display: none;
}

#settings-overlay.show { display: flex; align-items: flex-end; justify-content: center; }

#settings-panel {
  background: var(--surface);
  border-top: 1px solid var(--border);
  border-radius: 16px 16px 0 0;
  width: 100%;
  max-width: 480px;
  padding: 20px 16px;
  padding-bottom: calc(20px + var(--safe-bottom));
}

#settings-panel h3 {
  font-size: 17px;
  font-weight: 600;
  margin-bottom: 16px;
  text-align: center;
}

.settings-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
  font-size: 15px;
}

.settings-item:last-child { border-bottom: none; }

.settings-item .label { color: var(--text); }
.settings-item .value { color: var(--text-dim); font-size: 13px; }

#logout-btn {
  width: 100%;
  margin-top: 16px;
  padding: 12px;
  background: none;
  color: var(--error);
  border: 1px solid rgba(239, 68, 68, 0.3);
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
}
