* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --primary-color: #0175c2;
  --primary-dark: #015a94;
  --secondary-color: #02569b;
  --success-color: #4caf50;
  --danger-color: #f44336;
  --text-primary: #212121;
  --text-secondary: #757575;
  --border-color: #e0e0e0;
  --bg-gray: #f5f5f5;
  --shadow: 0 2px 4px rgba(0,0,0,0.1);
  --shadow-lg: 0 4px 12px rgba(0,0,0,0.15);
  --tab-bar-height: 3.5rem;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  color: var(--text-primary);
  background-color: var(--bg-gray);
  overflow: hidden;
}

/* --- MOBILE LAYOUT (THE FIX) ---
  This is the correct grid for mobile.
  Row 1: Main Content (1fr - fills all space and scrolls)
  Row 2: Tab Bar (auto height - fixed at bottom)
*/
.app-container {
  display: grid;
  grid-template-rows: 1fr var(--tab-bar-height);
  height: 100vh;
  max-height: 100vh;
  overflow: hidden;
}

.sidebar {
  display: none;
}

/* Header */
.app-header {
  background: white;
  padding: 1rem;
  box-shadow: var(--shadow);
  z-index: 10;
  /* Make header sticky so it stays visible while scrolling */
  position: sticky;
  top: 0;
}

.app-header h1 {
  font-size: 1.5rem;
  color: var(--primary-color);
  margin-bottom: 0.75rem;
}

.search-container {
  position: relative;
}

#searchInput {
  width: 100%;
  padding: 0.75rem 1rem;
  font-size: 1rem;
  border: 2px solid var(--border-color);
  border-radius: 8px;
  outline: none;
  transition: border-color 0.2s;
  -webkit-appearance: none;
}

#searchInput:focus {
  border-color: var(--primary-color);
}

/* Main Content */
.main-content {
  /* This is now the main scrolling container */
  grid-row: 1; /* Sits in the first (1fr) row */
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch; /* Fix for momentum scrolling on iOS */
}

.rules-container {
  /* This no longer needs to scroll */
  overflow: visible;
  padding: 1rem;
}

/* Rule Cards */
.rule-card {
  background: white;
  border-radius: 8px;
  padding: 1rem;
  margin-bottom: 0.75rem;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: all 0.2s;
}

.rule-card:hover {
  box-shadow: var(--shadow-lg);
}

.rule-card-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
}

.rule-info {
  flex: 1;
  min-width: 0;
}

.rule-id {
  font-family: 'Courier New', monospace;
  font-weight: 600;
  color: var(--primary-color);
  font-size: 0.95rem;
  margin-bottom: 0.25rem;
  word-break: break-all;
}

.rule-description {
  color: var(--text-secondary);
  font-size: 0.9rem;
  line-height: 1.4;
}

.rule-toggle-btn {
  padding: 0.5rem 1rem;
  border: 2px solid var(--primary-color);
  background: white;
  color: var(--primary-color);
  border-radius: 6px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.2s;
  font-size: 0.9rem;
}

.rule-toggle-btn:hover {
  background: var(--primary-color);
  color: white;
}

.rule-toggle-btn.active {
  background: var(--success-color);
  border-color: var(--success-color);
  color: white;
}

.rule-details {
  display: none;
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border-color);
}

.rule-card.expanded .rule-details {
  display: block;
}

.rule-details-section {
  margin-bottom: 1rem;
}

.rule-details-section h4 {
  font-size: 0.85rem;
  text-transform: uppercase;
  color: var(--text-secondary);
  margin-bottom: 0.5rem;
}

.rule-details-text {
  color: var(--text-primary);
  line-height: 1.6;
  margin-bottom: 0.75rem;
}

.code-example {
  background: #f8f8f8;
  border-left: 4px solid var(--primary-color);
  padding: 0.75rem;
  border-radius: 4px;
  font-family: 'Courier New', monospace;
  font-size: 0.85rem;
  overflow-x: auto;
  white-space: pre;
  margin-bottom: 0.5rem;
}

.code-example.bad {
  border-left-color: var(--danger-color);
  background: #fff5f5;
}

.code-example.good {
  border-left-color: var(--success-color);
  background: #f1f8f4;
}

/* --- Builder Tray - Mobile (THE FIX) --- 
  It is NO LONGER in the grid.
  It's just a normal block at the end of main-content.
  This fixes the "not popping up" bug.
*/
.builder-tray {
  background: white;
  box-shadow: 0 -4px 12px rgba(0,0,0,0.15);
  transition: transform 0.3s ease;
  transform: translateY(0);
  /* Add margin to separate from rules list */
  margin: 1rem;
  border-radius: 8px;
  overflow: hidden; /* For the border-radius */
}

.builder-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
  background: var(--primary-color);
  color: white;
  cursor: pointer;
  user-select: none;
}

.builder-header h3 {
  font-size: 1rem;
}

.rule-count {
  font-size: 0.9rem;
  opacity: 0.9;
}

.builder-content {
  /* This part is for the collapsible logic */
  max-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: max-height 0.3s ease-out;
}

/* This class is added by JS to open the tray */
.builder-tray.expanded .builder-content {
  max-height: 50vh; /* Allow it to grow */
}

.builder-content > pre {
  flex: 1; 
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  margin: 0;
}

