:root {
  --bg: #f5f6fa;
  --card: #fff;
  --sidebar: #183857;
  --primary: #183857;
  --primary-hover: #1e4570;
  --accent: #f1d029;
  --accent-hover: #e6c425;
  --text: #1f2937;
  --text-muted: #6b7280;
  --border: #e5e7eb;
  --danger: #dc2626;
  --success: #059669;
}

[data-theme="dark"] {
  --bg: #0f1419;
  --card: #161b22;
  --sidebar: #0d1117;
  --primary: #183857;
  --primary-hover: #1e4570;
  --accent: #f1d029;
  --accent-hover: #e6c425;
  --text: #e6edf3;
  --text-muted: #8b949e;
  --border: #30363d;
  --danger: #f85149;
  --success: #3fb950;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: 'Lato', 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  transition: background-color 0.2s ease, color 0.2s ease;
}
.card, .stat-card, .sidebar, .content { transition: background-color 0.2s ease, color 0.2s ease, border-color 0.2s ease; }
.hidden { display: none !important; }

#login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #183857 0%, #1e4570 50%, #0f1c2e 100%);
}
[data-theme="dark"] #login-page {
  background: linear-gradient(135deg, #0d1117 0%, #183857 50%, #161b22 100%);
}
.login-theme-toggle {
  position: fixed;
  top: 0.75rem;
  right: 0.75rem;
  z-index: 10;
  width: 44px;
  height: 44px;
  padding: 0;
  border: none;
  border-radius: 8px;
  background: rgba(255,255,255,0.15);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.25rem;
}
.login-theme-toggle:hover { background: rgba(255,255,255,0.25); }
.login-box {
  background: var(--card);
  padding: 2.5rem;
  border-radius: 12px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.2);
  width: 100%;
  max-width: 380px;
}
.login-box h1 { font-size: 1.75rem; margin-bottom: 0.25rem; color: var(--primary); }
.login-box .subtitle { color: var(--text-muted); font-size: 0.9rem; margin-bottom: 1.5rem; }
.login-box input {
  width: 100%;
  padding: 0.75rem 1rem;
  margin-bottom: 1rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  background: var(--card);
  color: var(--text);
}
.login-box button {
  width: 100%;
  padding: 0.75rem;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  cursor: pointer;
  font-weight: 600;
}
.login-box button:hover { background: var(--primary-hover); }
.login-box a { color: var(--accent); }
.login-box a:hover { color: var(--accent-hover); }
.error-msg { color: var(--danger); font-size: 0.875rem; margin-top: 0.5rem; min-height: 1.25rem; }

