:root {
  --bg: #0f1115;
  --bg2: #161a22;
  --bg3: #1c2230;
  --border: #2a3344;
  --text: #e8ecf4;
  --muted: #8b95a8;
  --accent: #6ea8fe;
  --accent2: #5b8def;
  --user: #243554;
  --bot: #1a2030;
  --danger: #ff6b7a;
  --ok: #3dd68c;
  --radius: 14px;
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

/* Auth */
.auth-view {
  min-height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background:
    radial-gradient(ellipse at 30% 20%, #1a2740 0%, transparent 50%),
    radial-gradient(ellipse at 70% 80%, #1a2238 0%, transparent 45%),
    var(--bg);
}
.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 36px 32px;
  text-align: center;
  box-shadow: 0 24px 64px rgba(0,0,0,.45);
}
.logo {
  width: 56px; height: 56px; margin: 0 auto 16px;
  border-radius: 16px;
  background: linear-gradient(135deg, var(--accent), #8b7cf7);
  display: grid; place-items: center;
  font-weight: 700; font-size: 1.4rem; color: #0b1020;
}
.auth-card h1 { margin: 0 0 8px; font-size: 1.35rem; font-weight: 600; }
.sub { color: var(--muted); margin: 0 0 24px; font-size: .95rem; line-height: 1.45; }
#pin-form { display: flex; flex-direction: column; gap: 12px; }
#pin-input {
  font-size: 1.75rem;
  letter-spacing: .4em;
  text-align: center;
  padding: 14px 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg3);
  color: var(--text);
  outline: none;
}
#pin-input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(110,168,254,.2); }
#pin-submit, #send-btn, .accent-btn {
  background: var(--accent2);
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 12px 18px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}
#pin-submit:hover, #send-btn:hover, .accent-btn:hover { filter: brightness(1.08); }
#pin-submit:disabled, #send-btn:disabled, .accent-btn:disabled { opacity: .5; cursor: not-allowed; }
.error { color: var(--danger); min-height: 1.4em; margin: 12px 0 0; font-size: .9rem; }

/* Layout */
.chat-view {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: 100vh;
  height: 100dvh;
}
.sidebar {
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px 12px;
  gap: 10px;
  min-height: 0;
  overflow: hidden;
}
.sidebar-head {
  display: flex; align-items: center; gap: 12px;
  padding: 8px 8px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.sidebar-head .mark {
  width: 40px; height: 40px; border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), #8b7cf7);
  display: grid; place-items: center;
  font-weight: 700; color: #0b1020;
}
.sidebar-head strong { display: block; font-size: .95rem; }
.sidebar-head small { color: var(--muted); font-size: .75rem; }

.mode-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 6px;
  flex-shrink: 0;
}
.mode-tab {
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 10px;
  padding: 8px;
  cursor: pointer;
  font-weight: 600;
  font-size: .85rem;
}
.mode-tab.active {
  color: var(--text);
  border-color: var(--accent);
  background: #1e2a40;
}

.sidebar-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
  flex: 1;
}
.files-sidebar { flex: 2; }
.section-label {
  font-size: .7rem;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  padding: 0 4px;
}
.section-label-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.label-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 0 2px;
  max-height: 120px;
  overflow: auto;
}
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg3);
  color: var(--muted);
  font-size: .78rem;
  cursor: pointer;
  user-select: none;
}
.chip .dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--accent);
}
.chip.active {
  color: var(--text);
  border-color: var(--accent);
  box-shadow: 0 0 0 1px rgba(110,168,254,.25);
}
.chip.all { font-weight: 600; }

.new-label-form {
  display: flex;
  gap: 6px;
}
.new-label-form input {
  flex: 1;
  min-width: 0;
  padding: 6px 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg3);
  color: var(--text);
  font: inherit;
  font-size: .8rem;
  outline: none;
}
.new-label-form input:focus { border-color: var(--accent); }

.file-list {
  list-style: none; margin: 0; padding: 0;
  flex: 1; overflow: auto;
}
.file-list li {
  padding: 10px 12px;
  border-radius: 10px;
  cursor: pointer;
  color: var(--muted);
  font-size: .88rem;
}
.file-list li.active, .file-list li:hover {
  background: var(--bg3);
  color: var(--text);
}

