/* ===== CSS Variables & Reset ===== */
:root {
  --bg: #f0f2f5;
  --surface: #ffffff;
  --surface2: #f8f9fa;
  --text: #1a1a2e;
  --text2: #555;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --green: #16a34a;
  --green-bg: #dcfce7;
  --red: #dc2626;
  --red-bg: #fee2e2;
  --blue: #2563eb;
  --blue-bg: #dbeafe;
  --orange: #ea580c;
  --border: #d1d5db;
  --shadow: 0 2px 8px rgba(0,0,0,0.08);
  --radius: 10px;
  --radius-sm: 6px;
  --header-bg: var(--primary);
  --header-text: #fff;
  --nav-bg: #fff;
  --nav-link: #555;
  --nav-active: var(--primary);
  --footer-bg: #e5e7eb;
  --card-shadow: 0 4px 12px rgba(0,0,0,0.06);
  --transition: 0.25s ease;
}

[data-theme="dark"] {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface2: #334155;
  --text: #e2e8f0;
  --text2: #94a3b8;
  --header-bg: #0f172a;
  --nav-bg: #1e293b;
  --nav-link: #94a3b8;
  --nav-active: #60a5fa;
  --border: #475569;
  --shadow: 0 2px 8px rgba(0,0,0,0.3);
  --card-shadow: 0 4px 12px rgba(0,0,0,0.2);
  --footer-bg: #1e293b;
  --green-bg: #14532d;
  --red-bg: #7f1d1d;
  --blue-bg: #1e3a5f;
}

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

html { font-size: 16px; scroll-behavior: smooth; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
}

/* ===== Header ===== */
#main-header {
  background: var(--header-bg);
  color: var(--header-text);
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow);
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  max-width: 1200px;
  margin: 0 auto;
}

.header-inner h1 {
  font-size: 1.2rem;
  font-weight: 700;
}

.header-actions { display: flex; gap: 8px; }

.btn-icon {
  background: rgba(255,255,255,0.15);
  border: none;
  color: #fff;
  font-size: 1.3rem;
  width: 40px;
  height: 40px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition);
}

.btn-icon:hover { background: rgba(255,255,255,0.25); }

/* ===== Navigation ===== */
#main-nav {
  background: var(--nav-bg);
  box-shadow: var(--shadow);
  position: sticky;
  top: 56px;
  z-index: 99;
  transition: max-height var(--transition);
  overflow: hidden;
}

.nav-closed { max-height: 0; }
.nav-open { max-height: 400px; }

#main-nav ul {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: 0;
  padding: 8px 12px;
  max-width: 1200px;
  margin: 0 auto;
}

#main-nav ul li { flex: 0 0 auto; }

.nav-link {
  display: block;
  padding: 8px 14px;
  color: var(--nav-link);
  text-decoration: none;
  font-size: 0.85rem;
  font-weight: 500;
  border-radius: var(--radius-sm);
  transition: all var(--transition);
  white-space: nowrap;
}

.nav-link:hover { background: var(--surface2); }
.nav-link.active {
  background: var(--nav-active);
  color: #fff;
}

/* ===== Main Content ===== */
#main-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 16px;
}

.section { display: none; }
.section.active-section { display: block; }

.section h2 {
  font-size: 1.4rem;
  margin-bottom: 16px;
  color: var(--text);
  border-bottom: 3px solid var(--primary);
  padding-bottom: 8px;
  display: inline-block;
}

/* ===== Cards / Stats ===== */
.stats-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 16px;
}

.stat-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--card-shadow);
  transition: transform var(--transition), box-shadow var(--transition);
}

.stat-card:hover { transform: translateY(-2px); box-shadow: 0 6px 16px rgba(0,0,0,0.1); }

.stat-label {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text2);
  margin-bottom: 4px;
}

.stat-value {
  font-size: 1.6rem;
  font-weight: 800;
}

.card-stock { border-left: 4px solid var(--blue); }
.card-received { border-left: 4px solid var(--green); }
.card-billed { border-left: 4px solid var(--red); }
.card-info { border-left: 4px solid var(--orange); }

.dash-meta {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 16px;
}

/* ===== Dashboard Filter ===== */
.dash-filter-bar {
  background: var(--surface2);
  border-radius: var(--radius);
  padding: 12px 16px;
  margin-bottom: 16px;
  box-shadow: var(--card-shadow);
}

.dash-filter-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}

.dash-filter-row label {
  font-weight: 600;
  font-size: 0.85rem;
  color: var(--text2);
  white-space: nowrap;
}

.dash-filter-select {
  width: auto;
  min-width: 160px;
  padding: 8px 12px;
  font-size: 0.9rem;
}

.dash-filter-row .btn {
  padding: 8px 18px;
  font-size: 0.85rem;
}

.dash-custom-range {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: flex-end;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}

.dash-custom-range .form-group {
  margin-bottom: 0;
  flex: 1;
  min-width: 140px;
}

.dash-custom-range .form-group label {
  font-size: 0.78rem;
}

