/* General styles */
body {
  font-family:
    -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  line-height: 1.6;
  color: #333;
  max-width: 1100px;
  width: 95%;
  margin: 0 auto;
  padding: 1rem 0;
  background-color: #f4f7f9;
}

/* Button and Input base styles */
.btn,
.btn-small,
.search-box {
  box-sizing: border-box;
  font-family: inherit;
}

.btn {
  display: inline-flex;
  align-items: center;
  padding: 8px 16px;
  text-decoration: none;
  color: white;
  background-color: #0066cc;
  border-radius: 6px;
  font-size: 0.9rem;
  font-weight: 600;
  border: none;
  white-space: nowrap;
}
.btn-json {
  background-color: #27ae60;
}

.btn-small {
  padding: 4px 10px;
  background-color: #f8f9fa;
  color: #1e293b;
  border: 1px solid #cbd5e1;
  border-radius: 4px;
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
  height: 31px;
  display: inline-flex;
  align-items: center;
  line-height: 1;
}
.btn-small:hover {
  background-color: #e2e8f0;
  color: #334155;
}
.btn-small:disabled {
  background-color: #f1f5f9;
  color: #94a3b8;
  border-color: #e2e8f0;
  cursor: not-allowed;
}
.btn-nav {
  display: inline-flex;
  align-items: center;
  background-color: #e2e8f0;
  color: #1e293b;
  text-decoration: none;
  padding: 8px 16px;
  border-radius: 6px;
  font-weight: 600;
  font-size: 0.9rem;
  transition: background-color 0.2s;
  white-space: nowrap;
}
.btn-nav:hover {
  background-color: #cbd5e1;
}

/* Hide the search navigation button when in raw data mode */
body.raw-mode #searchNavBtn {
  display: none !important;
}

.table-actions {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 1rem;
}

.search-box {
  padding: 4px 10px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 0.9rem;
  height: 31px;
}

/* --- DIRECTORY VIEW --- */
.container {
  background-color: white;
  padding: 2rem;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.header-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
  gap: 1rem; /* Prevents title and button from touching on small screens */
}

.controls {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
  flex-wrap: wrap;
  gap: 1rem;
}

.controls .search-box {
  flex: 1;
  max-width: 500px;
  height: 40px;
  font-size: 1rem;
}

.controls .btn {
  height: 40px;
}

/* --- RAW MODE OVERRIDES --- */
body.raw-mode {
  max-width: 100%;
  width: 100%;
  padding: 0;
  margin: 0;
  height: 100vh;
  overflow: hidden;
}

body.raw-mode .container {
  padding: 0.75rem;
  border-radius: 0;
  box-shadow: none;
  height: 100vh;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
}

body.raw-mode .header-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
  gap: 1rem;
}

