/* ScarabHive shell -- the frame around the chat and the panels.
 * Colours, spacing and type come from the kit tokens only (static/kit/kit.css). */

html, body.app { height: 100%; }
body.app {
  display: grid;
  grid-template-rows: 48px 1fr 28px;
  grid-template-columns: minmax(0, 1fr);  /* the page is the window: wide content scrolls inside, never pushes the header out */
  overflow: hidden;
  background: var(--surface-0);
}

/* ------------------------------------------------------------------ header */

.app-header {
  display: flex; align-items: center; gap: var(--space-2);
  padding: 0 var(--space-3);
  background: var(--surface-1);
  border-bottom: 1px solid var(--border-subtle);
  min-width: 0;
}
.app-brand {
  display: inline-flex; align-items: center; gap: var(--space-2);
  color: var(--brand); text-decoration: none;
  padding: 0 var(--space-2) 0 var(--space-1);
}
.app-brand span { color: var(--text-primary); font-weight: 600; font-size: var(--text-md); letter-spacing: -0.01em; }
.app-session {
  display: flex; align-items: center; gap: var(--space-2); min-width: 0; flex: 0 1 auto;
  padding-left: var(--space-3); border-left: 1px solid var(--border-subtle);
}
.app-session-title {
  max-width: 32ch; padding: 2px var(--space-1);
  font: 400 var(--text-sm)/1.4 var(--font-ui); color: var(--text-secondary); text-align: left;
  background: none; border: 0; border-radius: var(--radius-sm); cursor: pointer;
}
.app-session-title:hover:not(:disabled) { color: var(--text-primary); background: var(--surface-3); }
.app-session-title:disabled { cursor: default; }
.app-search {
  display: flex; align-items: center; gap: var(--space-2);
  margin: 0 auto; width: min(460px, 36vw); height: var(--control-h-md); padding: 0 var(--space-2) 0 var(--space-3);
  font: 400 var(--text-sm)/1 var(--font-ui); color: var(--text-muted); text-align: left;
  background: var(--field-bg); border: 1px solid var(--border-default); border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm); cursor: pointer;
}
.app-search:hover { border-color: var(--border-strong); color: var(--text-secondary); }
.app-header-actions { display: flex; align-items: center; gap: var(--space-1); }
.app-header-divider { width: 1px; height: 20px; margin: 0 var(--space-1); background: var(--border-default); }
/* the account menu's theme row: the three choices side by side, the one in use pressed */
.app-theme-choice { display: flex; gap: var(--space-1); padding: 0 var(--space-1) var(--space-1); }
.app-theme-choice > .pk-btn { flex: 1; }
.app-avatar {
  width: 28px; height: 28px; margin-left: var(--space-1); border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  font: 600 11px/1 var(--font-ui); color: var(--accent-text);
  background: var(--accent-subtle); border: 1px solid var(--border-subtle); cursor: pointer;
}

/* -------------------------------------------------------------------- body */

.app-body {
  /* --sessions-open-width: the width the viewer dragged, set by shell.js; closed overrides it below */
  /* the cap leaves the chat its 360px and an open dock its 320px + 5px resizer (see .app-dock) */
  --sessions-width: min(var(--sessions-open-width, 272px), 40vw, calc(100vw - 685px));
  --dock-width: 520px;
  position: relative;
  min-width: 0;
  display: grid;
  grid-template-columns: var(--sessions-width) minmax(360px, 1fr) auto auto;
  min-height: 0;
}
.app-body[data-sessions="closed"] { --sessions-width: 0px; }
.app-body[data-sessions="closed"] .app-sessions { visibility: hidden; }
/* out of the grid flow: it rides the pane's right edge without taking a column */
.app-sessions-resizer {
  position: absolute; top: 0; bottom: 0; left: calc(var(--sessions-width) - 3px); z-index: 5;
  width: 6px; cursor: col-resize; touch-action: none;
}
.app-sessions-resizer:hover, .app-sessions-resizer[data-dragging], .app-sessions-resizer:focus-visible { background: var(--accent-subtle); outline: none; }
.app-body[data-sessions="closed"] .app-sessions-resizer { display: none; }

