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

:root {
  --bg: #0d0f0e;
  --surface: #151816;
  --surface2: #1c1f1d;
  --border: #2a2e2c;
  --green: #4ade80;
  --green-dim: rgba(74,222,128,0.15);
  --red: #f87171;
  --amber: #fbbf24;
  --text: #f0f4f1;
  --muted: #6b7c72;
  --font: system-ui, -apple-system, sans-serif;
  --tab-h: 64px;
  --header-h: 56px;
}

html, body { height: 100%; background: var(--bg); color: var(--text); font-family: var(--font); font-size: 15px; line-height: 1.5; overscroll-behavior: none; }

/* SETUP SCREEN */
#setup {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  min-height: 100dvh; padding: 32px 24px; gap: 24px; text-align: center;
}
#setup h1 { font-size: 2rem; font-weight: 800; }
#setup h1 span { color: var(--green); }
#setup p { color: var(--muted); max-width: 320px; }
.setup-form { display: flex; flex-direction: column; gap: 12px; width: 100%; max-width: 360px; }
.setup-form label { font-size: 0.8rem; color: var(--muted); text-align: left; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; }
.setup-form input {
  background: var(--surface); border: 1px solid var(--border); color: var(--text);
  padding: 14px 16px; border-radius: 10px; font-size: 1rem; outline: none; width: 100%;
  transition: border-color 0.2s;
}
.setup-form input:focus { border-color: var(--green); }
.btn-green {
  background: var(--green); color: #0d0f0e; font-weight: 800; font-size: 1rem;
  padding: 15px; border-radius: 10px; border: none; cursor: pointer; transition: opacity 0.2s;
}
.btn-green:hover { opacity: 0.85; }
.btn-green:active { opacity: 0.7; }

/* APP SHELL */
#app { display: none; flex-direction: column; height: 100dvh; }

/* HEADER */
header {
  height: var(--header-h); display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px; border-bottom: 1px solid var(--border);
  background: var(--bg); flex-shrink: 0;
}
.header-title { font-weight: 800; font-size: 1.1rem; }
.header-title span { color: var(--green); }
.conn-dot {
  width: 10px; height: 10px; border-radius: 50%; background: var(--muted);
  transition: background 0.3s;
}
.conn-dot.connected { background: var(--green); box-shadow: 0 0 6px var(--green); }
.conn-dot.error { background: var(--red); }

/* PAGES */
#pages { flex: 1; overflow: hidden; position: relative; }
.page { display: none; height: 100%; overflow-y: auto; padding: 16px; -webkit-overflow-scrolling: touch; }
.page.active { display: block; }

/* TAB BAR */
nav {
  height: var(--tab-h); display: flex; border-top: 1px solid var(--border);
  background: var(--surface); flex-shrink: 0;
  padding-bottom: env(safe-area-inset-bottom);
}
.tab {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 4px; cursor: pointer; border: none; background: none; color: var(--muted);
  font-size: 0.7rem; font-weight: 600; transition: color 0.15s; min-height: 44px;
}
.tab .icon { font-size: 1.3rem; line-height: 1; }
.tab.active { color: var(--green); }

/* CARDS */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 16px; margin-bottom: 12px;
}
.card-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.card-title { font-weight: 700; font-size: 0.95rem; }
.card-sub { font-size: 0.82rem; color: var(--muted); }

/* BADGES */
.badge {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 0.72rem; font-weight: 700; padding: 3px 8px; border-radius: 6px;
}
.badge-green { background: var(--green-dim); color: var(--green); }
.badge-red { background: rgba(248,113,113,0.15); color: var(--red); }
.badge-amber { background: rgba(251,191,36,0.15); color: var(--amber); }
.badge-muted { background: var(--surface2); color: var(--muted); }

/* TOGGLE */
.toggle { position: relative; width: 44px; height: 26px; flex-shrink: 0; }
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle-slider {
  position: absolute; inset: 0; background: var(--surface2); border-radius: 13px;
  cursor: pointer; transition: background 0.2s;
}
.toggle-slider::before {
  content: ''; position: absolute; width: 20px; height: 20px; left: 3px; top: 3px;
  background: var(--muted); border-radius: 50%; transition: transform 0.2s, background 0.2s;
}
.toggle input:checked + .toggle-slider { background: var(--green-dim); border: 1px solid var(--green); }
.toggle input:checked + .toggle-slider::before { transform: translateX(18px); background: var(--green); }

