:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --text: #1a1f2e;
  --muted: #6b7280;
  --border: #e5e7eb;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --accent: #059669;
  --danger: #dc2626;
  --warn: #f59e0b;
  --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
  --radius: 10px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
}

/* ---------- Login ---------- */
.login-screen {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #1e3a8a, #6366f1);
  padding: 20px;
}
.login-card {
  background: white; border-radius: 14px; padding: 36px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
  width: 100%; max-width: 400px;
}
.login-card h1 { margin: 0 0 4px; font-size: 24px; }
.login-card .form-grid { margin: 24px 0 16px; grid-template-columns: 1fr; }

/* ---------- Topbar ---------- */
.topbar {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 12px 24px;
  display: flex; align-items: center; gap: 16px;
  flex-wrap: wrap;
  position: sticky; top: 0; z-index: 10;
  box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand .logo { font-size: 22px; }
.brand h1 { font-size: 18px; margin: 0; font-weight: 600; }

.tabs { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
.tab {
  background: transparent; border: none;
  padding: 8px 16px; border-radius: 8px;
  font-size: 14px; font-weight: 500;
  color: var(--muted); cursor: pointer;
  transition: all 0.15s;
}
.tab:hover { background: var(--bg); color: var(--text); }
.tab.active { background: var(--primary); color: white; }

.user-bar { display: flex; align-items: center; gap: 12px; }
.user-name { font-size: 14px; color: var(--muted); }

main { max-width: 1200px; margin: 0 auto; padding: 24px 16px; }

.panel { display: none; }
.panel.active { display: block; }

.card {
  background: var(--card); border-radius: var(--radius);
  padding: 24px; margin-bottom: 20px;
  box-shadow: var(--shadow);
}
.card-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 16px; flex-wrap: wrap; gap: 8px;
}
.card h2 { margin: 0; font-size: 18px; font-weight: 600; }
.card h3 { font-size: 15px; font-weight: 600; margin: 20px 0 10px; display: flex; align-items: center; gap: 10px; }

.muted { color: var(--muted); font-size: 13px; }

/* ---------- Metric cards ---------- */
.cards-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px; margin-bottom: 20px;
}
.metric-card {
  background: white; padding: 18px; border-radius: var(--radius);
  box-shadow: var(--shadow); border-left: 4px solid var(--primary);
}
.metric-card.alert-card { border-left-color: var(--warn); }
.metric-label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 6px; }
.metric-value { font-size: 26px; font-weight: 700; color: var(--text); }