/* sessions pane */
.app-sessions {
  display: flex; flex-direction: column; min-height: 0; overflow: hidden;
  background: var(--surface-1); border-right: 1px solid var(--border-subtle);
}
.sessions-head { display: flex; align-items: center; gap: var(--space-2); padding: var(--space-2) var(--space-2) var(--space-2) var(--space-3); }
.sessions-head h2 { font-size: var(--text-xs); font-weight: 500; color: var(--text-muted); text-transform: none; margin: 0; }
.sessions-filter { padding: 0 var(--space-2) var(--space-2); }
.sessions-list { flex: 1; overflow: auto; padding: 0 var(--space-2) var(--space-3); }
/* a day group's head: it folds its sessions (Today, Yesterday, This week open; months closed) */
.sessions-group {
  display: flex; align-items: center; gap: var(--space-1); width: 100%;
  padding: var(--space-3) var(--space-2) var(--space-1); font: 500 var(--text-xs)/1.2 var(--font-ui); color: var(--text-muted);
  text-align: left; background: none; border: 0; border-radius: var(--radius-sm); cursor: pointer;
}
.sessions-group:hover { color: var(--text-primary); }
.sessions-group .pk-icon, .sessions-folder-toggle > .pk-icon:first-child {
  flex: none; transition: transform var(--duration-fast); color: var(--text-muted);
}
.sessions-group[aria-expanded="true"] .pk-icon, .sessions-folder-toggle[aria-expanded="true"] > .pk-icon:first-child { transform: rotate(90deg); }
.sessions-count { flex: none; min-width: 2ch; font-size: 11px; color: var(--text-muted); font-variant-numeric: tabular-nums; text-align: right; }

/* folders, above the sessions in none: a row each, their sessions by day inside */
.sessions-folder { border-radius: var(--radius-md); margin-bottom: 1px; }
.sessions-folder-row {
  display: flex; align-items: center; gap: var(--space-1);
  min-height: 34px; padding: 0 var(--space-1) 0 var(--space-1); border-radius: var(--radius-md); color: var(--text-secondary);
}
.sessions-folder-row:hover { background: var(--surface-3); color: var(--text-primary); }
.sessions-folder-toggle {
  display: flex; align-items: center; gap: var(--space-2); flex: 1; min-width: 0; align-self: stretch;
  padding: 0 var(--space-1); font: 500 var(--text-sm)/1 var(--font-ui); color: inherit; text-align: left;
  background: none; border: 0; cursor: pointer;
}
.sessions-folder-toggle > .pk-icon:nth-child(2) { flex: none; color: var(--accent-text); }
.sessions-folder-edit { display: none; }
.sessions-folder-row:is(:hover, :focus-within) > .sessions-folder-edit { display: inline-flex; }
.sessions-folder-row:is(:hover, :focus-within) > .sessions-count { display: none; }
.sessions-folder-running { display: none; flex: none; width: 6px; height: 6px; border-radius: 50%; background: var(--accent); }
.sessions-folder.is-running > .sessions-folder-row > .sessions-folder-running { display: block; animation: session-running-pulse 1.6s ease-in-out infinite; }
@media (prefers-reduced-motion: reduce) {
  .sessions-folder.is-running > .sessions-folder-row > .sessions-folder-running { animation: none; }
}
.sessions-day-running { display: none; flex: none; width: 6px; height: 6px; border-radius: 50%; background: var(--accent); }
.sessions-day.is-running > .sessions-group > .sessions-day-running { display: block; }
.sessions-folder-body { margin-left: var(--space-3); padding-left: var(--space-1); border-left: 1px solid var(--border-subtle); }
.sessions-folder-body > .sessions-day:first-child > .sessions-group { padding-top: var(--space-1); }
.sessions-folder-empty { padding: var(--space-2); font-size: var(--text-xs); color: var(--text-muted); }
.sessions-divider { height: 1px; margin: var(--space-2) var(--space-2) 0; background: var(--border-subtle); }

/* dragging a session: where it can go shows, where it would land lights up */
.sessions-list.is-dragging [data-drop-folder] { border-radius: var(--radius-md); }
.sessions-list.is-dragging .sessions-unfiled { min-height: 40px; }
[data-drop-folder].is-drop { background: var(--accent-subtle); box-shadow: inset 0 0 0 1px var(--accent); }
.session-row[draggable="true"] { cursor: default; }