/* ICON BUTTONS */
.icon-btn {
  background: var(--surface2); border: 1px solid var(--border); color: var(--text);
  width: 36px; height: 36px; border-radius: 8px; cursor: pointer;
  display: flex; align-items: center; justify-content: center; font-size: 0.9rem;
  transition: background 0.15s; flex-shrink: 0;
}
.icon-btn:active { background: var(--green-dim); }

/* CHAT */
#chat-page { display: none; flex-direction: column; height: 100%; padding: 0; }
#chat-page.active { display: flex; }
#chat-messages {
  flex: 1; overflow-y: auto; padding: 12px 16px;
  display: flex; flex-direction: column; gap: 10px;
  -webkit-overflow-scrolling: touch;
}
.msg { max-width: 85%; display: flex; flex-direction: column; gap: 4px; }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.assistant { align-self: flex-start; }
.msg-bubble {
  padding: 10px 14px; border-radius: 16px; font-size: 0.9rem; line-height: 1.5;
  white-space: pre-wrap; word-break: break-word;
}
.msg.user .msg-bubble { background: var(--green); color: #0d0f0e; border-bottom-right-radius: 4px; }
.msg.assistant .msg-bubble { background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg-bubble.streaming::after { content: '▋'; animation: blink 1s infinite; color: var(--green); }
@keyframes blink { 0%,100%{opacity:1} 50%{opacity:0} }
.msg-time { font-size: 0.7rem; color: var(--muted); padding: 0 4px; }
#chat-input-row {
  display: flex; gap: 8px; padding: 10px 16px 10px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border); background: var(--bg);
}
#chat-input {
  flex: 1; background: var(--surface); border: 1px solid var(--border); color: var(--text);
  padding: 10px 14px; border-radius: 20px; font-size: 0.95rem; outline: none; resize: none;
  max-height: 120px; line-height: 1.4; font-family: var(--font);
}
#chat-input:focus { border-color: var(--green); }
#chat-send {
  background: var(--green); color: #0d0f0e; border: none; width: 42px; height: 42px;
  border-radius: 50%; cursor: pointer; font-size: 1.1rem; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center; transition: opacity 0.2s;
  align-self: flex-end;
}
#chat-send:active { opacity: 0.7; }

/* STAT ROW */
.stat-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 16px; }
.stat-card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px; }
.stat-val { font-size: 1.6rem; font-weight: 800; color: var(--green); letter-spacing: -0.02em; }
.stat-lbl { font-size: 0.75rem; color: var(--muted); margin-top: 2px; }

/* SECTION TITLE */
.section-title { font-size: 0.75rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.08em; margin-bottom: 10px; margin-top: 20px; }
.section-title:first-child { margin-top: 0; }

/* EMPTY STATE */
.empty { text-align: center; padding: 48px 24px; color: var(--muted); font-size: 0.9rem; }
.empty .icon { font-size: 2.5rem; display: block; margin-bottom: 12px; }

/* SPINNER */
.spinner { display: inline-block; width: 18px; height: 18px; border: 2px solid var(--border); border-top-color: var(--green); border-radius: 50%; animation: spin 0.7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* STATUS PAGE */
.status-row { display: flex; align-items: center; justify-content: space-between; padding: 12px 0; border-bottom: 1px solid var(--border); }
.status-row:last-child { border-bottom: none; }
.status-label { font-size: 0.9rem; }
.status-val { font-size: 0.85rem; color: var(--muted); }

/* SETTINGS GEAR */
.gear-btn { background: none; border: none; color: var(--muted); font-size: 1.2rem; cursor: pointer; padding: 4px; }

/* TOAST */
#toast {
  position: fixed; bottom: calc(var(--tab-h) + 16px); 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: 0.85rem; font-weight: 600;
  opacity: 0; transition: opacity 0.2s, transform 0.2s; pointer-events: none; white-space: nowrap;
  z-index: 999;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
