/* GoLinux Terminal UI — rebuilt minimalist obsidian aesthetic.
 *
 * Mobile & performance invariants:
 *   - Fixed viewport shell + --app-h synced to visualViewport so #keybar stays
 *     pinned above iOS/Android soft keyboards with zero outer rubber-banding.
 *   - 16px .xterm-helper-textarea prevents iOS Safari auto-zoom on focus.
 *   - touch-action: pan-y on .xterm-viewport enables smooth touch scrollback.
 *   - Floating #diskToast never resizes #terminal or shifts output.
 *   - Zero heavy blur filters on active terminal surface for low GPU/battery use.
 */
*, *::before, *::after { box-sizing: border-box; }

:root {
  --app-h: 100dvh;
  --bg: #080b11;
  --chrome: #0c1018;
  --surface: #101622;
  --surface-hi: #161e2e;
  --border: rgba(255, 255, 255, 0.07);
  --border-hi: rgba(255, 255, 255, 0.14);
  --fg: #e2e8f0;
  --fg-muted: #8b9bb4;
  --fg-dim: #64748b;
  --emerald: #34d399;
  --cyan: #38bdf8;
  --amber: #fbbf24;
  --rose: #f87171;
  --font-sans: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, Monaco, Consolas, monospace;
}

html, body {
  margin: 0;
  padding: 0;
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font: 13px/1.4 var(--font-sans);
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: var(--app-h);
  width: 100%;
  max-width: 100%;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
  will-change: transform;
}

[hidden] { display: none !important; }

/* ---------- Minimalist Top Chrome ---------- */
#bar {
  position: relative;
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  padding: 6px 12px;
  padding-top: max(6px, env(safe-area-inset-top));
  padding-left: max(12px, env(safe-area-inset-left));
  padding-right: max(12px, env(safe-area-inset-right));
  background: var(--chrome);
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
  min-width: 0;
  max-width: 100%;
}

#brandLink {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  text-decoration: none;
  color: var(--fg);
  flex: 0 0 auto;
}
#brandLink b {
  font-weight: 650;
  font-size: 13.5px;
  letter-spacing: -0.02em;
}
.brand-glyph {
  width: 24px;
  height: 24px;
  border-radius: 7px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: rgba(52, 211, 153, 0.12);
  border: 1px solid rgba(52, 211, 153, 0.28);
  color: var(--emerald);
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 9px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.035);
  border: 1px solid var(--border);
  min-width: 0;
  flex: 0 1 auto;
}
.s-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--amber);
  flex: 0 0 auto;
}
.s-dot.ok {
  background: var(--emerald);
  box-shadow: 0 0 8px rgba(52, 211, 153, 0.65);
}
.s-dot.err { background: var(--rose); }

#status {
  font-family: var(--font-mono);
  font-size: 11.5px;
  color: var(--fg-muted);
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#persistStatus {
  margin-left: auto;
  font-family: var(--font-mono);
  font-size: 11.5px;
  flex: 0 1 auto;
  min-width: 0;
  max-width: 34vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--fg-dim);
}
#persistStatus.ok { color: var(--emerald); }
#persistStatus.warn { color: var(--amber); }
#persistStatus.error { color: var(--rose); }

#barActions {
  display: flex;
  align-items: center;
  gap: 5px;
  flex: 0 0 auto;
}
#barMenuBtn { display: none; }

button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  background: var(--surface);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 5px 10px;
  font: 500 12px/1 var(--font-sans);
  cursor: pointer;
  transition: background 0.14s ease, border-color 0.14s ease, color 0.14s ease;
}
button:hover {
  background: var(--surface-hi);
  border-color: var(--border-hi);
}
button:disabled { opacity: 0.42; cursor: default; }

.ghost-btn {
  background: transparent;
  border-color: transparent;
  color: var(--fg-muted);
  height: 30px;
}
.ghost-btn:hover {
  background: rgba(255, 255, 255, 0.055);
  border-color: var(--border);
  color: var(--fg);
}
.danger-btn:hover {
  color: var(--rose);
  border-color: rgba(248, 113, 113, 0.25);
  background: rgba(248, 113, 113, 0.08);
}

a.nav-link {
  color: var(--fg-dim);
  font-size: 12px;
  font-weight: 500;
  text-decoration: none;
  padding: 5px 7px;
  border-radius: 6px;
}
a.nav-link:hover { color: var(--fg); background: rgba(255, 255, 255, 0.04); }

/* ---------- Terminal Canvas ---------- */
#terminal {
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
  padding: 10px 14px 8px;
  padding-left: max(14px, env(safe-area-inset-left));
  padding-right: max(14px, env(safe-area-inset-right));
  overflow: hidden;
  position: relative;
  background: var(--bg);
}
#terminal .xterm { height: 100%; width: 100%; }
#terminal .xterm-viewport {
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  scrollbar-width: thin;
  scrollbar-color: rgba(148, 163, 184, 0.25) transparent;
}
#terminal, #terminal * { -webkit-touch-callout: none; }