/* the folder dialog's icon choice: the folder set as a grid, the chosen one pressed */
.sessions-folder-dialog { width: min(480px, calc(100vw - 32px)); }
.sessions-folder-dialog fieldset:not(.pk-icon-grid) { margin: 0; padding: 0; border: 0; display: flex; flex-direction: column; gap: var(--space-1); }
.session-item { border-radius: var(--radius-md); }
.session-row {
  display: flex; align-items: center; gap: var(--space-1);
  min-height: 34px; padding: 0 var(--space-1) 0 calc(var(--space-2) + var(--depth, 0) * 14px);
  border-radius: var(--radius-md); color: var(--text-secondary);
}
.session-row:hover { background: var(--surface-3); color: var(--text-primary); }
.session-item[aria-current="true"] > .session-row { background: var(--accent-subtle); color: var(--text-primary); }
.session-open {
  flex: 1; min-width: 0; align-self: stretch; padding: 0;
  font: 400 var(--text-sm)/1 var(--font-ui); color: inherit; text-align: left;
  background: none; border: 0; cursor: pointer;
}
/* An agent is working in this session. Shown on the row itself, not only on the open
   one: the point is to see the runs you are NOT looking at. Kept next to the title so
   it survives the hover swap below, which hides the timestamp. */
.session-running { display: none; flex: none; width: 6px; height: 6px; border-radius: 50%; background: var(--accent, currentColor); }
.session-item.is-running > .session-row > .session-running { display: block; animation: session-running-pulse 1.6s ease-in-out infinite; }
@keyframes session-running-pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.25; } }
/* ...and which agent, where the timestamp would be: the agent is what tells a run you
   started from one somebody else started */
.session-agent { display: none; flex: 0 1 auto; max-width: 40%; font-size: var(--text-xs); color: var(--accent-text, var(--accent)); }
.session-item.is-running > .session-row > .session-agent:not(:empty) { display: block; }
.session-item.is-running > .session-row > .session-agent:not(:empty) ~ .session-meta { display: none; }
/* A run can last hours; a dot pulsing all that time in the corner of the eye is a
   cost with no message. Anyone who asked for less motion gets a steady dot. */
@media (prefers-reduced-motion: reduce) {
  .session-item.is-running > .session-row > .session-running { animation: none; }
}
.session-row .session-meta { font-size: 11px; color: var(--text-muted); white-space: nowrap; }
.session-row .session-actions { display: none; }
.session-row:hover .session-actions, .session-row:focus-within .session-actions { display: flex; }
.session-row:hover .session-meta, .session-row:focus-within .session-meta { display: none; }
/* the agent's name gives way to the actions as the timestamp does (outweighs the rule showing it) */
.session-item.is-running > .session-row:is(:hover, :focus-within) > .session-agent:not(:empty) { display: none; }
.session-expand { width: 20px; height: 20px; padding: 0; color: var(--text-muted); background: none; border: 0; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; }
.session-expand[aria-expanded="true"] .pk-icon { transform: rotate(90deg); }
.session-expand-spacer { width: 20px; flex: none; }

/* chat column */
.app-main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.app-chat-scroll { flex: 1; overflow-y: auto; scroll-behavior: smooth; }

/* dock */
.app-dock-resizer { width: 5px; cursor: col-resize; touch-action: none; background: transparent; border-left: 1px solid var(--border-subtle); }
.app-dock-resizer:hover, .app-dock-resizer[data-dragging] { background: var(--accent-subtle); }
.app-dock {
  /* the chat column keeps its 360px and the resizer its 5px, whatever width was stored */
  width: min(var(--dock-width), calc(100vw - var(--sessions-width) - 365px)); min-width: 320px;
  display: flex; flex-direction: column; min-height: 0; background: var(--surface-2);
}
/* put away by the header's right toggle, on a wide screen: a narrow one steps the panels aside instead (see below) */
@media not all and (max-width: 960px) {
  .app-body[data-dock="away"] :is(.app-dock, .app-dock-resizer) { display: none; }
}
.app-dock-bar { display: flex; align-items: stretch; background: var(--surface-1); border-bottom: 1px solid var(--border-subtle); }
.app-back { display: none; align-self: center; margin: 0 var(--space-1); }
/* As the kit's tabs: each as wide as its title while they all fit, then down to five characters of it, then the bar
   scrolls (workspace.js: the wheel turns it, a tab coming to the front is brought into sight). The least width:
   padding, icon, two gaps, three 22px buttons, the border and 4.5em of title for its five (as kit.css); a shorter
   title keeps its own width where the browser has calc-size(). */