.roster-root {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
  overflow: auto;
  min-height: 0;
}
.roster-category {
  border: 1px solid transparent;
  border-radius: 10px;
}
.roster-cat-header {
  list-style: none;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--muted);
  font-size: .72rem;
  font-weight: 600;
  letter-spacing: .04em;
  text-transform: uppercase;
  user-select: none;
}
.roster-cat-header::-webkit-details-marker { display: none; }
.roster-cat-header::marker { content: ""; }
.roster-cat-header:hover {
  color: var(--text);
  background: var(--bg3);
}
.roster-chevron {
  width: 0; height: 0;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-top: 5px solid currentColor;
  transition: transform .15s ease;
  flex-shrink: 0;
  opacity: .7;
}
.roster-category:not([open]) .roster-chevron {
  transform: rotate(-90deg);
}
.roster-bots {
  list-style: none;
  margin: 0;
  padding: 2px 0 4px 10px;
}
.roster-bot {
  padding: 7px 10px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--muted);
  font-size: .85rem;
  line-height: 1.25;
}
.roster-bot:hover {
  background: var(--bg3);
  color: var(--text);
}
.roster-bot.active {
  background: var(--bg3);
  color: var(--text);
  box-shadow: inset 2px 0 0 var(--accent);
}
.roster-empty {
  padding: 8px 10px;
  color: var(--muted);
  font-size: .78rem;
  opacity: .75;
  font-style: italic;
  cursor: default;
}
.file-list li .fname {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.file-list li .fmeta {
  font-size: .7rem;
  color: var(--muted);
  opacity: .85;
}

.ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 10px;
  padding: 10px;
  cursor: pointer;
  font-size: .85rem;
}
.ghost:hover:not(:disabled) { color: var(--text); border-color: var(--muted); }
.ghost:disabled { opacity: .4; cursor: not-allowed; }
.ghost.tiny { padding: 4px 8px; font-size: .85rem; line-height: 1; }
.upload-btn { cursor: pointer; display: inline-flex; align-items: center; }

#logout-btn { flex-shrink: 0; margin-top: auto; }

.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg);
  min-height: 0;
}
.panel {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

.chat-header, .editor-header {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 10px;
  padding: 12px 24px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--bg2);
  flex-shrink: 0;
}
.chat-header-top, .editor-title-row {
  display: flex;
  align-items: center;
  gap: 10px;
}
.chat-header h2, .editor-header h2 {
  margin: 0; font-size: 1.05rem; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.status { color: var(--ok); font-size: .85rem; margin-left: auto; }
.dirty { color: var(--accent); font-size: .9rem; }
.muted-meta { color: var(--muted); font-size: .75rem; margin-left: auto; }

.chat-toolbar, .editor-toolbar {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}
#search-input {
  flex: 1;
  min-width: 140px;
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg3);
  color: var(--text);
  font: inherit;
  font-size: .92rem;
  outline: none;
}
#search-input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(110,168,254,.15); }
.toolbar-btn { padding: 8px 12px; flex-shrink: 0; }
.accent-btn.toolbar-btn { padding: 8px 12px; border-radius: 10px; font-size: .85rem; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.bubble-row {
  display: flex;
  max-width: min(720px, 92%);
}
.bubble-row.user { align-self: flex-end; justify-content: flex-end; }
.bubble-row.assistant { align-self: flex-start; }
.bubble {
  padding: 12px 16px;
  border-radius: var(--radius);
  line-height: 1.5;
  font-size: 1.02rem;
  white-space: pre-wrap;
  word-break: break-word;
}
.bubble-row.user .bubble {
  background: var(--user);
  border-bottom-right-radius: 4px;
}
.bubble-row.assistant .bubble {
  background: var(--bot);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.meta {
  font-size: .7rem;
  color: var(--muted);
  margin-top: 4px;
  padding: 0 4px;
}
.bubble-row.user .meta { text-align: right; }
.bubble-row.search-hide { display: none; }
.bubble mark {
  background: rgba(110, 168, 254, 0.35);
  color: inherit;
  border-radius: 3px;
  padding: 0 2px;
}

.compose {
  display: flex;
  gap: 10px;
  padding: 16px 24px 20px;
  border-top: 1px solid var(--border);
  background: var(--bg2);
  align-items: flex-end;
  flex-shrink: 0;
  position: sticky;
  bottom: 0;
  z-index: 6;
  padding-bottom: max(20px, env(safe-area-inset-bottom, 0px));
}
#compose-input {
  flex: 1;
  resize: none;
  max-height: 160px;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg3);
  color: var(--text);
  font: inherit;
  font-size: 1rem;
  line-height: 1.4;
  outline: none;
}
#compose-input:focus { border-color: var(--accent); }
#send-btn { flex-shrink: 0; min-width: 88px; }

