:root {
  --term-bg: #0c0c0c;
  --border-color: #333333;
  --text-default: #cccccc;
  --text-bright: #ffffff;
  --text-dim: #767676;
  --pwsh-green: #16c60c;
  --dir-blue-bg: #005fb8;
  --selection-bg: #2b2b2b;
  --accent-cyan: #3b8eea;
  --accent-teal: #4ec9b0;
  --close-red: #e81123;
  --hover-subtle: #333333;
  --font-mono: "Cascadia Code", "Cascadia Mono", Consolas, "Courier New", monospace;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

body {
  background-color: #000000;
  color: var(--text-default);
  font-family: var(--font-mono);
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  min-height: 100vh;
  padding: 20px;
  overflow: hidden;
  background-image: 
    radial-gradient(circle at 50% 15%, rgba(30, 45, 65, 0.25) 0%, transparent 60%),
    radial-gradient(circle at 80% 80%, rgba(20, 20, 30, 0.4) 0%, transparent 70%);
}

/* Outer Window Frame */
.terminal-window {
  width: 100%;
  max-width: 1100px;
  height: calc(100vh - 40px);
  max-height: calc(100vh - 40px);
  background-color: var(--term-bg);
  border-radius: 8px;
  border: 1px solid var(--border-color);
  box-shadow: 0 16px 45px rgba(0, 0, 0, 0.85), 0 0 1px rgba(255, 255, 255, 0.1);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
  transition: all 0.2s cubic-bezier(0.1, 0.9, 0.2, 1);
}

/* Maximized Window State */
.terminal-window.maximized {
  max-width: 100vw;
  width: 100vw;
  height: 100vh;
  max-height: 100vh;
  border-radius: 0;
  border: none;
  box-shadow: none;
}

body.has-maximized {
  padding: 0;
}

/* Terminal Body Viewport - Constrained flex child with native overflow scroll */
.terminal-viewport {
  flex: 1 1 0;
  min-height: 0;
  background-color: var(--term-bg);
  padding: 24px 24px 30px 24px;
  overflow-y: auto !important;
  overflow-x: hidden;
  font-size: 14px;
  line-height: 1.5;
  color: var(--text-default);
  display: block; /* Pure block layout: prevents flex pinning/clipping on clear */
  position: relative;
}

.terminal-history {
  white-space: pre-wrap;
  word-break: break-word;
}

/* Prompt Line */
.active-line-wrapper {
  margin-top: 4px;
  position: relative;
}

.prompt-line {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  width: 100%;
}

.prompt-text {
  color: var(--text-bright);
  margin-right: 8px;
  white-space: nowrap;
  user-select: none;
}

.input-container {
  flex: 1;
  display: flex;
  align-items: center;
  min-width: 180px;
  position: relative;
}

.cli-input {
  background: transparent;
  border: none;
  outline: none;
  color: var(--text-bright);
  font-family: inherit;
  font-size: inherit;
  line-height: 1.5;
  width: 100%;
  caret-color: transparent; /* native caret replaced with authentic terminal block caret */
  padding: 0;
  margin: 0;
  position: relative;
  z-index: 1;
}

/* Authentic Windows Terminal Block Caret */
.custom-caret {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  left: 0;
  width: 9px;
  height: 18px;
  background-color: var(--text-bright);
  mix-blend-mode: difference;
  pointer-events: none;
  z-index: 2;
  animation: cursorBlink 1s steps(1) infinite;
  display: inline-block;
  box-sizing: border-box;
}

@keyframes cursorBlink {
  0%, 49% {
    opacity: 1;
  }
  50%, 100% {
    opacity: 0;
  }
}

/* Persistent Caret when Input Loses Focus / Window Blur (e.g. Save As dialog) */
.input-container:not(:focus-within) .custom-caret,
.cli-input:not(:focus) + .custom-caret,
.custom-caret.unfocused {
  opacity: 0.8 !important;
  animation: none !important;
  background-color: transparent !important;
  border: 1px solid var(--text-bright) !important;
  mix-blend-mode: normal !important;
}

/* Invisible text measurement mirror for sub-pixel caret tracking */
.caret-mirror {
  position: absolute;
  top: 0;
  left: 0;
  visibility: hidden;
  white-space: pre;
  font-family: inherit;
  font-size: inherit;
  line-height: 1.5;
  padding: 0;
  margin: 0;
  border: none;
  pointer-events: none;
  z-index: -1;
}

/* Compact 4-Line Scrollable Suggestions Frame (38px * 4 = 152px) */
.suggestion-box {
  background: #181818;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  max-height: 152px; /* Strictly 4 visible items */
  overflow-y: auto;
  overflow-x: hidden;
  margin-top: 6px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.7);
  z-index: 20;
}

