/* AFM Remote — one stylesheet, light + dark via tokens. */

:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #f0f2f7;
  --text: #161a23;
  --muted: #5d6475;
  --faint: #8a90a0;
  --border: #e2e5ee;
  --accent: #5b6cff;
  --accent-2: #8a4dff;
  --accent-ink: #ffffff;
  --accent-soft: rgba(91, 108, 255, 0.12);
  --ok: #19a463;
  --warn: #e8840c;
  --warn-soft: rgba(232, 132, 12, 0.10);
  --danger: #e0473e;
  --shadow: 0 1px 2px rgba(20, 24, 40, 0.05), 0 6px 24px rgba(20, 24, 40, 0.06);
  --radius: 16px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
  --topbar: 56px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1117;
    --surface: #171a22;
    --surface-2: #1f232d;
    --text: #e9ecf2;
    --muted: #a1a8b8;
    --faint: #737a8c;
    --border: #2a2f3b;
    --accent: #7383ff;
    --accent-soft: rgba(115, 131, 255, 0.16);
    --warn-soft: rgba(232, 132, 12, 0.14);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 28px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}
h1 { font-size: 26px; line-height: 1.2; margin: 0 0 6px; letter-spacing: -0.02em; }
h2 { font-size: 17px; margin: 0 0 4px; }
p { margin: 0 0 10px; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.hint { color: var(--faint); font-size: 13px; }
.center { text-align: center; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  height: var(--topbar);
  display: flex; align-items: center; gap: 10px;
  padding: 0 max(16px, env(safe-area-inset-left)) 0 max(16px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: saturate(160%) blur(14px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 9px; text-decoration: none; color: var(--text); font-weight: 650; font-size: 16px; white-space: nowrap; flex: none; }
.brand-dim { color: var(--muted); font-weight: 500; }
.relay-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); margin-left: 2px; transition: background .3s; }
body[data-relay="open"] .relay-dot { background: var(--ok); }
body[data-relay="connecting"] .relay-dot { background: var(--warn); animation: pulse 1.2s infinite; }
.device-chip {
  margin-left: auto; font-size: 13px; color: var(--muted);
  border: 1px solid var(--border); border-radius: 999px; padding: 3px 10px;
  max-width: 50vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* ---------- layout ---------- */
.view {
  max-width: 1120px; margin: 0 auto;
  padding: 24px max(16px, env(safe-area-inset-right)) 48px max(16px, env(safe-area-inset-left));
}
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 22px; margin: 0 0 18px;
}
.card.narrow, .narrow { max-width: 480px; margin-left: auto; margin-right: auto; }
.card.subtle { box-shadow: none; background: transparent; }
.stack { display: flex; flex-direction: column; gap: 8px; }
.row { display: flex; gap: 10px; align-items: center; }
.row.end { justify-content: flex-end; flex-wrap: wrap; }
.row.center { justify-content: center; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 4px 0 16px; flex-wrap: wrap; }
.page-head h1 { margin: 0; }

.hero { text-align: center; margin: 28px auto 22px; max-width: 480px; }
.hero.compact { margin-top: 16px; }
.hero-mark { display: inline-flex; margin-bottom: 10px; filter: drop-shadow(0 8px 18px rgba(91, 108, 255, .35)); }
.logo-bg { fill: var(--accent); }
.logo-link { stroke: #fff; stroke-opacity: .7; stroke-width: 3; stroke-linecap: round; }
.logo-hub { fill: #fff; }
.logo-node { fill: #fff; fill-opacity: .9; }

/* ---------- controls ---------- */
.label { font-weight: 600; font-size: 14px; }
.input {
  width: 100%; font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; outline: none; transition: border-color .15s, box-shadow .15s;
}
.input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.code-input { font-family: var(--mono); letter-spacing: .08em; text-transform: uppercase; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 600; font-size: 14px;
  border-radius: 10px; padding: 9px 16px; cursor: pointer; text-decoration: none; white-space: nowrap;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  transition: transform .06s, background .15s, opacity .15s;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: var(--accent-ink); border-color: transparent; }
.btn.primary.warn { background: var(--warn); }
.btn.ghost { background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { background: var(--danger); color: #fff; border-color: transparent; }
.btn.danger-ghost { background: transparent; color: var(--danger); border-color: color-mix(in srgb, var(--danger) 35%, transparent); }
.btn.small { padding: 5px 11px; font-size: 13px; }
.link { background: none; border: 0; padding: 0; font: inherit; color: var(--accent); cursor: pointer; text-decoration: none; }

.spinner {
  display: inline-block; width: 14px; height: 14px; border-radius: 50%;
  border: 2px solid currentColor; border-right-color: transparent; opacity: .7;
  animation: spin .7s linear infinite; vertical-align: -2px;
}
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes pulse { 50% { opacity: .35; } }
.loading { color: var(--muted); padding: 18px 4px; }

/* ---------- setup ---------- */
.options { display: grid; gap: 12px; }
.option {
  display: flex; gap: 14px; align-items: flex-start; text-align: left;
  font: inherit; color: var(--text); cursor: pointer;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow); transition: border-color .15s, transform .1s;
}
.option:hover { border-color: var(--accent); }
.option:active { transform: translateY(1px); }
.option-icon {
  flex: none; width: 38px; height: 38px; border-radius: 11px; display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent); font-size: 18px;
}
.option-text { display: flex; flex-direction: column; gap: 3px; }
.pair-code {
  font-family: var(--mono); font-size: clamp(30px, 8vw, 42px); font-weight: 700; letter-spacing: .12em;
  text-align: center; padding: 18px 8px; margin: 16px 0 6px;
  background: var(--surface-2); border-radius: 14px; user-select: all;
}
.pair-code.expired { opacity: .35; text-decoration: line-through; }

/* ---------- home ---------- */
.instances { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 14px; margin-bottom: 10px; }
.instance {
  display: flex; flex-direction: column; gap: 4px; text-decoration: none; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow); transition: transform .12s, border-color .15s;
}
.instance:hover { transform: translateY(-2px); border-color: var(--accent); }
.instance-top { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.instance-name { font-weight: 650; font-size: 17px; }
.instance-meta { color: var(--muted); font-size: 13px; }
.instance:not(.online) .mac-glyph { opacity: .45; }
.mac-glyph { display: inline-flex; flex-direction: column; align-items: center; }
.mac-screen { width: 38px; height: 26px; border-radius: 4px; border: 2.5px solid var(--text); background: linear-gradient(135deg, var(--accent-soft), transparent); }
.mac-foot { width: 16px; height: 4px; margin-top: 2px; border-radius: 0 0 4px 4px; background: var(--text); }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--faint); flex: none; }
.dot.on { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 22%, transparent); }
.empty {
  display: flex; flex-direction: column; gap: 6px; align-items: flex-start;
  grid-column: 1 / -1; padding: 22px; color: var(--text);
  background: var(--surface); border: 1px dashed var(--border); border-radius: var(--radius);
}
.empty.soft { border: 0; background: transparent; color: var(--muted); align-items: center; padding: 40px 0; }
.add-form { display: grid; grid-template-columns: 180px 1fr auto; gap: 10px; }
@media (max-width: 640px) { .add-form { grid-template-columns: 1fr; } }

/* ---------- devices ---------- */
.device-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 18px; }
.device-row {
  display: flex; align-items: center; gap: 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 12px 14px;
}
.device-main { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.device-name { font-weight: 600; }
.device-meta { color: var(--muted); font-size: 13px; }
.device-actions { display: flex; gap: 6px; flex: none; }
.chip { display: inline-block; margin-left: 8px; font-size: 11px; font-weight: 600; color: var(--accent); background: var(--accent-soft); border-radius: 999px; padding: 1px 8px; }

/* ---------- instance workspace ---------- */
.inst-head { display: flex; align-items: center; gap: 12px; margin: 0 0 14px; min-width: 0; }
.inst-head .link { white-space: nowrap; flex: none; }
.inst-head .pill { flex: none; }
.inst-title {
  font-size: 20px; font-weight: 700; letter-spacing: -0.01em;
  min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.pill { font-size: 12px; font-weight: 600; border-radius: 999px; padding: 2px 10px; background: var(--surface-2); color: var(--muted); }
.pill.ok { color: var(--ok); background: color-mix(in srgb, var(--ok) 14%, transparent); }
.pill.off { color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, transparent); }
.project-select { display: none; margin-bottom: 12px; }
.workspace {
  display: grid; grid-template-columns: 250px 1fr; gap: 16px;
  height: calc(100vh - var(--topbar) - 110px); min-height: 420px;
}
.projects { display: flex; flex-direction: column; gap: 4px; overflow: auto; }
.project {
  display: flex; flex-direction: column; text-decoration: none; color: var(--text);
  border-radius: 12px; padding: 10px 12px; border: 1px solid transparent;
}
.project:hover { background: var(--surface); border-color: var(--border); }
.project.active { background: var(--surface); border-color: var(--accent); box-shadow: var(--shadow); }
.project-name { font-weight: 600; }
.project-meta { color: var(--muted); font-size: 12.5px; }
.chat { min-width: 0; min-height: 0; display: flex; }
.chat > .empty { align-self: flex-start; }
.chat-inner {
  flex: 1; display: flex; flex-direction: column; min-height: 0;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  overflow: hidden;
}
.chat-head { display: flex; justify-content: space-between; gap: 16px; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.chat-title { font-weight: 700; }
.chat-sub { color: var(--muted); font-size: 13px; }
.chat-summary { color: var(--muted); font-size: 12.5px; max-width: 45%; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.offline-banner { background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); font-size: 13px; padding: 8px 18px; }
.scroller { flex: 1; overflow-y: auto; padding: 18px; scroll-behavior: smooth; }
.messages { display: flex; flex-direction: column; gap: 10px; }
.msg { display: flex; }
.msg.human { justify-content: flex-end; }
.bubble { max-width: min(78%, 720px); padding: 10px 14px; border-radius: 16px; overflow-wrap: anywhere; }
.msg.human .bubble { background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: var(--accent-ink); border-bottom-right-radius: 6px; white-space: pre-wrap; }
.msg.agent .bubble { background: var(--surface-2); border-bottom-left-radius: 6px; max-width: 100%; }
.bubble.error { background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--text); }
.working { color: var(--muted); font-size: 13px; display: flex; gap: 6px; align-items: center; padding-left: 4px; }

.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 8px; }
.md h3, .md h4, .md h5, .md h6 { margin: 10px 0 6px; font-size: 15.5px; }
.md ul, .md ol { margin: 4px 0 8px; padding-left: 22px; }
.md code { font-family: var(--mono); font-size: 13px; background: color-mix(in srgb, var(--text) 8%, transparent); padding: 1px 5px; border-radius: 5px; }
.md pre { background: #0f1117; color: #e9ecf2; border-radius: 10px; padding: 12px 14px; overflow-x: auto; margin: 6px 0 10px; }
.md pre code { background: none; padding: 0; color: inherit; }
.md blockquote { margin: 6px 0; padding-left: 12px; border-left: 3px solid var(--border); color: var(--muted); }
.md-table { overflow-x: auto; margin: 6px 0 10px; }
.md table { border-collapse: collapse; font-size: 13.5px; }
.md th, .md td { border: 1px solid var(--border); padding: 5px 10px; text-align: left; }
.md th { background: color-mix(in srgb, var(--text) 5%, transparent); }

/* approval cards — the remote twin of the Mac app's inline cards */
.cards { display: flex; flex-direction: column; gap: 12px; margin-top: 12px; }
.approval {
  border: 1px solid var(--border); border-left: 4px solid var(--accent);
  background: var(--accent-soft); border-radius: 14px; padding: 14px 16px;
}
.approval.run { border-left-color: var(--warn); background: var(--warn-soft); }
.approval-head { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; }
.approval-icon { font-size: 16px; }
.approval.run .approval-icon { color: var(--warn); }
.approval.ws .approval-icon { color: var(--accent); }
.approval-code {
  font-family: var(--mono); font-size: 12.5px; white-space: pre-wrap; overflow-wrap: anywhere;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; margin: 6px 0 8px;
}

.composer { display: flex; gap: 10px; align-items: flex-end; padding: 12px; border-top: 1px solid var(--border); }
.composer-input {
  flex: 1; resize: none; font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px; outline: none; max-height: 220px;
}
.composer-input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.send { flex: none; }

@media (max-width: 760px) {
  .workspace { grid-template-columns: 1fr; height: calc(100dvh - var(--topbar) - 150px); }
  .projects { display: none; }
  .project-select { display: block; }
  .chat-summary { display: none; }
  .bubble { max-width: 88%; }
  .view { padding-top: 16px; }
  .inst-title { font-size: 17px; }
}

/* ---------- toasts ---------- */
.toasts { position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 50; width: min(92vw, 420px); }
.toast { background: var(--text); color: var(--bg); border-radius: 12px; padding: 10px 14px; font-size: 14px; box-shadow: var(--shadow); transition: opacity .4s, transform .4s; }
.toast-ok { background: var(--ok); color: #fff; }
.toast-error { background: var(--danger); color: #fff; }
.toast.leaving { opacity: 0; transform: translateY(8px); }
.noscript { text-align: center; padding: 40px; }
