:root {
  --bg: #1e1e2e;
  --bg2: #252536;
  --bg-hover: #2e2e42;
  --bg-selected: #3a3a5c;
  --fg: #cdd6f4;
  --fg-dim: #6c7086;
  --accent: #89b4fa;
  --green: #a6e3a1;
  --red: #f38ba8;
  --yellow: #f9e2af;
  --border: #45475a;
  --input-bg: #313244;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100vh;
  font-family: 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
  font-size: 13px;
  color: var(--fg);
  background: var(--bg);
  overflow: hidden;
  user-select: none;
}

body {
  display: flex;
  height: 100vh;
}

button {
  font-family: inherit;
  font-size: 12px;
  color: var(--fg);
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 10px;
  cursor: pointer;
  transition: background 0.15s;
}
button:hover { background: var(--bg-hover); }
button:active { background: var(--bg-selected); }

input[type=text], input[type=radio] {
  font-family: inherit;
  font-size: 13px;
  color: var(--fg);
}
input[type=text], textarea {
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 6px 8px;
  width: 100%;
  outline: none;
  color: var(--fg);
  font-family: inherit;
  font-size: 13px;
}
textarea {
  resize: vertical;
  min-height: 48px;
  font-family: 'Cascadia Code', 'Consolas', monospace;
  font-size: 12px;
}
input[type=text]:focus, textarea:focus { border-color: var(--accent); }

/* =========================================== Sidebar */
#sidebar {
  width: 260px;
  min-width: 180px;
  max-width: 500px;
  flex-shrink: 0;
  background: var(--bg2);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width 0.18s ease, min-width 0.18s ease;
}
#sidebar.resizing {
  transition: none;
}
#sidebar.collapsed {
  width: 0 !important;
  min-width: 0 !important;
  border-right: none;
}
#sidebar.collapsed > * { display: none; }

#sidebar-resizer {
  width: 4px;
  flex-shrink: 0;
  cursor: col-resize;
  background: var(--border);
  transition: background 0.15s;
  position: relative;
}
#sidebar-resizer:hover,
#sidebar-resizer.dragging { background: var(--accent); }
#sidebar-resizer.hidden { display: none; }

/* Floating expand button shown when sidebar is collapsed */
.icon-btn.floating {
  position: fixed;
  top: 8px;
  left: 8px;
  z-index: 10;
  width: 24px;
  height: 24px;
  padding: 0;
  line-height: 1;
}
.icon-btn.floating.hidden { display: none; }

#sidebar-header {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sidebar-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.sidebar-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--accent);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.icon-btn {
  padding: 2px 6px;
  font-size: 11px;
  background: transparent;
  border: 1px solid transparent;
  color: var(--fg-dim);
  line-height: 1;
}
.icon-btn:hover {
  background: var(--bg-hover);
  color: var(--fg);
  border-color: var(--border);
}

.sidebar-actions {
  display: flex;
  gap: 6px;
}

.sidebar-actions button {
  flex: 1;
}

#session-list {
  flex: 1;
  overflow-y: auto;
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.session-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  position: relative;
}
.session-card:hover { background: var(--bg-hover); border-color: var(--accent); }
.session-card.selected { background: var(--bg-selected); border-color: var(--accent); }
.session-card.dragging { opacity: 0.4; }
.session-card.drop-above { box-shadow: 0 -2px 0 0 var(--accent); }
.session-card.drop-below { box-shadow: 0  2px 0 0 var(--accent); }

.drag-handle {
  color: var(--fg-dim);
  font-size: 12px;
  cursor: grab;
  padding-right: 2px;
  user-select: none;
}
.drag-handle:active { cursor: grabbing; }
.session-card:hover .drag-handle { color: var(--fg); }

.session-card-top {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 4px;
}

.session-name {
  font-weight: 600;
  color: var(--fg);
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.badge {
  font-size: 10px;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: 10px;
  letter-spacing: 0.5px;
}
.badge.local { background: rgba(166, 227, 161, 0.2); color: var(--green); }
.badge.ssh   { background: rgba(249, 226, 175, 0.2); color: var(--yellow); }
.badge.count { background: rgba(137, 180, 250, 0.2); color: var(--accent); }

.session-desc {
  font-size: 11px;
  color: var(--fg-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-bottom: 4px;
}

.session-actions {
  display: none;
  gap: 4px;
  margin-top: 4px;
}
.session-card.selected .session-actions { display: flex; }
.session-actions button {
  padding: 2px 8px;
  font-size: 11px;
}

/* =========================================== Main */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg);
}

