:root {
  /* Light Mode Colors - Premium Palette */
  --bg-subtle: #f8fafc;
  --bg-main: #ffffff;
  --panel-bg: rgba(255, 255, 255, 0.75);
  --panel-border: rgba(226, 232, 240, 0.8);
  
  --text-main: #0f172a;
  --text-muted: #64748b;
  --text-inverse: #ffffff;
  
  --accent-primary: #3b82f6; /* Modern Blue */
  --accent-hover: #2563eb;
  --accent-active: #1d4ed8;
  --accent-ghost-hover: rgba(59, 130, 246, 0.1);
  
  --success-bg: rgba(16, 185, 129, 0.15);
  --success-text: #047857;
  --success-border: rgba(16, 185, 129, 0.3);
  
  --warning-bg: rgba(245, 158, 11, 0.15);
  --warning-text: #b45309;

  --input-bg: rgba(255, 255, 255, 0.9);
  --input-border: #cbd5e1;
  --input-focus: rgba(59, 130, 246, 0.5);
  
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.05), 0 2px 4px -1px rgba(0, 0, 0, 0.03);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.05), 0 4px 6px -2px rgba(0, 0, 0, 0.03);
  --shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
  
  --glass-blur: blur(16px);
  --glass-bg: rgba(255, 255, 255, 0.65);
  
  /* Layout Constants */
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 18px;
  --radius-xl: 24px;
  --radius-full: 9999px;
  --transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  --tap-target: 44px; /* Minimum accessible tap target */
}

@media (prefers-color-scheme: dark) {
  :root {
    /* Dark Mode Colors - Deep & Sleek */
    --bg-subtle: #020617; /* Very dark slate */
    --bg-main: #0f172a;
    --panel-bg: rgba(30, 41, 59, 0.65);
    --panel-border: rgba(51, 65, 85, 0.8);
    
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
    --text-inverse: #ffffff;
    
    --accent-primary: #3b82f6; 
    --accent-hover: #60a5fa;
    --accent-active: #93c5fd;
    --accent-ghost-hover: rgba(96, 165, 250, 0.15);
    
    --success-bg: rgba(16, 185, 129, 0.2);
    --success-text: #34d399;
    --success-border: rgba(16, 185, 129, 0.3);
    
    --warning-bg: rgba(245, 158, 11, 0.2);
    --warning-text: #fbbf24;

    --input-bg: rgba(15, 23, 42, 0.8);
    --input-border: #475569;
    --input-focus: rgba(96, 165, 250, 0.5);
    
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.4);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.4), 0 2px 4px -1px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.5), 0 4px 6px -2px rgba(0, 0, 0, 0.3);
    --shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.6), 0 10px 10px -5px rgba(0, 0, 0, 0.3);
    
    --glass-bg: rgba(15, 23, 42, 0.65);
  }
}

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

