/* =========================================================
   Modernized styling (non-breaking, same selectors/behavior)
   - Keeps all class/ID names intact
   - Improves focus states, spacing, shadows
   - Adds dark-mode via prefers-color-scheme
   - Respects reduced-motion users
   ========================================================= */

/* --------- Base tokens --------- */
:root {
  /* Existing palette */
  --primary-color: #2563eb;
  --primary-hover: #1d4ed8;
  --secondary-color: #dc2626;
  --secondary-hover: #b91c1c;
  --background-color: #f3f4f6;
  --content-background: #ffffff;
  --text-color: #1f2937;
  --text-secondary: #4b5563;
  --border-color: #d1d5db;
  --priority-high: #dc2626;
  --priority-medium: #d97706;
  --priority-low: #6b7280;

  /* New design tokens */
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;

  --shadow-sm: 0 2px 10px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 20px rgba(0,0,0,0.08);
  --shadow-lg: 0 10px 32px rgba(0,0,0,0.14);

  --space-1: clamp(6px, 0.8vw, 8px);
  --space-2: clamp(10px, 1.2vw, 12px);
  --space-3: clamp(14px, 1.6vw, 16px);
  --space-4: clamp(18px, 2vw, 24px);

  --font-size-sm: clamp(12px, 0.9vw, 13px);
  --font-size-md: clamp(14px, 1vw, 15px);
  --font-size-lg: clamp(16px, 1.2vw, 18px);
}

/* --------- Global / base --------- */
*,
*::before,
*::after { box-sizing: border-box; }

html, body { height: 100%; }

body {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  font-synthesis-weight: none;

  font-family: 'Roboto', system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji", sans-serif;
  margin: 0;
  padding: var(--space-4);
  display: flex;
  background: linear-gradient(180deg, #f6f7fb 0%, var(--background-color) 25%);
  color: var(--text-color);
  min-height: 100vh;
}

/* Respect reduced motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}

/* --------- Layout shells --------- */
.sidebar {
  width: 240px;
  margin-right: var(--space-3);
  background: var(--content-background);
  padding: var(--space-4);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  transition: transform .3s ease, box-shadow .3s ease, background-color .2s ease;
  display: flex;
  flex-direction: column;
  min-height: calc(100vh - 48px);
  border: 1px solid color-mix(in oklab, var(--border-color) 80%, transparent);
  position: relative;
  z-index: 1;
}

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

.main-content {
  flex: 1;
  max-width: calc(100% - 280px);
  background: var(--content-background);
  padding: var(--space-4);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  transition: box-shadow .3s ease, background-color .2s ease;
  border: 1px solid color-mix(in oklab, var(--border-color) 80%, transparent);
}

.main-content:hover { box-shadow: var(--shadow-lg); }

/* --------- Top Row (Mobile Hamburger + Create Button) --------- */
.top-row {
  display: none;
  justify-content: space-between;
  align-items: center;
  position: fixed;
  top: 8px; left: 8px; right: 8px;
  z-index: 1100;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  padding: 8px 12px;
  background: rgba(255, 255, 255, 0.85);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
}

.top-row a.button {
  padding: 8px 16px;
  font-size: 13px;
  max-width: 140px;
}

/* --------- Hamburger Button --------- */
.hamburger {
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px;
  color: var(--primary-color);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color .18s ease, transform .06s ease;
}

.hamburger:hover { color: var(--primary-hover); transform: translateY(-1px); }
.hamburger-icon rect { fill: currentColor; }

.sr-only {
  position: absolute;
  left: -9999px;
  width: 1px;
  height: 1px;
  overflow: hidden;
}

/* --------- Sidebar Overlay --------- */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 999;
  backdrop-filter: blur(2px);
}

.sidebar-overlay.active { display: block; }

/* --------- Typography --------- */
h1, h2 {
  margin: 0 0 var(--space-3);
  color: var(--text-color);
  font-weight: 650;
  letter-spacing: 0.2px;
}

label {
  font-weight: 550;
  margin-bottom: var(--space-2);
  display: block;
  color: var(--text-secondary);
  font-size: var(--font-size-md);
}

/* --------- Inputs / Controls --------- */
select, a.button, button, input, textarea {
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-color);
  background: var(--content-background);
  font-size: var(--font-size-md);
  width: 100%;
  transition: border-color .18s ease, background .18s ease, box-shadow .18s ease;
  box-shadow: var(--shadow-sm);
}