#tab-bar {
  height: 34px;
  flex-shrink: 0;
  border-bottom: 1px solid var(--border);
  background: var(--bg2);
  display: flex;
  align-items: flex-end;
  overflow: hidden;
}

#tabs {
  display: flex;
  flex: 1;
  overflow-x: auto;
  overflow-y: hidden;
  height: 100%;
  align-items: flex-end;
  scrollbar-width: thin;
}

.tab {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 10px;
  height: 28px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-bottom: none;
  border-top-left-radius: 6px;
  border-top-right-radius: 6px;
  margin-right: 2px;
  margin-top: 6px;
  cursor: pointer;
  min-width: 120px;
  max-width: 220px;
  font-size: 12px;
  color: var(--fg-dim);
  flex-shrink: 0;
}
.tab.active {
  background: var(--bg);
  color: var(--fg);
  border-color: var(--accent);
  border-bottom: 1px solid var(--bg);
  margin-bottom: -1px;
}
.tab:hover:not(.active) { background: var(--bg-hover); color: var(--fg); }
.tab.dragging { opacity: 0.4; }
.tab.drop-before { box-shadow: -2px 0 0 0 var(--accent); }
.tab.drop-after  { box-shadow:  2px 0 0 0 var(--accent); }

.tab-status {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--green);
  flex-shrink: 0;
}
.tab.dead .tab-status { background: var(--red); }

.tab-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tab-close {
  display: none;
  width: 16px;
  height: 16px;
  line-height: 14px;
  text-align: center;
  border-radius: 3px;
  font-size: 14px;
  color: var(--fg-dim);
}
.tab:hover .tab-close,
.tab.active .tab-close { display: inline-block; }
.tab-close:hover { background: var(--red); color: white; }

#terminal-area {
  flex: 1;
  position: relative;
  background: var(--bg);
  min-height: 0;
}

.terminal-container {
  position: absolute;
  inset: 0;
  padding: 4px;
  display: none;
}
.terminal-container.active { display: block; }
.terminal-container .xterm { height: 100%; }
.terminal-container .xterm-viewport { background: var(--bg) !important; }

#welcome {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--fg-dim);
  text-align: center;
  gap: 10px;
}
#welcome.hidden { display: none; }
#welcome h1 {
  color: var(--accent);
  font-size: 28px;
  margin: 0;
}
#welcome p { margin: 0; }
#welcome .hint {
  font-size: 11px;
  font-family: 'Cascadia Code', 'Consolas', monospace;
}

/* =========================================== Modal */
#modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
#modal-overlay.hidden { display: none; }

#session-editor {
  width: 480px;
  max-height: 90vh;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.editor-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  font-weight: 600;
  color: var(--accent);
}
.editor-header button {
  border: none;
  background: transparent;
  font-size: 20px;
  color: var(--fg-dim);
  padding: 0 8px;
}
.editor-header button:hover { color: var(--red); background: transparent; }

#editor-form {
  padding: 14px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.field { display: flex; flex-direction: column; gap: 4px; }
.field label {
  font-size: 11px;
  color: var(--fg-dim);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.field label .dim { text-transform: none; letter-spacing: 0; opacity: 0.7; }

.radio-row {
  display: flex;
  gap: 16px;
  padding: 4px 0;
}

.check-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--fg);
  text-transform: none;
  letter-spacing: 0;
  cursor: pointer;
  padding: 2px 0;
}
.check-row input[type=checkbox] { cursor: pointer; }
.radio-row label {
  font-size: 13px;
  color: var(--fg);
  text-transform: none;
  letter-spacing: 0;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
}

.ssh-only.hidden { display: none; }

.editor-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding-top: 6px;
  border-top: 1px solid var(--border);
  margin-top: 4px;
  padding-top: 12px;
}
#editor-save {
  background: var(--accent);
  color: var(--bg);
  border-color: var(--accent);
  font-weight: 600;
}
#editor-save:hover { background: #a9c7ff; }