.dash-date-range-label {
  margin-top: 8px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text2);
  padding: 4px 8px;
  background: var(--surface);
  border-radius: var(--radius-sm);
  display: inline-block;
}

/* ===== Chart ===== */
.chart-container {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--card-shadow);
  margin-bottom: 20px;
}

.chart-container canvas { max-height: 300px; }

/* ===== Forms ===== */
.form-group { margin-bottom: 14px; }

.form-group label {
  display: block;
  font-weight: 600;
  font-size: 0.85rem;
  margin-bottom: 4px;
  color: var(--text2);
}

.form-input {
  width: 100%;
  padding: 12px 14px;
  font-size: 1rem;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  transition: border-color var(--transition);
  outline: none;
}

.form-input:focus { border-color: var(--primary); }

select.form-input { cursor: pointer; }

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.form-actions {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  margin-top: 8px;
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 12px 22px;
  font-size: 0.95rem;
  font-weight: 600;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition);
  text-decoration: none;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-primary:hover { background: var(--primary-hover); transform: translateY(-1px); }

.btn-secondary {
  background: var(--surface2);
  color: var(--text);
  border: 2px solid var(--border);
}

.btn-secondary:hover { background: var(--border); }

.btn-filter {
  padding: 8px 14px;
  font-size: 0.8rem;
  background: var(--surface2);
  color: var(--text);
  border: 2px solid var(--border);
  border-radius: 20px;
}

.btn-filter.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

.filter-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
}

.custom-date-range {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: flex-end;
  padding: 12px;
  background: var(--surface2);
  border-radius: var(--radius-sm);
  margin-bottom: 12px;
}

.custom-date-range .form-group { margin-bottom: 0; flex: 1; min-width: 140px; }

.hidden { display: none !important; }

.feedback {
  margin-top: 12px;
  padding: 10px;
  border-radius: var(--radius-sm);
  font-weight: 600;
}

.feedback.success { background: var(--green-bg); color: var(--green); }
.feedback.error { background: var(--red-bg); color: var(--red); }

/* ===== Table ===== */
.table-wrapper {
  overflow-x: auto;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  font-size: 0.85rem;
}

thead th {
  background: var(--surface2);
  padding: 12px 10px;
  text-align: left;
  font-weight: 700;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  color: var(--text2);
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
}

tbody td {
  padding: 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

tbody tr:hover { background: var(--surface2); }

tfoot td {
  padding: 10px;
  font-weight: 700;
  background: var(--surface2);
  border-top: 2px solid var(--border);
}

.action-btn {
  padding: 6px 12px;
  font-size: 0.78rem;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-weight: 600;
  transition: all var(--transition);
}

.btn-edit { background: var(--blue-bg); color: var(--blue); }
.btn-edit:hover { background: var(--blue); color: #fff; }
.btn-delete { background: var(--red-bg); color: var(--red); }
.btn-delete:hover { background: var(--red); color: #fff; }
.btn-restore { background: var(--green-bg); color: var(--green); }
.btn-restore:hover { background: var(--green); color: #fff; }
.btn-permanent-delete { background: var(--red-bg); color: var(--red); }
.btn-permanent-delete:hover { background: #991b1b; color: #fff; }

.actions-cell { display: flex; gap: 6px; }

/* ===== Modal ===== */
.modal {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 16px;
}

.modal.hidden { display: none; }

.modal-content {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px;
  max-width: 500px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 10px 30px rgba(0,0,0,0.2);
  position: relative;
}

.modal-close {
  position: absolute;
  top: 12px;
  right: 16px;
  font-size: 1.6rem;
  cursor: pointer;
  color: var(--text2);
  line-height: 1;
}

.modal-close:hover { color: var(--text); }

/* ===== Reports ===== */
.report-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 16px;
}

.report-output {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--card-shadow);
  overflow-x: auto;
}

/* ===== Export Filter ===== */
.export-filter-bar {
  background: var(--surface2);
  border-radius: var(--radius);
  padding: 12px 16px;
  margin-bottom: 12px;
  box-shadow: var(--card-shadow);
}

.export-filter-row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 10px;
}

.export-filter-label {
  font-weight: 600;
  font-size: 0.85rem;
  color: var(--text2);
  white-space: nowrap;
  padding-bottom: 4px;
}

.export-filter-row .form-group {
  margin-bottom: 0;
  flex: 1;
  min-width: 130px;
}

.export-filter-row .form-group label {
  font-size: 0.78rem;
}

.export-filter-row .btn {
  padding: 8px 16px;
  font-size: 0.85rem;
}

.export-filter-status {
  margin-top: 8px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text2);
  padding: 4px 8px;
  background: var(--surface);
  border-radius: var(--radius-sm);
  display: inline-block;
}

/* ===== Export / Import ===== */
.export-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 20px;
}

.import-section {
  background: var(--surface2);
  padding: 20px;
  border-radius: var(--radius);
}

