/* Agent Workspace — mobile-first UI (AW-07)
   Vanilla CSS, no build step. Primary target: 390 CSS px.
   Class/id names are preserved for app.js compatibility. */

:root {
  color-scheme: dark;

  --bg: #0f1713;
  --surface: #17211c;
  --surface-2: #1d2822;
  --surface-3: #24312a;
  --surface-sunken: #131c17;

  --line: #2a3830;
  --line-strong: #38493f;
  --line-input: #33463b;

  --ink: #e7efe9;
  --ink-strong: #f3f8f2;
  --ink-muted: #9fb1a6;
  --ink-quiet: #7f9086;

  --accent: #c6e7af;
  --accent-hover: #d6f2c0;
  --accent-ink: #15241a;
  --brand: #c6e7af;
  --focus: #8ecb95;

  --danger-ink: #f1aaa2;
  --danger-line: #653733;
  --danger-bg: #2c1a18;

  --status-idle: #8aa093;
  --status-working: #8fce8a;
  --status-attention: #e0c072;
  --status-error: #e88f86;

  --notice-bg: #2c2a17;
  --notice-error-bg: #2c1a18;

  --radius: 14px;
  --radius-sm: 10px;
  --touch: 44px;
  --tabbar-h: 60px;

  --sans: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --serif: Georgia, "Times New Roman", serif;

  font-family: var(--sans);
  font-synthesis: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  -webkit-text-size-adjust: 100%;
  min-height: 100dvh;
}

/* ---------- Typography ---------- */
h1 { font: 600 24px/1.2 var(--serif); margin: 0; letter-spacing: -0.01em; }
h2 { font: 600 20px/1.25 var(--serif); margin: 0 0 12px; }
h3 { font: 600 15px/1.3 var(--sans); margin: 0 0 6px; }
p { line-height: 1.6; margin: 0 0 10px; }
p:last-child { margin-bottom: 0; }
a { color: var(--accent); }
small { font-size: 12px; }
.eyebrow { font-size: 10px; letter-spacing: 0.14em; text-transform: uppercase; color: var(--ink-quiet); margin: 0 0 4px; }
.muted { color: var(--ink-muted); font-size: 13px; line-height: 1.6; }
.empty { padding: 28px 4px; color: var(--ink-muted); }

