:root {
  --bg: #0f1115; --bg1: #16191f; --bg2: #1d212a; --border: #262b36;
  --text: #e6e8ee; --dim: #8b93a3; --accent: #4f8cff; --accent-dim: #2f5fc0;
  --danger: #ff5c5c; --ok: #35c76a; --warn: #f2b03f; --radius: 8px;
  --mono: ui-monospace, "SF Mono", "Cascadia Code", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; display: flex; flex-direction: column; background: var(--bg); color: var(--text);
  font-family: var(--sans); font-size: 14px; line-height: 1.4; }

/* header */
.app-header { display: flex; align-items: center; justify-content: space-between; padding: 10px 16px;
  background: var(--bg1); border-bottom: 1px solid var(--border); flex: 0 0 auto; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { font-size: 16px; font-weight: 600; margin: 0; }
.brand-mark { width: 18px; height: 18px; border-radius: 5px; background: linear-gradient(135deg, var(--accent), #7c5cff); }

/* buttons */
.btn { appearance: none; border: 1px solid var(--border); background: var(--bg2); color: var(--text);
  padding: 7px 14px; border-radius: var(--radius); font-size: 13px; font-weight: 500; cursor: pointer; }
.btn:hover { background: #262c38; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent-dim); color: #fff; }
.btn-primary:hover { background: var(--accent-dim); }
.btn-danger { color: var(--danger); border-color: #4a2730; }
.btn-danger:hover { background: #2a1a20; }
.btn-warn { color: var(--warn); border-color: #4a3b1e; }
.btn-warn:hover { background: #2a2416; }

/* status line (toast) */
.status-line { flex: 0 0 auto; padding: 8px 16px; font-size: 13px; border-bottom: 1px solid var(--border);
  background: var(--bg1); }
.status-line.error { color: var(--danger); background: #241519; }
.status-line.info { color: var(--text); }

/* layout */
.app-main { flex: 1 1 auto; display: flex; min-height: 0; }
.sidebar { flex: 0 0 260px; display: flex; flex-direction: column; border-right: 1px solid var(--border);
  background: var(--bg1); min-height: 0; }
.sidebar-head { display: flex; justify-content: space-between; padding: 12px 14px 8px; font-size: 11px;
  font-weight: 600; text-transform: uppercase; letter-spacing: 0.8px; color: var(--dim); }
.session-list { list-style: none; margin: 0; padding: 0 8px; overflow-y: auto; min-height: 0; }
.session-item { padding: 9px 10px; border-radius: var(--radius); cursor: pointer; border: 1px solid transparent; margin-bottom: 4px; }
.session-item:hover { background: var(--bg2); }
.session-item.selected { background: var(--bg2); border-color: var(--accent-dim); }
.session-item-head { display: flex; align-items: baseline; gap: 8px; }
.session-name { font-weight: 600; }
.session-type { font-size: 10px; color: var(--dim); border: 1px solid var(--border); border-radius: 4px; padding: 1px 5px; text-transform: capitalize; }
.session-item-meta { display: flex; align-items: center; gap: 8px; margin-top: 5px; }
.versions { font-family: var(--mono); font-size: 11px; color: var(--ok); }
.empty { padding: 12px 14px; color: var(--dim); font-size: 13px; }
.muted { color: var(--dim); }

/* status pill */
.status-pill { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--dim); }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--dim); }
.status-pill[data-status="building"] .status-dot,
.status-pill[data-status="dev_deploying"] .status-dot,
.status-pill[data-status="publishing"] .status-dot { background: var(--warn); }
.status-pill[data-status="dev_running"] .status-dot,
.status-pill[data-status="published"] .status-dot { background: var(--ok); }
.status-pill[data-status="stopped"] .status-dot { background: var(--danger); }

/* per-app build routes */
.session-item-routes { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 12px 10px; }
.route-link { font-size: 11px; font-weight: 600; color: var(--accent); text-decoration: none;
  border: 1px solid var(--border); border-radius: 5px; padding: 2px 8px; }
.route-link:hover { border-color: var(--accent); background: var(--bg2); }
.route-link.route-app { color: var(--ok); }
.route-link.route-app:hover { border-color: var(--ok); }

/* workspace */
.workspace { flex: 1 1 auto; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.workspace-empty { flex: 1; display: flex; align-items: center; justify-content: center; color: var(--dim); font-size: 15px; }
.workspace-content { flex: 1 1 auto; display: flex; flex-direction: column; min-height: 0; }
.session-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 16px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.session-id { display: flex; align-items: center; gap: 12px; }
.slug { font-size: 15px; font-weight: 600; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* magic line */
.magic-line { padding: 8px 16px; font-size: 13px; border-bottom: 1px solid var(--border); background: var(--bg1);
  color: var(--dim); word-break: break-word; }
.magic-label { color: var(--text); font-weight: 600; }
.magic-url { color: var(--accent); }
.magic-code { appearance: none; font-family: var(--mono); font-size: 12px; color: var(--ok); background: var(--bg2);
  border: 1px dashed #2d4a35; border-radius: 5px; padding: 2px 8px; cursor: pointer; }
.magic-code:hover { background: #22311f; }

/* tabs + frames */
.tabs { display: flex; gap: 4px; padding: 0 16px; border-bottom: 1px solid var(--border); flex: 0 0 auto; }
.tab { appearance: none; background: none; border: none; color: var(--dim); padding: 11px 14px; font-size: 13px;
  font-weight: 500; cursor: pointer; border-bottom: 2px solid transparent; }
.tab:hover { color: var(--text); }
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.tab-badge { display: inline-flex; align-items: center; justify-content: center; min-width: 16px; height: 16px;
  margin-left: 6px; padding: 0 5px; border-radius: 8px; background: var(--bg2); color: var(--dim);
  font-size: 10px; font-weight: 700; }
.tab-badge-failed { background: var(--danger); color: #fff; }
.tab-panels { flex: 1 1 auto; display: flex; min-height: 0; }
.tab-panel { display: none; flex: 1 1 auto; min-width: 0; min-height: 0; }
.tab-panel.active { display: flex; }
.frame { flex: 1 1 auto; width: 100%; border: none; background: #fff; }
.frame-placeholder { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 4px; text-align: center; padding: 24px; color: var(--dim); }

/* build step audit trail (LLD §4.3.1) */
.steps-summary { flex: 0 0 auto; padding: 6px 16px; font-size: 12px; border-bottom: 1px solid var(--border);
  background: var(--bg1); }
.steps-summary-ok { color: var(--ok); }
.steps-summary-failed { color: var(--danger); }
.steps-list { list-style: none; margin: 0; padding: 10px 16px; overflow-y: auto; min-height: 0; flex: 1 1 auto; }
.step-row { padding: 8px 10px; border: 1px solid var(--border); border-left-width: 3px; border-radius: var(--radius);
  margin-bottom: 6px; background: var(--bg1); }
.step-row-head { display: flex; align-items: center; gap: 8px; }
.step-status { flex: 0 0 auto; text-transform: uppercase; font-size: 9px; font-weight: 700; letter-spacing: 0.6px; }
.step-name { flex: 1 1 auto; font-family: var(--mono); font-size: 12px; word-break: break-all; }
.step-meta { flex: 0 0 auto; font-family: var(--mono); font-size: 10px; }
.step-detail { margin-top: 4px; font-size: 12px; color: var(--dim); white-space: pre-wrap; word-break: break-word; }
.step-ok { border-left-color: var(--ok); }
.step-ok .step-status { color: var(--ok); }
.step-failed { border-left-color: var(--danger); }
.step-failed .step-status { color: var(--danger); }
.step-failed .step-detail { color: var(--danger); }
.step-started { border-left-color: var(--warn); }
.step-started .step-status { color: var(--warn); }

/* preview flock/member selector (compact) */
.preview-toolbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 6px 16px; border-bottom: 1px solid var(--border); background: var(--bg1); }
.preview-field { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; color: var(--dim);
  white-space: nowrap; }
.preview-field-label { font-weight: 600; text-transform: uppercase; font-size: 10px; letter-spacing: 0.6px; }
.preview-select { background: var(--bg); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; padding: 3px 6px; font-size: 12px; max-width: 180px; }
.preview-details { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--dim);
  flex: 1 1 auto; min-width: 0; }
.preview-details-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.access-error { color: #ef4444; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.preview-toolbar .btn { flex: 0 0 auto; padding: 4px 10px; font-size: 12px; }
.btn-compact { padding: 4px 10px; font-size: 12px; }

/* browser-style preview address bar */
.address-bar { display: flex; align-items: center; gap: 6px; padding: 6px 16px;
  border-bottom: 1px solid var(--border); background: var(--bg1); }
.address-icon { flex: 0 0 auto; width: 14px; height: 14px; border-radius: 50%; background: var(--ok);
  position: relative; }
.address-icon::after { content: ''; position: absolute; left: 4px; top: 8px; width: 7px; height: 2px;
  background: var(--ok); border-radius: 0 0 2px 2px; }
.address-input { flex: 1 1 auto; min-width: 0; background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 14px; padding: 4px 12px; font-size: 12px;
  font-family: var(--mono); }
.address-input:focus { outline: none; border-color: var(--accent); }
.address-btn { appearance: none; border: 1px solid var(--border); background: var(--bg2); color: var(--text);
  border-radius: 6px; padding: 3px 9px; font-size: 12px; cursor: pointer; text-decoration: none;
  white-space: nowrap; }
.address-btn:hover { background: #262c38; }
.address-link { color: var(--accent); font-weight: 600; }

/* device switcher */
.device-switcher { display: inline-flex; gap: 2px; background: var(--bg); border: 1px solid var(--border);
  border-radius: 8px; padding: 2px; margin: 0 4px; }
.device-btn { appearance: none; display: inline-flex; align-items: center; justify-content: center;
  width: 30px; height: 26px; border: none; border-radius: 6px; background: transparent;
  color: var(--dim); cursor: pointer; transition: background 0.15s, color 0.15s; }
.device-btn:hover { color: var(--text); background: var(--bg2); }
.device-btn.active { color: var(--accent); background: var(--bg2); }

/* preview panel: override tab-panel row default to column */
#panel-preview { flex-direction: column; }

/* preview viewport container */
.preview-viewport { flex: 1 1 auto; display: flex; background: var(--bg2); min-height: 0;
  overflow: auto; justify-content: center; padding: 16px; }
.preview-viewport--mobile { padding: 16px 0; }
.preview-viewport--tablet { padding: 16px 0; }
.preview-viewport--desktop { padding: 0; }

.preview-frame { border: none; background: #fff; transition: width 0.25s ease, max-width 0.25s ease;
  box-shadow: 0 0 0 1px var(--border); }
.preview-viewport--desktop .preview-frame { width: 100%; height: 100%; box-shadow: none; }
.preview-viewport--tablet .preview-frame { width: 768px; max-width: 100%; height: 100%; border-radius: 8px; }
.preview-viewport--mobile .preview-frame { width: 375px; max-width: 100%; height: 100%;
  border-radius: 20px; box-shadow: 0 0 0 4px #2a2f3a, 0 0 0 6px var(--border); }

/* modal */
.modal-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.55); display: flex; align-items: center;
  justify-content: center; padding: 16px; z-index: 50; }
.modal { width: 100%; max-width: 460px; background: var(--bg1); border: 1px solid var(--border); border-radius: 12px;
  padding: 20px; display: flex; flex-direction: column; gap: 6px; max-height: 90vh; overflow-y: auto; }
.modal h2 { margin: 0 0 10px; font-size: 17px; }
.modal label { font-size: 12px; font-weight: 600; color: var(--dim); margin-top: 8px; }
.modal input, .modal select, .modal textarea { background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius); padding: 9px 11px; font-size: 14px; resize: vertical; }
.modal input:focus, .modal select:focus, .modal textarea:focus { outline: none; border-color: var(--accent); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

/* mobile */
@media (max-width: 720px) {
  .app-main { flex-direction: column; }
  .sidebar { flex: 0 0 auto; border-right: none; border-bottom: 1px solid var(--border); max-height: 38vh; }
  .session-toolbar { flex-direction: column; align-items: stretch; }
  .actions .btn { flex: 1 1 auto; }
}

/* chat mode: ?chat={session_id} — minimal chat-only UI, no sidebar/tabs */
.chat-mode .sidebar { display: none; }
.chat-mode .tabs { display: none; }
.chat-mode .tab-panels { display: block; }
.chat-mode .tab-panel { display: none; }
.chat-mode .tab-panel.active { display: flex; }
.chat-mode #btn-new { display: none; }
.chat-mode #magic-line { display: none; }
.chat-mode .app-main { display: block; }
.chat-mode .workspace { width: 100%; }

/* The `hidden` attribute must always win over element display rules
 * (e.g. .modal-backdrop/display:flex) so toggling with el.hidden works. */
[hidden] { display: none !important; }