select:focus-visible,
button:focus-visible,
a.button:focus-visible,
input:focus-visible,
textarea:focus-visible {
  outline: none;
  border-color: var(--primary-color);
  background: #f9fafb;
  box-shadow: 0 0 0 3px color-mix(in oklab, var(--primary-color) 20%, transparent);
}

a.button, button {
  background: var(--primary-color);
  color: #ffffff;
  border: none;
  cursor: pointer;
  font-weight: 600;
  max-width: 150px;
  letter-spacing: .2px;
  text-align: center;
}

a.button:hover, button:hover { background: var(--primary-hover); transform: translateY(-1px); }
a.button:active, button:active { transform: translateY(0); }

#deleteButton {
  background: linear-gradient(180deg, var(--secondary-color), color-mix(in oklab, var(--secondary-color) 88%, black));
  color: #ffffff;
  max-width: 150px;
}

#deleteButton:hover { background: var(--secondary-hover); }

/* --------- Filter Container --------- */
.filter-container {
  display: flex;
  align-items: end;
  gap: 16px;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
}

.filter-group {
  flex: 1;
  min-width: 180px;
  max-width: 250px;
}

.filter-group label {
  display: block;
  margin-bottom: 6px;
  font-size: 0.9rem;
  color: var(--text-secondary);
}

.filter-group select { width: 100%; }

.spacer { flex: 1; }

#createNewItemButtonDesktop {
  white-space: nowrap;
  padding: 10px 20px;
  font-size: 0.95rem;
}

/* --------- Table Container & Table --------- */
.table-container {
  overflow-x: auto;
  margin-top: 1rem;
}

.maintenance-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--content-background);
  box-shadow: var(--shadow-sm);
  border-radius: var(--radius-sm);
}

.maintenance-table th {
  background: #1f2937;
  color: white;
  padding: 12px 16px;
  text-align: left;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  user-select: none;
  position: sticky;
  top: 0;
  z-index: 1;
}

.maintenance-table th.sortable:hover { background: #111827; }

.maintenance-table th.sorted-asc::after,
.maintenance-table th.sorted-desc::after {
  content: '';
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  border: 5px solid transparent;
}

.maintenance-table th.sorted-asc::after { border-bottom-color: white; }
.maintenance-table th.sorted-desc::after { border-top-color: white; }

.maintenance-table td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-color);
  font-size: 0.9rem;
  vertical-align: middle;
}

.maintenance-row {
  cursor: pointer;
  transition: background .15s ease;
}

