/* Canadian Code Compass — Mobile-first, dark mode default */

/* === CSS Custom Properties === */
:root,
[data-theme="dark"] {
  --bg-primary: #1a1a2e;
  --bg-secondary: #16213e;
  --bg-card: #1e2a45;
  --bg-input: #0f1829;
  --text-primary: #e8e8e8;
  --text-secondary: #a0a0a0;
  --text-muted: #6b7280;
  --accent: #cc785c;
  --accent-hover: #e08a6a;
  --accent-glow: rgba(204, 120, 92, 0.15);
  --border: #2a3550;
  --border-hover: #3d4f6f;
  --success: #4caf50;
  --warning: #ff9800;
  --danger: #ef5350;
  --badge-b149-1: #3b82f6;
  --badge-b149-2: #8b5cf6;
  --mark-bg: rgba(76, 175, 80, 0.25);
  --mark-color: #81c784;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  --radius: 10px;
  --radius-sm: 6px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

[data-theme="light"] {
  --bg-primary: #f5f5f7;
  --bg-secondary: #ffffff;
  --bg-card: #ffffff;
  --bg-input: #f0f0f2;
  --text-primary: #1a1a2e;
  --text-secondary: #4a5568;
  --text-muted: #9ca3af;
  --accent: #b05a3a;
  --accent-hover: #c96a48;
  --accent-glow: rgba(176, 90, 58, 0.1);
  --border: #e2e8f0;
  --border-hover: #cbd5e1;
  --mark-bg: rgba(76, 175, 80, 0.15);
  --mark-color: #2e7d32;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

/* === Reset === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html {
  font-family: var(--font);
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

body {
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100dvh;
  overflow-x: hidden;
}

/* === Layout === */
#app {
  max-width: 720px;
  margin: 0 auto;
  padding: 0 16px;
  padding-bottom: env(safe-area-inset-bottom, 16px);
}

/* === Header === */
.app-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  padding: 12px 0;
  margin: 0 -16px;
  padding-left: 16px;
  padding-right: 16px;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.brand-title {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--accent);
  line-height: 1.2;
}

.brand-subtitle {
  font-size: 0.75rem;
  color: var(--text-muted);
  display: block;
}

.header-actions { display: flex; align-items: center; gap: 8px; }

.icon-btn {
  background: none;
  border: none;
  font-size: 1.2rem;
  cursor: pointer;
  padding: 6px;
  /* 44px minimum touch target on every viewport, not just mobile */
  min-width: 44px;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  transition: background 0.15s;
}
.icon-btn:hover { background: var(--accent-glow); }

.offline-badge {
  font-size: 0.7rem;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--warning);
  color: #000;
  font-weight: 600;
}

/* === Install Button === */
.install-btn {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 7px 12px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.12s, background 0.15s;
  box-shadow: 0 2px 6px rgba(204, 120, 92, 0.35);
}
.install-btn:hover { background: var(--accent-hover); }
.install-btn:active { transform: scale(0.96); }
.install-icon { font-size: 0.9rem; font-weight: 700; }
.install-label { white-space: nowrap; }

/* === iOS Install Modal === */
.ios-install-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 2000;
  padding: 16px;
  padding-bottom: max(16px, env(safe-area-inset-bottom, 16px));
  animation: fade-in 0.2s ease-out;
}
.ios-install-content {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 22px 20px 20px;
  max-width: 420px;
  width: 100%;
  position: relative;
  box-shadow: 0 -4px 30px rgba(0, 0, 0, 0.4);
  animation: slide-up 0.25s ease-out;
}
.ios-install-close {
  position: absolute;
  top: 10px;
  right: 10px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.2rem;
  cursor: pointer;
  padding: 8px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
}
.ios-install-close:hover { background: var(--bg-secondary); }
.ios-install-content h2 {
  font-size: 1.1rem;
  color: var(--accent);
  margin-bottom: 8px;
  padding-right: 30px;
}
.ios-install-content p {
  font-size: 0.85rem;
  color: var(--text-secondary);
  line-height: 1.45;
  margin-bottom: 12px;
}
.ios-install-steps {
  list-style: none;
  counter-reset: step;
  padding: 0;
  margin: 12px 0;
}
.ios-install-steps li {
  counter-increment: step;
  position: relative;
  padding: 10px 10px 10px 42px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-bottom: 8px;
  font-size: 0.82rem;
  color: var(--text-primary);
  line-height: 1.4;
}
.ios-install-steps li::before {
  content: counter(step);
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  width: 24px;
  height: 24px;
  background: var(--accent);
  color: #fff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  font-weight: 700;
}
.ios-share-icon {
  display: inline-block;
  padding: 0 4px;
  color: var(--accent);
  font-size: 1rem;
}
.ios-install-note {
  font-size: 0.75rem !important;
  color: var(--text-muted) !important;
  text-align: center;
  margin-top: 8px !important;
  margin-bottom: 0 !important;
}