/* 16px prevents iOS Safari auto-zoom on focus. */
#terminal .xterm-helper-textarea {
  position: fixed !important;
  opacity: 0 !important;
  left: 0 !important;
  top: 0 !important;
  width: 1px !important;
  height: 1px !important;
  font-size: 16px !important;
  caret-color: transparent !important;
}

/* Floating disk-loading pill (zero layout shift) */
#diskToast {
  position: absolute;
  top: 10px;
  right: 14px;
  z-index: 4;
  display: none;
  align-items: center;
  gap: 7px;
  padding: 5px 11px;
  border-radius: 999px;
  background: rgba(12, 16, 24, 0.92);
  border: 1px solid rgba(52, 211, 153, 0.28);
  color: var(--emerald);
  font-family: var(--font-mono);
  font-size: 11px;
  pointer-events: none;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.45);
}
#diskToast.visible { display: inline-flex; }
#diskToast .toast-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--emerald);
}
#diskToast .disk-sub { color: var(--fg-muted); }

/* ---------- Progress & Offline Banner ---------- */
.bar {
  display: none;
  align-items: center;
  gap: 10px;
  padding: 6px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--chrome);
  flex: 0 0 auto;
  font-size: 12px;
}
.bar.visible { display: flex; }
.bar .label { color: var(--fg-muted); white-space: nowrap; }
.bar .track {
  flex: 1;
  height: 5px;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 999px;
  overflow: hidden;
  min-width: 80px;
}
.bar .fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--emerald), var(--cyan));
  transition: width 0.15s linear;
}
.bar .pct {
  font-family: var(--font-mono);
  color: var(--emerald);
  min-width: 7em;
  text-align: right;
  font-size: 11.5px;
}

#offlineBanner {
  display: none;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: rgba(251, 191, 36, 0.10);
  border-bottom: 1px solid rgba(251, 191, 36, 0.28);
  color: #fde68a;
  font-size: 12px;
  flex: 0 0 auto;
}
#offlineBanner.visible { display: flex; }
#offlineBanner .grow { flex: 1; min-width: 0; }
#offlineBanner button {
  padding: 4px 10px;
  font-size: 11.5px;
  background: rgba(251, 191, 36, 0.14);
  border-color: rgba(251, 191, 36, 0.32);
  color: #fef3c7;
}

/* ---------- Modals (/mnt/host Download & Packs) ---------- */
#packs, #filesModal {
  position: fixed;
  inset: 0;
  background: rgba(4, 6, 10, 0.78);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 30;
  padding: 16px;
}
#packs.visible, #filesModal.visible { display: flex; }

.modal-box {
  background: #0d121c;
  border: 1px solid var(--border-hi);
  border-radius: 14px;
  width: min(520px, 100%);
  max-height: calc(100dvh - 48px);
  display: flex;
  flex-direction: column;
  padding: 18px 20px 16px;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.65);
  overflow: auto;
}
.modal-box h2 { margin: 0 0 6px; font-size: 15px; font-weight: 650; color: var(--fg); }
.modal-box p.hint { margin: 0 0 14px; color: var(--fg-muted); font-size: 12.5px; line-height: 1.55; }
.modal-box code {
  font-family: var(--font-mono);
  font-size: 11.5px;
  background: rgba(255, 255, 255, 0.06);
  color: var(--fg);
  padding: 1px 5px;
  border-radius: 4px;
}

#filesList {
  flex: 1 1 auto;
  overflow: auto;
  min-height: 64px;
  max-height: 48dvh;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.host-file {
  display: flex;
  align-items: center;
  gap: 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  background: rgba(255, 255, 255, 0.025);
}
.host-file .hf-info { flex: 1; min-width: 0; }
.host-file .hf-name {
  font-family: var(--font-mono);
  color: var(--fg);
  word-break: break-all;
  font-size: 12.5px;
}
.host-file .hf-size { color: var(--fg-dim); font-size: 11px; margin-top: 2px; }
.host-file button {
  min-height: 32px;
  flex: 0 0 auto;
  background: rgba(52, 211, 153, 0.12);
  border-color: rgba(52, 211, 153, 0.3);
  color: var(--emerald);
}
.host-file button:hover { background: rgba(52, 211, 153, 0.2); }

#filesEmpty {
  color: var(--fg-muted);
  font-size: 12.5px;
  padding: 18px 12px;
  border: 1px dashed var(--border-hi);
  border-radius: 10px;
  text-align: center;
  line-height: 1.55;
}
#filesStatus, #packStatus { margin-top: 10px; font-size: 12px; color: var(--fg-muted); min-height: 1.2em; }
#filesStatus.ok { color: var(--emerald); }
#filesStatus.error { color: var(--rose); }
#filesFooter, #packsFooter {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 14px;
  gap: 8px;
}