body.raw-mode .header-actions {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

body.raw-mode .controls {
  display: none;
}

/* Matching styles in raw mode */
body.raw-mode .btn {
  padding: 4px 10px;
  font-size: 0.8rem;
  font-weight: 500;
  border-radius: 4px;
  height: 31px;
  line-height: 1;
}

body.raw-mode .search-box {
  width: 300px;
}

body.raw-mode #tableArea {
  display: flex !important;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

body.raw-mode .table-actions {
  margin-bottom: 0.25rem;
}

body.raw-mode .table-wrapper {
  flex: 1;
  max-height: none;
  overflow: auto;
  min-height: 0;
}

body.raw-mode h1 {
  font-size: 1.1rem;
  margin: 0;
}

/* Table elements */
.table-wrapper {
  margin-top: 1rem;
  border: 1px solid #eee;
  border-radius: 6px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

th {
  background-color: #f8f9fa;
  color: #555;
  text-align: left;
  padding: 12px;
  position: sticky;
  top: 0;
  z-index: 5;
  border-bottom: none;
  box-shadow: inset 0 -2px 0 0 #eee;
}

td {
  padding: 12px;
  border-bottom: 1px solid #eee;
  vertical-align: middle;
}
tr:hover {
  background-color: #f1f8ff;
}

th.sortable {
  cursor: pointer;
  user-select: none;
  transition: background-color 0.2s;
}
th.sortable:hover {
  background-color: #e2e8f0;
}

.headshot {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  object-fit: cover;
  object-position: center 15%;
  background-color: #eee;
  border: 1px solid #ddd;
}

.mla-name {
  font-weight: 600;
  color: #0066cc;
  text-decoration: none;
}
.mla-name:hover {
  text-decoration: underline;
}

.raw-table {
  font-size: 0.8rem;
  white-space: nowrap;
  border-collapse: separate;
  border-spacing: 0;
  width: 100%;
  border-left: 1px solid #e2e8f0;
}

.raw-table th,
.raw-table td {
  padding: 6px 12px;
  border-bottom: 1px solid #e2e8f0;
  border-right: 1px solid #e2e8f0;
}

.raw-table th {
  background-color: #f1f5f9;
  color: #334155;
  font-weight: 600;
  position: sticky;
  top: 0;
  z-index: 10;
  border-top: 1px solid #e2e8f0;
}

.loading {
  text-align: center;
  padding: 3rem;
  color: #888;
}
.footer {
  margin-top: 2rem;
  font-size: 0.85rem;
  color: #7f8c8d;
  text-align: center;
  padding: 0 1rem;
  box-sizing: border-box;
}

.footer p:last-child {
  font-size: 0.8rem;
  opacity: 0.85;
}

/* Restored Missing Table Interaction Styles */
.sort-icon::after {
  content: "\21D5";
  margin-left: 5px;
  opacity: 0.3;
}
th.sort-asc .sort-icon::after {
  content: "\2191";
  opacity: 1;
}
th.sort-desc .sort-icon::after {
  content: "\2193";
  opacity: 1;
}

.party-tag {
  font-size: 0.7rem;
  padding: 2px 10px;
  border-radius: 12px;
  font-weight: 700;
  display: inline-block;
  color: white;
  letter-spacing: 0.02em;
}

.email-link,
.phone-link {
  color: #0066cc;
  text-decoration: none;
}
.email-link:hover,
.phone-link:hover {
  text-decoration: underline;
}

.expandable-row {
  cursor: pointer;
  transition: background-color 0.2s;
}
.chevron {
  color: #999;
  font-size: 0.8rem;
  transition: transform 0.3s;
  display: inline-block;
  margin-right: 8px;
}
.expanded-row {
  background-color: #eef2f6 !important;
}
.expanded-row .chevron {
  transform: rotate(180deg);
}

.details-row {
  background-color: #fafbfc;
}
.details-row > td {
  padding: 0;
  border-bottom: 2px solid #ddd;
}
.details-content {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 1.5rem;
  padding: 1.5rem 2rem;
  border-top: 1px dashed #cbd5e1;
}

.detail-card h4 {
  margin: 0 0 0.5rem 0;
  color: #64748b;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.detail-card p {
  margin: 0 0 0.15rem 0;
  font-size: 0.9rem;
  color: #334155;
  line-height: 1.3;
}
.detail-card p.contact-number {
  margin-top: 0.75rem;
  padding-top: 0.5rem;
  border-top: 1px dashed #cbd5e1; /* Optional: adds the same dashed separator used in lookup-example */
}
.detail-card a {
  color: #0066cc;
  text-decoration: none;
  font-weight: 600;
}
.detail-card a:hover {
  text-decoration: underline;
}
.detail-card a.phone-link {
  font-weight: normal;
}

.raw-table tr:hover {
  background-color: #f8fafc;
}

.table-actions #viewToggle {
  margin-left: auto;
}

/* =========================================
   MOBILE RESPONSIVE STYLES (Max Width 768px)
   ========================================= */
@media (max-width: 768px) {
  /* Helper class to hide text exclusively on mobile screens */
  .hide-on-mobile {
    display: none;
  }

  /* 1. Global & Header Adjustments */
  body {
    width: 100%;
    padding: 0;
  }
  .container {
    padding: 1rem;
    border-radius: 0;
  }
  .controls {
    display: flex; /* Ensure flex is active */
    flex-direction: column;
    align-items: stretch;
  }
  #downloadButtons {
    display: flex;
    flex-direction: column;
    width: 100%;
    gap: 0.5rem;
    order: 1;
    margin-bottom: 0.5rem;
  }
  #downloadButtons .btn {
    width: 100%;
    justify-content: center;
  }
  .controls .search-box {
    max-width: 100%;
    order: 2;
    margin-bottom: 0;
  }
  .table-actions {
    flex-wrap: wrap;
  }

  /* 2. DIRECTORY VIEW: Convert Table to Card Layout */
  .table-wrapper {
    border: none;
  }
  #mlaTable thead {
    display: none; /* Hide column headers on mobile */
  }
  #mlaTable,
  #mlaTable tbody,
  #mlaTable tr.expandable-row {
    display: block;
    width: 100%;
    box-sizing: border-box;
  }

  /* Transform standard table row into a CSS Grid Card */
  #mlaTable tr.expandable-row {
    display: grid;
    grid-template-columns: 60px 1fr;
    grid-template-rows: auto auto auto auto;
    gap: 4px 12px;
    padding: 1rem;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    margin-bottom: 1rem;
    background: #fff;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.04);
    position: relative;
  }
  #mlaTable tr.expandable-row td {
    padding: 0;
    border-bottom: none;
  }

  /* Photo (Col 2) */
  #mlaTable tr.expandable-row td:nth-child(2) {
    grid-column: 1;
    grid-row: 1 / span 4;
    align-self: start;
  }
  #mlaTable tr.expandable-row td:nth-child(2) img {
    width: 60px;
    height: 60px;
  }

  /* District Name (Col 1) - Now Header */
  #mlaTable tr.expandable-row td:nth-child(1) {
    grid-column: 2;
    grid-row: 1;
    font-size: 1.1rem;
    font-weight: 700 !important; /* Overrides the inline style */
    color: #1e293b;
    align-self: center;
    padding-right: 24px; /* Prevents text from overlapping the absolute chevron */
  }

  /* MLA Name (Col 3) */
  #mlaTable tr.expandable-row td:nth-child(3) {
    grid-column: 2;
    grid-row: 2;
    font-size: 1rem;
    align-self: center;
  }

  /* Email (Col 4) */
  #mlaTable tr.expandable-row td:nth-child(4) {
    grid-column: 2;
    grid-row: 3;
    font-size: 0.85rem;
    word-break: break-all;
    align-self: center;
  }

  /* Party (Col 5) */
  #mlaTable tr.expandable-row td:nth-child(5) {
    grid-column: 2;
    grid-row: 4;
    align-self: center;
    padding-top: 4px; /* Optional: adds a tiny bit of breathing room above the badge */
  }

  /* Move the chevron arrow to the top right of the card */
  #mlaTable tr.expandable-row td:nth-child(1) .chevron {
    position: absolute;
    top: 1.25rem;
    right: 1rem;
    font-size: 1.2rem;
    margin-right: 0; /* Resets the desktop margin added earlier */
  }

  /* 3. Details Row (Expanded View) */
  #mlaTable tr.expandable-row.expanded-row {
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
    margin-bottom: 0;
  }
  /* Overrides JS inline "table-row" style using adjacent sibling selector */
  #mlaTable tr.expandable-row.expanded-row + tr.details-row {
    display: block !important;
    border: 1px solid #e2e8f0;
    border-top: none;
    border-bottom-left-radius: 8px;
    border-bottom-right-radius: 8px;
    margin-bottom: 1rem;
  }
  #mlaTable tr.details-row td {
    display: block;
    width: 100%;
    box-sizing: border-box;
    border-bottom: none; /* Removes the thick bottom line from desktop */
  }
  .details-content {
    grid-template-columns: 1fr; /* Stack the detail blocks */
    gap: 1.5rem;
    padding: 1.25rem;
  }

  /* 4. RAW VIEW MODE Adjustments */
  body.raw-mode .header-row {
    flex-direction: column;
    align-items: stretch;
  }
  body.raw-mode .header-actions {
    display: flex; /* Ensure flex layout applies */
    flex-wrap: wrap;
    width: 100%;
    gap: 8px;
  }
  body.raw-mode #downloadButtons {
    order: 1; /* Puts download buttons first */
    flex-direction: row;
    flex: 1.2;
    width: auto;
    margin-bottom: 0; /* Clears the margin we added in directory view */
  }
  body.raw-mode #downloadButtons .btn {
    flex: 1;
    padding: 4px 8px;
  }
  body.raw-mode #viewToggle {
    order: 2; /* Puts View Directory button next to the download buttons */
    flex: 1;
    width: auto;
    justify-content: center;
    margin-top: 0;
    padding: 4px 8px;
  }
  body.raw-mode .search-box {
    width: 100%;
    order: 3; /* Forces search bar to drop below all the buttons */
  }
  body.raw-mode .table-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
}