.suggestion-item {
  height: 38px;
  min-height: 38px;
  max-height: 38px;
  box-sizing: border-box;
  padding: 0 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  border-left: 3px solid transparent;
  transition: background-color 0.1s ease;
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
}

.suggestion-item:hover,
.suggestion-item.active {
  background-color: var(--selection-bg);
  border-left-color: var(--accent-cyan);
}

.item-name, .cmd-text {
  display: flex;
  align-items: center;
  gap: 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.item-meta, .cmd-desc {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
  margin-left: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Pure ASCII Windows Terminal Badges */
.badge-dir {
  color: #4ec9b0;
  font-size: 11px;
  background: rgba(78, 201, 176, 0.15);
  padding: 2px 6px;
  border-radius: 3px;
  font-weight: 700;
  letter-spacing: 0.5px;
}

.badge-file {
  color: #e5c07b;
  font-size: 11px;
  background: rgba(229, 192, 123, 0.15);
  padding: 2px 6px;
  border-radius: 3px;
  font-weight: 700;
  letter-spacing: 0.5px;
}

.badge-pkt {
  color: #569cd6;
  font-size: 11px;
  background: rgba(86, 156, 214, 0.15);
  padding: 2px 6px;
  border-radius: 3px;
  font-weight: 700;
  letter-spacing: 0.5px;
}

/* Red Grep '$' Direct Clone Warning Box */
.grep-warning-box {
  margin-top: 8px;
  padding: 10px 14px;
  background-color: #241113;
  border: 1px solid #e81123;
  border-radius: 4px;
  color: #ff8585;
  font-size: 13px;
  line-height: 1.5;
  animation: fadeIn 0.15s ease-out;
}

.grep-warning-box .clone-cmd {
  color: #ffffff;
  background: #381518;
  padding: 2px 6px;
  border-radius: 3px;
  font-family: inherit;
  user-select: all;
  border: 1px solid rgba(232, 17, 35, 0.4);
}

.grep-warning-box .grep-actions {
  margin-top: 6px;
  font-size: 12px;
  color: #ffb3b3;
}

/* Offline Grep Results Viewport (Tier 1 Navigation) */
.grep-results-viewport {
  margin-top: 8px;
  max-height: 260px;
  overflow-y: auto;
  overflow-x: hidden;
  background: #0f141c;
  border: 1px solid #30363d;
  border-radius: 6px;
  font-family: "Cascadia Code", "Cascadia Mono", Consolas, monospace;
  font-size: 12px;
  line-height: 1.5;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.5);
  animation: fadeIn 0.15s ease-out;
}

.grep-result-item {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 4px 10px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: #c9d1d9;
  border-left: 2px solid transparent;
  transition: background 0.1s ease, color 0.1s ease;
}

.grep-result-item:hover {
  background: #161b22;
}

.grep-result-item.selected {
  background: #161b22 !important;
  border-left: 2px solid #5391fe !important;
  color: #5391fe !important;
}

.grep-result-item .grep-pointer {
  display: inline-block;
  width: 12px;
  font-weight: bold;
  color: #5391fe;
  flex-shrink: 0;
}

.grep-result-item .grep-path-line {
  color: #58a6ff;
  font-weight: 500;
  flex-shrink: 0;
}

.grep-result-item.selected .grep-path-line {
  color: #79c0ff;
}

.grep-result-item .grep-text {
  color: #8b949e;
  overflow: hidden;
  text-overflow: ellipsis;
}

.grep-result-item.selected .grep-text {
  color: #f0f6fc;
}

/* Windows 11 custom scrollbar for grep viewport */
.grep-results-viewport::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}
.grep-results-viewport::-webkit-scrollbar-track {
  background: #0d1117;
}
.grep-results-viewport::-webkit-scrollbar-thumb {
  background: #30363d;
  border-radius: 4px;
}
.grep-results-viewport::-webkit-scrollbar-thumb:hover {
  background: #484f58;
}