/* ---------- Bar list ---------- */
.bar-list { display: flex; flex-direction: column; gap: 14px; }
.bar-row {
  display: grid; grid-template-columns: 180px 1fr 100px;
  gap: 14px; align-items: center;
}
.bar-row .name { font-weight: 500; font-size: 14px; }
.bar-row .amount { font-size: 13px; text-align: right; color: var(--muted); }
.bar-track {
  height: 22px; background: #f3f4f6; border-radius: 11px; overflow: hidden;
  position: relative;
}
.bar-fill {
  height: 100%; background: var(--accent); border-radius: 11px;
  transition: width 0.4s ease;
}
.bar-fill.warn { background: var(--warn); }
.bar-fill.danger { background: var(--danger); }
.bar-fill.over { background: linear-gradient(90deg, var(--danger), #7f1d1d); }
.bar-text {
  position: absolute; left: 10px; top: 50%; transform: translateY(-50%);
  font-size: 12px; font-weight: 500; color: white; mix-blend-mode: difference;
}
@media (max-width: 600px) {
  .bar-row { grid-template-columns: 1fr; gap: 4px; }
  .bar-row .amount { text-align: left; }
}

/* ---------- Two-col ---------- */
.two-col {
  display: grid; grid-template-columns: 1fr 1fr; gap: 20px;
}
@media (max-width: 800px) { .two-col { grid-template-columns: 1fr; } }

/* ---------- Buttons ---------- */
.btn {
  border: none; padding: 10px 18px; border-radius: 8px;
  font-size: 14px; font-weight: 500; cursor: pointer;
  transition: all 0.15s; display: inline-flex; align-items: center; gap: 6px;
}
.btn.primary { background: var(--primary); color: white; }
.btn.primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn.primary:disabled { background: #93c5fd; cursor: not-allowed; }
.btn.ghost { background: var(--bg); color: var(--text); border: 1px solid var(--border); }
.btn.ghost:hover { background: #ebedf0; }
.btn.danger-text { color: var(--danger); background: transparent; padding: 4px 8px; }
.btn.danger-text:hover { background: #fee2e2; }
.btn.small { padding: 6px 12px; font-size: 13px; }
.btn.big { padding: 14px 22px; font-size: 16px; align-self: stretch; justify-content: center; }

/* ---------- Forms ---------- */
.form-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 14px; margin-bottom: 16px;
}
.form-grid label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 500; color: var(--muted); }
.form-grid label.full { grid-column: 1 / -1; }
.form-grid input, .form-grid textarea, select, .filter-bar input, .filter-bar select {
  padding: 9px 11px; border: 1px solid var(--border);
  border-radius: 6px; font-size: 14px; font-family: inherit;
  color: var(--text); background: white;
}
.form-grid input:focus, .form-grid textarea:focus, select:focus {
  outline: none; border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.form-row {
  display: flex; gap: 8px; align-items: center; margin-top: 12px;
}
.form-row input[type="text"] { flex: 1; padding: 9px 11px; border: 1px solid var(--border); border-radius: 6px; font-size: 14px; }
.form-row input[type="color"] { width: 40px; height: 40px; border: 1px solid var(--border); border-radius: 6px; cursor: pointer; }

/* ---------- Upload ---------- */
.upload-card { text-align: center; border: 2px dashed var(--border); transition: border-color 0.2s, background 0.2s; }
.upload-card.dragover { border-color: var(--primary); background: #eff6ff; }
.upload-inner svg { color: var(--primary); margin-bottom: 8px; }
.upload-inner h2 { margin: 8px 0; font-size: 17px; }
.upload-buttons { margin-top: 16px; display: flex; justify-content: center; gap: 10px; flex-wrap: wrap; }
.file-preview { display: flex; flex-direction: column; gap: 14px; }
.file-meta {
  display: flex; align-items: center; gap: 14px;
  background: var(--bg); border-radius: 8px; padding: 12px 14px;
}
.file-icon { font-size: 28px; }
.file-name { font-weight: 600; word-break: break-all; }
.file-size { font-size: 12px; }
.file-meta > div { flex: 1; text-align: left; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 8px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--border); }
th { background: var(--bg); font-weight: 600; font-size: 12px; text-transform: uppercase; color: var(--muted); letter-spacing: 0.3px; }
tr:last-child td { border-bottom: none; }
table input { width: 100%; padding: 6px 8px; border: 1px solid transparent; border-radius: 4px; font-size: 14px; background: transparent; }
table input:focus { background: white; border-color: var(--primary); outline: none; }
table input.qty, table input.unit, table input.total, table input.alloc { width: 100px; text-align: right; }
.ralign { text-align: right; }
tfoot td { padding: 12px 10px; background: var(--bg); }

.save-row { display: flex; gap: 12px; margin-top: 20px; align-items: center; flex-wrap: wrap; }

.badge { padding: 4px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; background: #d1fae5; color: var(--accent); }
.badge.warn { background: #fef3c7; color: #92400e; }
.badge.danger { background: #fee2e2; color: var(--danger); }

.status { margin-top: 14px; padding: 12px 14px; border-radius: 8px; display: none; font-size: 14px; }
.status.show { display: block; }
.status.success { background: #d1fae5; color: #065f46; }
.status.error { background: #fee2e2; color: #991b1b; }
.status.info { background: #dbeafe; color: #1e40af; }

.cat-pill {
  display: inline-block; padding: 2px 8px; border-radius: 999px;
  font-size: 12px; font-weight: 500; color: white;
}

.filter-bar {
  display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px;
}
.filter-bar input[type="search"] { flex: 2; min-width: 200px; }
.results-summary { margin-bottom: 8px; }

.pager {
  display: flex; justify-content: center; align-items: center; gap: 12px;
  margin-top: 14px;
}

.invoice-list { display: flex; flex-direction: column; gap: 6px; }
.invoice-row {
  display: grid; grid-template-columns: auto 1fr auto auto;
  gap: 12px; align-items: center;
  padding: 10px 12px; background: var(--bg); border-radius: 6px;
  font-size: 14px;
}
.invoice-row .date { color: var(--muted); font-size: 13px; }
.invoice-row .supplier { font-weight: 500; }
.invoice-row .total { font-weight: 600; color: var(--accent); }

.supplier-list { display: flex; flex-direction: column; gap: 6px; }
.supplier-row {
  display: grid; grid-template-columns: 1fr auto auto;
  gap: 12px; align-items: center;
  padding: 8px 12px; background: var(--bg); border-radius: 6px;
  font-size: 14px;
}
.supplier-row .total { font-weight: 600; color: var(--accent); }

.config-status {
  background: var(--bg); padding: 12px; border-radius: 6px; font-size: 13px;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  white-space: pre-wrap;
}

.spinner {
  display: inline-block; width: 14px; height: 14px;
  border: 2px solid rgba(255,255,255,0.3); border-top-color: white;
  border-radius: 50%; animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 600px) {
  .topbar { padding: 10px 14px; }
  main { padding: 14px 10px; }
  .card { padding: 16px; }
  .save-row { flex-direction: column; align-items: stretch; }
  .save-row > * { width: 100%; }
  .upload-buttons { flex-direction: column; }
  .upload-buttons .btn { width: 100%; justify-content: center; }
  .invoice-row { grid-template-columns: 1fr 1fr; }
  .filter-bar input[type="search"] { width: 100%; }
}
