/* layout.css — webchat redesigned frame (grid, rail, sessions-sidebar, top-bar) */

/* Top-level grid: rail | [top-bar / sidebar+main] */
body {
  display: grid;
  grid-template-columns: 56px auto 1fr;
  grid-template-rows: 44px 1fr;
  height: 100vh;
  height: 100dvh;
  padding-top: env(safe-area-inset-top);
  overflow: hidden;
}

/* ===== Nav rail ===== */
#nav-rail {
  grid-row: 1 / -1;
  grid-column: 1;
  background: var(--surface-chrome, var(--surface));
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px 0;
  gap: 10px;
  z-index: 20;
}

#nav-rail .rail-expand {
  /* Subtle expand chevron shown only when sessions sidebar is collapsed */
  opacity: 0;
  pointer-events: none;
  height: 0;
  overflow: hidden;
  transition: opacity 0.2s, height 0.2s;
  background: transparent;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 16px;
}
body.sidebar-collapsed #nav-rail .rail-expand {
  opacity: 1;
  pointer-events: auto;
  height: 28px;
  color: var(--text);
}
body.sidebar-collapsed #nav-rail .rail-expand:hover { opacity: 1; color: var(--accent); }

#nav-rail .rail-logo {
  width: 36px; height: 36px;
  cursor: default;
  position: relative;
  flex-shrink: 0;
}
#nav-rail .rail-logo img { width: 100%; height: 100%; object-fit: contain; }
#nav-rail .rail-logo .logo-tooltip {
  visibility: hidden; opacity: 0;
  position: absolute; left: 100%; top: 50%; transform: translateY(-50%);
  margin-left: 10px;
  background: var(--surface2); color: var(--accent); border: 1px solid var(--border);
  border-radius: 4px; padding: 2px 8px; font-size: 13px; font-weight: 600;
  white-space: nowrap; letter-spacing: 1px;
  transition: opacity 0.2s;
  pointer-events: none;
  z-index: 30;
}
#nav-rail .rail-logo:hover .logo-tooltip { visibility: visible; opacity: 1; }

#nav-rail .rail-agent {
  width: 40px; height: 28px;
  border-radius: var(--radius-sm);
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 11px;
  font-family: inherit;
  font-weight: 600;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  text-overflow: ellipsis;
  overflow: hidden;
}
#nav-rail .rail-agent:hover { border-color: var(--accent); color: var(--accent); }

#nav-rail .rail-divider {
  width: 24px; height: 1px;
  background: var(--border);
  margin: 6px 0;
}

#nav-rail .rail-section {
  width: 40px; height: 40px;
  border: none; background: transparent;
  color: var(--text-dim);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 18px;
  position: relative;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.15s, color 0.15s;
}
#nav-rail .rail-section:hover { background: var(--chip-bg); color: var(--text); }
#nav-rail .rail-section.active {
  background: var(--accent-subtle);
  color: var(--accent);
}
#nav-rail .rail-section.active::before {
  content: '';
  position: absolute;
  left: -12px; top: 25%; bottom: 25%;
  width: 3px;
  background: var(--accent);
  border-radius: 0 2px 2px 0;
}
#nav-rail .rail-section .rail-tooltip {
  visibility: hidden; opacity: 0;
  position: absolute; left: 100%; top: 50%; transform: translateY(-50%);
  margin-left: 10px;
  background: var(--surface2); color: var(--text); border: 1px solid var(--border);
  border-radius: 4px; padding: 4px 10px; font-size: 12px; font-weight: 600;
  white-space: nowrap;
  pointer-events: none;
  transition: opacity 0.15s;
  z-index: 30;
}
#nav-rail .rail-section:hover .rail-tooltip { visibility: visible; opacity: 1; }

/* ===== Agent popover ===== */
.agent-popover {
  position: absolute;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  padding: 4px;
  min-width: 160px;
  z-index: 50;
}
.agent-popover button {
  display: block; width: 100%; text-align: left;
  background: transparent; border: none;
  padding: 8px 12px;
  color: var(--text); font-family: inherit; font-size: 13px; font-weight: 500;
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.agent-popover button:hover { background: var(--chip-bg); color: var(--accent); }
.agent-popover button.active { background: var(--accent-subtle); color: var(--accent); }

/* ===== Sessions sidebar ===== */
#sessions-sidebar {
  grid-row: 2;
  grid-column: 2;
  width: 260px;
  background: var(--surface-chrome, var(--surface));
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  overflow: hidden;
  transition: width 0.2s ease;
}
body.sidebar-collapsed #sessions-sidebar {
  width: 0;
  border-right: none;
}
body.section-schedules #sessions-sidebar {
  display: none;
}