/* Tier 2 Context Action Box */
.grep-action-box {
  margin-top: 8px;
  padding: 10px 14px;
  background: #161b22;
  border: 1px solid #5391fe;
  border-radius: 6px;
  font-family: "Cascadia Code", "Cascadia Mono", Consolas, monospace;
  font-size: 12px;
  color: #f0f6fc;
  box-shadow: 0 4px 14px rgba(83, 145, 254, 0.25);
  animation: fadeIn 0.15s ease-out;
}

.grep-action-box .action-title {
  font-weight: 600;
  margin-bottom: 6px;
  color: #58a6ff;
}

.grep-action-box .action-options {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  color: #c9d1d9;
}

.grep-action-box .action-key {
  color: #5391fe;
  font-weight: bold;
}

/* Actions Panel */
.action-box {
  margin-top: 10px;
  padding: 12px 16px;
  background: #141414;
  border: 1px dashed #444444;
  border-radius: 6px;
  animation: fadeIn 0.15s ease-out;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

.action-header {
  margin-bottom: 8px;
  font-size: 13px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
}

.action-file-path {
  color: #ffffff;
  font-weight: 600;
  word-break: break-all;
}

.action-buttons-group {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin-top: 6px;
}

.action-btn {
  background: #202020;
  border: 1px solid #333333;
  color: var(--text-default);
  font-family: inherit;
  font-size: 12px;
  padding: 5px 10px;
  border-radius: 4px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  transition: all 0.15s ease;
}

.action-btn:hover {
  background: #2a2a2a;
  border-color: var(--accent-cyan);
  color: #ffffff;
}

.key-badge {
  color: var(--accent-teal);
  font-weight: bold;
}

/* Directory listing classes matching PowerShell */
.ps-listing {
  margin: 6px 0;
  font-family: var(--font-mono);
}

.ps-dir-path {
  color: #cccccc;
  margin-bottom: 6px;
}

.ps-header {
  color: var(--pwsh-green);
  font-weight: bold;
  user-select: none;
}

.ps-row {
  white-space: pre;
  line-height: 1.5;
}

.ps-dir,
.badge-folder {
  background-color: var(--dir-blue-bg);
  color: #ffffff;
  padding: 0 4px;
  border-radius: 2px;
  font-weight: bold;
}

.ps-file {
  color: #cccccc;
}

/* Terminal Status Bar */
.terminal-statusbar {
  height: 24px;
  min-height: 24px;
  flex-shrink: 0;
  background-color: #141414;
  border-top: 1px solid #222222;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 12px;
  font-size: 11px;
  color: var(--text-dim);
  user-select: none;
}

.status-left, .status-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.status-indicator {
  font-size: 10px;
  line-height: 1;
  font-weight: bold;
}

.status-indicator.online {
  color: #107c41;
}

.status-indicator.cached {
  color: #3b8eea;
}

.status-indicator.offline {
  color: #f7630c;
}

.status-badge {
  background: #1f1f1f;
  padding: 1px 6px;
  border-radius: 3px;
  color: #888888;
}

/* Native-styled scrollbar */
.terminal-viewport::-webkit-scrollbar,
.suggestion-box::-webkit-scrollbar {
  width: 8px;
}

.terminal-viewport::-webkit-scrollbar-track,
.suggestion-box::-webkit-scrollbar-track {
  background: transparent;
}

.terminal-viewport::-webkit-scrollbar-thumb,
.suggestion-box::-webkit-scrollbar-thumb {
  background: #333333;
  border-radius: 4px;
}

.terminal-viewport::-webkit-scrollbar-thumb:hover,
.suggestion-box::-webkit-scrollbar-thumb:hover {
  background: #444444;
}

/* Presence Widget & Popover (Hidden by default for unauthenticated users) */
.presence-widget-container {
  position: relative;
  display: none !important;
  align-items: center;
}

.presence-widget-container.authenticated {
  display: inline-flex !important;
}

/* Session Auto-Logout Timer Widget (Authenticated Users Only) */
#sessionTimerWidget,
.session-timer-item {
  display: none;
  align-items: center;
  gap: 4px;
  font-family: inherit;
  font-size: 11px;
  color: var(--status-fg, #858585);
  user-select: none;
}

#sessionTimerWidget.authenticated,
.session-timer-item.authenticated {
  display: inline-flex;
}

.session-timer-item .timer-label {
  color: #666666;
}

.session-timer-item .timer-clock {
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.5px;
}

/* Session Countdown Threshold States */
.session-timer-item.timer-warning,
.session-timer-item.timer-warning .timer-clock,
.session-timer-item.timer-warning .timer-label {
  color: #e5c07b !important;
}

.session-timer-item.timer-critical,
.session-timer-item.timer-critical .timer-clock,
.session-timer-item.timer-critical .timer-label {
  color: #e81123 !important;
  animation: timerPulse 1.5s infinite ease-in-out;
}

@keyframes timerPulse {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
}

/* Local VFS Status Badge */
.local-vfs-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: #858585;
  font-size: 11px;
  cursor: pointer;
  user-select: none;
  transition: color 0.15s ease;
}

.local-vfs-badge:hover {
  color: #cccccc;
}

.local-vfs-badge .vfs-status-dot {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background-color: #6a737d;
  transition: background-color 0.2s ease, box-shadow 0.2s ease;
}

.local-vfs-badge.mounted {
  color: #cccccc !important;
}

.local-vfs-badge.mounted .vfs-status-dot {
  background-color: #16c60c !important;
  box-shadow: 0 0 6px rgba(22, 198, 12, 0.5);
}

.live-presence-widget {
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  transition: background-color 0.15s ease, color 0.15s ease;
  user-select: none;
}

.live-presence-widget:hover {
  background: #2a2a2a;
  color: #ffffff;
}

.presence-dot {
  font-size: 10px;
  line-height: 1;
  color: #16c60c;
}

.presence-dot.pulse {
  animation: presence-pulse 2s infinite ease-in-out;
}

@keyframes presence-pulse {
  0% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.45; transform: scale(0.9); }
  100% { opacity: 1; transform: scale(1); }
}