.maintenance-row:hover { background: #f8fafc; }

.item-location { font-weight: 600; color: var(--text-color); }

.item-priority .priority,
.item-status .status {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 700;
}

.item-priority .priority-high { background: #dc2626; color: white; }
.item-priority .priority-medium { background: #d97706; color: white; }
.item-priority .priority-low { background: #6b7280; color: white; }

.item-status .status {
  background: #dbeafe;
  color: #2563eb;
}

.item-summary {
  font-style: italic;
  color: var(--text-secondary);
  max-width: 200px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.item-author,
.item-date,
.item-aging {
  color: var(--text-secondary);
  font-size: 0.85rem;
}

/* --------- History Styles --------- */
.history-container {
  background: #fafafa;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 16px;
  margin-bottom: var(--space-3);
  box-shadow: var(--shadow-sm);
}

.history-entry {
  margin-bottom: 12px;
  padding-bottom: 10px;
  border-bottom: 1px dashed #e5e7eb;
  line-height: 1.5;
}

.history-entry:last-child {
  border-bottom: none;
  margin-bottom: 0;
  padding-bottom: 0;
}

.history-meta {
  display: block;
  margin-top: 4px;
  color: var(--text-secondary);
  font-size: 0.875rem;
}

/* --------- Details/Summary (Collapsible) --------- */
details summary {
  list-style: none;
  cursor: pointer;
  font-weight: 600;
  user-select: none;
}
details[open] > summary { margin-bottom: 0; }
details > div {
  background: #ffffff;
  border: 1px solid var(--border-color);
  border-top: none;
  border-radius: 0 0 8px 8px;
  padding: 0;
}

/* ========= DARK MODE ========= */
@media (prefers-color-scheme: dark) {
  :root {
    --background-color: #0b0f17;
    --content-background: #0f1622;
    --text-color: #e5e7eb;
    --text-secondary: #9aa4b2;
    --border-color: #243244;
  }

  body {
    background: radial-gradient(1200px 600px at 10% -10%, #142033 0%, #0b0f17 40%, #0b0f17 100%);
  }

  .sidebar, .main-content, .form-container, .action-item, .history-container {
    box-shadow: 0 1px 0 rgba(255,255,255,0.03), var(--shadow-sm);
    border-color: color-mix(in oklab, var(--border-color) 90%, transparent);
  }

  tr:nth-child(even) { background: #0f1520; }
  tr:hover { background: #132033; }
  th { background: linear-gradient(180deg, #0f172a, #0b1220); }

  .history-container { background: #1a1f2e; }
  .history-entry { border-bottom-color: #334155; }
  .history-meta { color: #94a3b8; }

  .top-row { background: rgba(15, 22, 34, 0.9); }
}

/* ========= MOBILE RESPONSIVE (≤768px) ========= */
@media (max-width: 768px) {
  body { flex-direction: column; padding: 8px; }

  /* Top Row (Hamburger + Create Button) */
  .top-row { display: flex; }

  /* Sidebar (Slide-in Drawer) */
  .sidebar {
    position: fixed;
    top: 0; left: 0;
    height: 100vh;
    width: 260px;
    z-index: 1000;
    transform: translateX(-100%);
    transition: transform .3s ease-in-out;
    min-height: 100%;
    border-radius: 0;
    margin: 0;
    box-shadow: var(--shadow-lg);
  }
  .sidebar.active { transform: translateX(0); }

  /* Main Content */
  .main-content {
    max-width: 100%;
    width: calc(100% - 16px);
    margin: 64px 8px 8px 8px;
    box-sizing: border-box;
  }

  /* Filters */
  .filter-container {
    flex-direction: column;
    align-items: stretch;
  }
  .filter-group { min-width: unset; max-width: unset; }
  #createNewItemButtonDesktop, .spacer { display: none; }

  /* Table → Card List */
  .maintenance-table,
  .maintenance-table thead,
  .maintenance-table tbody,
  .maintenance-table th,
  .maintenance-table td,
  .maintenance-table tr {
    display: block;
  }

  .maintenance-table thead tr {
    position: absolute;
    top: -9999px;
    left: -9999px;
  }

  .maintenance-row {
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    padding: 14px;
    margin-bottom: 12px;
    background: var(--content-background);
    box-shadow: var(--shadow-sm);
    cursor: pointer;
  }

  .maintenance-row td {
    border: none;
    padding: 0;
    display: none; /* Hide all by default, then show specific ones */
  }

  .maintenance-row td::before {
    display: none; /* Remove data-label styling */
  }

  /* First Row: Location on left, Priority + Status badges stacked on right */
  .maintenance-row .item-location,
  .maintenance-row .item-priority,
  .maintenance-row .item-status {
    display: block;
  }

  .maintenance-row .item-location {
    float: left;
    width: auto;
    max-width: 50%;
    font-size: 1rem;
    font-weight: 700;
    padding-right: 8px;
  }

  .maintenance-row .item-priority,
  .maintenance-row .item-status {
    float: right;
    width: auto;
    clear: right;
    margin-bottom: 4px;
    text-align: right;
  }

  .maintenance-row .item-priority .priority,
  .maintenance-row .item-status .status {
    min-width: 90px;
    text-align: center;
  }

  .maintenance-row .item-status {
    margin-bottom: 8px;
  }

  /* Second Row: Full-width summary */
  .maintenance-row .item-summary {
    display: block;
    clear: both;
    width: 100%;
    max-width: none;
    white-space: normal;
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px solid color-mix(in oklab, var(--border-color) 60%, transparent);
    font-size: 0.9rem;
  }

  /* History on Mobile */
  .history-container { padding: 12px; }
  .history-entry { margin-bottom: 10px; font-size: 0.9rem; }
  .history-meta { font-size: 0.8rem; }

  /* Buttons & Inputs */
  select, textarea, button, a.button { font-size: 13px; padding: 8px; }
}