.pack {
  display: flex; gap: 10px; align-items: flex-start;
  border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; margin-bottom: 8px;
}
.pack.installed { border-color: rgba(52, 211, 153, 0.35); background: rgba(52, 211, 153, 0.06); }
.pack .meta { flex: 1; min-width: 0; }
.pack .meta .size, .pack .meta .badge { color: var(--emerald); font-size: 11.5px; margin-left: 6px; }
.pack .meta .desc { color: var(--fg-muted); font-size: 12px; margin-top: 3px; }
.pack .actions { display: flex; gap: 6px; }
#saveDataNote { font-size: 12px; color: var(--amber); margin: 0 0 10px; display: none; }
#saveDataNote.visible { display: block; }
#packQuota { font-size: 12px; color: var(--fg-dim); }

/* ---------- Loading & Fatal Overlays ---------- */
#loading {
  position: fixed;
  inset: 0;
  z-index: 15;
  display: none;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  padding: 24px;
}
#loading.visible { display: flex; }
.loading-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  text-align: center;
  max-width: 360px;
}
#loading .title { color: var(--fg); font-size: 15px; font-weight: 600; }
#loading .detail { color: var(--fg-muted); font-size: 12.5px; line-height: 1.5; }
#loading .spin {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.1);
  border-top-color: var(--emerald);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

#fatal {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(5, 7, 11, 0.94);
  align-items: center;
  justify-content: center;
  padding: 24px;
}
#fatal.visible { display: flex; }
#fatal .box {
  max-width: 480px;
  border: 1px solid rgba(248, 113, 113, 0.35);
  border-radius: 14px;
  background: #140d10;
  padding: 22px 24px;
}
#fatal .fatal-title { color: var(--rose); font-weight: 650; font-size: 15px; margin-bottom: 8px; }
#fatal .fatal-detail { color: var(--fg); line-height: 1.55; font-size: 13px; }
#fatal .fatal-hint { color: var(--fg-muted); font-size: 12px; margin-top: 12px; }
#fatal .fatal-actions { margin-top: 16px; display: flex; gap: 8px; }
#fatalReload {
  background: rgba(52, 211, 153, 0.14);
  border-color: rgba(52, 211, 153, 0.35);
  color: var(--emerald);
  min-height: 36px;
  padding: 6px 16px;
}

/* ---------- Mobile Overflow Menu ---------- */
#barMenu {
  position: absolute;
  right: 8px;
  top: calc(100% + 4px);
  display: flex;
  flex-direction: column;
  gap: 2px;
  background: #0f1522;
  border: 1px solid var(--border-hi);
  border-radius: 12px;
  padding: 6px;
  z-index: 25;
  min-width: 210px;
  box-shadow: 0 14px 36px rgba(0, 0, 0, 0.6);
}
#barMenu button, #barMenu a {
  justify-content: flex-start;
  text-align: left;
  padding: 10px 12px;
  min-height: 44px;
  display: flex;
  align-items: center;
  background: transparent;
  border: 0;
  color: var(--fg);
  font: 500 13px/1 var(--font-sans);
  text-decoration: none;
  border-radius: 8px;
  cursor: pointer;
}
#barMenu button:hover, #barMenu a:hover { background: rgba(255, 255, 255, 0.06); }
#barMenu .danger-item { color: var(--rose); }

/* ---------- Sleek Mobile Touch Key Bar ---------- */
#keybar {
  display: none;
  flex-direction: column;
  gap: 5px;
  padding: 6px 8px;
  padding-bottom: max(6px, env(safe-area-inset-bottom));
  padding-left: max(8px, env(safe-area-inset-left));
  padding-right: max(8px, env(safe-area-inset-right));
  border-top: 1px solid var(--border);
  background: var(--chrome);
  flex: 0 0 auto;
}
#keybar.visible { display: flex; }
#keybar .kb-row { display: flex; gap: 5px; }
#keybar button {
  flex: 1 1 0;
  min-width: 0;
  min-height: 42px;
  padding: 0 4px;
  font: 500 13px/1 var(--font-mono);
  background: var(--surface);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 9px;
  white-space: nowrap;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
#keybar button:active {
  background: var(--surface-hi);
  border-color: var(--border-hi);
}
#keybar button[aria-pressed="true"] {
  background: rgba(52, 211, 153, 0.16);
  border-color: rgba(52, 211, 153, 0.45);
  color: var(--emerald);
  box-shadow: 0 0 0 1px rgba(52, 211, 153, 0.25);
}

/* ---------- Responsive ---------- */
@media (max-width: 760px) {
  #bar { padding: 4px 8px; padding-top: max(4px, env(safe-area-inset-top)); gap: 6px; min-height: 44px; }
  #brandLink b { font-size: 13px; }
  #persistStatus { max-width: 26vw; font-size: 11px; }
  .wide-only { display: none !important; }
  #barMenuBtn {
    display: inline-flex;
    min-width: 38px;
    min-height: 38px;
    font-size: 17px;
  }
  #restart { min-height: 36px; padding: 0 10px; }
  #restart .btn-lbl { display: none; }
  #terminal { padding: 6px 8px 4px; }
}

@media (max-height: 440px) {
  #keybar button { min-height: 36px; font-size: 12px; }
  #bar { min-height: 36px; padding: 3px 8px; }
}