.app-dock-tabs {
  display: flex; align-items: stretch; gap: 1px; min-height: 36px; min-width: 0;
  overflow-x: auto; overflow-y: hidden; scrollbar-width: thin;
}
.dock-tab {
  --dock-tab-least: calc(var(--space-3) + 3 * var(--space-1) + 14px + 3 * 22px + 1px + 4.5em);
  display: inline-flex; align-items: center; gap: var(--space-1); flex: 0 1 auto; min-width: var(--dock-tab-least);
  padding: 0 var(--space-1) 0 var(--space-3); font-size: var(--text-sm); color: var(--text-secondary);
  background: transparent; border: 0; border-right: 1px solid var(--border-subtle); cursor: pointer; white-space: nowrap;
}
@supports (min-width: calc-size(max-content, size)) {
  .dock-tab { min-width: calc-size(max-content, min(size, var(--dock-tab-least))); }
}
.dock-tab[aria-selected="true"] { color: var(--text-primary); background: var(--surface-2); box-shadow: inset 0 -2px 0 var(--accent); }
.dock-tab[data-dragging] { opacity: 0.5; }
.dock-tab .dock-tab-title { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; }  /* a short title: the buttons stay at the end */
.dock-tab .dock-tab-actions { display: inline-flex; }
.dock-tab .dock-tab-actions .pk-btn--icon { width: 22px; }  /* three per tab: the kit's 28px leaves the icons far apart */
.dock-tab:focus-visible { outline-offset: -2px; }  /* inside: the scrolling bar would cut a ring outside off */
.app-dock-frames { position: relative; flex: 1; min-height: 0; }
.panel-frame { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; background: var(--surface-2); }

/* ------------------------------------------------------- floating windows */

.app-floating-layer { position: fixed; inset: 48px 0 28px 0; pointer-events: none; z-index: 56; }  /* above the narrow-screen dock (55) */
.app-window {
  position: absolute; display: flex; flex-direction: column; pointer-events: auto;
  min-width: 320px; min-height: 200px;
  background: var(--surface-2); border: 1px solid var(--border-default); border-radius: var(--radius-lg);
  box-shadow: var(--shadow-overlay); overflow: hidden;
}
.app-window[data-focused="true"] { border-color: var(--border-strong); }
.app-window-bar {
  display: flex; align-items: center; gap: var(--space-2); height: 36px; flex: none;
  padding: 0 var(--space-1) 0 var(--space-3); background: var(--surface-1);
  border-bottom: 1px solid var(--border-subtle); cursor: grab; user-select: none; touch-action: none;
}
.app-window-bar:active { cursor: grabbing; }
.app-window-bar > .pk-btn--icon + .pk-btn--icon { margin-left: calc(-1 * var(--space-2)); }  /* the buttons closer together */
.app-window-title { flex: 1; min-width: 0; font-size: var(--text-sm); font-weight: 500; }
.app-window-body { position: relative; flex: 1; min-height: 0; }
.app-window-resize { position: absolute; right: 0; bottom: 0; width: 14px; height: 14px; cursor: nwse-resize; touch-action: none; }
.app-window-resize::after {
  content: ""; position: absolute; right: 3px; bottom: 3px; width: 6px; height: 6px;
  border-right: 2px solid var(--border-strong); border-bottom: 2px solid var(--border-strong);
}
.app-dragging iframe { pointer-events: none; }
.app-dragging { user-select: none; }

/* --------------------------------------------------------- launcher, palette */