/* =========================================== Context menu */
#context-menu {
  position: fixed;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px;
  min-width: 140px;
  z-index: 200;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
}
#context-menu.hidden { display: none; }
.menu-item {
  padding: 6px 12px;
  cursor: pointer;
  border-radius: 4px;
  font-size: 12px;
}
.menu-item:hover { background: var(--bg-hover); }
.menu-item.danger:hover { background: var(--red); color: white; }
.menu-sep {
  height: 1px;
  background: var(--border);
  margin: 4px 2px;
}

/* =========================================== Notifications */
.notification {
  position: fixed;
  bottom: 16px;
  right: 16px;
  padding: 10px 14px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-left-width: 4px;
  border-radius: 4px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  font-size: 12px;
  color: var(--fg);
  z-index: 500;
  animation: slide-in 0.2s ease-out;
}
.notification.success { border-left-color: var(--green); }
.notification.error   { border-left-color: var(--red); }
.notification.info    { border-left-color: var(--accent); }

@keyframes slide-in {
  from { transform: translateX(20px); opacity: 0; }
  to   { transform: translateX(0); opacity: 1; }
}

/* =========================================== Scrollbars */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover { background: var(--fg-dim); }

/* =========================================== Auth (login/register) screen */
#auth-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}
#auth-overlay.hidden { display: none; }

#auth-box {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 24px;
  width: min(420px, 92vw);
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.auth-form.hidden { display: none; }

.auth-title {
  color: var(--accent);
  font-weight: 600;
  font-size: 18px;
}
.auth-sub {
  margin: 0 0 4px;
  font-size: 12px;
  color: var(--fg-dim);
  line-height: 1.5;
}
.auth-sub strong {
  color: var(--yellow);
  font-family: 'Cascadia Code', 'Consolas', monospace;
}

.auth-form label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 11px;
  color: var(--fg-dim);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.auth-form input[type=text],
.auth-form input[type=password] {
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 8px 10px;
  color: var(--fg);
  outline: none;
  font-size: 14px;
  font-family: 'Cascadia Code', 'Consolas', monospace;
  text-transform: none;
  letter-spacing: 0;
}
.auth-form input:focus { border-color: var(--accent); }
.auth-form .hint {
  text-transform: none;
  letter-spacing: 0;
  color: var(--fg-dim);
  font-size: 11px;
}
.auth-form .hint code {
  background: var(--input-bg);
  padding: 0 4px;
  border-radius: 3px;
}

.auth-form button.primary {
  background: var(--accent);
  color: var(--bg);
  border-color: var(--accent);
  font-weight: 600;
  padding: 10px 14px;
  margin-top: 4px;
  min-height: 36px;
}
.auth-form button.primary:hover { background: #a9c7ff; }

.auth-error.error {
  color: var(--red);
  font-size: 12px;
  min-height: 14px;
  word-wrap: break-word;
}

.auth-loading {
  color: var(--fg-dim);
  text-align: center;
  font-size: 13px;
  padding: 20px;
}
.auth-loading.hidden { display: none; }

/* =========================================== Mobile / narrow viewport */
@media (max-width: 600px) {
  body { font-size: 14px; }

  /* On phones the sidebar covers the whole screen as an overlay so
     terminal can use the full width when shown. Toggling .collapsed
     hides it; the floating expand button brings it back. */
  #sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: 80vw !important;
    max-width: 320px;
    z-index: 50;
    box-shadow: 4px 0 16px rgba(0, 0, 0, 0.4);
  }
  #sidebar-resizer { display: none; }
  #main { width: 100vw; }

  .icon-btn.floating {
    width: 36px;
    height: 36px;
    font-size: 16px;
    background: var(--bg2);
    border: 1px solid var(--border);
  }

  .tab { min-width: 90px; max-width: 160px; }
  .tab-close { display: inline-block; }

  #session-editor { width: 95vw; max-height: 95vh; }

  button { min-height: 32px; padding: 6px 12px; }
  .session-card { padding: 12px; }
  .session-actions button { padding: 6px 10px; font-size: 12px; }

  html, body { user-select: none; -webkit-user-select: none; }
  .terminal-container, .terminal-container * { user-select: text; -webkit-user-select: text; }
}
