/* Layout */
body { display: flex; min-height: 100dvh; }

/* Sidebar (desktop only — hidden on mobile) */
#sidebar {
  width: 240px; flex-shrink: 0;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  padding: 16px 12px;
  z-index: 100;
  transition: transform 0.25s ease;
}

.sidebar-brand {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 8px 20px; border-bottom: 1px solid var(--border); margin-bottom: 12px;
}
.brand-logo { height: 36px; width: auto; }
.brand-text { font-size: 15px; font-weight: 700; color: var(--text); }

.sidebar-nav { flex: 1; overflow-y: auto; }
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: var(--radius-sm);
  color: var(--text-muted); font-size: 14px; font-weight: 500;
  cursor: pointer; transition: all 0.15s; user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.nav-item:hover { background: var(--bg-hover); color: var(--text); }
.nav-item.active { background: var(--accent-light); color: var(--accent); font-weight: 600; }
.nav-item svg { width: 18px; height: 18px; flex-shrink: 0; }

.sidebar-footer { padding-top: 12px; border-top: 1px solid var(--border); margin-top: 12px; }
.user-info { padding: 8px; margin-bottom: 8px; }
.user-info .name { font-size: 14px; font-weight: 600; }
.user-info .role { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; }

/* Main */
#main {
  flex: 1; display: flex; flex-direction: column;
  min-width: 0; /* prevent flex blowout */
  height: 100dvh;
}

#top-bar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
  background: var(--bg-dark); flex-shrink: 0;
  min-height: 52px;
}
.top-left { display: flex; align-items: center; gap: 10px; min-width: 0; }
.top-left h2 { font-size: 17px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mobile-menu-btn { display: none; background: none; border: none; color: var(--text); cursor: pointer; padding: 4px; }

#content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
}

/* Content grids */
.grid { display: grid; gap: 12px; }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }

/* Stat cards */
.stat-card { text-align: center; padding: 16px 12px; }
.stat-card .stat-value { font-size: 28px; font-weight: 800; color: var(--text); }
.stat-card .stat-label { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; margin-top: 2px; }
.stat-card .stat-icon { font-size: 24px; margin-bottom: 4px; }

/* Job cards */
.job-card {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px; cursor: pointer; transition: background 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.job-card:hover, .job-card:active { background: var(--bg-hover); }
.job-number { font-size: 12px; font-weight: 700; color: var(--accent); min-width: 44px; }
.job-name { font-size: 14px; font-weight: 500; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.job-arrow { color: var(--text-muted); font-size: 18px; flex-shrink: 0; }

/* File browser */
.file-entry {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 14px; cursor: pointer; transition: background 0.15s;
  border-bottom: 1px solid var(--border);
  -webkit-tap-highlight-color: transparent;
}
.file-entry:hover, .file-entry:active { background: var(--bg-hover); }
.file-icon { font-size: 18px; flex-shrink: 0; }
.file-name { font-size: 14px; flex: 1; word-break: break-word; min-width: 0; }

/* Breadcrumbs */
.breadcrumbs { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 12px; font-size: 13px; }
.breadcrumbs span { color: var(--text-muted); cursor: pointer; }
.breadcrumbs span:hover { color: var(--text); text-decoration: underline; }
.breadcrumbs .sep { color: var(--border); cursor: default; }
.breadcrumbs .sep:hover { text-decoration: none; }
.breadcrumbs .current { color: var(--text); font-weight: 600; cursor: default; }
.breadcrumbs .current:hover { text-decoration: none; }

/* Section headers */
.section-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 12px; gap: 12px;
}
.section-header h3 { font-size: 16px; font-weight: 700; white-space: nowrap; }

/* Search */
.search-bar {
  display: flex; align-items: center; gap: 8px;
  background: var(--bg-input); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 0 12px;
}
.search-bar svg { color: var(--text-muted); flex-shrink: 0; }
.search-bar input {
  flex: 1; padding: 10px 0; font-size: 14px;
  background: none; border: none; color: var(--text); outline: none;
}

/* Sparky Panel */
.ai-panel {
  position: fixed; right: 0; top: 0; bottom: 0; width: 100vw;
  background: var(--bg-card);
  display: flex; flex-direction: column;
  transform: translateX(100%); transition: transform 0.3s ease;
  z-index: 200;
}
.ai-panel.open { transform: translateX(0); }
.ai-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.5);
  z-index: 199; opacity: 0; pointer-events: none; transition: opacity 0.3s;
}
.ai-overlay.open { opacity: 1; pointer-events: auto; }

.ai-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.ai-header h3 { font-size: 16px; font-weight: 700; }
.ai-close { background: none; border: none; color: var(--text-muted); font-size: 28px; cursor: pointer; line-height: 1; padding: 4px 8px; }

#sparky-chat-tab { display: flex; flex-direction: column; flex: 1; min-height: 0; }
#sparky-search-tab { flex-direction: column; flex: 1; min-height: 0; }