#presencePopover,
.presence-popover {
  display: none;
  position: absolute;
  bottom: 28px;
  right: 0;
  min-width: 380px;
  width: max-content;
  max-width: 460px;
  box-sizing: border-box;
  overflow-x: hidden;
  padding: 10px 14px;
  background: #141414;
  border: 1px solid #333333;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.85);
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 11px;
  line-height: 1.5;
  color: #cccccc;
  z-index: 10000;
  pointer-events: auto;
}

#presencePopover.visible,
#presencePopover.pinned,
.presence-popover.visible,
.presence-popover.pinned,
.presence-widget-container:hover #presencePopover,
.presence-widget-container:hover .presence-popover {
  display: block;
}

.popover-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #282828;
  padding-bottom: 5px;
  margin-bottom: 6px;
}

.popover-title {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.5px;
  color: #4ec9b0;
}

.popover-close-btn {
  background: transparent;
  border: none;
  color: #858585;
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  padding: 0 2px;
}

.popover-close-btn:hover {
  color: #ffffff;
}

.presence-user-group {
  display: flex;
  flex-direction: column;
}

.presence-user-row,
.presence-item {
  display: flex;
  align-items: center;
  gap: 6px;
  line-height: 22px;
  font-family: inherit;
  font-size: 11px;
  width: 100%;
  box-sizing: border-box;
  white-space: nowrap;
}

