:root {
  --bg: #0b1020;
  --bg-elev: #141b2d;
  --bg-elev-2: #1b243a;
  --panel: rgba(20, 27, 45, 0.92);
  --border: rgba(255, 255, 255, 0.08);
  --text: #e8eefc;
  --muted: #9fb0d0;
  --accent: #00e5ff;
  --accent-2: #7c4dff;
  --danger: #ff5c7a;
  --ok: #3ddc97;
  --warn: #ffcc66;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  --status-h: 40px;
  --dock-h: 72px;
  --mobile-nav-h: 64px;
  --radius: 14px;
  --radius-sm: 10px;
  --transition: 180ms ease;
  font-synthesis-weight: none;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
  background: radial-gradient(1200px 800px at 20% 0%, #121a33 0%, var(--bg) 60%);
  color: var(--text);
  overflow: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }

.hidden { display: none !important; }

#os-root {
  height: 100%;
  width: 100%;
  position: relative;
}

/* Status bar */
.status-bar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--status-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 0 10px;
  background: rgba(10, 14, 28, 0.85);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  z-index: 10000;
  font-size: 13px;
}
.status-left, .status-right, .status-center {
  display: flex; align-items: center; gap: 8px; min-width: 0;
}
.status-center { flex: 1; justify-content: center; font-variant-numeric: tabular-nums; }
.status-right { justify-content: flex-end; flex-wrap: nowrap; }
.brand { font-weight: 700; letter-spacing: 0.3px; color: var(--accent); }
.badge {
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  color: var(--muted);
  white-space: nowrap;
}
.badge.online { color: #042; background: rgba(61,220,151,0.18); border-color: rgba(61,220,151,0.35); }
.badge.offline { color: #ffd9df; background: rgba(255,92,122,0.12); border-color: rgba(255,92,122,0.35); }
.status-btn, .icon-btn, .primary-btn, .secondary-btn, .danger-btn, .nav-btn {
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 10px;
}
.status-btn:hover, .icon-btn:hover, .secondary-btn:hover, .nav-btn:hover { border-color: rgba(255,255,255,0.18); }
.primary-btn { background: linear-gradient(135deg, rgba(0,229,255,0.25), rgba(124,77,255,0.25)); border-color: rgba(0,229,255,0.35); }
.danger-btn { background: rgba(255,92,122,0.18); border-color: rgba(255,92,122,0.4); color: #ffd9df; }
.icon-btn { padding: 4px 8px; line-height: 1; }

/* Shell visibility */
.desktop-shell, .mobile-shell {
  position: absolute;
  top: var(--status-h); left: 0; right: 0; bottom: 0;
}
.mode-desktop .mobile-shell { display: none; }
.mode-mobile .desktop-shell { display: none; }

/* Desktop */
.desktop-shell {
  padding: 14px 14px calc(var(--dock-h) + 16px);
  overflow: hidden;
}
.desktop-icons {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(92px, 1fr));
  gap: 10px;
  max-width: 720px;
  align-content: start;
}
.desktop-icon, .app-tile {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 10px 6px;
  border-radius: 12px;
  border: 1px solid transparent;
  background: transparent;
  text-align: center;
  user-select: none;
}
.desktop-icon:hover, .app-tile:hover { background: rgba(255,255,255,0.05); border-color: var(--border); }
.app-glyph {
  width: 44px; height: 44px; border-radius: 12px;
  display: grid; place-items: center;
  font-size: 22px;
  background: linear-gradient(145deg, rgba(0,229,255,0.18), rgba(124,77,255,0.18));
  border: 1px solid rgba(255,255,255,0.08);
  box-shadow: inset 0 0 0 1px rgba(255,255,255,0.02);
}
.app-label { font-size: 12px; line-height: 1.2; color: var(--text); word-break: break-word; }
.app-sublabel { font-size: 11px; color: var(--muted); }

.windows-layer {
  position: absolute;
  inset: 0 0 var(--dock-h) 0;
  pointer-events: none;
}
.window {
  position: absolute;
  min-width: 280px;
  min-height: 180px;
  background: var(--panel);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  pointer-events: auto;
}
.window.maximized {
  left: 8px !important; top: 8px !important;
  width: calc(100% - 16px) !important; height: calc(100% - 16px) !important;
  border-radius: 12px;
}
.window.minimized { display: none; }
.window-titlebar {
  height: 38px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 8px 0 10px;
  background: rgba(255,255,255,0.03);
  border-bottom: 1px solid var(--border);
  cursor: move;
  user-select: none;
  touch-action: none;
}
.window-title { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.window-controls { display: flex; gap: 6px; }
.win-btn {
  width: 26px; height: 22px; border-radius: 7px; border: 1px solid var(--border);
  background: var(--bg-elev-2); color: var(--text); padding: 0; line-height: 1;
}
.win-btn.close:hover { background: rgba(255,92,122,0.25); border-color: rgba(255,92,122,0.5); }
.window-body { flex: 1; min-height: 0; position: relative; background: rgba(10,14,28,0.35); }
.window-body iframe { width: 100%; height: 100%; border: 0; background: #fff; }
.window-resize {
  position: absolute; width: 16px; height: 16px; right: 0; bottom: 0;
  cursor: nwse-resize; touch-action: none;
}
.window-resize::after {
  content: ""; position: absolute; right: 3px; bottom: 3px; width: 8px; height: 8px;
  border-right: 2px solid rgba(255,255,255,0.25); border-bottom: 2px solid rgba(255,255,255,0.25);
}

.dock {
  position: absolute;
  left: 50%; bottom: 10px; transform: translateX(-50%);
  height: var(--dock-h);
  max-width: calc(100% - 24px);
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px;
  background: rgba(20, 27, 45, 0.8);
  backdrop-filter: blur(12px);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  overflow-x: auto;
  z-index: 5000;
}
.dock .dock-item {
  width: 48px; height: 48px; flex: 0 0 auto; border-radius: 14px;
  display: grid; place-items: center; font-size: 22px;
  border: 1px solid var(--border); background: var(--bg-elev-2);
  position: relative;
}
.dock .dock-item.running::after {
  content: ""; position: absolute; bottom: -4px; left: 50%; transform: translateX(-50%);
  width: 5px; height: 5px; border-radius: 50%; background: var(--accent);
}

/* Mobile */
.mobile-shell { display: flex; flex-direction: column; }
.mobile-home {
  flex: 1; overflow: auto; padding: 16px 14px calc(var(--mobile-nav-h) + 80px);
}
.mobile-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px 8px;
}
.mobile-home-hint {
  position: fixed; left: 0; right: 0; bottom: calc(var(--mobile-nav-h) + 10px);
  display: flex; justify-content: center; pointer-events: none; z-index: 5;
}
.drawer-pill {
  pointer-events: auto; padding: 8px 14px; border-radius: 999px;
  background: rgba(20,27,45,0.85); border: 1px solid var(--border); color: var(--muted);
}
.mobile-app-view {
  position: absolute; inset: 0 0 var(--mobile-nav-h) 0;
  background: var(--bg); display: flex; flex-direction: column; z-index: 20;
}
.mobile-app-header {
  height: 44px; display: flex; align-items: center; padding: 0 14px;
  border-bottom: 1px solid var(--border); background: rgba(10,14,28,0.7); font-weight: 600;
}
.mobile-app-content { flex: 1; min-height: 0; overflow: auto; position: relative; }
.mobile-app-content iframe { width: 100%; height: 100%; border: 0; background: #fff; }

.app-drawer {
  position: absolute; left: 0; right: 0; bottom: var(--mobile-nav-h);
  height: 75%; max-height: 680px;
  background: rgba(16, 22, 38, 0.98);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
  border-radius: 18px 18px 0 0;
  transform: translateY(110%);
  transition: transform 220ms ease;
  z-index: 40; display: flex; flex-direction: column;
}
.app-drawer.open { transform: translateY(0); }
.drawer-handle { width: 42px; height: 4px; border-radius: 4px; background: rgba(255,255,255,0.2); margin: 8px auto 0; }
.drawer-header, .recents-header { display: flex; align-items: center; justify-content: space-between; padding: 8px 14px; }
.drawer-header h2, .recents-header h2 { margin: 0; font-size: 16px; }
.drawer-content { flex: 1; overflow: auto; padding: 4px 14px 18px; }
.drawer-group { margin-bottom: 16px; }
.drawer-group h3 { margin: 10px 2px; font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.6px; }
.drawer-grid { display: grid; grid-template-columns: repeat(4, minmax(0,1fr)); gap: 12px 8px; }

.mobile-nav {
  position: fixed; left: 0; right: 0; bottom: 0; height: var(--mobile-nav-h);
  display: grid; grid-template-columns: 1fr 1fr 1fr; align-items: center;
  background: rgba(10,14,28,0.95); border-top: 1px solid var(--border); z-index: 50;
  padding-bottom: env(safe-area-inset-bottom);
}
.nav-btn { height: 100%; border: 0; border-radius: 0; background: transparent; font-size: 18px; color: var(--muted); }
.nav-btn:active, .nav-btn:hover { color: var(--text); background: rgba(255,255,255,0.03); }
.nav-home { font-size: 22px; }

.recents-view {
  position: absolute; inset: 0 0 var(--mobile-nav-h) 0;
  background: rgba(11,16,32,0.98); z-index: 35; display: flex; flex-direction: column;
}
.recents-list { flex: 1; overflow: auto; padding: 8px 14px 18px; display: grid; gap: 10px; }
.recent-card {
  padding: 14px; border: 1px solid var(--border); border-radius: 12px; background: var(--bg-elev);
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
}
.recent-card .meta { display: flex; align-items: center; gap: 10px; }

/* Generic app content / cards */
.app-surface { padding: 14px; height: 100%; overflow: auto; color: var(--text); }
.card {
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: 12px; padding: 14px; margin-bottom: 12px;
}
.card h3, .card h4 { margin: 0 0 8px; }
.muted { color: var(--muted); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.stack { display: grid; gap: 8px; }
input[type="text"], input[type="url"], input[type="password"], textarea, select {
  width: 100%; padding: 9px 10px; border-radius: 8px; border: 1px solid var(--border);
  background: #0f1526; color: var(--text);
}
textarea { min-height: 120px; resize: vertical; }
label { font-size: 13px; color: var(--muted); display: block; margin-bottom: 4px; }
hr.sep { border: 0; border-top: 1px solid var(--border); margin: 12px 0; }
.list { display: grid; gap: 8px; }
.list-item {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px; border: 1px solid var(--border); border-radius: 10px; background: rgba(255,255,255,0.02);
}
.pill { font-size: 11px; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--border); color: var(--muted); }
.empty { color: var(--muted); padding: 18px; text-align: center; border: 1px dashed var(--border); border-radius: 12px; }

/* Offline/setup card inside windows */
.offline-card-wrap { height: 100%; display: grid; place-items: center; padding: 20px; background: radial-gradient(600px 300px at 50% 0%, #18223a, var(--bg)); }
.offline-card { max-width: 520px; width: 100%; text-align: center; }
.offline-card .big { font-size: 42px; margin-bottom: 8px; }
.offline-card h3 { margin: 6px 0; }
.offline-card p { color: var(--muted); }

/* Agent drawer */
.agent-drawer {
  position: fixed; top: var(--status-h); right: 0; bottom: 0;
  width: min(420px, 100%);
  background: rgba(16, 22, 38, 0.98);
  backdrop-filter: blur(12px);
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow);
  z-index: 12000;
  display: flex; flex-direction: column;
}
.agent-header {
  padding: 12px; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
.agent-header h2 { margin: 0; font-size: 16px; }
.agent-mode-indicator { font-size: 12px; color: var(--muted); margin-top: 2px; }
.agent-header-actions { display: flex; gap: 6px; }
.agent-messages { flex: 1; overflow: auto; padding: 12px; display: grid; gap: 8px; align-content: start; }
.msg { padding: 9px 11px; border-radius: 12px; border: 1px solid var(--border); max-width: 95%; white-space: pre-wrap; word-break: break-word; font-size: 14px; }
.msg.user { margin-left: auto; background: rgba(0,229,255,0.12); border-color: rgba(0,229,255,0.25); }
.msg.agent { background: var(--bg-elev); }
.msg.system { background: rgba(255,204,102,0.08); border-color: rgba(255,204,102,0.25); color: #ffe8b0; font-size: 13px; }
.agent-form { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--border); }
.agent-form input { flex: 1; }

/* Confirm & toast */
.confirm-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.55); display: grid; place-items: center; z-index: 13000; padding: 16px;
}
.confirm-card { width: min(420px, 100%); background: var(--bg-elev); border: 1px solid var(--border); border-radius: 14px; padding: 16px; box-shadow: var(--shadow); }
.confirm-card h3 { margin: 0 0 6px; }
.confirm-card p { color: var(--muted); margin: 0 0 14px; }
.confirm-actions { display: flex; justify-content: flex-end; gap: 8px; }
.toast-container { position: fixed; right: 12px; bottom: calc(var(--mobile-nav-h) + 12px); display: grid; gap: 8px; z-index: 14000; pointer-events: none; }
.mode-desktop .toast-container { bottom: 12px; }
.toast { pointer-events: auto; padding: 10px 12px; border-radius: 10px; background: var(--bg-elev-2); border: 1px solid var(--border); box-shadow: var(--shadow); font-size: 13px; }

/* Mobile adjustments */
.mode-mobile .status-bar { font-size: 11px; padding: 0 6px; gap: 4px; }
.mode-mobile .brand { display: none; }
.mode-mobile .status-left, .mode-mobile .status-right, .mode-mobile .status-center { gap: 4px; }
.mode-mobile .badge, .mode-mobile .status-btn { padding: 3px 6px; font-size: 11px; }
.mode-mobile .agent-drawer { width: 100%; border-left: 0; }
@media (max-width: 480px) {
  .mobile-grid, .drawer-grid { grid-template-columns: repeat(4, minmax(0,1fr)); }
  .status-center { display: none; }
  #status-storage { max-width: 110px; overflow: hidden; text-overflow: ellipsis; }
}

/* Scrollbars (subtle) */
*::-webkit-scrollbar { width: 8px; height: 8px; }
*::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.15); border-radius: 8px; }
*::-webkit-scrollbar-track { background: transparent; }