/* Editor */
.ask-box {
  display: flex;
  gap: 8px;
  padding: 10px 24px;
  border-bottom: 1px solid var(--border);
  background: #1a2438;
  align-items: center;
}
#ask-message {
  flex: 1;
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg3);
  color: var(--text);
  font: inherit;
  outline: none;
}
.editor-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  padding: 40px;
  text-align: center;
  gap: 8px;
}
.editor-empty .hint { font-size: .85rem; max-width: 420px; line-height: 1.45; }

.editor-wrap {
  flex: 1;
  display: grid;
  grid-template-columns: auto 1fr;
  min-height: 0;
  overflow: hidden;
  background: #0c0e12;
}
.gutter {
  padding: 16px 10px 16px 14px;
  text-align: right;
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.55;
  color: #5a6578;
  background: #12151c;
  border-right: 1px solid var(--border);
  user-select: none;
  overflow: hidden;
  white-space: pre;
}
.editor {
  width: 100%;
  height: 100%;
  border: none;
  resize: none;
  padding: 16px 18px;
  background: transparent;
  color: var(--text);
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.55;
  outline: none;
  overflow: auto;
  tab-size: 2;
  white-space: pre;
}
.scratch-status {
  padding: 8px 24px;
  border-top: 1px solid var(--border);
  background: var(--bg2);
  color: var(--muted);
  font-size: .8rem;
  flex-shrink: 0;
}

@media (max-width: 860px) {
  .chat-view { grid-template-columns: 1fr; }
  .messages { padding: 16px; }
  .compose { padding: 12px 14px max(16px, env(safe-area-inset-bottom, 0px)); }
}


/* ---- Apps ---- */
.apps-sidebar { flex: 2; }
.apps-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.apps-list li button {
  width: 100%;
  text-align: left;
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 12px;
  cursor: pointer;
}
.apps-list li button.active {
  border-color: var(--accent);
  background: #1e2a40;
}
.apps-list li button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
.apps-list .app-title { display: block; font-weight: 600; font-size: .9rem; }
.apps-list .app-desc { display: block; color: var(--muted); font-size: .72rem; margin-top: 2px; }
.apps-hint { color: var(--muted); font-size: .72rem; margin: 8px 0 0; }
.apps-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.apps-title-row { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.apps-title-row h2 { margin: 0; font-size: 1.05rem; }
.apps-toolbar { display: flex; gap: 6px; flex-shrink: 0; }
.apps-frame-wrap {
  flex: 1;
  min-height: 0;
  display: flex;
  background: #0a0e18;
}
.apps-frame {
  flex: 1;
  width: 100%;
  height: 100%;
  border: 0;
  background: #111;
}
#panel-apps.panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
}
#panel-apps.panel.hidden { display: none; }