.presence-toggle-slot {
  width: 18px;
  min-width: 18px;
  text-align: center;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  flex-shrink: 0;
}

.presence-toggle-btn {
  font-family: var(--font-mono);
  cursor: pointer;
  user-select: none;
  color: #5391fe;
  display: inline-block;
  line-height: 1;
}

.presence-toggle-spacer {
  width: 18px;
  min-width: 18px;
  display: inline-block;
  flex-shrink: 0;
}

.presence-tree-branch {
  font-family: var(--font-mono);
  color: #5391fe;
  font-size: 11px;
  user-select: none;
  flex-shrink: 0;
  display: inline-block;
  line-height: 1;
  white-space: pre;
}

.presence-status-dot,
.status-dot {
  font-size: 9px;
  width: 8px;
  height: 8px;
  min-width: 8px;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.presence-status-dot.active {
  color: #16c60c;
}

.presence-status-dot.idle {
  color: #e5c07b;
}

.presence-status-dot.offline {
  color: #e81123;
}

.presence-user-name {
  color: #cccccc;
  white-space: nowrap;
}

.presence-device-tag {
  color: #6a737d;
  font-size: 10px;
  margin-left: 2px;
  white-space: nowrap;
}

.presence-sub-sessions {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-top: 2px;
  margin-bottom: 3px;
  width: 100%;
}

.presence-subsession-row,
.presence-sub-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding-left: 28px;
  line-height: 20px;
  font-size: 11px;
  color: #858585;
  width: 100%;
  box-sizing: border-box;
  white-space: nowrap;
}

.presence-offline-row {
  display: flex;
  align-items: center;
  gap: 6px;
  line-height: 22px;
  font-size: 11px;
  width: 100%;
  box-sizing: border-box;
  white-space: nowrap;
}

.session-device {
  color: #cccccc;
  font-size: 10px;
}

.session-current-badge {
  color: #888888;
  font-style: italic;
  font-size: 10px;
}

.session-status-tag {
  font-size: 10px;
  font-family: inherit;
}

.session-status-tag.active {
  color: #16c60c;
}

.session-status-tag.idle {
  color: #e5c07b;
}

.end-session-btn,
.end-all-btn {
  background: #2d1517;
  color: #f85149;
  border: 1px solid #da3633;
  padding: 1px 7px;
  font-size: 10px;
  font-family: inherit;
  border-radius: 3px;
  cursor: pointer;
  margin-left: auto;
  flex-shrink: 0;
  white-space: nowrap;
  line-height: 1.2;
}

.end-session-btn:hover,
.end-all-btn:hover {
  background: #da3633;
  color: #ffffff;
}

.end-session-btn:disabled,
.end-all-btn:disabled {
  opacity: 0.6;
  cursor: wait;
}

.presence-section {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.presence-status-dot.idle {
  color: #e5c07b;
}

.presence-status-dot.offline {
  color: #e81123;
}

/* Responsive adjustments */
@media (max-width: 768px) {
  body {
    padding: 0;
  }
  .terminal-window {
    height: 100vh;
    max-height: 100vh;
    border-radius: 0;
    border: none;
  }
  .prompt-text {
    font-size: 12px;
  }
}
