/* Cowork Orchestrator dashboard styling. Dark + light themes, per-pane colors. */

/* ---------- THEMES ---------- */
:root, [data-theme="dark"] {
  --bg-0: #0b0d12;
  --bg-1: #11151c;
  --bg-2: #161b25;
  --bg-3: #1d2330;
  --bg-4: #262d3c;
  --line: #2a3142;
  --text: #e6ebf3;
  --muted: #8590a8;
  --accent: #00d488;
  --accent-2: #4cc9ff;
  --warn: #ffb33d;
  --danger: #ff5c5c;
  --ok: #5af07b;
  --info: #79b8ff;
  --term-bg: #000000;
  --shadow: 0 4px 16px rgba(0,0,0,.4);
}

[data-theme="light"] {
  --bg-0: #f5f6f8;
  --bg-1: #ffffff;
  --bg-2: #f0f2f5;
  --bg-3: #e6eaf0;
  --bg-4: #d9dfe8;
  --line: #cfd6e0;
  --text: #1a2030;
  --muted: #5b6b81;
  --accent: #00a86b;
  --accent-2: #0a7ad6;
  --warn: #d97a06;
  --danger: #d63838;
  --ok: #1f9d44;
  --info: #1a6fbf;
  --term-bg: #1a1f2b;       /* terminals stay dark even in light mode — easier on the eyes */
  --shadow: 0 2px 12px rgba(60,70,90,.18);
}

/* Per-pane accent palette. Pane N (0-indexed) picks --pane-color-N.
   Each pane's header tints to its color so projects are instantly distinguishable. */
:root {
  --pane-color-0: #00d488;   /* mint */
  --pane-color-1: #4cc9ff;   /* sky */
  --pane-color-2: #ffb33d;   /* amber */
  --pane-color-3: #ff7eb9;   /* pink */
  --pane-color-4: #c896ff;   /* lavender */
  --pane-color-5: #79b8ff;   /* blue */
  --pane-color-6: #ffe066;   /* yellow */
  --pane-color-7: #5af07b;   /* lime */
  --pane-color-8: #ff8a5c;   /* coral */
  --pane-color-9: #7adfff;   /* cyan */
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg-0);
  color: var(--text);
  font: 13px/1.4 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  /* Flex column for the whole page so the master input bar is always anchored
     at the bottom regardless of how tall the topbar / panes grow. Replaces
     the old calc(100vh - X - Y) approach which broke when contents overflowed. */
  display: flex;
  flex-direction: column;
  height: 100vh;
}
.topbar             { flex: 0 0 auto; }
.main               { flex: 1 1 auto; min-height: 0; }
.mobile-quickkeys   { flex: 0 0 auto; }
.orchestrator-bar   { flex: 0 0 auto; }

code, .cmd, pre { font: 12.5px/1.45 ui-monospace, "Cascadia Mono", "Consolas", "Menlo", monospace; }

button { font: inherit; cursor: pointer; }
input, textarea, select {
  font: inherit; color: var(--text); background: var(--bg-2);
  border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; outline: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent-2); }