#main-app { display: flex; min-height: 100vh; position: relative; }
.theme-toggle-wrap {
  position: fixed;
  top: 0.75rem;
  right: 0.75rem;
  z-index: 99;
}
.theme-toggle {
  width: 44px;
  height: 44px;
  padding: 0;
  border: none;
  border-radius: 8px;
  background: var(--card);
  color: var(--text);
  cursor: pointer;
  box-shadow: 0 1px 3px rgba(0,0,0,0.12);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.25rem;
  transition: background 0.2s, color 0.2s;
}
.theme-toggle:hover { background: var(--border); }
[data-theme="dark"] .theme-toggle { background: var(--card); box-shadow: 0 1px 3px rgba(0,0,0,0.3); }
[data-theme="dark"] .theme-toggle:hover { background: #21262d; }
.sidebar {
  width: 220px;
  background: var(--sidebar);
  color: #fff;
  padding: 1.5rem 0;
  flex-shrink: 0;
}
.sidebar .logo { font-weight: 700; font-size: 1.25rem; padding: 0 1.5rem; margin-bottom: 1.5rem; }
.sidebar nav a {
  display: block;
  padding: 0.6rem 1.5rem;
  color: rgba(255,255,255,0.85);
  text-decoration: none;
  font-size: 0.95rem;
}
.sidebar nav a:hover { background: rgba(255,255,255,0.1); color: #fff; }
.sidebar nav a.active { background: var(--accent); color: #183857; font-weight: 600; }
.sidebar #logout { margin-top: 1rem; color: rgba(255,255,255,0.7); }
[data-theme="dark"] .sidebar nav a.active { color: #0d1117; }

.content { flex: 1; padding: 2rem; overflow: auto; }
.page-content { max-width: 1200px; margin: 0 auto; }

h2 { font-size: 1.5rem; margin-bottom: 1.25rem; color: var(--text); }
.card {
  background: var(--card);
  border-radius: 10px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
  padding: 1.25rem;
  margin-bottom: 1.25rem;
}
.stat-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 1rem; margin-bottom: 1.5rem; }
.stat-card {
  background: var(--card);
  padding: 1.25rem;
  border-radius: 10px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}
.stat-card .value { font-size: 1.5rem; font-weight: 700; color: var(--primary); }
.stat-card .label { font-size: 0.8rem; color: var(--text-muted); margin-top: 0.25rem; }
.chart-toggle-row { display: flex; align-items: center; gap: 0.75rem; margin-bottom: 1rem; flex-wrap: wrap; }
.chart-toggle-label { font-size: 0.9rem; color: var(--text-muted); }
.chart-toggle { display: inline-flex; border-radius: 8px; overflow: hidden; box-shadow: 0 1px 2px rgba(0,0,0,0.05); }
.chart-toggle-btn { margin: 0; border-radius: 0; border: 1px solid var(--border); background: var(--card); color: var(--text); }
.chart-toggle-btn:first-of-type { border-right: none; }
.chart-toggle-btn.active { background: var(--accent); color: #183857; border-color: var(--accent); font-weight: 600; }
[data-theme="dark"] .chart-toggle-btn.active { color: #0d1117; }
.dashboard-charts { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 1.25rem; margin-bottom: 1.5rem; }
.dashboard-charts.hidden { display: none; }
.chart-card { padding: 1rem; }
.chart-card h3 { margin: 0 0 0.75rem; font-size: 1rem; color: var(--text); }
.chart-card-wide { grid-column: 1 / -1; max-width: 500px; }
.chart-wrap { position: relative; height: 220px; }
.actions-row { display: flex; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.5rem; align-items: center; }
.export-actions { display: inline-flex; gap: 0.5rem; flex-wrap: wrap; }
.btn {
  padding: 0.6rem 1.2rem;
  border-radius: 8px;
  border: none;
  font-size: 0.9rem;
  cursor: pointer;
  font-weight: 500;
  text-decoration: none;
  display: inline-block;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--border); color: var(--text); }
.btn-secondary { color: var(--text); }
.btn-secondary:hover { background: #d1d5db; }
[data-theme="dark"] .btn-secondary { background: #21262d; border: 1px solid var(--border); }
[data-theme="dark"] .btn-secondary:hover { background: #30363d; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-sm { padding: 0.4rem 0.8rem; font-size: 0.85rem; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 0.75rem; text-align: left; border-bottom: 1px solid var(--border); }
th { font-weight: 600; color: var(--text-muted); font-size: 0.8rem; text-transform: uppercase; }
tr:hover { background: var(--bg); }
[data-theme="dark"] tr:hover { background: rgba(255,255,255,0.03); }
.badge { display: inline-block; padding: 0.2rem 0.5rem; border-radius: 4px; font-size: 0.75rem; font-weight: 500; }
.badge-draft { background: #e5e7eb; color: #374151; }
.badge-sent { background: #dbeafe; color: #1d4ed8; }
.badge-viewed { background: #e0e7ff; color: #3730a3; }
.badge-paid { background: #d1fae5; color: #065f46; }
.badge-overdue { background: #fee2e2; color: #991b1b; }
.badge-accepted { background: #d1fae5; color: #065f46; }
.badge-rejected { background: #fee2e2; color: #991b1b; }
.badge-expired { background: #fef3c7; color: #92400e; }
[data-theme="dark"] .badge-draft { background: #21262d; color: #8b949e; }
[data-theme="dark"] .badge-sent { background: rgba(24,56,87,0.5); color: #58a6ff; }
[data-theme="dark"] .badge-viewed { background: rgba(111,66,193,0.3); color: #a371f7; }
[data-theme="dark"] .badge-paid, [data-theme="dark"] .badge-accepted { background: rgba(63,185,80,0.2); color: #3fb950; }
[data-theme="dark"] .badge-overdue, [data-theme="dark"] .badge-rejected { background: rgba(248,81,73,0.2); color: #f85149; }
[data-theme="dark"] .badge-expired { background: rgba(241,208,41,0.2); color: #f1d029; }
.form-group { margin-bottom: 1rem; }
.form-group label { display: block; font-size: 0.875rem; font-weight: 500; margin-bottom: 0.35rem; }
.form-group input, .form-group textarea, .form-group select {
  width: 100%;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.95rem;
  background: var(--card);
  color: var(--text);
}
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.5); display: flex; align-items: center; justify-content: center; z-index: 100;
}
.modal { background: var(--card); border-radius: 12px; padding: 1.5rem; max-width: 500px; width: 90%; max-height: 90vh; overflow: auto; }
.modal h3 { margin-bottom: 1rem; }
.modal-actions { margin-top: 1.25rem; display: flex; gap: 0.75rem; justify-content: flex-end; }
.items-table th:nth-child(1), .items-table td:nth-child(1) { width: 40%; }
.items-table th:nth-child(2), .items-table td:nth-child(2) { width: 15%; }
.items-table th:nth-child(3), .items-table td:nth-child(3) { width: 20%; }
.items-table th:nth-child(4), .items-table td:nth-child(4) { width: 20%; }
.currency { font-variant-numeric: tabular-nums; }

/* ---------- Mobile: hamburger & overlay sidebar ---------- */
.sidebar-toggle {
  display: none;
  position: fixed;
  top: 0.75rem;
  left: 0.75rem;
  z-index: 99;
  width: 44px;
  height: 44px;
  padding: 0;
  background: var(--sidebar);
  color: #fff;
  border: none;
  border-radius: 8px;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 5px;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}
.sidebar-toggle-bar {
  display: block;
  width: 20px;
  height: 2px;
  background: currentColor;
  border-radius: 1px;
}
.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 98;
  opacity: 0;
  transition: opacity 0.2s ease;
}
body.sidebar-open .sidebar-backdrop {
  display: block;
  opacity: 1;
}

@media (max-width: 768px) {
  #main-app { flex-direction: row; position: relative; }
  .theme-toggle-wrap { top: 0.75rem; right: 0.75rem; }
  .sidebar-toggle { display: flex; }
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 260px;
    max-width: 85vw;
    z-index: 100;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    padding-top: 3.5rem;
    box-shadow: 4px 0 20px rgba(0,0,0,0.15);
  }
  body.sidebar-open .sidebar {
    transform: translateX(0);
  }
  body.sidebar-open { overflow: hidden; }
  .sidebar nav a { min-height: 44px; display: flex; align-items: center; padding: 0.75rem 1.5rem; }
  .content {
    padding: 1rem;
    padding-top: 3.5rem;
    padding-left: env(safe-area-inset-left, 1rem);
    padding-right: env(safe-area-inset-right, 1rem);
    padding-bottom: env(safe-area-inset-bottom, 1rem);
    min-height: 100vh;
  }
  .page-content { max-width: 100%; }
  h2 { font-size: 1.35rem; margin-bottom: 1rem; }
  .stat-cards { grid-template-columns: 1fr 1fr; gap: 0.75rem; margin-bottom: 1rem; }
  .stat-card { padding: 1rem; }
  .stat-card .value { font-size: 1.25rem; }
  .card { padding: 1rem; margin-bottom: 1rem; border-radius: 8px; }
  .card.table-card { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .actions-row { gap: 0.5rem; margin-bottom: 1rem; flex-wrap: wrap; }
  .btn {
    min-height: 44px;
    padding: 0.65rem 1rem;
    font-size: 0.95rem;
  }
  .btn-sm { min-height: 38px; padding: 0.5rem 0.75rem; }
  /* Table: horizontal scroll on small screens */
  .card > table { display: block; }
  .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -1rem; padding: 0 1rem; }
  table { min-width: 600px; }
  th, td { padding: 0.6rem 0.5rem; font-size: 0.9rem; }
  .modal { width: 95%; max-width: none; max-height: 95vh; margin: 1rem; padding: 1.25rem; border-radius: 10px; }
  .modal-overlay { align-items: flex-start; padding: 0.5rem 0; overflow-y: auto; }
  .form-group input, .form-group textarea, .form-group select { font-size: 16px; min-height: 44px; }
  .login-box { margin: 1rem; padding: 1.5rem; max-width: 100%; }
  .login-box input { font-size: 16px; min-height: 48px; padding: 0.85rem 1rem; }
  .login-box button { min-height: 48px; padding: 0.85rem; }
}

@media (max-width: 480px) {
  .stat-cards { grid-template-columns: 1fr; }
  .sidebar-toggle { top: env(safe-area-inset-top, 0.75rem); left: env(safe-area-inset-left, 0.75rem); }
  .content { padding-top: 4rem; }
  .modal { width: 100%; margin: 0.5rem; max-height: 90vh; }
  #public-view-page .card { margin: 1rem; padding: 1rem; }
}