.app-launcher { width: min(560px, calc(100vw - 32px)); max-height: min(640px, calc(100vh - 96px)); padding: var(--space-2); overflow: hidden; flex-direction: column; gap: var(--space-2); }
.app-launcher:popover-open { display: flex; }
.app-launcher-list { overflow: auto; }
.launcher-group { padding: var(--space-2) var(--space-2) var(--space-1); font-size: var(--text-xs); color: var(--text-muted); }
.launcher-item {
  display: grid; grid-template-columns: 28px 1fr auto; align-items: center; gap: var(--space-2);
  width: 100%; padding: var(--space-2); text-align: left; color: var(--text-primary);
  background: none; border: 0; border-radius: var(--radius-md); cursor: pointer;
}
.launcher-item:hover, .launcher-item:focus-visible { background: var(--surface-3); outline: none; }
.launcher-icon { width: 28px; height: 28px; display: inline-flex; align-items: center; justify-content: center; border-radius: var(--radius-md); background: var(--surface-1); color: var(--text-secondary); }
.launcher-text { min-width: 0; }
.launcher-title { font-size: var(--text-sm); font-weight: 500; }
.launcher-desc { font-size: var(--text-xs); color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.launcher-fold > summary { list-style: none; }
.launcher-fold > summary::-webkit-details-marker { display: none; }
.launcher-fold > summary > .pk-icon { color: var(--text-muted); transition: transform var(--duration-fast); }
.launcher-fold[open] > summary > .pk-icon { transform: rotate(90deg); }
/* the instances, not the group's own row: its summary is a launcher-item too */
.launcher-fold > .launcher-item:not(summary) { padding-left: calc(var(--space-2) + 28px); }
.launcher-title .pk-badge { margin-left: var(--space-1); vertical-align: 1px; }
.launcher-pin { color: var(--text-muted); opacity: 0; }
.launcher-item:hover .launcher-pin, .launcher-pin:focus-visible { opacity: 1; }
.launcher-pin[aria-pressed="true"] { color: var(--accent-text); opacity: 1; }

.app-palette { width: min(640px, calc(100vw - 32px)); padding: var(--space-2); margin-top: 12vh; }
.app-palette[open] { display: flex; flex-direction: column; gap: var(--space-2); }
.app-palette-list { max-height: 52vh; overflow: auto; }
.palette-group { padding: var(--space-2) var(--space-2) var(--space-1); font-size: var(--text-xs); color: var(--text-muted); }
.palette-item {
  display: flex; align-items: center; gap: var(--space-2); padding: var(--space-2);
  font-size: var(--text-sm); color: var(--text-primary); border-radius: var(--radius-md); cursor: pointer;
}
.palette-item[aria-selected="true"] { background: var(--accent-subtle); }
.palette-item .pk-icon { color: var(--text-muted); }
.palette-hint { margin-left: auto; font-size: var(--text-xs); color: var(--text-muted); }
/* the label is what is searched for: a long hint (an agent's description) is cut, not the label. Only a label
   past 70% of the row is cut itself, so its hint (a session's agent) keeps the rest */
.palette-label { flex-shrink: 0; max-width: 70%; }
.picker-head { display: flex; align-items: center; flex-wrap: wrap; gap: var(--space-2); padding: var(--space-1) var(--space-2) 0; }
.picker-head .pk-dialog-title { font-size: var(--text-md); margin-right: auto; }
.picker-groups { display: flex; gap: var(--space-1); }
.picker-item { align-items: flex-start; }
.picker-check { flex: none; width: 16px; padding-top: 2px; }
.picker-check .pk-icon { color: var(--accent-text); }
.picker-text { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.picker-name { display: flex; align-items: center; gap: var(--space-2); min-width: 0; font-weight: 500; }
.picker-hint { font-size: var(--text-xs); color: var(--text-secondary); }
.picker-item .palette-hint { max-width: 40%; padding-top: 2px; }

/* -------------------------------------------------------------- status bar */

.app-status {
  display: flex; align-items: center; gap: var(--space-4); padding: 0 var(--space-3);
  font-size: var(--text-xs); color: var(--text-secondary);
  background: var(--surface-1); border-top: 1px solid var(--border-subtle);
}
.app-status-item { display: inline-flex; align-items: center; gap: var(--space-1); }
.floating-chip {
  display: inline-flex; align-items: center; gap: var(--space-1); height: 20px; padding: 0 var(--space-2);
  font: 400 var(--text-xs)/1 var(--font-ui); color: var(--text-secondary);
  background: var(--surface-3); border: 0; border-radius: 10px; cursor: pointer;
}

/* ------------------------------------------------------------------ mobile */

/* Below 961px sessions 272 + chat 360 + resizer 5 + dock 320 do not fit side by side.
 * shell.js reads "narrow" from here: the sessions pane turns fixed. */
@media (max-width: 960px) {
  .app-body { grid-template-columns: 1fr; }
  .app-sessions { position: fixed; inset: 48px auto 28px 0; width: min(320px, 86vw); z-index: 60; box-shadow: var(--shadow-overlay); }
  .app-body[data-sessions="closed"] .app-sessions { display: none; }
  .app-dock { position: fixed; inset: 48px 0 28px 0; width: auto; max-width: none; z-index: 55; }
  .app-body[data-panels="aside"] .app-dock, .app-body[data-panels="aside"] ~ .app-floating-layer { display: none; }
  .app-back { display: inline-flex; }
  .app-dock-resizer, .app-sessions-resizer { display: none; }
  .app-search span, .app-search kbd, .app-brand span, .app-session { display: none; }
  .app-search { width: auto; margin-left: auto; }
  .app-window { inset: 0 !important; width: auto !important; height: auto !important; border-radius: 0; }
}

/* The dock's help button: at the end of its bar, for the tab in front (workspace.js, openHelp) */
.app-dock-help { align-self: center; margin: 0 var(--space-1) 0 auto; flex: none; }
.app-dock-help[data-idle] { visibility: hidden; }  /* the tab in front has no guide: nothing to click, the place kept */