/* Slice A — presence + inbox */
.presence-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px 2px;
  flex-shrink: 0;
}
.presence-chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 5px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg3);
  font-size: .75rem;
  color: var(--muted);
  max-width: 100%;
  min-width: 0;
  flex: 1;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  cursor: default;
}
.presence-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--muted);
  flex-shrink: 0;
}
.presence-chip.status-online .presence-dot { background: var(--ok); box-shadow: 0 0 0 3px rgba(61,214,140,.2); }
.presence-chip.status-degraded .presence-dot { background: #e6b84d; box-shadow: 0 0 0 3px rgba(230,184,77,.2); }
.presence-chip.status-offline .presence-dot { background: var(--danger); box-shadow: 0 0 0 3px rgba(255,107,122,.18); }
.presence-chip.status-online { color: var(--ok); }
.presence-chip.status-degraded { color: #e6b84d; }
.presence-chip.status-offline { color: var(--danger); }
#presence-label { overflow: hidden; text-overflow: ellipsis; }

.inbox-toggle {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg3);
  color: var(--text);
  font-size: .75rem;
  font-weight: 600;
  cursor: pointer;
}
.inbox-toggle:hover { border-color: var(--accent); }
.inbox-toggle.open { border-color: var(--accent); background: #1e2a40; }
.inbox-badge {
  min-width: 1.25rem;
  height: 1.25rem;
  padding: 0 5px;
  border-radius: 999px;
  background: var(--accent2);
  color: #fff;
  font-size: .7rem;
  display: inline-grid;
  place-items: center;
  line-height: 1;
}
.inbox-badge.zero { background: var(--border); color: var(--muted); }

.inbox-panel {
  margin: 0 4px 4px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg3);
  max-height: 240px;
  overflow: auto;
  flex-shrink: 0;
}
.inbox-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px 4px;
  font-size: .72rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--muted);
  position: sticky;
  top: 0;
  background: var(--bg3);
}
.inbox-list {
  list-style: none;
  margin: 0;
  padding: 4px 6px 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.inbox-empty { margin: 0 10px 10px; font-size: .8rem; }
.inbox-item {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px;
  background: var(--bg2);
}
.inbox-item-title {
  font-size: .8rem;
  line-height: 1.35;
  margin: 0 0 4px;
  word-break: break-word;
}
.inbox-item-meta {
  font-size: .68rem;
  color: var(--muted);
  margin: 0 0 6px;
}
.inbox-item-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}
.inbox-item-actions button {
  font-size: .7rem;
  padding: 4px 8px;
  border-radius: 8px;
}

/* Slice B — session memory + scratch diff */
.session-memory {
  margin: 6px 0 8px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg3);
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.session-memory-head {
  display: flex;
  align-items: center;
  gap: 8px;
}
.session-memory-head .section-label {
  margin: 0;
  flex: 1;
}
.sm-field {
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-size: .72rem;
  color: var(--muted);
}
.sm-field input,
.sm-field textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  padding: 6px 8px;
  font: inherit;
  font-size: .8rem;
  resize: vertical;
}
.sm-field input:focus,
.sm-field textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.diff-panel {
  margin: 0 12px 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #0d1117;
  overflow: hidden;
  flex-shrink: 0;
  max-height: 40%;
  display: flex;
  flex-direction: column;
}
.diff-panel.hidden { display: none; }
.diff-panel-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  background: var(--bg3);
  flex-wrap: wrap;
}
.diff-panel-head strong { font-size: .8rem; }
.diff-actions {
  margin-left: auto;
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}
.diff-view {
  margin: 0;
  padding: 10px 12px;
  overflow: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: .75rem;
  line-height: 1.35;
  white-space: pre;
  color: #c9d1d9;
  min-height: 4rem;
  max-height: 18rem;
}
.diff-line { display: inline; }
.diff-add { color: #3dd68c; background: rgba(61, 214, 140, .08); }
.diff-del { color: #ff6b7a; background: rgba(255, 107, 122, .08); }
.diff-hunk { color: #6ea8fe; }


/* ---- Slice C: split view + mobile chrome (F5 + F8) ---- */
html, body {
  overflow-x: hidden;
  max-width: 100vw;
}

.workspace-chrome {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg2);
  flex-shrink: 0;
  min-width: 0;
  flex-wrap: wrap;
  z-index: 8;
}

.nav-toggle {
  display: none;
  min-width: 44px;
  min-height: 44px;
  align-items: center;
  justify-content: center;
  font-size: 1.15rem;
  padding: 8px 12px;
}

.chrome-modes {
  display: none;
  gap: 6px;
  flex: 1;
  min-width: 0;
}

.chrome-mode {
  flex: 1;
  min-height: 44px;
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 10px;
  padding: 8px 10px;
  cursor: pointer;
  font-weight: 600;
  font-size: .85rem;
}

.chrome-mode.active {
  color: var(--text);
  border-color: var(--accent);
  background: #1e2a40;
}

.chrome-layout {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-left: auto;
  flex-wrap: wrap;
}

.layout-btn {
  min-height: 34px;
  white-space: nowrap;
}

.layout-btn[aria-pressed="true"] {
  color: var(--text);
  border-color: var(--accent);
  background: #1e2a40;
}

.chrome-status { display: none; }

.sidebar-backdrop {
  display: none;
}

/* Wide split: Chat | Files or Chat | Apps */
.main.split-on {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr);
  min-height: 0;
}

.main.split-on > .workspace-chrome {
  grid-column: 1 / -1;
}

.main.split-on > .panel {
  min-width: 0;
  min-height: 0;
  height: 100%;
  border-right: 1px solid var(--border);
}

.main.split-on > .panel:last-child {
  border-right: none;
}

.main.split-on #panel-chat .messages {
  /* slightly tighter in split */
  padding: 16px;
}