.import-section h3 { margin-bottom: 12px; }
.import-section .btn { margin-top: 12px; }

/* ===== Auto Backup ===== */
.auto-backup-section {
  background: var(--surface2);
  padding: 20px;
  border-radius: var(--radius);
  margin-top: 20px;
}

.auto-backup-section h3 { margin-bottom: 12px; }

.auto-backup-row {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: flex-end;
}

.auto-backup-row .form-group {
  margin-bottom: 0;
  flex: 1;
  min-width: 160px;
}

.backup-last-status {
  margin-top: 10px;
  font-size: 0.82rem;
  color: var(--text2);
  padding: 6px 10px;
  background: var(--surface);
  border-radius: var(--radius-sm);
}

.backup-storage-info {
  margin-top: 8px;
  font-size: 0.8rem;
  color: var(--text2);
  padding: 6px 10px;
  background: var(--surface);
  border-radius: var(--radius-sm);
}

.auto-backup-list-wrapper {
  margin-top: 14px;
}

.auto-backup-list-wrapper h4 {
  font-size: 0.9rem;
  color: var(--text);
  margin-bottom: 8px;
}

.auto-backup-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 240px;
  overflow-y: auto;
}

.backup-item {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 12px;
  background: var(--surface);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  font-size: 0.82rem;
}

.backup-item-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1;
  min-width: 140px;
}

.backup-item-date { font-weight: 600; color: var(--text); }
.backup-item-meta { font-size: 0.75rem; color: var(--text2); }

.backup-item-actions {
  display: flex;
  gap: 6px;
}

.btn-backup-restore {
  padding: 4px 10px;
  font-size: 0.75rem;
  background: var(--blue-bg);
  color: var(--blue);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-weight: 600;
}

.btn-backup-restore:hover { background: var(--blue); color: #fff; }

.btn-backup-delete {
  padding: 4px 10px;
  font-size: 0.75rem;
  background: var(--red-bg);
  color: var(--red);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-weight: 600;
}

.btn-backup-delete:hover { background: var(--red); color: #fff; }

.btn-backup-download {
  padding: 4px 10px;
  font-size: 0.75rem;
  background: var(--green-bg);
  color: var(--green);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-weight: 600;
  line-height: 1;
}

.btn-backup-download:hover { background: var(--green); color: #fff; }

/* ===== Footer ===== */
#main-footer {
  text-align: center;
  padding: 16px;
  background: var(--footer-bg);
  color: var(--text2);
  font-size: 0.85rem;
  margin-top: 24px;
}

/* ===== Responsive ===== */
@media (min-width: 640px) {
  .stats-row { grid-template-columns: repeat(3, 1fr); }
  .dash-meta { grid-template-columns: 1fr 1fr; }
  .header-inner h1 { font-size: 1.5rem; }
  #main-nav { position: static; }
  .nav-closed { max-height: none; }
  #mobile-menu-toggle { display: none; }
  #main-nav ul { flex-wrap: nowrap; justify-content: center; }
  .nav-link { font-size: 0.9rem; }
}

@media (min-width: 900px) {
  .stats-row { grid-template-columns: repeat(3, 1fr); }
  .dash-meta { grid-template-columns: 1fr 1fr; }
  .section { padding: 0 8px; }
}

/* ===== Print ===== */
@media print {
  #main-header, #main-nav, #main-footer,
  .filter-bar, .form-group, .report-buttons,
  .export-buttons, .import-section, .chart-container,
  .dash-filter-bar, .btn, .action-btn, #mobile-menu-toggle, #toggle-dark { display: none !important; }
  .section { display: block !important; }
  .stat-card { break-inside: avoid; }
  table { font-size: 0.75rem; }
}

/* ===== Login Screen ===== */
#login-screen {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
  padding: 16px;
}

.login-container {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: 0 8px 32px rgba(0,0,0,0.12);
  padding: 40px 32px 32px;
  width: 100%;
  max-width: 400px;
  animation: fadeIn 0.4s ease;
}

.login-logo {
  text-align: center;
  margin-bottom: 32px;
}

.login-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 56px;
  height: 56px;
  background: var(--primary);
  color: #fff;
  font-size: 1.8rem;
  border-radius: 14px;
  margin-bottom: 12px;
}

.login-logo h1 {
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--text);
}

.login-btn {
  width: 100%;
  padding: 14px;
  font-size: 1rem;
  margin-top: 8px;
}

.login-error {
  margin-top: 14px;
  padding: 10px 14px;
  background: var(--red-bg);
  color: var(--red);
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  font-weight: 600;
  text-align: center;
}

.remember-label {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 12px 0;
  font-size: 0.88rem;
  color: var(--text2);
  cursor: pointer;
  user-select: none;
}

.remember-checkbox {
  width: 16px;
  height: 16px;
  accent-color: var(--primary);
  cursor: pointer;
}

/* ===== Animations ===== */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.section.active-section { animation: fadeIn 0.3s ease; }
.stat-card { animation: fadeIn 0.3s ease; }