@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes slide-up {
  from { transform: translateY(20px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

/* === Mode Tabs === */
.mode-tabs {
  display: flex;
  gap: 4px;
  margin-top: 16px;
  background: var(--bg-secondary);
  border-radius: var(--radius);
  padding: 4px;
  border: 1px solid var(--border);
}

.mode-tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 10px 6px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 0.72rem;
  font-weight: 500;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.15s;
}

.mode-tab.active {
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}

.tab-icon { font-size: 1.1rem; }

/* === Search === */
.search-container { margin-top: 16px; }

.search-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.search-input {
  width: 100%;
  padding: 14px 40px 14px 16px;
  font-size: 1rem;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-input);
  color: var(--text-primary);
  outline: none;
  transition: border-color 0.15s;
}
.search-input:focus { border-color: var(--accent); }
.search-input::placeholder { color: var(--text-muted); }

.clear-btn {
  position: absolute;
  right: 8px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.1rem;
  cursor: pointer;
  padding: 8px;
}

.search-hint {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 6px;
  padding-left: 4px;
}

/* === Filter Bar === */
.filter-bar {
  display: flex;
  gap: 6px;
  margin-top: 12px;
}

.filter-btn {
  flex: 1;
  padding: 8px 4px;
  font-size: 0.72rem;
  font-weight: 500;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s;
}

.filter-btn.active {
  background: var(--accent-glow);
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 600;
}

/* === Stats Bar === */
.stats-bar {
  display: flex;
  justify-content: center;
  gap: 8px;
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 10px;
}
.stat-sep { opacity: 0.4; }

/* === Results === */
.results-area { margin-top: 16px; }

.result-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 10px;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.result-card:hover {
  border-color: var(--border-hover);
  box-shadow: var(--shadow);
}

.result-card.severity-immediate_hazard { border-left: 3px solid var(--danger); }
.result-card.severity-code_violation { border-left: 3px solid var(--warning); }

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 6px;
}

.clause-number {
  font-size: 1.3rem;
  font-weight: 800;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.card-badges { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }

.code-badge {
  font-size: 0.65rem;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 999px;
  color: #fff;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.code-badge.b149-1 { background: var(--badge-b149-1); }
.code-badge.b149-2 { background: var(--badge-b149-2); }

.bulletin-badge {
  font-size: 0.65rem;
  padding: 2px 6px;
  border-radius: 999px;
  background: rgba(255, 152, 0, 0.2);
  color: var(--warning);
  font-weight: 600;
}

.category-pill {
  font-size: 0.6rem;
  padding: 2px 6px;
  border-radius: 999px;
  background: var(--accent-glow);
  color: var(--accent);
}

.card-title {
  font-size: 0.9rem;
  font-weight: 600;
  margin-bottom: 4px;
  line-height: 1.3;
}

.card-desc {
  font-size: 0.8rem;
  color: var(--text-secondary);
  line-height: 1.45;
  margin-bottom: 8px;
}

mark {
  background: var(--mark-bg);
  color: var(--mark-color);
  padding: 1px 2px;
  border-radius: 2px;
}

.related-clauses {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-bottom: 8px;
}
.related-chip {
  display: inline-block;
  padding: 1px 6px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 0.7rem;
  margin-right: 4px;
}

.card-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

.copy-btn {
  min-height: 44px;
  min-width: 44px;
  padding: 8px 16px;
  font-size: 0.8rem;
  font-weight: 600;
  border: 1px solid var(--accent);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--accent);
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}
.copy-btn:hover, .copy-btn:active {
  background: var(--accent);
  color: #fff;
}
.copy-btn.copied {
  background: var(--success);
  border-color: var(--success);
  color: #fff;
}
.copy-btn.small {
  min-height: 36px;
  padding: 4px 10px;
  font-size: 0.72rem;
}

.citation-preview {
  font-size: 0.7rem;
  color: var(--text-muted);
  font-family: monospace;
}

/* === No Results === */
.no-results {
  text-align: center;
  padding: 40px 16px;
  color: var(--text-secondary);
}
.no-results-icon { font-size: 2rem; margin-bottom: 10px; }
.no-results-hint { font-size: 0.8rem; color: var(--text-muted); margin-top: 6px; }

/* === Section Browser === */
.section-browser { margin-top: 16px; }

.code-tree { margin-bottom: 20px; }

.code-tree-title {
  font-size: 1rem;
  font-weight: 700;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.section-node {
  margin-bottom: 2px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}

.section-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  cursor: pointer;
  background: var(--bg-secondary);
  transition: background 0.15s;
}
.section-header:hover { background: var(--accent-glow); }

.section-arrow {
  font-size: 0.7rem;
  color: var(--text-muted);
  min-width: 12px;
}
.section-label {
  font-weight: 600;
  font-size: 0.85rem;
  min-width: 70px;
}
.section-title {
  font-size: 0.8rem;
  color: var(--text-secondary);
  flex: 1;
}
.clause-count {
  font-size: 0.7rem;
  color: var(--text-muted);
  background: var(--bg-primary);
  padding: 2px 8px;
  border-radius: 999px;
}

.section-body { padding: 0 12px; }

.browse-clause {
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.browse-clause:last-child { border-bottom: none; }

.browse-clause-header {
  display: flex;
  align-items: baseline;
  gap: 8px;
}
.clause-num {
  font-weight: 700;
  color: var(--accent);
  font-size: 0.85rem;
  min-width: 50px;
}
.browse-title {
  font-size: 0.82rem;
  font-weight: 500;
}
.browse-desc {
  font-size: 0.75rem;
  color: var(--text-secondary);
  margin: 4px 0 6px;
  line-height: 1.4;
}

/* === Bulletins === */
.bulletins-section { margin-top: 24px; }

.bulletins-toggle {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 14px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}

.toggle-arrow { font-size: 0.7rem; color: var(--text-muted); }

.bulletins-list { margin-top: 8px; }

.bulletin-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-left: 3px solid var(--warning);
  border-radius: var(--radius-sm);
  padding: 12px;
  margin-bottom: 8px;
}
.bulletin-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 4px;
}
.bulletin-number {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--warning);
}
.bulletin-date {
  font-size: 0.7rem;
  color: var(--text-muted);
}
.bulletin-title {
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 4px;
}
.bulletin-summary {
  font-size: 0.78rem;
  color: var(--text-secondary);
  line-height: 1.4;
}
.bulletin-clauses {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-top: 6px;
}
.bulletin-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 10px;
  padding: 10px 14px;
  min-height: 40px;
  font-size: 0.8rem;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  border-radius: var(--radius-sm);
  text-decoration: none;
  transition: background 0.15s ease, transform 0.1s ease;
  -webkit-tap-highlight-color: transparent;
}
.bulletin-link:hover { background: var(--accent-hover); }
.bulletin-link:active { transform: scale(0.97); }
.no-bulletins {
  font-size: 0.8rem;
  color: var(--text-muted);
  padding: 10px;
}