.main:not(.split-on) {
  display: flex;
  flex-direction: column;
}

/* When split, secondary sidebars share space with roster */
.sidebar.split-secondary #chat-sidebar {
  flex: 1;
}
.sidebar.split-secondary #files-sidebar,
.sidebar.split-secondary #apps-sidebar {
  flex: 1.4;
}

@media (min-width: 861px) and (max-width: 1099px) {
  /* Mid width: sidebar mode tabs suffice; hide empty chrome bar */
  .workspace-chrome { display: none; }
}

@media (min-width: 861px) {
  .workspace-chrome .chrome-modes { display: none; }
  .nav-toggle { display: none; }
}

@media (max-width: 1099px) {
  /* Split only useful on wide viewports */
  .chrome-layout { display: none; }
  .main.split-on {
    display: flex;
    flex-direction: column;
  }
  .main.split-on > .panel {
    border-right: none;
  }
}

@media (max-width: 860px) {
  .nav-toggle { display: inline-flex; }
  .chrome-modes { display: flex; }
  .chrome-layout { display: none !important; }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(320px, 88vw);
    z-index: 50;
    transform: translateX(-105%);
    transition: transform .2s ease;
    display: flex;
    box-shadow: 8px 0 32px rgba(0,0,0,.4);
  }

  .chat-view.nav-open .sidebar {
    transform: translateX(0);
  }

  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.45);
    z-index: 45;
  }
  .sidebar-backdrop.hidden { display: none !important; }

  /* Larger tap targets */
  .mode-tab,
  .chrome-mode,
  .roster-bot,
  .apps-list li button,
  .file-list li button,
  .inbox-toggle,
  .toolbar-btn,
  .ghost.tiny {
    min-height: 44px;
  }

  .ghost.tiny {
    padding: 10px 12px;
  }

  #compose-input {
    font-size: 16px; /* avoid iOS zoom */
    min-height: 44px;
    padding: 12px 14px;
  }

  #send-btn {
    min-width: 72px;
    min-height: 48px;
    padding: 12px 16px;
    font-size: 1.05rem;
  }

  .chat-header, .editor-header, .apps-header {
    padding: 10px 14px 12px;
  }

  .editor-toolbar, .apps-toolbar, .chat-toolbar {
    gap: 6px;
  }

  .editor-toolbar .toolbar-btn,
  .apps-toolbar .toolbar-btn {
    min-height: 40px;
    padding: 8px 10px;
  }

  /* Apps / Desktop usable — no horizontal overflow traps */
  #panel-apps,
  .apps-frame-wrap,
  .apps-frame,
  #panel-files,
  .editor-wrap,
  .editor {
    max-width: 100%;
  }

  .apps-frame-wrap {
    min-height: 50vh;
    -webkit-overflow-scrolling: touch;
  }

  .apps-frame {
    touch-action: manipulation;
  }

  .editor-toolbar {
    overflow-x: auto;
    flex-wrap: nowrap;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 2px;
  }

  .session-memory {
    max-height: 28vh;
    overflow: auto;
  }

  .bubble-row {
    max-width: 96%;
  }
}

