* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg-primary);
}

body {
  margin: 0;
  padding: 0;
  height: 100%;
  height: -webkit-fill-available;
  overflow: hidden;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Inter', system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.app-container {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding-top: env(safe-area-inset-top);
  background: var(--bg-secondary);
}

/* iOS PWA: window.innerHeight = Safari viewport (793px), screen.height = full screen (852px).
   Use screen.height via --screen-h to fill the true physical height. */
@media (max-width: 1023px) {
  html {
    background: var(--bg-secondary);
  }
  .app-container {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 100vh;
    height: var(--screen-h, 100vh);
  }
}

/* ── Top bar ─────────────────────────────────────── */
.top-bar {
  display: flex;
  align-items: center;
  height: 52px;
  padding: 0 12px;
  background: var(--bg-secondary);
  backdrop-filter: blur(var(--blur-amount));
  -webkit-backdrop-filter: blur(var(--blur-amount));
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
  gap: 8px;
  z-index: 10;
  position: relative;
}

/* Accent line at top */
.top-bar::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 1px;
  background: var(--accent-gradient);
  opacity: 0.5;
}

.menu-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 1.25rem;
  width: 38px;
  height: 38px;
  padding: 0;
  cursor: pointer;
  border-radius: var(--radius-sm);
  transition: background var(--transition-fast), color var(--transition-fast);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.menu-btn:hover {
  background: var(--bg-tertiary);
  color: var(--text-primary);
}

.app-title {
  font-weight: 700;
  font-size: 0.95rem;
  letter-spacing: -0.02em;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 7px;
}

.title-status {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  flex-shrink: 0;
  background: var(--text-dimmed);
  transition: background 0.3s ease;
}

.title-status.idle {
  background: var(--yellow);
  opacity: 0.7;
}

.title-status.working {
  background: var(--green);
  animation: status-pulse 1.8s ease-in-out infinite;
}

.top-bar-right {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 10px;
}

/* Connection status dot */
.connection-status {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--red);
  box-shadow: 0 0 5px var(--red);
  flex-shrink: 0;
  transition: background var(--transition-normal), box-shadow var(--transition-normal);
}

.connection-status.connected {
  background: var(--green);
  box-shadow: 0 0 6px var(--green);
}

@keyframes status-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.45; }
}

.connection-status:not(.connected) {
  animation: status-pulse 2.5s ease-in-out infinite;
}



.terminal-container.scroll-x { overflow-x: auto; }
.terminal-container.scroll-x .terminal-output {
  white-space: pre;
  word-break: normal;
}

.reload-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 1.15rem;
  padding: 0;
  cursor: pointer;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  transition: background var(--transition-fast), color var(--transition-fast), transform 0.3s ease;
  flex-shrink: 0;
}
.reload-btn:hover { background: var(--bg-tertiary); color: var(--text-primary); }
.reload-btn:active { transform: rotate(180deg); }


/* ── Terminal container ──────────────────────────── */
.terminal-container {
  flex: 1;
  min-height: 0;
  overflow-y: scroll;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  touch-action: pan-y;
  background: var(--terminal-bg);
  position: relative;
}

.terminal-container::-webkit-scrollbar {
  width: 4px;
}
.terminal-container::-webkit-scrollbar-track {
  background: transparent;
}
.terminal-container::-webkit-scrollbar-thumb {
  background: var(--scrollbar-thumb);
  border-radius: 2px;
}

/* ── Desktop layout ──────────────────────────────── */
@media (min-width: 1024px) {
  body {
    display: flex;
    flex-direction: row;
  }

  .sidebar-overlay {
    display: none;
  }

  .app-container {
    flex: 1;
    min-width: 0;
    height: 100vh;
    height: 100dvh;
  }

  .top-bar {
    height: 50px;
    padding: 0 18px;
  }

}

@media (prefers-reduced-motion: reduce) {
  .connection-status:not(.connected) { animation: none; }
}

.connection-status.reconnecting {
  background: var(--yellow);
  box-shadow: 0 0 5px var(--yellow);
}

.terminal-container.stale { position: relative; }
.terminal-container.stale::after {
  content: '';
  position: absolute;
  inset: 0;
  background: var(--bg-primary);
  opacity: 0.4;
  pointer-events: none;
  z-index: 1;
}

/* ── Settings button ─────────────────────────── */
.settings-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 1.05rem;
  padding: 0;
  cursor: pointer;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  transition: background var(--transition-fast), color var(--transition-fast);
  flex-shrink: 0;
}
.settings-btn:hover { background: var(--bg-tertiary); color: var(--text-primary); }

/* ── Settings panel ──────────────────────────── */
.settings-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 200;
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}
.settings-overlay.visible { display: block; }

.settings-panel {
  position: fixed;
  top: 0;
  right: -280px;
  width: 260px;
  height: 100%;
  padding-top: env(safe-area-inset-top);
  background: var(--bg-secondary);
  border-left: 1px solid var(--border-color);
  z-index: 201;
  display: flex;
  flex-direction: column;
  transition: right var(--transition-normal);
}
.settings-panel.open { right: 0; }

.settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 48px;
  padding: 0 16px;
  border-bottom: 1px solid var(--border-color);
  font-weight: 600;
  font-size: 0.88rem;
  color: var(--text-primary);
  flex-shrink: 0;
}
.settings-close {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 1.2rem;
  cursor: pointer;
  padding: 0;
  width: 28px;
  height: 28px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
}
.settings-close:hover { background: var(--bg-tertiary); color: var(--text-primary); }

.settings-body { flex: 1; overflow-y: auto; padding: 8px 0; }

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 13px 16px;
  cursor: pointer;
  transition: background var(--transition-fast);
  -webkit-user-select: none;
  user-select: none;
}
.settings-row:hover { background: var(--bg-tertiary); }
.settings-row:active { background: var(--bg-tertiary); }

.settings-label {
  font-size: 0.88rem;
  color: var(--text-primary);
}

/* Toggle switch */
.settings-toggle {
  position: relative;
  width: 44px;
  height: 26px;
  flex-shrink: 0;
}

.settings-cb {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.settings-toggle-slider {
  position: absolute;
  inset: 0;
  background: var(--border-color);
  border-radius: 26px;
  transition: background 0.2s;
}

.settings-toggle-slider::before {
  content: '';
  position: absolute;
  width: 20px;
  height: 20px;
  left: 3px;
  top: 3px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.2s;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

.settings-cb:checked + .settings-toggle-slider {
  background: var(--accent);
}

.settings-cb:checked + .settings-toggle-slider::before {
  transform: translateX(18px);
}