/* === Footer === */
.app-footer {
  text-align: center;
  padding: 24px 0 16px;
  font-size: 0.7rem;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
  margin-top: 24px;
}
.footer-note { margin-top: 4px; font-size: 0.65rem; }

/* === Toast === */
.toast {
  position: fixed;
  bottom: max(80px, calc(env(safe-area-inset-bottom, 0px) + 70px));
  left: 50%;
  transform: translateX(-50%);
  padding: 12px 28px;
  background: var(--success);
  color: #fff;
  font-weight: 600;
  font-size: 0.85rem;
  border-radius: 999px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  z-index: 1000;
  transition: opacity 0.2s;
  max-width: calc(100vw - 32px);
  text-align: center;
  animation: toast-in 0.2s ease-out;
}

@keyframes toast-in {
  from { transform: translateX(-50%) translateY(10px); opacity: 0; }
  to { transform: translateX(-50%) translateY(0); opacity: 1; }
}

/* === Loading === */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg-primary);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 999;
  transition: opacity 0.3s;
}
.loading-overlay.fade-out { opacity: 0; pointer-events: none; }

.loading-spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-bottom: 12px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* === Utility === */
.hidden { display: none !important; }

/* === Mobile Touch Enhancements === */
@media (hover: none) and (pointer: coarse) {
  /* Larger tap targets on touch devices */
  .mode-tab { padding: 12px 6px; min-height: 48px; }
  .filter-btn { min-height: 44px; display: flex; align-items: center; justify-content: center; }
  .section-header { padding: 14px 12px; min-height: 48px; }
  .copy-btn { min-height: 48px; padding: 10px 20px; }
  .icon-btn { padding: 10px; min-width: 44px; min-height: 44px; }
  .clear-btn { padding: 12px; min-width: 44px; min-height: 44px; }
  .bulletins-toggle { min-height: 48px; }

  /* Active states for touch feedback (replaces hover) */
  .mode-tab:active { transform: scale(0.97); }
  .filter-btn:active { transform: scale(0.97); }
  .copy-btn:active { background: var(--accent); color: #fff; transform: scale(0.97); }
  .section-header:active { background: var(--accent-glow); }
  .result-card:active { border-color: var(--accent); }
  .bulletins-toggle:active { background: var(--accent-glow); }
}

/* Pull-to-feel: overscroll background matches theme */
body { overscroll-behavior-y: contain; }

/* Prevent text selection on interactive elements */
.mode-tab,
.filter-btn,
.copy-btn,
.section-header,
.bulletins-toggle {
  -webkit-user-select: none;
  user-select: none;
}

/* Smooth scrolling for section browser */
.section-browser { scroll-behavior: smooth; }

/* Landscape phone: tighten up */
@media (max-height: 500px) and (orientation: landscape) {
  .app-header { padding: 6px 16px; }
  .mode-tabs { margin-top: 8px; }
  .search-container { margin-top: 8px; }
  .mode-tab { padding: 6px; }
}

/* === Responsive === */
@media (min-width: 600px) {
  .mode-tab { flex-direction: row; gap: 6px; font-size: 0.82rem; }
  .search-input { font-size: 1.05rem; padding: 16px 44px 16px 18px; }
  .clause-number { font-size: 1.5rem; }
  .filter-btn { font-size: 0.8rem; }
}

@media (min-width: 768px) {
  #app { padding: 0 24px; }
  .result-card { padding: 18px; }
}