/* Coreidon hub shell.
 *
 * The full-page chat is the main app and owns the sidebar (its own workspace
 * rail, extended with the hub's Settings item and per-app unread counts). This
 * file only styles what wraps it:
 *
 *   Settings — an overlay that covers the chat's list + thread columns but
 *              stops at the rail, so the sidebar stays usable. Notifications
 *              have no overlay: each app's rail panel lists its own.
 *   app mode — a hosted app fills the ENTIRE window, edge to edge, rail
 *              included; a small handle brings the switcher back. The app must
 *              look like it IS the installed webapp, because to the user it is.
 */

/* Rail width, mirrored from widget.js's `.wrap` grid — the overlays start
   exactly where the chat's rail ends. */
:root { --rail-w: 76px; }

:root {
  color-scheme: light dark;
  --bar-bg: #ffffff; --bar-fg: #1a1f2b; --bar-line: #e9ebf0; --bar-muted: #6b7280;
  --surface: #f6f7fb; --surface-2: #ffffff; --rail-bg: #f6f7fb; --hover: #eef0f6;
  --brand1: #6c5ce7; --brand2: #00b894;
  --accent: #6c5ce7; --accent-weak: rgba(108, 92, 231, 0.12);
  --danger: #e11d48;
}
:root[data-theme="dark"] {
  --bar-bg: #0f131b; --bar-fg: #e7eaf1; --bar-line: #222838; --bar-muted: #98a2b6;
  --surface: #0b0e15; --surface-2: #151a25; --rail-bg: #10141d; --hover: #1b2130;
  --brand1: #8b7df0; --brand2: #2fd0a6;
  --accent: #8b7df0; --accent-weak: rgba(139, 125, 240, 0.18);
  --danger: #fb7185;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  height: 100vh; height: 100dvh;   /* dvh so mobile browser chrome can't cut it off */
  overflow: hidden;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--surface); color: var(--bar-fg);
  -webkit-text-size-adjust: 100%;
}

#hub { display: flex; height: 100%; }

/* ── view stack ──────────────────────────────────────────────────────────── */
#views { position: relative; flex: 1 1 auto; min-width: 0; min-height: 0; }

/* The chat is always mounted and always the bottom layer: it owns the rail,
   which must stay visible and clickable underneath the overlays. */
.view[data-view="chat"] { position: absolute; inset: 0; display: flex; flex-direction: column; }
coreidon-chatable[full] { flex: 1 1 auto; min-height: 0; }

/* Settings: covers the chat list + thread, stops at the rail. */
.view.overlay {
  position: absolute; top: 0; right: 0; bottom: 0; left: var(--rail-w);
  overflow-y: auto; -webkit-overflow-scrolling: touch;
  background: var(--surface); padding: 16px; display: none; z-index: 5;
}
.view.overlay.active { display: block; }

/* The dashboard brings its own header, padding and (light) surface — the
   overlay's 16px inset would just add a border of hub background around it. */
.view.overlay[data-view="dashboard"] { padding: 0; background: #f5f6f8; }
#dashboard-host { min-height: 100%; }

.view-head { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.view-head h2 { margin: 0; font-size: 17px; flex: 1; }
.view-head .back {
  border: 1px solid var(--bar-line); background: transparent; color: var(--bar-muted);
  width: 34px; height: 34px; border-radius: 10px; cursor: pointer; font-size: 16px; flex: none;
}
.view-head .back:hover { color: var(--bar-fg); border-color: var(--accent); }

/* Phones: the chat hides its rail once a thread is open, so an overlay that
   stopped at the rail would leave a dead strip — go full width and rely on the
   overlay's own back button. */
@media (max-width: 720px) {
  .view.overlay { left: 0; }
}

/* ── hosted apps ─────────────────────────────────────────────────────────── */
/* One frame per open app, stacked; only the active one is visible. Frames are
   kept mounted so switching preserves each app's scroll position and state. */
#frames { position: absolute; inset: 0; display: none; }
#hub.app-mode #frames { display: block; }
#frames iframe {
  position: absolute; inset: 0; width: 100%; height: 100%;
  border: 0; display: none; background: var(--bar-bg);
}
#frames iframe.active { display: block; }

/* In app mode every piece of hub chrome gets out of the way — the chat (and
   with it the rail) included. */
#hub.app-mode .view { display: none; }

/* The one affordance that survives: a handle to bring the rail back. */
#handle {
  display: none; position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(env(safe-area-inset-bottom) + 4px);
  width: 120px; height: 26px; border: 0; border-radius: 13px 13px 0 0;
  background: transparent; cursor: pointer; z-index: 40;
}
#hub.app-mode #handle { display: block; }
#handle::after {
  content: ""; display: block; margin: 10px auto 0;
  width: 46px; height: 5px; border-radius: 3px;
  background: var(--bar-muted); opacity: 0.45;
}
#handle:hover::after { opacity: 0.8; }

/* Notifications have no view here: each app's rail panel lists its own (see
   widget.js `_renderAppNotifications`), and the rail item carries the count. */

/* ── settings ────────────────────────────────────────────────────────────── */
.section-title { font-size: 12px; font-weight: 700; letter-spacing: 0.08em;
  text-transform: uppercase; color: var(--bar-muted); margin: 4px 0 12px; }
