:root {
  color-scheme: dark only;
  --bg: #070b10;
  --bg-elevated: #0c1118;
  --panel: #121a24;
  --line: #1e2a3a;
  --ink: #e8f1f8;
  --muted: #8aa0b5;
  --teal: #2dd4bf;
  --teal-dim: #149e8c;
  --danger: #f07178;
  --ok: #7fd962;
  --warn: #e6c07b;
  --radius: 14px;
  --font: "SF Pro Text", "Segoe UI", system-ui, sans-serif;
  --mono: "SF Mono", "JetBrains Mono", ui-monospace, Menlo, monospace;
  --field: #0a1018;
}

* { box-sizing: border-box; }
html {
  background: #070b10;
  color-scheme: dark only;
}
html, body {
  margin: 0;
  height: 100%;
  height: 100dvh;
  background: #070b10;
  background-image: radial-gradient(1000px 520px at 12% -8%, #122033 0%, #070b10 58%);
  color: var(--ink);
  font-family: var(--font);
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}
body {
  min-height: 100%;
  min-height: 100dvh;
  /* iOS Safari: pin to visual viewport when keyboard opens */
  height: var(--app-height, 100dvh);
  overflow: hidden;
  background-color: #070b10;
  color: var(--ink);
}

/* Force dark controls even when the phone is in light mode */
input, textarea, select, button {
  color-scheme: dark;
}
input, textarea, select {
  background-color: var(--field);
  color: var(--ink);
  caret-color: var(--teal);
}
input::placeholder, textarea::placeholder {
  color: #6b8299;
  opacity: 1;
}
/* Kill Safari / Chrome autofill yellow/white flash */
input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus,
textarea:-webkit-autofill,
select:-webkit-autofill {
  -webkit-text-fill-color: var(--ink) !important;
  caret-color: var(--teal);
  box-shadow: 0 0 0 1000px var(--field) inset !important;
  transition: background-color 99999s ease-out;
}
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
  background: #070b10;
}
::-webkit-scrollbar-thumb {
  background: #243447;
  border-radius: 8px;
}
::-webkit-scrollbar-thumb:hover { background: #2f4258; }
::selection {
  background: #1e3a36;
  color: #e8f1f8;
}
body.is-touch {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.app {
  height: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px 14px calc(10px + env(safe-area-inset-bottom, 0px));
  padding-left: max(14px, env(safe-area-inset-left, 0px));
  padding-right: max(14px, env(safe-area-inset-right, 0px));
  padding-top: max(12px, env(safe-area-inset-top, 0px));
}
.app > .work {
  flex: 1 1 auto;
  min-height: 0;
}
.mobile-only { display: none !important; }

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0,0,0,.35);
}
h1 {
  margin: 0;
  font-size: 1.15rem;
  letter-spacing: -0.02em;
  font-weight: 650;
}
.sub { margin: 2px 0 0; color: var(--muted); font-size: 0.82rem; }

.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.btn {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  border-radius: 999px;
  padding: 8px 14px;
  font: inherit;
  font-size: 0.86rem;
  cursor: pointer;
  transition: .15s ease;
}
.btn:hover { border-color: #2f4258; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary {
  background: linear-gradient(180deg, var(--teal), var(--teal-dim));
  border-color: transparent;
  color: #04201c;
  font-weight: 650;
}
.btn.danger {
  background: #3a171b;
  border-color: #5c242b;
  color: #ffb4b8;
}
.btn.ghost { background: transparent; }

.meta { display: flex; gap: 8px; flex-wrap: wrap; }
.pill {
  background: rgba(18, 26, 36, .85);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 0.78rem;
  color: var(--muted);
}
.pill.ok { color: var(--ok); border-color: #2c4a2f; }
.pill.warn { color: var(--warn); border-color: #4a3d22; }
.pill.bad { color: var(--danger); border-color: #4a2228; }

.share-box {
  background: rgba(18, 26, 36, .9);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 14px;
}
.share-box label {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
  margin-bottom: 8px;
}
.row { display: flex; gap: 8px; }
#shareUrl {
  flex: 1;
  min-width: 0;
  background: var(--field);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--teal);
  font-family: var(--mono);
  font-size: 16px; /* iOS: avoid auto-zoom on focus */
  padding: 10px 12px;
  -webkit-text-fill-color: var(--teal);
}
.chrome-extra { display: contents; }
.hint { margin: 8px 0 0; color: var(--muted); font-size: 0.75rem; }

.tab-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 38px;
}
.tab-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  flex: 1;
  min-width: 0;
}
.tab {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--line);
  background: rgba(18, 26, 36, .85);
  color: var(--muted);
  border-radius: 10px;
  padding: 6px 8px 6px 12px;
  font: inherit;
  font-size: 0.82rem;
  cursor: pointer;
  max-width: 180px;
}
.tab.active {
  color: var(--ink);
  border-color: #2a6b62;
  background: #102029;
  box-shadow: inset 0 0 0 1px rgba(45, 212, 191, .18);
}
.tab-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tab-close {
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  border-radius: 6px;
  width: 22px;
  height: 22px;
  line-height: 1;
  font-size: 1rem;
  padding: 0;
}
.tab-close:hover { color: var(--danger); background: rgba(240, 113, 120, .12); }
.tab-add, .tab-restore {
  border: 1px dashed #2a3d52;
  background: transparent;
  color: var(--teal);
  border-radius: 10px;
  min-width: 34px;
  height: 34px;
  padding: 0 8px;
  font-size: 1.05rem;
  cursor: pointer;
  flex-shrink: 0;
}
.tab-add:hover, .tab-restore:hover { border-color: var(--teal); background: rgba(45, 212, 191, .08); }
.tab-add:disabled, .tab-restore:disabled { opacity: .4; cursor: not-allowed; }

