:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --ink: #111827;
  --muted: #4b5563;
  --line: #e2dfd7;
  --accent: #b45309;
  --accent-dark: #92400e;
  --ok: #065f46;
  --ok-bg: #ecfdf5;
  --bad: #991b1b;
  --bad-bg: #fef2f2;
  --sans: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --serif: Georgia, "Iowan Old Style", serif;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; height: 100%; font-family: var(--sans); background: var(--bg); color: var(--ink); }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 10px 16px; border-radius: 9px; font-size: 14px; font-weight: 600; border: 1px solid transparent; cursor: pointer; text-decoration: none; white-space: nowrap; flex-shrink: 0; }
.btn.sm { padding: 6px 11px; font-size: 13px; border-radius: 7px; }
.btn.full { width: 100%; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-dark { background: var(--ink); color: #fff; }
.btn-accent { background: var(--accent); color: #fff; }
.btn-ghost { background: var(--surface); color: var(--ink); border-color: var(--line); }
.btn-danger { background: var(--bad); color: #fff; }
.link-btn { background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 13px; padding: 4px 6px; white-space: nowrap; flex-shrink: 0; }
.link-btn:hover { color: var(--ink); }

/* Auth */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.auth-card { width: 100%; max-width: 420px; background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 28px; box-shadow: 0 12px 34px rgba(17,24,39,0.06); }
.brand { display: inline-flex; align-items: center; gap: 8px; text-decoration: none; color: var(--ink); font-weight: 700; margin-bottom: 18px; }
.brand-mark { width: 26px; height: 26px; border-radius: 7px; background: var(--ink); color: #fff; display: inline-grid; place-items: center; font-family: var(--serif); font-weight: 700; font-size: 14px; text-decoration: none; flex-shrink: 0; }
.auth-card h1 { font-family: var(--serif); font-size: 24px; margin: 0 0 8px; }
.auth-card .lede { color: var(--muted); font-size: 14px; margin: 0 0 18px; line-height: 1.5; }
.pending-banner { background: #fef3c7; color: #78350f; border: 1px solid #fde68a; border-radius: 10px; padding: 10px 12px; font-size: 13px; margin-bottom: 16px; }
.label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.auth-card input { width: 100%; padding: 11px 12px; border: 1px solid #d1d5db; border-radius: 9px; font-size: 15px; margin-bottom: 12px; }
.msg { font-size: 13px; margin: 0 0 10px; min-height: 18px; }
.msg.err { color: var(--bad); }
.msg.ok { color: var(--ok); }
.auth-foot { font-size: 12px; color: var(--muted); margin: 16px 0 0; padding-top: 14px; border-top: 1px solid var(--line); }
.code-actions { display: flex; gap: 8px; justify-content: center; margin-top: 12px; align-items: center; color: var(--muted); }

/* App Shell */
.app-shell { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }
.topbar { min-height: 52px; background: var(--surface); border-bottom: 1px solid var(--line); padding: 6px 12px; display: flex; align-items: center; gap: 8px; flex-shrink: 0; overflow-x: auto; scrollbar-width: none; }
.topbar::-webkit-scrollbar { display: none; }
.site-picker { display: flex; align-items: center; gap: 6px; min-width: 0; flex-shrink: 1; }
.site-picker select { font-size: 13px; font-weight: 600; padding: 6px 9px; border: 1px solid var(--line); border-radius: 7px; background: var(--bg); color: var(--ink); max-width: 220px; min-width: 110px; }
.badge { font-size: 10px; font-weight: 700; text-transform: uppercase; padding: 2px 7px; border-radius: 999px; flex-shrink: 0; }
.badge.live { background: var(--ok-bg); color: var(--ok); }
.badge.draft { background: #fef3c7; color: #92400e; }
.spacer { flex: 1; min-width: 4px; }
.topbar-actions { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }

.split { flex: 1; display: grid; grid-template-columns: 420px 1fr; min-height: 0; }
.chat-pane { display: flex; flex-direction: column; background: var(--surface); border-right: 1px solid var(--line); min-height: 0; min-width: 0; }
.rescue-bar { display: flex; gap: 6px; padding: 9px 10px; background: #fffbeb; border-bottom: 1px solid #fde68a; min-width: 0; align-items: center; }
.rescue-bar input { flex: 1; min-width: 0; border: 1px solid #e5d5a8; border-radius: 7px; padding: 7px 9px; font-size: 13px; background: #fff; }
.style-select { border: 1px solid #e5d5a8; border-radius: 7px; padding: 6px 7px; font-size: 12px; font-weight: 600; background: #fff; color: #78350f; flex-shrink: 0; max-width: 112px; }
.chat-log { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.bubble { max-width: 92%; padding: 10px 13px; border-radius: 12px; font-size: 14px; line-height: 1.5; white-space: pre-wrap; word-break: break-word; }
.bubble.user { align-self: flex-end; background: var(--ink); color: #fff; border-bottom-right-radius: 4px; }
.bubble.assistant { align-self: flex-start; background: var(--bg); color: var(--ink); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.bubble a { color: inherit; text-decoration: underline; }

/* Live Build Progress Card */
.typing { align-self: stretch; background: #fffbeb; border: 1px solid #fcd34d; border-radius: 12px; padding: 12px 14px; font-size: 13px; color: #78350f; box-shadow: 0 2px 8px rgba(180,83,9,0.06); }
.prog-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.prog-title { display: inline-flex; align-items: center; gap: 7px; font-weight: 700; color: #78350f; }
.pulse-dot { width: 8px; height: 8px; border-radius: 999px; background: var(--accent); box-shadow: 0 0 0 0 rgba(180,83,9,0.5); animation: srPulse 1.5s infinite; flex-shrink: 0; }
@keyframes srPulse { 0% { box-shadow: 0 0 0 0 rgba(180,83,9,0.45); } 70% { box-shadow: 0 0 0 7px rgba(180,83,9,0); } 100% { box-shadow: 0 0 0 0 rgba(180,83,9,0); } }
.prog-clock { font-variant-numeric: tabular-nums; font-size: 12px; font-weight: 600; color: #92400e; white-space: nowrap; }
.prog-track { width: 100%; height: 6px; background: #fde68a; border-radius: 999px; overflow: hidden; margin-bottom: 10px; }
.prog-fill { height: 100%; background: linear-gradient(90deg, #d97706, #b45309); border-radius: 999px; transition: width 0.45s ease; }
.prog-steps { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 4px; margin-bottom: 10px; }
.prog-step { text-align: center; font-size: 10px; font-weight: 600; padding: 4px 2px; border-radius: 6px; background: rgba(255,255,255,0.55); color: #a16207; border: 1px solid rgba(217,119,6,0.18); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.prog-step.done { background: var(--ok-bg); color: var(--ok); border-color: #a7f3d0; }
.prog-step.active { background: #fef3c7; color: #78350f; border-color: #d97706; font-weight: 700; }
.prog-metrics { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.prog-chip { display: inline-flex; align-items: center; gap: 4px; font-size: 11px; font-weight: 600; padding: 3px 8px; border-radius: 999px; background: #fff; border: 1px solid #f5d08a; color: #78350f; font-variant-numeric: tabular-nums; }
.prog-chip.good { background: var(--ok-bg); border-color: #a7f3d0; color: var(--ok); }
.prog-chip.bad { background: var(--bad-bg); border-color: #fecaca; color: var(--bad); }
.prog-log { border-top: 1px dashed rgba(180,83,9,0.22); padding-top: 6px; display: flex; flex-direction: column; gap: 3px; }
.typing .step { font-size: 11.5px; color: #92400e; line-height: 1.35; }

/* Persistent Site Telemetry Strip */
.metrics-strip { display: flex; align-items: center; gap: 8px; padding: 6px 12px; background: #faf9f6; border-bottom: 1px solid var(--line); overflow-x: auto; flex-shrink: 0; scrollbar-width: none; }
.metrics-strip::-webkit-scrollbar { display: none; }
.m-pill { display: inline-flex; align-items: center; gap: 5px; font-size: 11.5px; font-weight: 600; padding: 3px 9px; border-radius: 999px; background: var(--surface); border: 1px solid var(--line); color: var(--ink); white-space: nowrap; font-variant-numeric: tabular-nums; }
.m-pill .k { color: var(--muted); font-weight: 500; }
.m-pill.bad { background: var(--bad-bg); border-color: #fecaca; color: var(--bad); cursor: pointer; }
.m-pill.warn { background: #fffbeb; border-color: #fde68a; color: #92400e; cursor: pointer; }
.m-pill.ok { background: var(--ok-bg); border-color: #a7f3d0; color: var(--ok); }

/* Preview Live Build Card */
.preview-build-card { width: 100%; max-width: 440px; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 20px; text-align: left; box-shadow: 0 10px 28px rgba(17,24,39,0.06); }
.preview-build-card h3 { font-family: var(--serif); font-size: 19px; color: var(--ink); margin: 0 0 6px; }
.preview-build-card .sub { font-size: 12.5px; color: var(--muted); margin: 0 0 14px; }
.composer { padding: 10px 12px; border-top: 1px solid var(--line); background: var(--surface); }
.attach-strip { display: flex; gap: 6px; margin-bottom: 8px; flex-wrap: wrap; }
.attach-chip { font-size: 12px; background: var(--bg); border: 1px solid var(--line); padding: 3px 8px; border-radius: 6px; }
.composer-box { border: 1px solid #d1d5db; border-radius: 10px; padding: 8px; }
.composer-box textarea { width: 100%; border: 0; outline: 0; resize: none; font-family: inherit; font-size: 14px; }
.composer-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 6px; }
.hint { font-size: 11px; color: var(--muted); }

/* Preview Pane */
.preview-pane { display: flex; flex-direction: column; min-height: 0; background: #e8e6e0; }
.preview-toolbar { height: 48px; background: var(--surface); border-bottom: 1px solid var(--line); padding: 0 12px; display: flex; align-items: center; gap: 10px; flex-shrink: 0; overflow-x: auto; }
.seg { display: inline-flex; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 2px; }
.seg button { border: 0; background: transparent; padding: 4px 10px; font-size: 12px; font-weight: 600; color: var(--muted); border-radius: 6px; cursor: pointer; }
.seg button.on { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,0.08); }
.url-pill { flex: 1; font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 4px 10px; background: var(--bg); border-radius: 6px; min-width: 120px; }
.preview-btns { display: flex; gap: 6px; align-items: center; }

.preview-stage { flex: 1; display: flex; min-height: 0; padding: 12px; gap: 12px; overflow: hidden; }
.before-panel { width: 360px; flex-shrink: 0; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 16px; overflow-y: auto; }
.before-panel.full { width: 100%; max-width: 760px; margin: 0 auto; }
.after-wrap { flex: 1; display: flex; justify-content: center; position: relative; min-width: 0; }
.after-wrap iframe { width: 100%; height: 100%; border: 1px solid var(--line); border-radius: 12px; background: #fff; transition: width 0.2s; }
.after-wrap.mobile iframe { width: 390px; max-width: 100%; }
.preview-empty { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; padding: 32px; color: var(--muted); }
.preview-empty h2 { font-family: var(--serif); color: var(--ink); margin: 0 0 8px; }
.preview-empty p { max-width: 44ch; margin: 0 auto; font-size: 14px; }

.audit-score { display: flex; align-items: center; gap: 12px; padding: 12px; border-radius: 10px; background: var(--bad-bg); border: 1px solid #fecaca; margin-bottom: 14px; }
.audit-score .num { font-size: 28px; font-weight: 800; color: var(--bad); }
.audit-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 8px; }
.audit-list li { padding: 9px 11px; border-radius: 8px; background: var(--bg); border: 1px solid var(--line); font-size: 12px; }
.audit-list li strong { display: block; font-size: 13px; margin-bottom: 2px; }

/* Modals */
.modal-overlay { position: fixed; inset: 0; background: rgba(17,24,39,0.45); display: grid; place-items: center; padding: 20px; z-index: 40; }
.modal-card { width: 100%; max-width: 640px; max-height: 85vh; overflow: auto; background: var(--surface); border-radius: 14px; padding: 20px; box-shadow: 0 20px 50px rgba(0,0,0,0.2); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding-bottom: 12px; border-bottom: 1px solid var(--line); margin-bottom: 16px; }
.modal-head h2 { margin: 0; font-family: var(--serif); font-size: 20px; }
.plan-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.plan-box { border: 1px solid var(--line); border-radius: 12px; padding: 16px; display: flex; flex-direction: column; justify-content: space-between; gap: 12px; }
.plan-box h3 { margin: 0; font-size: 17px; }
.plan-box .p { font-family: var(--serif); font-size: 28px; margin: 4px 0; }
.csv-table { width: 100%; border-collapse: collapse; font-size: 12px; margin: 10px 0; }
.csv-table th, .csv-table td { border: 1px solid var(--line); padding: 4px; }
.csv-table input { width: 100%; border: 0; font-size: 12px; padding: 4px; }

@media (max-width: 900px) {
  .split { grid-template-columns: 1fr; grid-template-rows: 1fr 1fr; }
  .plan-grid { grid-template-columns: 1fr; }
  .before-panel { width: 100%; }
}