#yamlOutput {
  display: block;
  background: #1e1e1e;
  color: #d4d4d4;
  padding: 1rem;
  font-family: 'Courier New', monospace;
  font-size: 0.85rem;
  line-height: 1.5;
  margin: 0;
  white-space: pre;
}

.builder-actions {
  display: flex;
  gap: 0.5rem;
  padding: 1rem;
  background: white;
  border-top: 1px solid var(--border-color);
}

.action-button {
  flex: 1;
  padding: 0.75rem;
  border: none;
  border-radius: 6px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  font-size: 1rem;
}

.action-button.primary {
  background: var(--primary-color);
  color: white;
}

.action-button.primary:hover {
  background: var(--primary-dark);
}

.action-button.secondary {
  background: white;
  color: var(--text-primary);
  border: 2px solid var(--border-color);
}

.action-button.secondary:hover {
  background: var(--bg-gray);
}

/* Tab Bar - Mobile */
.tab-bar {
  grid-row: 2; /* Sits in the second (and last) row */
  display: flex;
  background: white;
  border-top: 1px solid var(--border-color);
  height: var(--tab-bar-height);
  z-index: 20; /* Keep it on top */
}

.tab-button {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.25rem;
  border: none;
  background: none;
  color: var(--text-secondary);
  cursor: pointer;
  transition: color 0.2s;
}

.tab-button.active {
  color: var(--primary-color);
}

.tab-button svg {
  width: 24px;
  height: 24px;
}

.tab-button span {
  font-size: 0.75rem;
}

/* Modal - Mobile */
.modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  z-index: 100;
  align-items: flex-end;
}

.modal.active {
  display: flex;
}

.modal-content {
  background: white;
  width: 100%;
  max-height: 80vh;
  border-radius: 16px 16px 0 0;
  animation: slideUp 0.3s ease;
}

@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1.25rem;
  border-bottom: 1px solid var(--border-color);
}

.modal-header h2 {
  font-size: 1.25rem;
}

.close-button {
  background: none;
  border: none;
  font-size: 2rem;
  color: var(--text-secondary);
  cursor: pointer;
  line-height: 1;
}

.modal-body {
  padding: 1rem;
  overflow-y: auto;
}

/* Presets */
.presets-list {
  padding: 1rem;
}

.preset-button {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.25rem;
  padding: 1rem;
  margin-bottom: 0.75rem;
  background: white;
  border: 2px solid var(--border-color);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s;
  text-align: left;
}

.preset-button:hover {
  border-color: var(--primary-color);
  background: #f0f7ff;
}

.preset-button strong {
  color: var(--primary-color);
  font-size: 1rem;
}

.preset-button span {
  color: var(--text-secondary);
  font-size: 0.875rem;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 5rem;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: #323232;
  color: white;
  padding: 1rem 1.5rem;
  border-radius: 8px;
  box-shadow: var(--shadow-lg);
  z-index: 1000;
  opacity: 0;
  transition: all 0.3s ease;
  pointer-events: none;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* Desktop Layout */
@media (min-width: 768px) {
  .app-container {
    /* This is the desktop layout: 3 columns */
    grid-template-columns: 280px 1fr 400px;
    grid-template-rows: 1fr; /* A single row */
    gap: 0;
  }

  .sidebar {
    display: flex;
    flex-direction: column;
    background: white;
    border-right: 1px solid var(--border-color);
    overflow-y: auto;
    grid-row: 1; 
    grid-column: 1; 
  }

  .sidebar-header {
    padding: 1.5rem 1rem;
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-gray);
  }

  .sidebar-header h2 {
    font-size: 1.25rem;
    color: var(--primary-color);
  }

  .presets-list {
    padding: 1rem;
  }

  .main-content {
    grid-row: 1;
    grid-column: 2;
    /* On desktop, we need to put it back to flex
       so the rules container can scroll independently */
    display: flex;
    flex-direction: column;
    overflow: hidden; 
  }
  
  .app-header {
      padding: 1.5rem;
      position: static; /* Not sticky on desktop */
  }

  .rules-container {
    padding: 1.5rem;
    flex: 1; /* Take up all space */
    overflow-y: auto; /* And scroll */
    -webkit-overflow-scrolling: touch;
  }

  .rule-card {
    padding: 1.25rem;
  }

  .builder-tray {
    /* This is the magic fix.
      We move the builder tray from .main-content
      to its own grid column.
    */
    grid-column: 3;
    grid-row: 1;
    
    /* And reset all its properties to be a column */
    position: static; 
    display: flex;
    flex-direction: column;
    border-left: 1px solid var(--border-color);
    box-shadow: none;
    transform: none; /* Reset mobile transform */
    margin: 0;
    border-radius: 0;
    overflow: hidden;
  }

  .builder-header {
    cursor: default;
  }

  .builder-content {
    flex: 1; /* Make it fill the height */
    display: flex;
    flex-direction: column;
    max-height: none !important; /* Override mobile style */
    overflow: hidden;
  }

  .builder-content > pre {
    flex: 1; /* Make code block fill space */
    max-height: none;
    overflow-y: auto;
  }
  
  .builder-actions {
    /* No changes needed */
  }

  .tab-bar {
    display: none; /* Hide mobile-only tab bar */
  }

  .modal {
    display: none !important; /* Hide mobile-only modal */
  }
}

@media (min-width: 1024px) {
  .app-container {
    grid-template-columns: 320px 1fr 480px;
  }

  .app-header h1 {
    font-size: 1.75rem;
  }
}