.ai-messages {
  flex: 1; overflow-y: auto; padding: 8px 16px; display: flex; flex-direction: column; gap: 8px;
  -webkit-overflow-scrolling: touch;
}
.ai-msg {
  padding: 10px 14px; border-radius: 12px; font-size: 14px;
  max-width: 90%; line-height: 1.5; word-wrap: break-word;
}
.ai-msg-user { background: var(--accent); color: #fff; align-self: flex-end; border-bottom-right-radius: 4px; }
.ai-msg-assistant { background: var(--bg-input); color: var(--text); align-self: flex-start; border-bottom-left-radius: 4px; }
.ai-msg-system { background: transparent; color: var(--text-muted); font-size: 13px; text-align: center; align-self: center; font-style: italic; padding: 20px; }

.ai-input-wrap {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 12px 16px; border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.ai-input-wrap textarea {
  flex: 1; padding: 10px 14px; font-size: 16px; font-family: inherit;
  background: var(--bg-input); color: var(--text);
  border: 1px solid var(--border); border-radius: 12px;
  outline: none; resize: none; max-height: 120px;
}
.ai-input-wrap textarea:focus { border-color: var(--accent); }
.ai-send {
  width: 40px; height: 40px; border-radius: 50%;
  background: var(--accent); color: #fff; border: none;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  flex-shrink: 0; transition: opacity 0.15s;
}
.ai-send:active { opacity: 0.7; }

/* Bottom nav (mobile) */
#bottom-nav {
  display: none; position: fixed; bottom: 0; left: 0; right: 0;
  background: var(--bg-sidebar); border-top: 1px solid var(--border);
  z-index: 100;
  padding-bottom: env(safe-area-inset-bottom, 0);
}
.bottom-nav-items {
  display: flex; justify-content: space-around; align-items: center;
}
.bottom-nav-item {
  display: flex; flex-direction: column; align-items: center; gap: 1px;
  padding: 6px 4px 4px; color: var(--text-muted); font-size: 8px;
  font-weight: 600; cursor: pointer; text-transform: uppercase; letter-spacing: 0.2px;
  -webkit-tap-highlight-color: transparent;
  min-width: 0; flex: 1;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.bottom-nav-item.active { color: var(--accent); }
.bottom-nav-item svg { width: 20px; height: 20px; }

/* ============== MOBILE ============== */
@media (max-width: 768px) {
  body { overflow: hidden; width: 100vw; }

  #sidebar {
    position: fixed; left: 0; top: 0; bottom: 0;
    transform: translateX(-100%); z-index: 150;
    width: 75vw; max-width: 280px;
  }
  #sidebar.open { transform: translateX(0); }

  .mobile-menu-btn { display: block; }
  #bottom-nav { display: block; }
  #main { height: 100dvh; width: 100vw; }

  #top-bar { padding: 8px 12px; min-height: 44px; }
  .top-left h2 { font-size: 14px; }

  #content {
    padding: 10px;
    padding-bottom: calc(64px + env(safe-area-inset-bottom, 0px));
    width: 100%;
  }

  /* All grids single column except stat cards */
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
  .grid-4 { grid-template-columns: repeat(2, 1fr); gap: 8px; }

  .stat-card { padding: 10px 8px; }
  .stat-card .stat-value { font-size: 18px; }
  .stat-card .stat-label { font-size: 9px; }
  .stat-card .stat-icon { font-size: 16px; margin-bottom: 2px; }

  .card { padding: 12px; overflow: hidden; }
  .card-header h3 { font-size: 12px; }

  /* Buttons */
  .btn { padding: 10px 14px; font-size: 13px; }
  .btn-sm { padding: 7px 12px; font-size: 12px; }

  /* Entries */
  .file-entry { padding: 12px 10px; }
  .job-card { padding: 12px 10px; }
  .file-name { font-size: 13px; }
  .job-name { font-size: 13px; }
  .job-number { font-size: 11px; min-width: 36px; }

  /* Tables */
  td { padding: 8px 6px; font-size: 12px; }
  th { padding: 6px; font-size: 10px; }

  /* Sparky panel */
  .ai-panel { width: 100vw; }
  .ai-msg { font-size: 13px; padding: 8px 12px; }
  .ai-input-wrap { padding: 8px 10px; }
  .ai-input-wrap textarea { font-size: 14px; padding: 8px 10px; }

  /* Section header */
  .section-header { flex-wrap: wrap; gap: 8px; }
  .search-bar { width: 100% !important; }

  /* Breadcrumbs */
  .breadcrumbs { font-size: 11px; }

  /* Prevent anything from overflowing */
  .table-wrap { max-width: 100%; }
  table { font-size: 12px; }
}

/* Small phones (iPhone SE, small Androids) */
@media (max-width: 360px) {
  #content { padding: 6px; padding-bottom: calc(64px + env(safe-area-inset-bottom, 0px)); }
  .stat-card .stat-value { font-size: 16px; }
  .card { padding: 10px; }
  .btn { padding: 8px 10px; font-size: 12px; }
  .grid-4 { gap: 6px; }
}

/* Tablet */
@media (min-width: 769px) and (max-width: 1024px) {
  .grid-4 { grid-template-columns: repeat(2, 1fr); }
  .grid-3 { grid-template-columns: repeat(2, 1fr); }
  .ai-panel { width: min(420px, 50vw); border-left: 1px solid var(--border); }
}

/* Desktop */
@media (min-width: 1025px) {
  .ai-panel { width: 420px; border-left: 1px solid var(--border); }
}

/* Email styles */
.email-entry { cursor: pointer; }
.email-unread { background: rgba(59, 130, 246, 0.05); }
.email-unread:hover { background: rgba(59, 130, 246, 0.1); }

/* Voice listening pulse */
@keyframes voice-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.5); }
  50% { box-shadow: 0 0 0 10px rgba(239, 68, 68, 0); }
}
.voice-pulse { animation: voice-pulse 1.2s ease-in-out infinite; }