.btn {
  background: var(--bg-3); color: var(--text); border: 1px solid var(--line);
  border-radius: 6px; padding: 6px 10px; transition: background .12s, border-color .12s;
}
.btn:hover { background: var(--bg-4); }
.btn.primary { background: var(--accent); color: #001a10; border-color: var(--accent); font-weight: 600; }
.btn.primary:hover { filter: brightness(.92); }
.btn.danger { background: var(--danger); color: #fff; border-color: var(--danger); font-weight: 600; }
.btn.danger:hover { filter: brightness(.92); }
.btn.small { padding: 3px 7px; font-size: 12px; }
.btn.full { width: 100%; }
.btn.ghost { background: transparent; }

.muted { color: var(--muted); }
.small { font-size: 12px; }
.hidden { display: none !important; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  height: 44px; padding: 0 12px; background: var(--bg-1);
  border-bottom: 1px solid var(--line); user-select: none;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { color: var(--accent); font-size: 14px; }
.brand-name { font-weight: 600; letter-spacing: .2px; }
.brand-status { font-size: 11px; color: var(--muted); padding: 2px 6px; border: 1px solid var(--line); border-radius: 4px; }
.brand-status.ok    { color: var(--ok);    border-color: var(--ok); }
.brand-status.warn  { color: var(--warn);  border-color: var(--warn); }
.brand-status.error { color: var(--danger); border-color: var(--danger); }

.toolbar { display: flex; align-items: center; gap: 8px; }
.layout-switcher { display: inline-flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.layout-switcher button {
  background: var(--bg-2); color: var(--muted); border: none; padding: 5px 9px; font-weight: 600;
  border-right: 1px solid var(--line);
}
.layout-switcher button:last-child { border-right: none; }
.layout-switcher button.active { background: var(--accent); color: #001a10; }

.audio-toggle { display: inline-flex; align-items: center; gap: 6px; padding: 4px 8px; background: var(--bg-3); border: 1px solid var(--line); border-radius: 6px; }
.audio-toggle input { accent-color: var(--accent); }

/* ---------- Main layout ---------- */
.main { display: grid; grid-template-columns: 1fr 320px; height: 100%; min-height: 0; }

.sidebar {
  background: var(--bg-1); border-left: 1px solid var(--line);
  display: flex; flex-direction: column;
}
.sidebar-tabs {
  display: flex; gap: 0; border-bottom: 1px solid var(--line);
}
.sidebar-tabs .tab {
  flex: 1; background: var(--bg-1); color: var(--muted); border: none; padding: 8px 6px;
  border-right: 1px solid var(--line); font-size: 12px;
}
.sidebar-tabs .tab:last-child { border-right: none; }
.sidebar-tabs .tab.active { color: var(--text); background: var(--bg-2); border-bottom: 2px solid var(--accent); }
.tab-panel { flex: 1; overflow-y: auto; padding: 8px; }
.badge { background: var(--danger); color: #fff; border-radius: 10px; padding: 0 6px; font-size: 10px; vertical-align: middle; margin-left: 4px; display: none; }
.badge:not(:empty):not(.zero) { display: inline-block; }

.alerts-controls { display: flex; gap: 6px; margin-bottom: 6px; }
.alerts-list, .projects-list, .snapshots-list, .templates-list, .rules-list { list-style: none; margin: 0; padding: 0; }
.alerts-list li {
  background: var(--bg-2); border: 1px solid var(--line); border-radius: 6px;
  padding: 8px 10px; margin-bottom: 6px; cursor: pointer; display: grid;
  grid-template-columns: auto 1fr auto; gap: 8px; align-items: start;
  min-width: 0; word-break: break-word; overflow: hidden;
}
.alerts-list li:hover { border-color: var(--accent-2); background: var(--bg-3); }
.alerts-list li.kind-needs-input  { border-left: 3px solid var(--warn); }
.alerts-list li.kind-error         { border-left: 3px solid var(--danger); }
.alerts-list li.kind-completed     { border-left: 3px solid var(--ok); }
.alerts-list li.kind-keyword       { border-left: 3px solid var(--accent-2); }
.alerts-list li.kind-disk-warning  { border-left: 3px solid var(--warn); }
.alerts-list .kind-icon { font-size: 14px; padding-top: 1px; }
.alerts-list .alert-title { font-weight: 600; font-size: 12px; line-height: 1.3; word-break: break-word; }
/* Snippets get truncated to 2 lines so a long Claude TUI capture can't
   blow the alert sidebar apart. -webkit-line-clamp works in Chrome/Safari. */
.alerts-list .alert-snippet {
  color: var(--muted); font-size: 11px; line-height: 1.35; margin-top: 2px;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  overflow: hidden; word-break: break-word;
}
.alerts-list .alert-time { font-size: 10px; color: var(--muted); white-space: nowrap; padding-top: 1px; }

.projects-list li, .snapshots-list li, .templates-list li, .rules-list li {
  padding: 8px 10px; margin-bottom: 4px; background: var(--bg-2); border: 1px solid var(--line);
  border-radius: 6px; display: flex; justify-content: space-between; gap: 8px; align-items: center;
  min-width: 0;
}
/* Project rows: stack name + path, allow path to shrink with ellipsis. */
.projects-list li > .proj-info { flex: 1 1 auto; min-width: 0; overflow: hidden; }
.projects-list li > .proj-info .proj-name { font-weight: 600; font-size: 13px; }
.projects-list li > .proj-info .proj-cwd {
  display: block; color: var(--muted); font-size: 11px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  direction: rtl; text-align: left;   /* rtl trick: keep the meaningful end (folder name) visible */
}
.projects-list .proj-actions { display: flex; gap: 4px; flex-shrink: 0; flex-wrap: wrap; justify-content: flex-end; }
.projects-list .proj-actions .btn.small { padding: 4px 8px; font-size: 11px; min-width: 56px; }
.snapshots-list li { flex-direction: column; align-items: stretch; gap: 4px; }
.snapshots-list .snap-row { display: flex; justify-content: space-between; gap: 6px; align-items: center; }
.snapshots-list .snap-meta { color: var(--muted); font-size: 11px; }
.snapshots-list .snap-actions { display: flex; gap: 4px; }
.templates-list li { cursor: pointer; flex-direction: column; align-items: stretch; gap: 4px; }
.templates-list li:hover { border-color: var(--accent-2); }
.templates-list .tmpl-row { display: flex; justify-content: space-between; gap: 6px; align-items: center; }
.rules-list li { font-size: 11px; }
.rules-list .rule-level { font-size: 10px; padding: 1px 5px; border-radius: 3px; background: var(--bg-3); }
.rules-list .lvl-irreversible { color: var(--danger); border: 1px solid var(--danger); }
.rules-list .lvl-destructive  { color: var(--warn);   border: 1px solid var(--warn); }
.rules-list .lvl-caution      { color: var(--info);   border: 1px solid var(--info); }

/* ---------- Terminal grid ---------- */
.terminal-grid { display: grid; gap: 6px; padding: 6px; background: var(--bg-0); height: 100%; position: relative; }
/* "Spawn here" placeholder for empty grid slots (added by JS when grid has fewer panes than the layout). */
.spawn-placeholder {
  display: flex; align-items: center; justify-content: center; flex-direction: column;
  background: var(--bg-1); border: 2px dashed var(--line); border-radius: 8px;
  color: var(--muted); cursor: pointer; gap: 8px; padding: 20px;
  transition: border-color .15s, color .15s;
}
.spawn-placeholder:hover { border-color: var(--accent); color: var(--text); }
.spawn-placeholder .ph-plus { font-size: 28px; line-height: 1; color: var(--accent); }
.spawn-placeholder .ph-label { font-size: 13px; font-weight: 500; }
.spawn-placeholder .ph-hint { font-size: 11px; color: var(--muted); text-align: center; }
.layout-1   { grid-template: 1fr / 1fr; }
.layout-2   { grid-template: 1fr / 1fr 1fr; }
.layout-4   { grid-template: 1fr 1fr / 1fr 1fr; }
.layout-6   { grid-template: 1fr 1fr / 1fr 1fr 1fr; }
.layout-9   { grid-template: 1fr 1fr 1fr / 1fr 1fr 1fr; }
.layout-10  { grid-template: 1fr 1fr / 1fr 1fr 1fr 1fr 1fr; }

.pane {
  display: flex; flex-direction: column; background: var(--bg-1);
  border: 1px solid var(--line); border-left: 4px solid var(--pane-color, var(--accent));
  border-radius: 8px; overflow: hidden; min-height: 0; min-width: 0; position: relative;
  transition: box-shadow .15s, border-color .15s;
}
.pane.focused { border-color: var(--pane-color, var(--accent)); box-shadow: 0 0 0 1px var(--pane-color, var(--accent)); }
.pane.alert-needs-input { border-color: var(--warn); animation: pulse-warn 1.4s infinite; }
.pane.alert-error       { border-color: var(--danger); animation: pulse-danger 1.4s infinite; }

@keyframes pulse-warn   { 0%,100%{box-shadow:0 0 0 1px var(--warn);} 50%{box-shadow:0 0 0 4px rgba(255,179,61,.18);} }
@keyframes pulse-danger { 0%,100%{box-shadow:0 0 0 1px var(--danger);} 50%{box-shadow:0 0 0 4px rgba(255,92,92,.2);} }

.pane-header {
  display: flex; align-items: center; justify-content: space-between; gap: 6px;
  padding: 4px 8px;
  background: linear-gradient(to right, color-mix(in srgb, var(--pane-color, var(--accent)) 18%, var(--bg-2)), var(--bg-2) 50%);
  border-bottom: 1px solid var(--line); user-select: none;
  flex-shrink: 0;
}
.pane-title { display: flex; align-items: center; gap: 6px; min-width: 0; flex: 1; }
.pane-title .led {
  display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--pane-color, var(--muted));
  box-shadow: 0 0 6px var(--pane-color, transparent);
}
.pane-title .led.needs-input { background: var(--warn); box-shadow: 0 0 6px var(--warn); }
.pane-title .led.error       { background: var(--danger); box-shadow: 0 0 6px var(--danger); }
.pane-title .led.exited      { background: var(--muted); box-shadow: none; }
.pane-title .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--text); }
.pane-title .id { color: var(--muted); font-size: 11px; }
.pane-title .cwd { color: var(--muted); font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.pane-actions { display: flex; gap: 4px; flex-shrink: 0; }
.pane-actions input[type=checkbox] { accent-color: var(--pane-color, var(--accent)); transform: translateY(1px); }
.pane-actions button { background: transparent; border: 1px solid var(--line); color: var(--muted); padding: 1px 6px; border-radius: 4px; font-size: 11px; }
.pane-actions button:hover { color: var(--text); border-color: var(--pane-color, var(--accent-2)); }

.pane-body { flex: 1; min-height: 0; min-width: 0; padding: 4px; background: var(--term-bg); }
.pane-body .xterm { height: 100%; }
.pane-body .xterm-viewport, .pane-body .xterm-screen { background: transparent !important; }

.pane-empty {
  display: flex; align-items: center; justify-content: center; flex-direction: column;
  color: var(--muted); height: 100%; gap: 8px; padding: 20px;
}

/* ---------- Orchestrator bar ---------- */
.orchestrator-bar {
  display: grid; grid-template-columns: auto auto 1fr auto; align-items: center; gap: 8px;
  height: 56px; padding: 0 12px; background: var(--bg-1); border-top: 1px solid var(--line);
  /* Defensive: keep it above any grid content that might overflow. */
  position: relative; z-index: 5;
}
.oc-label { color: var(--muted); font-size: 12px; }
.oc-targets { color: var(--accent-2); font-size: 12px; }
#oc-input { width: 100%; }

/* ---------- Modals ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 100;
  display: flex; align-items: center; justify-content: center;
  backdrop-filter: blur(2px);
}
[data-theme="light"] .modal-backdrop { background: rgba(40,50,70,.4); }
.modal {
  background: var(--bg-1); border: 1px solid var(--line); border-radius: 10px;
  width: min(620px, 92vw); max-height: 90vh; overflow: auto; box-shadow: var(--shadow);
}
.modal-head { padding: 12px 16px; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; font-size: 15px; }
.modal-head.warn { background: rgba(255,179,61,.1); border-bottom-color: var(--warn); }
.modal-body { padding: 14px 16px; display: flex; flex-direction: column; gap: 10px; }
.modal-body label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.modal-body input, .modal-body textarea, .modal-body select { color: var(--text); }
.modal-actions { padding: 10px 16px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 8px; }

.kv { display: grid; grid-template-columns: 110px 1fr; gap: 8px; align-items: start; font-size: 12px; }
.kv > span:first-child { color: var(--muted); }
.kv code, .kv b { word-break: break-all; }
.kv code.cmd { background: var(--bg-2); padding: 4px 6px; border-radius: 4px; display: block; }
.kv ul { margin: 0; padding-left: 18px; }

.warn-note { font-size: 12px; color: var(--warn); margin: 0; padding: 6px 8px; border: 1px dashed var(--warn); border-radius: 6px; }

/* xterm overrides */
.xterm .xterm-cursor-block { background-color: var(--accent) !important; }

/* ---------- Mobile quick-keys (only shown <720px) ---------- */
.mobile-quickkeys {
  display: none; gap: 4px; padding: 6px 8px; overflow-x: auto;
  background: var(--bg-1); border-top: 1px solid var(--line);
  scrollbar-width: none;
}
.mobile-quickkeys::-webkit-scrollbar { display: none; }
.mobile-quickkeys button {
  flex-shrink: 0; min-width: 44px; height: 36px;
  background: var(--bg-3); color: var(--text); border: 1px solid var(--line);
  border-radius: 6px; font-size: 14px; font-weight: 600;
  font-family: ui-monospace, "Cascadia Mono", "Consolas", monospace;
  padding: 0 10px;
}
.mobile-quickkeys button:active { background: var(--accent); color: #001a10; }

/* ---------- Mobile pane-tabs strip (only shown <720px via @media) ---------- */
.pane-tabs {
  display: none; gap: 4px; padding: 4px 6px; overflow-x: auto;
  background: var(--bg-1); border-bottom: 1px solid var(--line);
  scrollbar-width: none;
}
.pane-tabs::-webkit-scrollbar { display: none; }
.pane-tab {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 10px; background: var(--bg-2); border: 1px solid var(--line);
  border-left: 3px solid var(--pane-color, var(--accent));
  border-radius: 6px; color: var(--text); cursor: pointer; white-space: nowrap;
  font-size: 13px; font-weight: 500; flex-shrink: 0;
}
.pane-tab.active { background: var(--bg-3); border-color: var(--pane-color, var(--accent)); }
.pane-tab .pt-led {
  display: inline-block; width: 7px; height: 7px; border-radius: 50%;
  background: var(--pane-color, var(--muted));
}
.pane-tab .pt-led.needs-input { background: var(--warn); animation: pulse-warn 1.4s infinite; }
.pane-tab .pt-led.error       { background: var(--danger); animation: pulse-danger 1.4s infinite; }
.pane-tab .pt-close {
  background: transparent; border: none; color: var(--muted); padding: 0 4px;
  font-size: 16px; line-height: 1; border-radius: 3px;
}
.pane-tab .pt-close:hover, .pane-tab .pt-close:active { color: var(--danger); background: var(--bg-4); }
.pane-tab.empty {
  color: var(--muted); cursor: default; border-left-color: var(--muted); font-style: italic;
}

/* ---------------------------------------------------------------------------
 * Responsive layout for mobile / narrow screens (PWA mode on phones).
 * ------------------------------------------------------------------------- */
@media (max-width: 720px) {
  /* Mobile uses dynamic viewport height so the soft keyboard pushes content
     up correctly instead of disappearing under it. */
  body { height: 100dvh; }
  .mobile-quickkeys { display: flex; }
  .pane-tabs { display: flex; }
  .topbar { height: auto; min-height: 44px; padding: 4px 8px; flex-wrap: wrap; gap: 4px; }
  .brand-name { display: none; }
  .brand-status { font-size: 10px; padding: 1px 5px; }
  .toolbar { flex-wrap: wrap; gap: 4px; }
  /* Layout switcher is meaningless on mobile (we always show 1 pane). Hide it. */
  .toolbar .layout-switcher { display: none; }
  /* Master button label is too verbose on small screens — shorten. */
  #btn-master { font-size: 0; padding: 5px 8px; }
  #btn-master::before { content: '⚏'; font-size: 14px; }
  /* New-pane button: keep label short. */
  #btn-new-pane { padding: 5px 10px; }

  /* Mobile-only: show the pane tabs strip. */
  .pane-tabs { display: flex; }

  .main {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
    overflow: hidden;
  }
  /* Pane tabs sit between topbar and grid */
  #pane-tabs { grid-column: 1; grid-row: 1; }
  #terminal-grid { grid-column: 1; grid-row: 2; min-height: 0; overflow: hidden; }
  .sidebar {
    position: fixed; right: -340px; top: 0; bottom: 0; width: 320px; z-index: 50;
    transition: right .2s; border-left: 1px solid var(--line);
    grid-column: unset; grid-row: unset;
  }
  .sidebar.open { right: 0; }
  /* Sidebar toggle moved to top-right so it doesn't overlap the master-input
     bar at the bottom. Smaller too. */
  .sidebar-toggle {
    position: fixed; right: 8px; top: 8px; z-index: 51;
    background: var(--accent); color: #001a10; border: none; border-radius: 50%;
    width: 36px; height: 36px; font-size: 16px; box-shadow: var(--shadow);
  }

  .terminal-grid { padding: 4px; gap: 4px; }
  .terminal-grid:not(.layout-1) { grid-template: 1fr / 1fr !important; }
  .terminal-grid > .pane:not(.focused) { display: none; }
  /* When no pane is focused (e.g. all 10 already exist on first load), still
     render the first one rather than showing a blank screen. */
  .terminal-grid > .pane:first-child:not(.focused):not(.exited) { display: flex; }

  .pane-header { padding: 6px 8px; font-size: 13px; }
  /* Bigger tap targets for actions on mobile. */
  .pane-actions button { padding: 4px 10px; font-size: 14px; min-width: 36px; min-height: 32px; }

  .orchestrator-bar {
    grid-template-columns: 1fr auto; height: 50px; padding: 4px 6px; gap: 4px;
  }
  .orchestrator-bar .oc-label,
  .orchestrator-bar .oc-targets { display: none; }
  /* font-size 16+ stops iOS Safari from zooming the page on focus. */
  #oc-input { font-size: 16px; padding: 8px 10px; min-width: 0; }
  /* Shrink Send button on mobile so it doesn't squeeze the input. */
  #oc-send { padding: 8px 12px; font-size: 14px; min-height: 38px; flex-shrink: 0; }

  .modal { width: 96vw; max-height: 90vh; }
}

@supports (padding: max(0px)) {
  body { padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom); }
}
