* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
}

html {
  /* The single knob for every non-terminal size below, via `rem` — settings.js
     is the only thing that ever sets this (default 15px). The terminal's own
     size is completely separate (--terminal-font-size, read directly by
     terminal.js in px, never derived from `rem`), so this can't affect it. */
  font-size: var(--font-size-ui, 15px);
}

body {
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-ui);
  overflow: hidden;
}

#app {
  height: 100vh; /* fallback for browsers without dvh support */
  height: 100dvh; /* actual visible viewport on mobile Safari, unlike vh, which is sized for when the address bar is hidden and overflows off-screen while it's shown */
  display: flex;
  flex-direction: column;
}

#main-app {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

button {
  font-family: inherit;
  cursor: pointer;
}

[hidden] {
  display: none !important;
}

/* ---------- Login gate ---------- */

#login-screen {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-box {
  width: min(360px, 90vw);
  padding: 32px;
  border-radius: 12px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
}

.login-box h1 {
  margin: 0 0 4px;
  font-size: 1.4rem;
}

.login-box p.subtitle {
  margin: 0 0 20px;
  color: var(--muted);
  font-size: 0.85rem;
}

.login-box input {
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-terminal);
  font-size: 1rem;
}

.login-box button {
  width: 100%;
  margin-top: 12px;
  padding: 10px 12px;
  border-radius: 8px;
  border: none;
  background: var(--accent);
  color: var(--accent-fg);
  font-size: 1rem;
}

.login-box .error {
  margin-top: 10px;
  color: var(--danger);
  font-size: 0.85rem;
  min-height: 1.2em;
}

/* ---------- Top bar ---------- */

#topbar {
  flex: 0 0 auto;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 14px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

#topbar .connection-info {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 1rem;
}

#topbar .status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
  flex: none;
}

#topbar .status-dot[data-status='connected'] {
  background: var(--ansi-green);
}

#topbar .status-dot[data-status='connecting'],
#topbar .status-dot[data-status='reconnecting'] {
  background: var(--ansi-yellow);
}

#topbar .status-dot[data-status='disconnected'] {
  background: var(--danger);
}

#topbar .top-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

#topbar button.icon-btn {
  border: none;
  background: transparent;
  color: var(--fg);
  font-size: 1.3rem;
  line-height: 1;
  padding: 8px 12px;
  border-radius: 8px;
}

#topbar button.icon-btn:hover {
  background: var(--bg-elevated);
}

/* ---------- Workspace / panes ---------- */

#workspace {
  flex: 1;
  display: flex;
  flex-direction: row;
  min-height: 0;
  min-width: 0;
  padding: 10px;
  gap: 8px;
}

/* Two side-by-side narrow strips are unusable on a phone/tablet held
   upright, so a split stacks top/bottom instead — same `flex` ratios on the
   panes (session.js) work unchanged either way, since they apply along
   whichever axis is current the main axis. */
@media (orientation: portrait) {
  #workspace {
    flex-direction: column;
  }

  .split-handle {
    width: auto;
    height: 6px;
    cursor: row-resize;
  }
}

