:root {
  --bg0: #080a0f;
  --bg1: #0d1020;
  --panel: rgba(255, 255, 255, 0.06);
  --panel2: rgba(255, 255, 255, 0.08);
  --line: rgba(255, 255, 255, 0.12);
  --text: rgba(255, 255, 255, 0.90);
  --muted: rgba(255, 255, 255, 0.65);
  --bad: #ff4d6d;
  --good: #6ee7b7;
  --radius: 6px;
  --apph: 100dvh;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  color: var(--text);
  background: #0b0b0f;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji";
}

#app {
  height: 100vh;
  height: 100dvh;
  height: var(--apph, 100dvh);
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 8px 8px 8px;
  border-bottom: 1px solid var(--line);
  background: #0b0b0f;
  overflow: hidden;
}
.topbar > * { flex: 0 0 auto; }
.topbar .controls {
  flex: 1 1 auto;
  display: flex;
  flex-wrap: nowrap;
  gap: 6px;
  min-width: 0;
  align-items: center;
}
.topbar .brand { flex: 0 0 auto; min-width: 0; }

.brandTitle { font-weight: 800; letter-spacing: 0.08em; font-size: 13px; text-transform: uppercase; }
.brandSub { margin-top: 2px; font-size: 12px; color: var(--muted); }

@media (max-width: 520px) {
  .brandSub { display: none; } /* keep one-line top bar on small screens */
  select.ctrl { max-width: 120px; }
}

.body {
  flex: 1;
  min-height: 0;
  display: flex;
  position: relative;
}