#sessions-sidebar .sidebar-header {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
#sessions-sidebar .sidebar-collapse {
  background: transparent; border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 18px;
  font-weight: 700;
  padding: 4px 6px;
  border-radius: 4px;
}
#sessions-sidebar .sidebar-collapse:hover { color: var(--accent); background: var(--chip-bg); }

#sessions-sidebar .new-chat-btn {
  flex: 1;
  background: var(--accent-subtle);
  color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 20px;
  padding: 5px 14px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.02em;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center; gap: 6px;
  transition: all 0.2s ease;
}
#sessions-sidebar .new-chat-btn:hover { background: var(--accent); color: #fff; border-color: var(--accent); transform: translateY(-1px); }

#sessions-sidebar .session-list {
  flex: 1;
  overflow-y: auto;
  padding: 6px 8px;
}
#sessions-sidebar .session-item {
  display: block; width: 100%; text-align: left;
  background: transparent; border: none;
  padding: 8px 10px;
  margin: 2px 0;
  border-radius: var(--radius-sm);
  color: var(--text);
  cursor: pointer;
  position: relative;
  font-family: inherit;
}
#sessions-sidebar .session-item:hover { background: var(--chip-bg); }
#sessions-sidebar .session-item.active {
  background: var(--accent-subtle);
}
#sessions-sidebar .session-item.active::before {
  content: '';
  position: absolute;
  left: 0; top: 6px; bottom: 6px;
  width: 3px;
  background: var(--accent);
  border-radius: 0 2px 2px 0;
}
#sessions-sidebar .session-title {
  display: block;
  font-size: 14px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text);
}
#sessions-sidebar .session-item.active .session-title { color: var(--accent); }
#sessions-sidebar .session-summary {
  display: block;
  font-size: 13px;
  color: var(--text-secondary, #555);
  margin-top: 2px;
  position: relative;
}
#sessions-sidebar .session-summary.typing::after {
  content: '';
  position: absolute;
  width: 1.5px;
  height: 1em;
  background: var(--accent);
  animation: cursor-blink 0.6s step-end infinite;
}
@keyframes cursor-blink {
  50% { opacity: 0; }
}
#sessions-sidebar .session-time {
  display: block;
  font-size: 11px;
  color: var(--text-secondary, #555);
  font-style: italic;
  margin-top: 2px;
  text-align: right;
}
/* Delete button — hidden by default, revealed on hover */
#sessions-sidebar .session-delete {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  background: var(--surface, #fff);
  border: 1px solid var(--border, #ddd);
  opacity: 0;
  pointer-events: none;
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
  transition: opacity 0.15s ease, background 0.15s ease;
  color: var(--text-dim);
  user-select: none;
  white-space: nowrap;
  z-index: 1;
}
#sessions-sidebar .session-delete.reveal {
  opacity: 1;
  pointer-events: auto;
}
#sessions-sidebar .session-delete:hover {
  background: #3c1111;
  border-color: #e53935;
  color: #ef5350;
}
html.light #sessions-sidebar .session-delete:hover {
  background: #fbe9e7;
  border-color: #e53935;
  color: #e53935;
}
#sessions-sidebar .session-delete .confirm-tooltip {
  position: absolute;
  bottom: calc(100% + 6px);
  right: 0;
  background: #333;
  color: #fff;
  font-size: 11px;
  font-weight: 500;
  padding: 3px 8px;
  border-radius: 4px;
  white-space: nowrap;
  pointer-events: none;
  animation: tooltip-in 0.15s ease;
}
#sessions-sidebar .session-delete .confirm-tooltip::after {
  content: '';
  position: absolute;
  top: 100%;
  right: 10px;
  border: 4px solid transparent;
  border-top-color: #333;
}
@keyframes tooltip-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}
#sessions-sidebar .session-delete[data-confirm="1"] {
  opacity: 1;
  pointer-events: auto;
  width: auto;
  padding: 0 8px;
  background: #e53935;
  box-shadow: 0 0 0 2px var(--surface, #fff);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  overflow: visible;
}