/* Compact autocomplete chips — never stretch to fill the terminal area */
.suggest {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 6px;
  flex: 0 0 auto;
  height: auto;
  max-height: 40px;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  padding: 0;
}
.suggest::-webkit-scrollbar { display: none; }
.suggest[hidden] { display: none !important; }
.suggest-item {
  display: inline-flex;
  align-items: center;
  align-self: center;
  flex: 0 0 auto;
  height: 32px;
  max-height: 32px;
  gap: 8px;
  border: 1px solid var(--line);
  background: #0f1822;
  color: var(--ink);
  border-radius: 999px;
  padding: 0 12px;
  font: inherit;
  font-size: 0.78rem;
  font-family: var(--mono);
  cursor: pointer;
  white-space: nowrap;
  line-height: 1;
}
.suggest-item.active, .suggest-item:hover {
  border-color: #2a6b62;
  background: #102029;
  color: var(--teal);
}
.suggest-item kbd {
  font-size: 0.65rem;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 1px 5px;
}

.work {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: minmax(0, 1fr);
  gap: 0;
  min-height: 0;
  min-width: 0;
  flex: 1 1 auto;
}
.work > .term-wrap {
  min-height: 0;
  min-width: 0;
  height: 100%;
}

.tab-history,
.tab-attach {
  border: 1px solid var(--line);
  background: rgba(18, 26, 36, .85);
  color: var(--muted);
  border-radius: 10px;
  min-height: 34px;
  padding: 0 10px;
  font: inherit;
  font-size: 0.78rem;
  cursor: pointer;
  flex-shrink: 0;
  white-space: nowrap;
}
.tab-history[aria-expanded="true"],
.tab-history:hover,
.tab-attach:hover {
  color: var(--teal);
  border-color: #2a6b62;
  background: #102029;
}
.tab-attach {
  color: var(--teal);
  border-color: #2a6b62;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.file-input-ios {
  /* iOS needs a real file input in the DOM — not display:none / hidden */
  position: fixed;
  left: 0;
  bottom: 0;
  width: 1px;
  height: 1px;
  opacity: 0.01;
  overflow: hidden;
  z-index: 0;
  pointer-events: none;
}
.attach-btn {
  min-width: 44px;
  min-height: 44px;
  border-radius: 12px !important;
  font-size: 1.25rem !important;
  padding: 0 !important;
  flex-shrink: 0;
  color: var(--teal) !important;
  border-color: #2a6b62 !important;
  display: inline-flex !important;
  align-items: center;
  justify-content: center;
  margin: 0;
}
.attach-status {
  flex: 0 0 auto;
  font-size: 0.75rem;
  color: var(--muted);
  padding: 0 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.attach-status.ok { color: var(--ok); }
.attach-status.err { color: var(--danger); }
.attach-status.busy { color: var(--warn); }

.history-drop {
  flex: 0 0 auto;
  max-height: min(40vh, 280px);
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  background: rgba(18, 26, 36, .96);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 6px;
}
.history-drop[hidden] { display: none !important; }
.history-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.history-item {
  text-align: left;
  border: 0;
  background: transparent;
  color: var(--ink);
  font-family: var(--mono);
  font-size: 0.8rem;
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-height: 40px;
}
.history-item:hover,
.history-item:active { background: rgba(45, 212, 191, .12); color: var(--teal); }
.history-empty { color: var(--muted); font-size: 0.78rem; line-height: 1.4; padding: 10px 12px; }

.term-wrap {
  position: relative;
  min-height: 200px;
  height: 100%;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: #05080c;
  overflow: hidden;
  box-shadow: inset 0 0 0 1px rgba(45, 212, 191, .05);
}
#terminals { height: 100%; min-height: 200px; position: relative; background: #05080c; }
.term-pane {
  position: absolute;
  inset: 0;
  padding: 8px;
  display: none;
  overflow: hidden;
  background: #05080c;
}
.term-pane.active { display: block; }
/* Keep the xterm cursor on the glyph grid; avoid viewport scroll fighting TUIs */
.term-pane .xterm { height: 100%; width: 100%; background: #05080c !important; }
.term-pane .xterm-viewport {
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch;
  background: #05080c !important;
}
.term-pane .xterm-screen { background: #05080c !important; }
/* Do NOT opacity:0 the helper textarea — iOS Safari will not open the keyboard */
.term-pane .xterm-helper-textarea {
  font-size: 16px !important;
  transform: translateZ(0);
  background: transparent !important;
  color: var(--ink) !important;
  -webkit-text-fill-color: var(--ink) !important;
  caret-color: var(--teal) !important;
}
.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(7, 11, 16, .72);
  backdrop-filter: blur(6px);
}
.overlay[hidden] { display: none; }
.card {
  text-align: center;
  padding: 28px 24px;
  max-width: 360px;
}
.card img { border-radius: 16px; margin-bottom: 12px; }
.card h2 { margin: 0 0 8px; font-size: 1.15rem; }
.card p { margin: 0; color: var(--muted); line-height: 1.45; font-size: 0.9rem; }

.foot {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 0.72rem;
  padding: 0 2px;
}

.xterm { height: 100%; }
.xterm-viewport { overflow-y: auto !important; }

.login-app {
  place-items: center;
  display: grid;
  min-height: 100vh;
}
.login-card {
  width: min(380px, 92vw);
  background: rgba(18, 26, 36, .92);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 28px 24px 22px;
  text-align: center;
  box-shadow: 0 24px 60px rgba(0,0,0,.35);
}
.login-card img { border-radius: 16px; margin-bottom: 10px; }
.login-card h1 { margin: 0 0 6px; font-size: 1.2rem; }
.login-card .sub { margin: 0 0 18px; }
.login-card label {
  display: block;
  text-align: left;
  font-size: 0.78rem;
  color: var(--muted);
  margin-bottom: 6px;
}
.login-card input {
  width: 100%;
  background: var(--field);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--ink);
  -webkit-text-fill-color: var(--ink);
  font: inherit;
  font-size: 16px;
  padding: 12px;
  margin-bottom: 14px;
}
.login-card .btn { width: 100%; border-radius: 10px; padding: 12px; min-height: 44px; }
.login-card .hint { color: var(--danger); margin-top: 12px; }

.pill.web { color: var(--teal); border-color: #1d4a44; }

.mobile-keys,
.mobile-compose {
  display: none;
}

@media (max-width: 720px) {
  h1 { font-size: 1rem; }
  .row { flex-direction: column; }
  .logo { width: 28px; height: 28px; }
  .btn { min-height: 36px; padding: 8px 12px; }
  .tab { max-width: 120px; min-height: 36px; }
  .tab-add, .tab-restore, .tab-history, .tab-attach { min-height: 40px; }
  .tab-add, .tab-restore { min-width: 40px; height: 40px; }
  .term-wrap, #terminals, .work > .term-wrap { min-height: 180px; }
  .share-box { padding: 10px; }
  .actions #btnCopy { display: none; }
}

/* Touch / iPhone layout: keyboard bar + compact chrome */
body.is-touch .mobile-only { display: inline-flex !important; }
body.is-touch .mobile-keys,
body.is-touch .mobile-compose { display: flex; }
/* Always show Attach on phones even if chrome is collapsed */
body.is-touch .tab-attach {
  display: inline-flex;
  min-height: 40px;
  padding: 0 12px;
}
body.is-touch:not(.chrome-open) .chrome-extra {
  display: none;
}
body.is-touch.chrome-open .chrome-extra {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 0 0 auto;
}
body.is-touch .foot { display: none; }
body.is-touch .work > .term-wrap,
body.is-touch #terminals { min-height: 160px; }
body.is-touch .actions #btnCopy { display: none; }

.mobile-keys {
  gap: 6px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding: 2px 0;
  scrollbar-width: none;
}
.mobile-keys::-webkit-scrollbar { display: none; }
.mobile-keys button {
  flex: 0 0 auto;
  min-width: 44px;
  min-height: 40px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  border-radius: 10px;
  font: inherit;
  font-size: 0.82rem;
  font-weight: 600;
  padding: 0 10px;
}
.mobile-keys button:active { background: #1a2a38; color: var(--teal); }

.mobile-compose {
  gap: 8px;
  align-items: center;
}
.mobile-compose input {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  background: var(--field);
  border: 1px solid var(--line);
  border-radius: 12px;
  color: var(--ink);
  -webkit-text-fill-color: var(--ink);
  font-family: var(--mono);
  font-size: 16px;
  padding: 10px 12px;
}
.mobile-compose .btn {
  min-height: 44px;
  border-radius: 12px;
  padding: 10px 16px;
  flex-shrink: 0;
}

body.kb-open .history-drop { display: none !important; }
body.kb-open .term-wrap,
body.kb-open #terminals,
body.kb-open .work > .term-wrap { min-height: 140px; }