/* ---------- Controls ---------- */
button, input, select, textarea { font: inherit; font-size: 16px; color: var(--ink); }
button {
  cursor: pointer;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  min-height: var(--touch);
  background: var(--surface-2);
  color: var(--ink);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
button:hover { background: var(--surface-3); }
button:disabled { opacity: 0.55; cursor: not-allowed; }
button[aria-busy="true"] { cursor: wait; }
button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, summary:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}
.primary { background: var(--accent); color: var(--accent-ink); border: 0; font-weight: 650; }
.primary:hover { background: var(--accent-hover); }
.danger { color: var(--danger-ink); border-color: var(--danger-line); background: var(--danger-bg); }
.danger:hover { background: #3a201d; }
.quiet {
  background: none; border: 0; min-height: 0; padding: 6px 4px;
  color: var(--accent); text-decoration: underline; text-underline-offset: 2px;
}
.quiet:hover { background: none; color: var(--accent-hover); }

input, select, textarea {
  border: 1px solid var(--line-input);
  border-radius: var(--radius-sm);
  padding: 11px 12px;
  min-height: var(--touch);
  background: var(--surface-2);
  color: var(--ink);
  max-width: 100%;
  width: 100%;
}
textarea { min-height: 84px; resize: vertical; line-height: 1.5; }
input::placeholder, textarea::placeholder { color: var(--ink-quiet); }
form label { display: grid; gap: 6px; font-size: 12px; font-weight: 600; color: var(--ink-muted); margin: 14px 0; }

/* ---------- Rows / grids ---------- */
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.between { justify-content: space-between; }
.grid { display: grid; grid-template-columns: 1fr; gap: 12px; }
.section-space { margin-top: 24px; }

/* ---------- Shell (mobile-first) ---------- */
.shell { min-height: 100dvh; }
.sidebar { display: contents; }
.sidebar > .brand,
.sidebar > #new-chat,
.sidebar > .workspace-label,
.sidebar > #conversation-list,
.sidebar > footer { display: none; }

.shell > main,
.shell main { display: flex; flex-direction: column; min-height: 100dvh; min-width: 0; }
header {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 16px; background: var(--bg); border-bottom: 1px solid var(--line);
}
header .titles { min-width: 0; }
.header-meta { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.build-badge, .badge {
  font-size: 11px; background: var(--surface-3); color: var(--ink-muted);
  padding: 4px 9px; border-radius: 20px; white-space: nowrap;
}
.conn-pill {
  font-size: 11px; padding: 4px 10px; border-radius: 20px; white-space: nowrap;
  border: 1px solid var(--line-strong); background: var(--surface-2); color: var(--ink-muted);
  display: inline-flex; align-items: center; gap: 6px; max-width: 52vw; overflow: hidden; text-overflow: ellipsis;
}
.conn-pill.is-ok { color: var(--status-working); border-color: #2f5c39; }
.conn-pill.is-warn { color: var(--status-attention); border-color: #5c4f2a; }
.conn-pill::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; flex: 0 0 auto; }

#content { flex: 1; min-width: 0; padding: 16px; padding-bottom: calc(16px + var(--tabbar-h) + env(safe-area-inset-bottom, 0px)); }

#notice { margin: 12px 16px 0; padding: 12px 14px; background: var(--notice-bg); border-radius: var(--radius-sm); font-size: 13px; border-left: 3px solid var(--status-attention); }
#notice.is-error { background: var(--notice-error-bg); border-left-color: var(--danger-ink); }

/* ---------- Bottom tab bar (mobile) ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
  display: flex; background: var(--surface);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
.tab {
  flex: 1; min-width: 0; min-height: var(--tabbar-h);
  border: 0; border-radius: 0; background: none; color: var(--ink-quiet);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 12px; font-weight: 600; position: relative;
}
.tab:hover { background: var(--surface-2); }
.tab.active { color: var(--accent); }
.tab.active::after { content: ""; position: absolute; top: 0; left: 20%; right: 20%; height: 2px; background: var(--accent); }
.tab-label { line-height: 1.1; }
.tab-badge {
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--status-error); color: #fff; font-size: 11px; font-weight: 700;
  display: inline-flex; align-items: center; justify-content: center;
  position: absolute; top: 8px; left: calc(50% + 14px);
}

/* ---------- Rooms landing ---------- */
.intro { margin: 4px 0 8px; }
.intro h2 { font-size: 22px; }
.quick-actions { display: grid; grid-template-columns: 1fr; gap: 10px; margin: 16px 0 4px; }
.quick-actions button { min-height: 48px; font-weight: 600; justify-content: flex-start; padding-left: 16px; }
.room-list { display: grid; gap: 10px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.room-card { display: grid; gap: 6px; text-align: left; }
.room-card { width: 100%; cursor: pointer; }
.room-card:hover { border-color: var(--line-strong); background: var(--surface-2); }
.room-card .room-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.room-card h3 { margin: 0; overflow-wrap: anywhere; }
.room-card .room-latest { color: var(--ink-muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin: 0; }
.room-card .room-parts { color: var(--ink-quiet); font-size: 12px; margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge { align-self: flex-start; }
.badge.unread { background: var(--accent); color: var(--accent-ink); font-weight: 700; }
.unread-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--accent); flex: 0 0 auto; }

.avatar { display: grid; place-items: center; width: 38px; height: 38px; border-radius: 12px; background: var(--surface-3); color: var(--accent); font: 600 18px var(--serif); }

/* ---------- Attention ---------- */
.attention-list { display: grid; gap: 14px; }
.attention-list h2 { overflow-wrap: anywhere; }
.attention-card { border-left: 3px solid var(--status-attention); }
.attention-permission { border-left-color: var(--status-attention); }
.attention-blocker, .attention-connection { border-left-color: var(--status-error); }
.attention-card details { margin: 8px 0; }
.attention-card pre { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 11px; background: var(--surface-sunken); padding: 10px; border-radius: 8px; }
.attention-group summary { cursor: pointer; font-weight: 600; padding: 10px 0; min-height: var(--touch); display: flex; align-items: center; gap: 8px; }
.attention-group .count { font-weight: 400; color: var(--ink-quiet); }

/* ---------- Room view ---------- */
.room-back { align-self: flex-start; margin-bottom: 8px; }
.room-layout { display: flex; flex-direction: column; flex: 1; min-height: 0; min-width: 0; }
.member-strip { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.member-strip .member-chip { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; border: 1px solid var(--line-strong); padding: 6px 10px; border-radius: 20px; background: var(--surface-2); }
.member-strip .member-chip.is-off { border-color: var(--danger-line); }
.member-strip .member-chip .quiet { margin: 0; font-size: 12px; padding: 0; }
.member-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--status-idle); }
.member-dot.on { background: var(--status-working); }
.member-dot.off { background: var(--status-error); }
.saved { font-size: 11px; color: var(--ink-quiet); margin: 0 0 8px; }

.wall-wrap { position: relative; flex: 1; min-height: 0; display: flex; }
.wall, .timeline { flex: 1; min-height: 0; overflow: auto; overscroll-behavior: contain; display: flex; flex-direction: column; gap: 12px; padding: 4px 2px 12px; }
.message, .bubble { background: var(--surface); border: 1px solid var(--line); border-left: 3px solid var(--line-strong); border-radius: 12px; padding: 14px; max-width: min(760px, 100%); overflow-wrap: anywhere; }
.message > strong { color: var(--ink-strong); }
.message small { font-size: 11px; color: var(--ink-quiet); }
.bubble-body { white-space: pre-wrap; margin: 8px 0; }
.bubble-human { border-left-color: var(--focus); }
.bubble-api { border-left-color: #6ba3cf; }
.bubble-agent { border-left-color: var(--status-attention); }
.bubble-system { border-left-color: var(--status-idle); background: var(--surface-sunken); }
.task { border-left: 3px solid var(--status-attention); padding: 8px 12px; margin: 10px 0 0; background: var(--surface-sunken); font-size: 12px; border-radius: 0 8px 8px 0; }
.task button { min-height: 34px; padding: 6px 10px; font-size: 12px; margin-top: 6px; }
.task pre { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 11px; max-width: 100%; }
.task details { margin: 8px 0; }
.task summary { cursor: pointer; font-weight: 600; }

.new-msg-indicator {
  position: absolute; left: 50%; transform: translateX(-50%); bottom: 12px;
  background: var(--accent); color: var(--accent-ink); border: 0; font-weight: 650;
  border-radius: 20px; padding: 8px 16px; min-height: 36px; box-shadow: 0 6px 18px #0008; z-index: 5;
}

/* ---------- Composer ---------- */
.composer {
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
  padding: 12px; margin-top: 8px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
}
.composer textarea { border: 1px solid var(--line-input); background: var(--surface-2); min-height: 60px; width: 100%; }
.composer .row { margin-top: 8px; }
.composer select { width: 100%; }
.composer-actions { align-items: stretch; flex-direction: column; }
.composer-actions .row { width: 100%; }
.mention-chips, .mention-bar, .reply-chip { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.mention-bar button { min-height: 34px; padding: 6px 10px; font-size: 13px; }
.chip, .reply-chip span { font-size: 12px; border: 1px solid var(--line-strong); border-radius: 20px; padding: 4px 10px; background: var(--surface-2); }

/* ---------- New-room picker ---------- */
.picker { border: 1px solid var(--line); border-radius: 12px; padding: 12px; margin: 10px 0; }
.picker-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.picker-count { font-size: 12px; color: var(--ink-muted); }
.picker-count.is-over { color: var(--status-error); }
.picker-list { display: grid; gap: 6px; max-height: 40vh; overflow: auto; }
.picker-option { display: flex; align-items: flex-start; gap: 10px; padding: 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); font-weight: 500; margin: 0; cursor: pointer; }
.picker-option input { width: 20px; height: 20px; min-height: 0; flex: 0 0 auto; margin-top: 2px; accent-color: var(--accent); }
.picker-option .who { display: grid; gap: 2px; }
.picker-option .who .role { font-size: 12px; color: var(--ink-quiet); font-weight: 400; }
.picker-empty { padding: 12px; border: 1px dashed var(--line-strong); border-radius: 10px; text-align: center; }
.divider { border: 0; border-top: 1px solid var(--line); margin: 18px 0 6px; }

/* ---------- Settings / keys ---------- */
.settings { display: grid; gap: 8px; }
.key-row { margin-bottom: 12px; }
.key-row.revoked h3, .key-row.revoked .key-prefix { text-decoration: line-through; opacity: 0.55; }
.key-prefix, .key-secret { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; }
.key-secret { display: block; background: var(--surface-sunken); color: var(--accent); border-radius: 9px; padding: 14px; margin: 14px 0; overflow-wrap: anywhere; user-select: all; }
.key-row .row { gap: 6px; margin: 8px 0; }
.scope-option { display: flex; flex-direction: row; align-items: center; gap: 10px; font-weight: 400; margin: 8px 0; min-height: var(--touch); color: var(--ink); }
.scope-option input { width: 20px; height: 20px; min-height: 0; accent-color: var(--accent); }

/* ---------- Team editor ---------- */
.editor-member { border: 1px solid var(--line); border-radius: 12px; padding: 14px; margin: 14px 0; }
.editor-member .grid { grid-template-columns: 1fr; gap: 10px; }
.route-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.field-error { color: var(--danger-ink); font-size: 0.85em; margin: 4px 0 0; }
label.invalid select { border-color: var(--danger-line); outline-color: var(--danger-line); }

/* ---------- Sidebar conversation list (used on desktop) ---------- */
.status-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--status-idle); margin-right: 6px; }

/* ---------- Dialog ---------- */
dialog {
  border: 1px solid var(--line); border-radius: 16px; padding: 20px;
  max-width: 640px; width: calc(100% - 24px); max-height: 88dvh;
  background: var(--surface); color: var(--ink);
}
dialog::backdrop { background: #05100b99; }
#dialog-body form { margin: 0; }
#close-dialog { margin-top: 14px; }

/* ---------- Login (bare <main>, no .shell) ---------- */
body > main { max-width: 420px; margin: 0 auto; padding: 40px 20px; }
body > main h1 { font-size: 30px; margin-bottom: 8px; }
#auth { display: grid; gap: 4px; }
#error { color: var(--danger-ink); }

/* ---------- Room-open (mobile chat mode) ---------- */
.room-open .tabbar { display: none; }
.room-open #content { overflow: hidden; padding-bottom: 16px; }

/* ==========================================================================
   Desktop / wide screens: sidebar layout
   ========================================================================== */
@media (min-width: 761px) {
  .shell {
    display: grid;
    grid-template-columns: 264px 1fr;
    grid-template-rows: 1fr;
  }
  .sidebar {
    display: flex; flex-direction: column; gap: 16px;
    background: var(--surface); border-right: 1px solid var(--line);
    padding: 22px 18px; height: 100dvh; position: sticky; top: 0; overflow: auto;
  }
  .sidebar > .brand,
  .sidebar > #new-chat,
  .sidebar > .workspace-label,
  .sidebar > #conversation-list,
  .sidebar > footer { display: revert; }
  .brand { display: flex; gap: 10px; align-items: center; color: var(--brand); font: 700 26px var(--serif); text-decoration: none; }
  .brand span { font: 600 14px var(--sans); color: var(--ink); }
  .workspace-label { font-size: 10px; letter-spacing: 0.16em; text-transform: uppercase; color: var(--ink-quiet); }

  .tabbar {
    position: static; display: grid; gap: 4px; border-top: 0; padding: 0; background: none;
  }
  .tab {
    flex-direction: row; justify-content: flex-start; min-height: var(--touch);
    border-radius: var(--radius-sm); padding: 10px 14px; color: var(--ink-muted); font-weight: 600;
  }
  .tab:hover { background: var(--surface-2); }
  .tab.active { background: var(--surface-3); color: var(--ink-strong); }
  .tab.active::after { display: none; }
  .tab-badge { position: static; margin-left: auto; }

  #conversation-list { display: grid; gap: 6px; overflow: auto; }
  #conversation-list button { background: none; border: 0; color: var(--ink-muted); text-align: left; min-height: 0; padding: 8px 10px; border-radius: 8px; display: grid; gap: 2px; align-items: start; }
  #conversation-list button:hover { background: var(--surface-2); color: var(--ink); }
  #conversation-list button.active { background: var(--surface-3); color: var(--ink); }
  #conversation-list button.unread { font-weight: 700; color: var(--ink); }
  #conversation-list .room-preview { display: block; font-size: 11px; color: var(--ink-quiet); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  footer { margin-top: auto; font-size: 12px; line-height: 1.7; color: var(--ink-muted); }
  footer small { font-size: 10px; color: var(--ink-quiet); }

  .shell main { min-height: 100dvh; }
  #content { padding: 28px clamp(20px, 4vw, 56px); padding-bottom: 28px; max-width: 1200px; }
  header { padding: 20px clamp(20px, 4vw, 56px); }
  h1 { font-size: 30px; }
  .conn-pill { max-width: 320px; }

  .room-back { display: none; }
  .room-open .tabbar { display: grid; }
  .room-open #content { overflow: visible; }
  .grid { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
  .quick-actions { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
  .room-layout { min-height: calc(100dvh - 220px); }
  .editor-member .grid { grid-template-columns: 1fr 1fr; }
  .composer-actions { flex-direction: row; align-items: flex-end; justify-content: space-between; }
  .composer-actions .row { width: auto; }
  .composer { padding-bottom: 12px; }
}

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; animation: none !important; transition: none !important; }
}