.keybar {
  flex: 0 0 auto;
  display: flex;
  gap: 6px;
  align-items: center;
  padding: 8px;
  border-top: 1px solid var(--line);
  background: #0b0b0f;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.keybar[hidden] { display: none !important; }
.kbBtn {
  height: 34px;
  padding: 0 10px;
  border-radius: 4px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  white-space: nowrap;
  user-select: none;
}
.kbBtn:active { background: rgba(255, 255, 255, 0.07); }
.kbBtn.kbOn {
  background: rgba(110, 231, 183, 0.12);
  border-color: rgba(110, 231, 183, 0.35);
}
.kbSep {
  width: 1px;
  height: 22px;
  background: rgba(255, 255, 255, 0.14);
  flex: 0 0 auto;
}

/* Mobile-first: show keybar on small/touch screens */
@media (max-width: 720px) {
  .keybar { display: flex; }
}

body.fs .keybar { display: none !important; }

.drawer {
  width: min(86vw, 380px);
  max-width: 380px;
  background: #0f0f16;
  border-right: 1px solid var(--line);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}

/* Mobile: drawer overlays. Desktop: drawer is docked. */
@media (max-width: 919px) {
  .drawer {
    position: absolute;
    z-index: 30;
    top: 0;
    bottom: 0;
    left: 0;
    transform: translateX(-102%);
    transition: transform 160ms ease;
  }
  .drawer.open { transform: translateX(0); }
}

@media (min-width: 920px) {
  .drawer { position: relative; transform: none; }
  #scrim { display: none !important; }
}

.drawerSection { display: flex; flex-direction: column; gap: 6px; }
.sectionTitle { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
.sectionHead { display: flex; align-items: center; justify-content: space-between; gap: 8px; }

.miniBtn {
  height: 24px;
  padding: 0 8px;
  border-radius: 3px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.04);
  color: var(--muted);
  font-size: 11px;
  cursor: pointer;
}

body.drawer-collapsed .drawer { display: none; }

.drawerTop { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.drawerTitle { font-weight: 800; letter-spacing: 0.04em; }

.searchBox input {
  width: 100%;
  padding: 10px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
  font-size: 16px; /* keep 16px to avoid iOS zoom */
  outline: none;
}
.searchBox input:focus { border-color: rgba(83, 212, 255, 0.35); box-shadow: 0 0 0 3px rgba(83, 212, 255, 0.12); }

.list { display: flex; flex-direction: column; gap: 4px; }
.empty {
  padding: 8px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.03);
  color: var(--muted);
  font-size: 13px;
}

.item {
  width: 100%;
  text-align: left;
  padding: 6px 8px;
  border-radius: 4px;
  border: 1px solid rgba(255, 255, 255, 0.10);
  background: rgba(255, 255, 255, 0.03);
  color: var(--text);
  cursor: pointer;
  display: flex;
  gap: 8px;
  align-items: center;
  min-height: 34px;
}
.itemRow { display: flex; gap: 4px; align-items: stretch; }
.itemRow .item { flex: 1 1 auto; min-width: 0; }
.labelChip {
  flex: 0 0 auto;
  max-width: 120px;
  font-size: 11px;
  padding: 1px 5px;
  border: 1px solid rgba(83, 212, 255, 0.35);
  color: rgba(83, 212, 255, 0.95);
  border-radius: 3px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.labelBtn {
  flex: 0 0 auto;
  width: 42px;
  border-radius: 4px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.04);
  color: var(--muted);
  cursor: pointer;
  font-size: 11px;
}
.closeBtn {
  flex: 0 0 auto;
  width: 34px;
  border-radius: 4px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.04);
  color: rgba(255, 120, 120, 0.9);
  cursor: pointer;
}
.item:hover { background: rgba(255, 255, 255, 0.05); }
.item:active { background: rgba(255, 255, 255, 0.07); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace; font-size: 12px; color: var(--muted); }
.grow { flex: 0 0 auto; font-size: 13px; }
.path {
  flex: 1;
  min-width: 0;
  font-size: 13px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.right { flex: 0 0 auto; display: inline-flex; align-items: center; gap: 6px; }
.tag {
  font-size: 11px;
  padding: 2px 6px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 3px;
  color: var(--muted);
}
.tag.ok { border-color: rgba(110, 231, 183, 0.35); color: rgba(110, 231, 183, 0.9); }

.main {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.termWrap {
  flex: 1;
  min-height: 0; /* critical for flex shrink */
  padding: 10px;
}
.term {
  height: 100%;
  border-radius: 0;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: #000;
  overflow: hidden;
}

/* Keep cursor visible even when xterm isn't focused (menus, etc). */
.xterm .xterm-cursor-layer { opacity: 1 !important; }
.xterm:not(.focus) .xterm-cursor-layer { opacity: 1 !important; }

.ctrl {
  height: 40px;
  padding: 0 10px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
  font-size: 16px; /* prevent iOS zoom */
  outline: none;
}
select.ctrl { min-width: 120px; max-width: 200px; }
#cwdInput { flex: 1; min-width: 0; }

.btn {
  height: 40px;
  padding: 0 12px;
  border-radius: var(--radius);
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  cursor: pointer;
  font-weight: 650;
}
.btn.primary {
  border-color: rgba(83, 212, 255, 0.35);
  background: rgba(83, 212, 255, 0.12);
}
.btn.ghost { background: transparent; }
.btn.wide { width: 100%; }

.iconBtn {
  height: 40px;
  min-width: 40px;
  border-radius: var(--radius);
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 10px;
  user-select: none;
}
.icon { font-size: 18px; line-height: 1; }

.toolsMenu {
  position: absolute;
  z-index: 40;
  top: calc(56px + env(safe-area-inset-top));
  right: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: #0f0f16;
}
.toolsMenu[hidden] { display: none !important; }
.menuSep {
  height: 1px;
  background: rgba(255, 255, 255, 0.12);
  margin: 4px 0;
}
.menuBtn {
  height: 36px;
  padding: 0 10px;
  border-radius: 4px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
  cursor: pointer;
  text-align: left;
}
.uploadBlock {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding-top: 2px;
}
.uploadLabel {
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 0.02em;
}
.uploadInput {
  max-width: 260px;
  color: var(--muted);
  font-size: 12px;
}
.uploadStatus {
  max-width: 260px;
  font-size: 11px;
  line-height: 1.35;
  color: var(--muted);
  word-break: break-word;
}

.scrim {
  position: absolute;
  inset: 0;
  z-index: 25;
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(2px);
}

.login {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(8, 10, 15, 0.80);
  backdrop-filter: blur(14px);
  z-index: 50;
}
.login[hidden] { display: none !important; }
.loginCard {
  width: min(520px, 100%);
  border-radius: 22px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.05);
  padding: 18px;
}
.loginTitle { font-weight: 900; font-size: 18px; }
.loginSub { margin-top: 4px; color: var(--muted); font-size: 13px; }
.loginForm { display: flex; flex-direction: column; gap: 12px; margin-top: 16px; }
.field .label { font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.field input {
  width: 100%;
  height: 44px;
  padding: 0 12px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: rgba(0, 0, 0, 0.25);
  color: var(--text);
  font-size: 16px;
  outline: none;
}
.field input:focus { border-color: rgba(83, 212, 255, 0.35); box-shadow: 0 0 0 3px rgba(83, 212, 255, 0.12); }
.err { min-height: 18px; font-size: 13px; color: rgba(255, 77, 109, 0.95); }

/* Fullscreen-ish mode: hide sidebar + top bar */
body.fs .topbar { display: none; }
body.fs .drawer { display: none; }
body.fs .termWrap { padding: 0; }
body.fs .term { border-radius: 0; border: none; }

.exitFs {
  position: absolute;
  z-index: 60;
  top: calc(10px + env(safe-area-inset-top));
  right: 10px;
  height: 36px;
  padding: 0 10px;
  border-radius: 4px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  background: rgba(0, 0, 0, 0.55);
  color: var(--text);
  cursor: pointer;
}
.exitFs[hidden] { display: none !important; }
