:root {
  --bg: #0b1220;
  --bg2: #111a2e;
  --card: #16213a;
  --card2: #1c2a47;
  --border: #253453;
  --text: #e6edf7;
  --muted: #8a9bb8;
  --primary: #3b82f6;
  --primary-2: #2563eb;
  --accent: #22d3ee;
  --ok: #22c55e;
  --warn: #f59e0b;
  --danger: #ef4444;
  --radius: 14px;
  --nav-h: 60px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif; font-size: 15px; -webkit-tap-highlight-color: transparent; }
body { overscroll-behavior: none; }
a { color: var(--primary); text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; }
h1, h2, h3 { margin: 0; }
h1 { font-size: 20px; font-weight: 700; }
h2 { font-size: 17px; font-weight: 600; }
h3 { font-size: 15px; font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; }
.row { display: flex; align-items: center; gap: 10px; }
.between { justify-content: space-between; }
.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.grid2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
@media (max-width: 480px) { .grid3 { grid-template-columns: repeat(2, 1fr); } }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

#app { min-height: 100%; display: flex; flex-direction: column; }
.screen { flex: 1; display: flex; flex-direction: column; min-height: 100vh; }

/* Topbar */
.topbar { position: sticky; top: 0; z-index: 20; background: rgba(11,18,32,.92); backdrop-filter: blur(10px); border-bottom: 1px solid var(--border); padding: calc(10px + var(--safe-t)) 14px 10px; display: flex; align-items: center; gap: 10px; min-height: 54px; }
.topbar .title { font-weight: 700; font-size: 16px; }
.topbar .sub { font-size: 12px; color: var(--muted); }
.iconbtn { width: 38px; height: 38px; border-radius: 10px; display: inline-flex; align-items: center; justify-content: center; background: var(--card); color: var(--text); border: 1px solid var(--border); flex-shrink: 0; }
.iconbtn:active { background: var(--card2); }
.iconbtn svg { width: 20px; height: 20px; }
.badge { display: inline-flex; align-items: center; justify-content: center; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; background: var(--danger); color: #fff; font-size: 11px; font-weight: 700; }
.badge.warn { background: var(--warn); color: #111; }
.badge.ok { background: var(--ok); color: #052e16; }
.badge.info { background: var(--primary); }

/* Content */
.content { flex: 1; padding: 14px; padding-bottom: calc(var(--nav-h) + var(--safe-b) + 14px); overflow-y: auto; }
.content.nonav { padding-bottom: 20px; }
.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; }
.card.clickable { cursor: pointer; }
.card.clickable:active { background: var(--card2); }
.card + .card { margin-top: 10px; }
.kpi { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; }
.kpi .label { font-size: 12px; color: var(--muted); }
.kpi .value { font-size: 20px; font-weight: 700; margin-top: 2px; }
.kpi .delta { font-size: 12px; }
.pill { display: inline-block; padding: 3px 9px; border-radius: 999px; font-size: 12px; background: var(--card2); border: 1px solid var(--border); color: var(--muted); }
.pill.meta { color: #93c5fd; border-color: #1d4ed8; }
.pill.google_ads { color: #fde68a; border-color: #b45309; }
.pill.ga4 { color: #fdba74; border-color: #c2410c; }
.pill.ok { color: #86efac; border-color: #15803d; }
.pill.warn { color: #fcd34d; border-color: #b45309; }
.pill.danger { color: #fca5a5; border-color: #b91c1c; }

/* Bottom nav */
.bottomnav { position: fixed; bottom: 0; left: 0; right: 0; z-index: 30; display: flex; background: rgba(11,18,32,.96); backdrop-filter: blur(10px); border-top: 1px solid var(--border); padding-bottom: var(--safe-b); height: calc(var(--nav-h) + var(--safe-b)); }
.bottomnav a { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; color: var(--muted); font-size: 11px; position: relative; }
.bottomnav a.active { color: var(--primary); }
.bottomnav svg { width: 22px; height: 22px; }
.bottomnav .badge { position: absolute; top: 6px; right: calc(50% - 20px); }

/* Buttons */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 10px 16px; border-radius: 10px; background: var(--primary); color: #fff; font-weight: 600; border: 1px solid transparent; }
.btn:active { background: var(--primary-2); }
.btn.secondary { background: var(--card2); border-color: var(--border); color: var(--text); }
.btn.ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn.danger { background: var(--danger); }
.btn.ok { background: var(--ok); color: #052e16; }
.btn.sm { padding: 6px 10px; font-size: 13px; border-radius: 8px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
.btn svg { width: 18px; height: 18px; flex-shrink: 0; }

/* Forms */
.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: 12.5px; color: var(--muted); font-weight: 500; }
.input, textarea.input, select.input { width: 100%; padding: 10px 12px; border-radius: 10px; background: var(--bg2); border: 1px solid var(--border); color: var(--text); outline: none; }
.input:focus { border-color: var(--primary); }
textarea.input { resize: vertical; min-height: 80px; }
.switch { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.switch:last-child { border-bottom: 0; }
.toggle { width: 44px; height: 26px; border-radius: 13px; background: var(--border); position: relative; transition: .15s; flex-shrink: 0; }
.toggle::after { content: ''; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; transition: .15s; }
.toggle.on { background: var(--ok); }
.toggle.on::after { left: 21px; }
.help { font-size: 12px; color: var(--muted); line-height: 1.4; }
.error { color: #fca5a5; font-size: 13px; }
.success { color: #86efac; font-size: 13px; }

/* Lists */
.list { display: flex; flex-direction: column; }
.item { display: flex; align-items: center; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--border); }
.item:last-child { border-bottom: 0; }
.item .title { font-weight: 600; }
.item .sub { font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.avatar { width: 40px; height: 40px; border-radius: 12px; display: flex; align-items: center; justify-content: center; font-weight: 700; color: #fff; flex-shrink: 0; }

/* Tables */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 8px 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--muted); font-weight: 600; font-size: 12px; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

/* Chat */
.chat-screen { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }
.chat-messages { flex: 1; overflow-y: auto; padding: 14px 14px 8px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 88%; padding: 10px 13px; border-radius: 16px; line-height: 1.45; word-wrap: break-word; font-size: 14.5px; }
.msg.user { align-self: flex-end; background: var(--primary); color: #fff; border-bottom-right-radius: 5px; }
.msg.assistant { align-self: flex-start; background: var(--card); border: 1px solid var(--border); border-bottom-left-radius: 5px; }
.msg.tool { align-self: flex-start; background: transparent; border: 1px dashed var(--border); color: var(--muted); font-size: 12.5px; padding: 6px 10px; border-radius: 10px; max-width: 95%; }
.msg.tool summary { cursor: pointer; }
.msg.tool pre { margin: 6px 0 0; white-space: pre-wrap; word-break: break-all; font-size: 11.5px; max-height: 240px; overflow: auto; }
.msg .time { font-size: 10.5px; opacity: .6; margin-top: 4px; text-align: right; }
.msg.assistant .md p { margin: 0 0 8px; }
.msg.assistant .md p:last-child { margin-bottom: 0; }
.msg.assistant .md ul, .msg.assistant .md ol { margin: 4px 0 8px; padding-left: 20px; }
.msg.assistant .md h1, .msg.assistant .md h2, .msg.assistant .md h3 { font-size: 15px; margin: 8px 0 6px; }
.msg.assistant .md table { font-size: 12.5px; margin: 6px 0; display: block; overflow-x: auto; }
.msg.assistant .md code { background: var(--bg2); padding: 1px 5px; border-radius: 5px; font-size: 12.5px; }
.msg.assistant .md pre { background: var(--bg2); padding: 8px; border-radius: 8px; overflow-x: auto; }
.msg.assistant .md img { max-width: 100%; border-radius: 10px; }
.msg .atts { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.msg .atts img { width: 90px; height: 90px; object-fit: cover; border-radius: 8px; }
.typing { align-self: flex-start; color: var(--muted); font-size: 13px; display: flex; align-items: center; gap: 8px; padding: 6px 4px; }
.dots span { display: inline-block; width: 6px; height: 6px; background: var(--muted); border-radius: 50%; margin-right: 3px; animation: blink 1.2s infinite; }
.dots span:nth-child(2) { animation-delay: .2s; } .dots span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .2; } 40% { opacity: 1; } }
.composer { border-top: 1px solid var(--border); background: var(--bg); padding: 8px 10px calc(8px + var(--safe-b)); }
.composer .pending { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 6px; }
.composer .pending .thumb { position: relative; width: 56px; height: 56px; border-radius: 8px; overflow: hidden; border: 1px solid var(--border); background: var(--card); display: flex; align-items: center; justify-content: center; font-size: 10px; color: var(--muted); }
.composer .pending img { width: 100%; height: 100%; object-fit: cover; }
.composer .pending .x { position: absolute; top: 2px; right: 2px; width: 18px; height: 18px; border-radius: 50%; background: rgba(0,0,0,.7); color: #fff; font-size: 12px; display: flex; align-items: center; justify-content: center; }
.composer .bar { display: flex; align-items: flex-end; gap: 8px; }
.composer textarea { flex: 1; background: var(--card); border: 1px solid var(--border); border-radius: 20px; padding: 10px 14px; max-height: 140px; min-height: 42px; resize: none; outline: none; line-height: 1.35; }
.composer textarea:focus { border-color: var(--primary); }
.composer .iconbtn { width: 42px; height: 42px; border-radius: 50%; }
.composer .iconbtn.send { background: var(--primary); border-color: var(--primary); color: #fff; }
.composer .iconbtn.rec { background: var(--danger); border-color: var(--danger); color: #fff; animation: pulse 1s infinite; }
@keyframes pulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.08); } }
.suggestions { display: flex; gap: 8px; overflow-x: auto; padding: 4px 14px 8px; }
.suggestions button { flex-shrink: 0; padding: 7px 12px; border-radius: 999px; background: var(--card); border: 1px solid var(--border); color: var(--text); font-size: 13px; }

/* Misc */
.empty { text-align: center; color: var(--muted); padding: 40px 20px; }
.empty svg { width: 48px; height: 48px; opacity: .4; margin-bottom: 10px; }
.center { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 20px; }
.auth-card { width: 100%; max-width: 400px; }
.logo { display: flex; align-items: center; gap: 10px; margin-bottom: 20px; }
.logo img { width: 40px; height: 40px; border-radius: 10px; }
.logo .name { font-weight: 800; font-size: 22px; }
.toast { position: fixed; left: 50%; bottom: calc(var(--nav-h) + var(--safe-b) + 16px); transform: translateX(-50%); background: #fff; color: #111; padding: 10px 16px; border-radius: 10px; font-size: 14px; z-index: 100; box-shadow: 0 8px 24px rgba(0,0,0,.4); max-width: 90%; }
.toast.err { background: var(--danger); color: #fff; }
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.6); z-index: 50; display: flex; align-items: flex-end; justify-content: center; }
@media (min-width: 640px) { .modal-bg { align-items: center; } }
.modal { background: var(--bg2); border: 1px solid var(--border); border-radius: 18px 18px 0 0; width: 100%; max-width: 560px; max-height: 92vh; overflow-y: auto; padding: 18px 16px calc(18px + var(--safe-b)); }
@media (min-width: 640px) { .modal { border-radius: 18px; } }
.tabs { display: flex; gap: 4px; background: var(--bg2); padding: 4px; border-radius: 10px; margin-bottom: 12px; overflow-x: auto; }
.tabs button { flex: 1; padding: 8px 10px; border-radius: 8px; color: var(--muted); font-weight: 600; font-size: 13px; white-space: nowrap; }
.tabs button.active { background: var(--card); color: var(--text); }
.spinner { width: 18px; height: 18px; border: 2px solid var(--border); border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
.progress { height: 8px; background: var(--bg2); border-radius: 4px; overflow: hidden; }
.progress > div { height: 100%; background: var(--primary); }
.progress.over > div { background: var(--danger); }
.progress.under > div { background: var(--warn); }
.chart { width: 100%; height: 120px; }
.thumbgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); gap: 8px; }
.thumbgrid .t { position: relative; aspect-ratio: 1; border-radius: 10px; overflow: hidden; background: var(--bg2); border: 1px solid var(--border); display: flex; align-items: center; justify-content: center; font-size: 11px; color: var(--muted); text-align: center; padding: 4px; }
.thumbgrid img { width: 100%; height: 100%; object-fit: cover; }
.thumbgrid .t .name { position: absolute; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,.6); color: #fff; font-size: 10px; padding: 3px 5px; text-align: left; }
.md-report { line-height: 1.55; }
.md-report table { display: block; overflow-x: auto; }
.md-report h1, .md-report h2, .md-report h3 { margin: 16px 0 8px; }
.md-report img { max-width: 100%; }

/* Desktop layout */
@media (min-width: 900px) {
  .screen.with-side { flex-direction: row; }
  .sidebar { width: 260px; border-right: 1px solid var(--border); background: var(--bg2); padding: 16px 12px; display: flex; flex-direction: column; gap: 4px; position: sticky; top: 0; height: 100vh; }
  .sidebar a { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 10px; color: var(--muted); font-weight: 500; }
  .sidebar a.active, .sidebar a:hover { background: var(--card); color: var(--text); }
  .sidebar svg { width: 20px; height: 20px; }
  .bottomnav { display: none; }
  .content { padding-bottom: 20px; max-width: 1100px; }
  .main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
  .chat-screen { height: 100vh; }
  .msg { max-width: 75%; }
}
@media (max-width: 899px) { .sidebar { display: none; } }