body {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  color: var(--text-main);
  background-color: var(--bg-subtle);
  /* A very subtle premium mesh gradient feel */
  background-image: 
    radial-gradient(circle at 10% 0%, rgba(59, 130, 246, 0.12), transparent 40%),
    radial-gradient(circle at 90% 100%, rgba(16, 185, 129, 0.08), transparent 40%);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

a {
  color: var(--accent-primary);
  text-decoration: none;
  transition: var(--transition);
}

a:hover {
  color: var(--accent-hover);
}

button, input, select, textarea {
  font-family: inherit;
  font-size: 15px;
}

/* Layout Core */
.shell {
  max-width: 1040px;
  margin: 0 auto;
  padding: 24px 16px 40px;
}

/* Typography */
h1, h2, h3, h4 {
  color: var(--text-main);
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: -0.02em;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-start;
  margin-bottom: 24px;
}

.topbar h1 {
  font-size: clamp(1.75rem, 5vw, 2.5rem);
  letter-spacing: -0.03em;
  background: linear-gradient(135deg, var(--text-main) 0%, var(--text-muted) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.eyebrow {
  text-transform: uppercase;
  letter-spacing: 0.15em;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  display: block;
  margin-bottom: 4px;
}

.topbar-user {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

/* Badges & Chips */
.pill, .code-chip {
  display: inline-flex;
  align-items: center;
  padding: 4px 10px;
  border-radius: var(--radius-full);
  background: var(--warning-bg);
  color: var(--warning-text);
  font-size: 0.75rem;
  font-weight: 600;
  border: 1px solid transparent;
}

.code-chip {
  background: var(--bg-main);
  border-color: var(--panel-border);
  color: var(--text-muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

.muted-pill {
  background: var(--bg-subtle);
  color: var(--text-muted);
  border: 1px solid var(--panel-border);
}

.edit-badge {
  display: inline-block;
  margin-top: 6px;
  padding: 2px 8px;
  border-radius: var(--radius-full);
  background: var(--accent-ghost-hover);
  color: var(--accent-primary);
  font-size: 0.7rem;
  font-weight: 600;
}

/* Navigation */
.nav {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 24px;
  padding: 8px;
  background: var(--glass-bg);
  backdrop-filter: var(--glass-blur);
  -webkit-backdrop-filter: var(--glass-blur);
  border-radius: var(--radius-full);
  border: 1px solid var(--panel-border);
  box-shadow: var(--shadow-sm);
  position: sticky;
  top: 16px;
  z-index: 40;
}

.nav a {
  padding: 8px 16px;
  border-radius: var(--radius-full);
  color: var(--text-main);
  font-weight: 600;
  font-size: 0.9rem;
  transition: var(--transition);
}

.nav a:hover, .nav a:active {
  background: var(--accent-ghost-hover);
  color: var(--accent-primary);
}

/* Notices & Toasts! */
.notice, .toast-notification {
  margin-bottom: 24px;
  padding: 16px 20px;
  border-radius: var(--radius-md);
  background: var(--success-bg);
  border: 1px solid var(--success-border);
  color: var(--success-text);
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 12px;
  box-shadow: var(--shadow-md);
  animation: slideIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes slideIn {
  from { transform: translateY(-20px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

/* Glass & Panel System */
.panel {
  background: var(--panel-bg);
  backdrop-filter: var(--glass-blur);
  -webkit-backdrop-filter: var(--glass-blur);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: 24px;
}

.stack {
  display: grid;
  gap: 16px;
}

.hero-card, .form-card {
  padding: 24px;
  border-radius: var(--radius-lg);
  background: var(--panel-bg);
  backdrop-filter: var(--glass-blur);
  -webkit-backdrop-filter: var(--glass-blur);
  border: 1px solid var(--panel-border);
  box-shadow: var(--shadow-md);
}

.split-card {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: center;
}

/* Grid Layouts */
.search-grid, .two-column {
  display: grid;
  gap: 12px;
  margin-top: 16px;
}

.search-grid {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.two-column {
  grid-template-columns: minmax(280px, 360px) minmax(0, 1fr);
  align-items: start;
}

/* Typography Utils */
.results-head {
  margin: 24px 0 12px;
  color: var(--text-muted);
  font-size: 0.95rem;
  font-weight: 500;
}

.muted { color: var(--text-muted); }

.meta-note {
  margin-top: 4px;
  color: var(--text-muted);
  font-size: 0.75rem;
  line-height: 1.3;
}

.helper-strip {
  margin-top: 12px;
  padding: 12px 16px;
  border-radius: var(--radius-md);
  background: var(--bg-subtle);
  border: 1px solid var(--panel-border);
  color: var(--text-muted);
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Forms & Inputs */
input, select, textarea {
  width: 100%;
  min-height: var(--tap-target); /* Accessible target */
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--input-border);
  background: var(--input-bg);
  color: var(--text-main);
  transition: var(--transition);
  box-shadow: var(--shadow-sm);
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent-primary);
  box-shadow: 0 0 0 3px var(--input-focus);
}

textarea {
  resize: vertical;
  min-height: 80px;
}

label {
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--text-main);
  margin-bottom: 2px;
  display: block;
}

/* Buttons */
button {
  min-height: var(--tap-target);
  padding: 0 20px;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  background: var(--accent-primary);
  color: #ffffff;
  font-weight: 600;
  transition: var(--transition);
  box-shadow: var(--shadow-sm);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

button:hover {
  background: var(--accent-hover);
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

button:active {
  transform: translateY(1px) scale(0.98);
  background: var(--accent-active);
  box-shadow: none;
}

.ghost-button {
  background: transparent;
  color: var(--accent-primary);
  border: 1px solid var(--input-border);
  box-shadow: none;
}

.ghost-button:hover {
  background: var(--accent-ghost-hover);
  border-color: var(--accent-primary);
  color: var(--accent-hover);
  box-shadow: none;
}

/* Forms */
.inline-form { display: inline; }
.table-form, .party-settings-form {
  display: grid;
  gap: 8px;
  align-items: start;
}

.filter-strip {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin: 16px 0;
  padding: 12px 16px;
  border-radius: var(--radius-lg);
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  box-shadow: var(--shadow-sm);
}

.check-row {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
}

.check-row input[type="checkbox"] {
  width: 20px;
  height: 20px;
  min-height: 20px;
  cursor: pointer;
  accent-color: var(--accent-primary);
}

/* Desktop Tables */
.table-wrap {
  overflow-x: auto;
  border-radius: var(--radius-lg);
  border: 1px solid var(--panel-border);
  background: var(--panel-bg);
  box-shadow: var(--shadow-md);
}

table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
}

th, td {
  padding: 16px;
  text-align: left;
  border-bottom: 1px solid var(--panel-border);
  vertical-align: middle;
}

th {
  color: var(--text-muted);
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 700;
  background: var(--bg-subtle);
}

tr:last-child td {
  border-bottom: none;
}

tr:hover td {
  background: rgba(var(--text-main), 0.02);
}

.empty-state {
  padding: 40px 20px;
  text-align: center;
  border-radius: var(--radius-lg);
  border: 2px dashed var(--panel-border);
  color: var(--text-muted);
  font-weight: 500;
  background: var(--bg-subtle);
}

.auth-shell {
  max-width: 440px;
  margin: 40px auto;
}

/* 
 * MOBILE / NATIVE APP STYLING 
 * Transforms the table into beautiful cards + Bottom Sheets
 */
@media (max-width: 900px) {
  body {
    -webkit-tap-highlight-color: transparent;
  }
  
  .shell { padding: 12px 12px 100px; /* pad bottom for scrolling */ }

  /* Redefined Grid to stack cleanly */
  .search-grid, .two-column { grid-template-columns: 1fr; }
  
  .topbar {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
  }
  
  .topbar-user {
    justify-content: flex-start;
    padding: 12px;
    background: var(--panel-bg);
    border-radius: var(--radius-md);
    border: 1px solid var(--panel-border);
  }

  .nav {
    display: flex;
    overflow-x: auto;
    flex-wrap: nowrap;
    scroll-snap-type: x mandatory;
    gap: 8px;
    padding: 8px;
    /* Hide scrollbar */
    -ms-overflow-style: none;
    scrollbar-width: none;
  }
  .nav::-webkit-scrollbar { display: none; }
  
  .nav a {
    flex: 0 0 auto;
    scroll-snap-align: start;
    white-space: nowrap;
  }
  
  .split-card {
    flex-direction: column;
    align-items: flex-start;
  }

  /* Hide raw table structures on mobile entirely */
  .responsive-table thead { display: none; }
  .responsive-table {
    display: block;
    width: 100%;
    border: none;
  }
  .responsive-table tbody {
    display: flex;
    flex-direction: column;
    gap: 16px;
  }
  .table-wrap {
    background: transparent;
    border: none;
    box-shadow: none;
    overflow: visible;
  }

  /* Translate TR to Card using CSS Grid for maximum density */
  .responsive-table tr.search-row {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "party date"
      "item  item"
      "desc  desc"
      "source source"
      "num   rem"
      "rate  rate";
    gap: 8px 12px;
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    border-radius: var(--radius-lg);
    padding: 16px;
    box-shadow: var(--shadow-sm);
    position: relative;
    overflow: hidden;
  }
  
  /* Reset TDs for grid children */
  .responsive-table td {
    border: none;
    padding: 0;
    display: block; /* Override default table display */
  }
  
  /* Map classes to Grid Areas */
  .col-party { grid-area: party; }
  .col-date { grid-area: date; text-align: right; }
  .col-item-name { grid-area: item; }
  .col-item-desc { grid-area: desc; }
  .col-rate { grid-area: rate; }
  .col-number { grid-area: num; }
  .col-remarks { grid-area: rem; }
  .col-source { grid-area: source; }

  /* Hide raw table structures on mobile entirely */
  .responsive-table td::before { display: none; } /* NO LABELS on mobile */
  
  /* Styling internal card elements for density */
  .col-party strong {
    font-size: 1.15rem;
    color: var(--text-main);
    line-height: 1.2;
  }

  .col-date {
    font-size: 0.8rem;
    color: var(--text-muted);
    font-weight: 500;
  }

  .col-item-name {
    font-weight: 600;
    color: var(--text-main);
  }

  .col-item-desc {
    font-size: 0.9rem;
    color: var(--text-muted);
    line-height: 1.4;
    padding-bottom: 8px;
    border-bottom: 1px dashed var(--panel-border);
  }

  .col-source {
    display: flex;
    justify-content: flex-end;
    margin-top: -6px;
    margin-bottom: 4px;
  }

  /* Compact Value Displays */
  .val-display, .val-display-remarks {
    font-size: 1rem;
    font-weight: 700;
    color: var(--text-main);
    margin-bottom: 6px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  
  .val-display { color: var(--accent-primary); }

  .col-number, .col-remarks {
    background: var(--bg-subtle);
    border: 1px solid var(--panel-border);
    border-radius: var(--radius-md);
    padding: 10px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
  }

  .edit-btn {
    width: 100%;
    padding: 8px 10px;
    min-height: 36px; /* slightly smaller but still tappable */
    font-size: 0.85rem;
  }

  .meta-container {
    margin-top: 6px;
    min-height: 18px; /* reserve space so buttons align evenly even if one lacks a badge */
  }
  
  /* Filter stack */
  .filter-strip {
    flex-direction: column;
    align-items: flex-start;
  }
  .filter-strip button {
    width: 100%;
  }
  
  /* Native App Bottom Sheet System */
  .bottom-sheet-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    z-index: 100;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
  }
  
  .bottom-sheet-overlay.active {
    opacity: 1;
    pointer-events: auto;
  }
  
  .bottom-sheet {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    background: var(--bg-main);
    border-top-left-radius: var(--radius-xl);
    border-top-right-radius: var(--radius-xl);
    padding: 24px 20px 40px;
    z-index: 101;
    transform: translateY(100%);
    transition: transform 0.4s cubic-bezier(0.175, 0.885, 0.32, 1);
    box-shadow: 0 -10px 40px rgba(0,0,0,0.15);
  }
  
  .bottom-sheet.active {
    transform: translateY(0);
  }
  
  .bottom-sheet-handle {
    width: 40px;
    height: 5px;
    background: var(--input-border);
    border-radius: var(--radius-full);
    margin: 0 auto 20px;
  }
  
  .bottom-sheet-title {
    font-size: 1.25rem;
    font-weight: 700;
    margin-bottom: 4px;
  }
  .bottom-sheet-subtitle {
    color: var(--text-muted);
    font-size: 0.9rem;
    margin-bottom: 24px;
  }
  
  /* Specific Mobile Card Overrides for rewritten index.php structure */
  .mobile-card {
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    border-radius: var(--radius-xl);
    padding: 20px;
    box-shadow: var(--shadow-sm);
    display: flex;
    flex-direction: column;
    gap: 12px;
  }
  
  .m-card-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
  }
  
  .m-card-title {
    font-size: 1.15rem;
    font-weight: 700;
    color: var(--text-main);
    line-height: 1.3;
  }
  
  .m-card-party {
    font-size: 0.85rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    font-weight: 600;
    margin-top: 4px;
  }
  
  .m-card-rate {
    font-size: 1.25rem;
    font-weight: 800;
    color: var(--accent-primary);
  }
  
  .m-card-meta {
    display: flex;
    gap: 16px;
    font-size: 0.85rem;
    color: var(--text-muted);
    padding-bottom: 12px;
    border-bottom: 1px dashed var(--input-border);
  }
  
  .m-card-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    margin-top: 4px;
  }
  
  .m-action-btn {
    background: var(--bg-subtle);
    border: 1px solid var(--input-border);
    color: var(--text-main);
    padding: 12px;
    border-radius: var(--radius-md);
    font-weight: 600;
    font-size: 0.9rem;
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
  }
  
  .m-action-val {
    font-size: 1rem;
    color: var(--accent-primary);
    margin-top: 4px;
  }
}