@media (min-width: 1100px) {
  .chrome-layout { display: flex; }
}

/* ===== Slice D: selection toolbar + vault ===== */
.sel-toolbar {
  position: fixed;
  z-index: 1200;
  display: flex;
  gap: 4px;
  padding: 4px;
  background: var(--bg2, #1a2438);
  border: 1px solid var(--border, #2a3548);
  border-radius: 10px;
  box-shadow: 0 8px 28px rgba(0,0,0,.45);
}
.sel-toolbar.hidden { display: none; }
.sel-btn {
  border: none;
  background: transparent;
  color: var(--text, #e8eefc);
  font: inherit;
  font-size: .8rem;
  padding: 6px 10px;
  border-radius: 8px;
  cursor: pointer;
  white-space: nowrap;
}
.sel-btn:hover { background: var(--bg3, #243044); }
.sel-btn.accent {
  background: #3d5afe;
  color: #fff;
}
.sel-btn.accent:hover { background: #536dfe; }

.sel-label-picker {
  position: fixed;
  inset: 0;
  z-index: 1300;
  background: rgba(0,0,0,.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.sel-label-picker.hidden { display: none; }
.sel-label-picker-card {
  background: var(--bg2, #1a2438);
  border: 1px solid var(--border, #2a3548);
  border-radius: 14px;
  padding: 14px 16px;
  width: min(360px, 100%);
  box-shadow: 0 12px 40px rgba(0,0,0,.5);
}
.sel-label-picker-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}
.sel-label-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}
.sel-label-list .chip {
  cursor: pointer;
}

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1400;
  background: #1e2a44;
  color: #e8eefc;
  border: 1px solid #3a4a66;
  border-radius: 10px;
  padding: 10px 16px;
  font-size: .88rem;
  box-shadow: 0 8px 24px rgba(0,0,0,.4);
  max-width: min(420px, 92vw);
}
.toast.hidden { display: none; }

.vault-sidebar .vault-search {
  width: 100%;
  margin: 6px 0 8px;
  padding: 7px 10px;
  border-radius: 8px;
  border: 1px solid var(--border, #2a3548);
  background: var(--bg3, #121820);
  color: var(--text, #e8eefc);
  font: inherit;
  font-size: .85rem;
  outline: none;
}
.vault-list {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 40vh;
  overflow: auto;
}
.vault-list li {
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  margin-bottom: 2px;
}
.vault-list li:hover { background: var(--bg3, #243044); }
.vault-list li.active { background: #2a3a5c; }
.vault-list .vault-item-title {
  font-size: .88rem;
  font-weight: 600;
}
.vault-list .vault-item-tags {
  font-size: .72rem;
  color: var(--muted, #8b97ad);
  margin-top: 2px;
}
.vault-empty { margin: 8px 4px; }

#panel-vault {
  display: flex;
  flex-direction: column;
  min-height: 0;
}
#panel-vault.hidden { display: none; }
.vault-header {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border, #2a3548);
  background: #1a2438;
}
.vault-title-row {
  display: flex;
  align-items: baseline;
  gap: 10px;
}
.vault-title-row h2 { margin: 0; font-size: 1.1rem; }
.vault-toolbar { display: flex; gap: 6px; flex-shrink: 0; }
.vault-editor-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 16px 20px;
  min-height: 0;
  overflow: auto;
}
.vault-editor-wrap.hidden { display: none; }
.vault-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: .8rem;
  color: var(--muted, #8b97ad);
}
.vault-field input,
.vault-field textarea {
  font: inherit;
  font-size: .92rem;
  color: var(--text, #e8eefc);
  background: var(--bg3, #0c0e12);
  border: 1px solid var(--border, #2a3548);
  border-radius: 10px;
  padding: 10px 12px;
  outline: none;
}
.vault-body-field { flex: 1; min-height: 0; }
.vault-body-field textarea {
  flex: 1;
  min-height: 220px;
  resize: vertical;
  font-family: var(--mono, ui-monospace, monospace);
  font-size: 13px;
  line-height: 1.5;
}