#sessions-sidebar .session-empty {
  padding: 16px 10px;
  color: var(--text-dim);
  font-size: 13px;
  font-style: italic;
  text-align: center;
}

/* ===== Top bar ===== */
#top-bar {
  grid-row: 1;
  grid-column: 2 / -1;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  background: var(--surface-chrome, var(--surface));
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}
#top-bar .topbar-left {
  display: flex; align-items: center; gap: 10px;
  min-width: 0;
}
#top-bar .topbar-right {
  display: flex; align-items: center; gap: 8px;
  margin-left: auto;
}
#top-bar select {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 5px 10px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  max-width: 220px;
  text-overflow: ellipsis;
}
#top-bar select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-subtle);
}
#top-bar .topbar-workspace-label {
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
}
#top-bar .topbar-user {
  font-size: 14px;
  font-weight: 700;
  color: var(--accent);
  white-space: nowrap;
}
#top-bar button:not(.ws-dropdown-trigger):not(.ws-dropdown-item):not(.pty-badge) {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 5px 12px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.15s ease;
}
#top-bar button:not(.ws-dropdown-trigger):not(.ws-dropdown-item):not(.pty-badge):hover { background: var(--accent-subtle); color: var(--accent); border-color: var(--accent); }

/* ===== Main content ===== */
#main {
  grid-row: 2;
  grid-column: 3;
  display: flex; flex-direction: column;
  overflow: hidden;
  min-width: 0;
}
body.section-schedules #main { grid-column: 2 / -1; }
body.section-chat #main { grid-column: 3; }
body.section-workspaces #main { grid-column: 2 / -1; }
body.section-workspaces #workspacesPanel { display: block; }
body.section-workspaces #sessions-sidebar { display: none; }
body.section-tools #main { grid-column: 2 / -1; }
body.section-tools #toolsPanel { display: block; }
body.section-tools #sessions-sidebar { display: none; }
body.section-tools #messages,
body.section-tools #inputArea { display: none; }
#toolsPanel { display: none; }

/* ===== Responsive ===== */
/* Off-canvas drawer backdrop — inert until the mobile breakpoint activates it. */
.sidebar-backdrop { display: none; }
/* Chevron badge hinting the Chat icon opens the drawer — mobile-only. */
.rail-drawer-hint { display: none; }

@media (max-width: 700px) {
  #top-bar { padding: 0 10px; gap: 8px; }
  #top-bar .topbar-user { display: none; }
}
@media (max-width: 520px) {
  body { grid-template-columns: 56px 0 1fr; }

  /* The sessions sidebar becomes an off-canvas drawer that slides in over the
     chat (triggered by the rail's Chat icon — see nav-rail.js), so New chat and
     chat switching stay reachable in portrait without a docked sidebar eating
     horizontal space. The `.sidebar-collapsed` selector is included to override
     the docked-state width:0 rule at equal specificity. */
  body.sidebar-collapsed #sessions-sidebar,
  #sessions-sidebar {
    position: fixed;
    top: calc(44px + env(safe-area-inset-top));
    left: 0;
    bottom: 0;
    width: 260px;
    border-right: 1px solid var(--border);
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: var(--shadow-lg);
    z-index: 40;
  }
  body.drawer-open #sessions-sidebar { transform: translateX(0); }

  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
    z-index: 39;
  }
  body.drawer-open .sidebar-backdrop { opacity: 1; pointer-events: auto; }

  /* Accent pill on the Chat icon signalling it fans out the sessions drawer.
     Rotates 180° (» → «) while the drawer is open to read as "tap to close". */
  .rail-section[data-section="chat"] .rail-drawer-hint {
    display: block;
    position: absolute;
    right: 3px;
    bottom: 1px;
    font-size: 10px;
    font-weight: 700;
    line-height: 1;
    color: currentColor;
    opacity: 0.55;
    transition: transform 0.2s ease;
    pointer-events: none;
  }
  body.drawer-open .rail-section[data-section="chat"] .rail-drawer-hint {
    transform: rotate(180deg);
  }

  #top-bar button:not(.ws-dropdown-trigger):not(.ws-dropdown-item) { padding: 4px 8px; font-size: 12px; }
  /* Prevent iOS Safari from auto-zooming (and shifting the layout) when the
     message field is focused — inputs under 16px trigger the zoom. */
  .input-row textarea { font-size: max(16px, calc(16px * var(--font-scale))); }
}