.card {
  background: var(--surface-2); border: 1px solid var(--bar-line);
  border-radius: 16px; padding: 14px; margin-bottom: 14px;
}
.card h3 { margin: 0 0 4px; font-size: 15px; }
.card p.hint { margin: 0 0 12px; font-size: 12px; color: var(--bar-muted); line-height: 1.45; }
.row { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--bar-line); }
.row:last-child { border-bottom: 0; }
.row .grow { flex: 1; min-width: 0; }
.row .label { font-size: 13px; }
.row .label small { display: block; color: var(--bar-muted); font-size: 11px; margin-top: 2px; }
.btn {
  font: inherit; font-size: 13px; font-weight: 600; cursor: pointer;
  border: 1px solid var(--bar-line); background: transparent; color: var(--bar-fg);
  border-radius: 10px; padding: 9px 14px;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn:disabled { opacity: 0.5; cursor: default; }
/* The app filter reuses .btn so it matches the buttons beside it. */
select.btn { padding: 8px 10px; max-width: 55%; background: var(--surface-2); color: var(--bar-fg); }

/* Event matrix: label + two checkbox columns, header aligned with the rows. */
.events-head, .event-row {
  display: grid; grid-template-columns: 1fr 56px 56px; gap: 8px; align-items: center;
  border-bottom: 1px solid var(--bar-line);
}
.events-head { padding: 4px 0 10px; }
.events-head .col {
  font-size: 10px; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase;
  color: var(--bar-muted); text-align: center;
}
.event-row { padding: 12px 0; }
.event-row:last-child { border-bottom: 0; }
.event-row .label { font-size: 13px; line-height: 1.35; }
.event-row .label small { display: block; color: var(--bar-muted); font-size: 11px; margin-top: 2px; }

input[type="checkbox"].ch {
  -webkit-appearance: none; appearance: none; margin: 0; justify-self: center;
  width: 20px; height: 20px; border: 1.5px solid var(--bar-line); border-radius: 6px;
  background: transparent; cursor: pointer; display: grid; place-content: center;
}
input[type="checkbox"].ch::before {
  content: ""; width: 11px; height: 11px; transform: scale(0);
  transition: transform 0.12s ease; box-shadow: inset 0 0 0 11px #fff;
  clip-path: polygon(14% 44%, 0 65%, 50% 100%, 100% 16%, 80% 0%, 43% 62%);
}
input[type="checkbox"].ch:checked { background: var(--accent); border-color: var(--accent); }
input[type="checkbox"].ch:checked::before { transform: scale(1); }

/* iOS-style switch for the single master toggles. */
.switch { position: relative; width: 46px; height: 27px; flex: none; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.switch span {
  position: absolute; inset: 0; border-radius: 14px; background: var(--bar-line);
  transition: background 0.15s ease; pointer-events: none;
}
.switch span::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 21px; height: 21px;
  border-radius: 50%; background: #fff; transition: transform 0.15s ease;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
}
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(19px); }

/* ── switcher sheet ──────────────────────────────────────────────────────── */
#switcher {
  position: fixed; inset: 0; z-index: 50; display: none;
  background: rgba(8, 10, 16, 0.45);
}
#switcher.open { display: block; }
#switcher .sheet {
  position: absolute; left: 0; right: 0; bottom: 0;
  background: var(--bar-bg); border-top: 1px solid var(--bar-line);
  border-radius: 18px 18px 0 0; padding: 10px 12px calc(env(safe-area-inset-bottom) + 12px);
  max-height: 70%; overflow-y: auto;
}
#switcher .grip { width: 46px; height: 5px; border-radius: 3px; background: var(--bar-line); margin: 2px auto 12px; }
.sw-row { display: flex; align-items: center; gap: 10px; padding: 10px 4px; }
.sw-row .grow { flex: 1; min-width: 0; }
.sw-row .t { font-size: 14px; font-weight: 650; }
.sw-row .u { font-size: 11px; color: var(--bar-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sw-row.selected .t { color: var(--accent); }
.sw-row button.mini {
  border: 1px solid var(--bar-line); background: transparent; color: var(--bar-muted);
  width: 32px; height: 32px; border-radius: 9px; cursor: pointer; font-size: 14px; flex: none;
}
.sw-row button.mini:hover { color: var(--bar-fg); border-color: var(--accent); }
.sw-open { flex: 1; display: flex; align-items: center; gap: 10px; background: transparent;
  border: 0; font: inherit; color: inherit; text-align: left; cursor: pointer; padding: 0; min-width: 0; }

/* ── misc ────────────────────────────────────────────────────────────────── */
.empty, .error { font-size: 13px; color: var(--bar-muted); padding: 24px 8px; text-align: center; }
.error { color: var(--danger); }
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(env(safe-area-inset-bottom) + 78px); z-index: 60;
  background: var(--bar-fg); color: var(--bar-bg);
  font-size: 13px; padding: 10px 16px; border-radius: 12px; max-width: 90vw;
}
/* "New version ready" — stays until acted on, so it also needs to sit above a
   full-bleed app frame. */
.toast.update { display: flex; align-items: center; gap: 12px; padding: 10px 10px 10px 16px; }
.toast.update .btn { padding: 6px 12px; }