.pane {
  position: relative;
  min-width: 0;
  border-radius: 10px;
  overflow: hidden;
  background: var(--bg);
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.pane.focused {
  border-color: var(--accent);
}

.pane .pane-close {
  position: absolute;
  top: 6px;
  right: 6px;
  z-index: 5;
  border: none;
  background: var(--bg-elevated);
  color: var(--muted);
  width: 22px;
  height: 22px;
  border-radius: 50%;
  font-size: 0.8rem;
  line-height: 1;
  opacity: 0.6;
}

.pane .pane-close:hover {
  opacity: 1;
  color: var(--danger);
}

.pane .xterm-container {
  flex: 1;
  min-height: 0;
  padding: 6px 10px;
}

/* Some fonts (Fira Code in particular) define OpenType contextual-alternate
   rules for punctuation runs that go well beyond the usual "programming
   ligatures" — since xterm's DOM renderer merges consecutive same-style
   characters into one text run, that lets the browser's shaper apply those
   substitutions across an arbitrary run of terminal output, silently
   swapping in a glyph that isn't the character actually printed. Disabling
   ligature/contextual-alternate features keeps one glyph per character,
   which is what a terminal needs regardless of which font is picked. */
.xterm-rows {
  font-variant-ligatures: none;
  font-feature-settings: 'calt' 0, 'liga' 0, 'clig' 0, 'dlig' 0;
}

.split-handle {
  flex: none;
  width: 6px;
  border-radius: 3px;
  background: var(--border);
  cursor: col-resize;
}

.split-presets {
  position: absolute;
  bottom: 8px;
  right: 8px;
  z-index: 5;
  display: flex;
  gap: 4px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 3px;
}

.split-presets button {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 0.75rem;
  padding: 3px 7px;
  border-radius: 6px;
}

.split-presets button.active {
  background: var(--accent);
  color: var(--accent-fg);
}

/* ---------- Server picker (shown inside a pane before it connects) ---------- */

.picker {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
}

.picker h2 {
  margin: 0 0 16px;
  font-size: 1.2rem;
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 12px;
}

.server-card {
  position: relative;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  border-radius: 10px;
  padding: 16px;
  text-align: left;
}

.server-card button.connect {
  width: 100%;
  border: none;
  background: transparent;
  color: var(--fg);
  text-align: left;
  padding: 0;
  font-size: 1rem;
}

.server-card .host {
  font-weight: bold;
  word-break: break-all;
}

.server-card .port {
  color: var(--muted);
  font-size: 0.8rem;
  margin-top: 4px;
}

.server-card .delete {
  position: absolute;
  top: 8px;
  right: 8px;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 0.8rem;
}

.server-card .delete:hover {
  color: var(--danger);
}

.server-card.add-card {
  border-style: dashed;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
}

.server-card.add-card button {
  background: none;
  border: none;
  color: inherit;
  font-size: 1rem;
  width: 100%;
  height: 100%;
}

.add-server-form {
  margin-top: 16px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.add-server-form input {
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-terminal);
  font-size: 1rem;
}

.add-server-form input[name='host'] {
  flex: 1;
  min-width: 160px;
}

.add-server-form input[name='port'] {
  /* `em` (not `px`) so this keeps fitting its placeholder as UI text size changes */
  width: 6.5em;
}

.add-server-form button {
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-fg);
  padding: 8px 14px;
  font-size: 1rem;
}

/* ---------- Settings overlay ---------- */

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}

.settings-panel {
  width: min(420px, 92vw);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
}

.settings-panel h2 {
  margin: 0 0 16px;
  font-size: 1.2rem;
}

.settings-panel .field {
  margin-bottom: 14px;
}

.settings-panel label {
  display: block;
  margin-bottom: 6px;
  font-size: 0.9rem;
  color: var(--muted);
}

.settings-panel select {
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-ui);
  font-size: 1rem;
}

.settings-panel .stepper {
  display: flex;
  align-items: center;
  gap: 12px;
}

.settings-panel .stepper button {
  width: 36px;
  height: 36px;
  flex: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--fg);
  font-size: 1.2rem;
  line-height: 1;
}

.settings-panel .stepper button:disabled {
  opacity: 0.4;
  cursor: default;
}

.settings-panel .stepper .value {
  min-width: 3.5em;
  text-align: center;
  font-size: 1rem;
}

.settings-panel .snippet-list {
  max-height: 140px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-bottom: 8px;
}

.settings-panel .snippet-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 10px;
  font-size: 1rem;
  border-bottom: 1px solid var(--border);
}

.settings-panel .snippet-item:last-child {
  border-bottom: none;
}

.settings-panel .snippet-item button {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 0.95rem;
}

.settings-panel .snippet-item button.insert {
  color: var(--accent);
}

.settings-panel .snippet-item button.remove:hover {
  color: var(--danger);
}

.settings-panel .close-btn {
  margin-top: 4px;
  width: 100%;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: transparent;
  color: var(--fg);
  font-size: 1rem;
}

/* ---------- Mobile key toolbar ---------- */

#key-toolbar {
  flex: 0 0 auto;
  display: flex;
  gap: 6px;
  padding: 6px 10px;
  overflow-x: auto;
  background: var(--bg);
  border-top: 1px solid var(--border);
}

#key-toolbar button {
  flex: none;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--fg);
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 0.8rem;
}

#key-toolbar button.active {
  background: var(--accent);
  color: var(--accent-fg);
}

@media (min-width: 900px) {
  #key-toolbar {
    display: none;
  }
}
