:root {
  --bg: #0f172a;
  --bg-soft: #1e293b;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-soft: #64748b;
  --muted: #94a3b8;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-soft: #eff6ff;
  --success: #16a34a;
  --success-soft: #dcfce7;
  --warning: #d97706;
  --warning-soft: #fef3c7;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(15,23,42,.06), 0 8px 24px rgba(15,23,42,.06);
  --shadow-lg: 0 10px 40px rgba(15,23,42,.18);
  --sidebar-w: 250px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background: var(--surface-2);
  color: var(--text);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; text-decoration: none; }
button { font-family: inherit; cursor: pointer; border: none; background: none; }
input, select, textarea { font-family: inherit; font-size: 14px; }

/* ---------- LOGIN ---------- */
.login-wrap {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 1.1fr 1fr;
}
.login-hero {
  background: linear-gradient(135deg, #1e3a8a 0%, #2563eb 55%, #3b82f6 100%);
  color: #fff;
  padding: 64px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  position: relative;
  overflow: hidden;
}
.login-hero::after {
  content: ""; position: absolute; right: -120px; bottom: -120px;
  width: 380px; height: 380px; border-radius: 50%;
  background: rgba(255,255,255,.08);
}
.login-hero h1 { font-size: 40px; font-weight: 800; line-height: 1.1; margin-bottom: 18px; }
.login-hero p { font-size: 16px; opacity: .85; max-width: 420px; line-height: 1.6; }
.login-badge {
  display: inline-flex; align-items: center; gap: 10px;
  font-weight: 700; letter-spacing: .5px; margin-bottom: 40px; font-size: 18px;
}
.login-badge .dot { width: 36px; height: 36px; border-radius: 10px; background: #fff; color: var(--primary); display:grid; place-items:center; font-weight:800; }
.login-features { margin-top: 40px; display: grid; gap: 14px; }
.login-features div { display: flex; align-items: center; gap: 12px; opacity: .92; }
.login-features .ic { width: 22px; height: 22px; }

.login-form-side { display: grid; place-items: center; padding: 32px; }
.login-card { width: 100%; max-width: 380px; }
.login-card h2 { font-size: 24px; font-weight: 700; margin-bottom: 6px; }
.login-card .sub { color: var(--text-soft); margin-bottom: 28px; }
.demo-hint {
  background: var(--primary-soft); border: 1px solid #dbeafe; color: #1e40af;
  padding: 12px 14px; border-radius: var(--radius-sm); font-size: 13px; margin-top: 18px;
}
.demo-hint b { font-weight: 700; }

@media (max-width: 880px) {
  .login-wrap { grid-template-columns: 1fr; }
  .login-hero { display: none; }
}

/* ---------- FORM ELEMENTS ---------- */
.field { margin-bottom: 16px; }
.field label { display: block; font-weight: 600; font-size: 13px; margin-bottom: 6px; color: #334155; }
.field .req { color: var(--danger); }
.input, .select, .textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: var(--radius-sm); background: #fff; color: var(--text);
  transition: border-color .15s, box-shadow .15s;
}
.input:focus, .select:focus, .textarea:focus {
  outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,.12);
}
.textarea { resize: vertical; min-height: 80px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3,1fr); gap: 0 16px; }
.grid-4 { display: grid; grid-template-columns: repeat(4,1fr); gap: 0 16px; }
.col-span-2 { grid-column: span 2; }
@media (max-width: 720px) { .grid-2,.grid-3,.grid-4 { grid-template-columns: 1fr; } .col-span-2 { grid-column: auto; } }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 10px 16px; border-radius: var(--radius-sm); font-weight: 600; font-size: 14px;
  transition: background .15s, transform .05s, box-shadow .15s; white-space: nowrap;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-block { width: 100%; }
.btn-ghost { background: transparent; color: var(--text-soft); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { background: var(--danger-soft); color: var(--danger); }
.btn-danger:hover { background: #fecaca; }
.btn-icon { padding: 8px; border-radius: var(--radius-sm); color: var(--text-soft); }
.btn-icon:hover { background: var(--surface-2); color: var(--text); }
.btn-sm { padding: 7px 12px; font-size: 13px; }

/* ---------- APP SHELL ---------- */
.shell { display: grid; grid-template-columns: var(--sidebar-w) 1fr; min-height: 100vh; }
.sidebar {
  background: var(--bg); color: #cbd5e1; padding: 22px 16px; display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh;
}
.sidebar .brand { display: flex; align-items: center; gap: 12px; padding: 0 8px 22px; }
.sidebar .brand .logo { width: 38px; height: 38px; border-radius: 10px; background: var(--primary); color:#fff; display: grid; place-items: center; font-weight: 800; font-size: 18px; }
.sidebar .brand .name { font-weight: 700; color: #fff; font-size: 15px; line-height: 1.2; }
.sidebar .brand .name small { display:block; color: var(--muted); font-weight: 500; font-size: 11px; }
.nav { display: flex; flex-direction: column; gap: 4px; margin-top: 8px; }
.nav-label { font-size: 11px; text-transform: uppercase; letter-spacing: .8px; color: #64748b; margin: 16px 8px 6px; font-weight: 600; }
.nav a {
  display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: var(--radius-sm);
  color: #cbd5e1; font-weight: 500; transition: background .15s, color .15s;
}
.nav a:hover { background: rgba(255,255,255,.06); color: #fff; }
.nav a.active { background: var(--primary); color: #fff; }
.nav a svg { width: 18px; height: 18px; flex-shrink: 0; }
.sidebar .spacer { flex: 1; }
.user-box { border-top: 1px solid rgba(255,255,255,.08); padding-top: 14px; display: flex; align-items: center; gap: 10px; }
.avatar { width: 36px; height: 36px; border-radius: 50%; background: var(--primary); color: #fff; display: grid; place-items: center; font-weight: 700; }
.user-box .info { flex: 1; min-width: 0; }
.user-box .info .n { color: #fff; font-weight: 600; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-box .info .r { color: var(--muted); font-size: 11px; text-transform: capitalize; }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar {
  background: #fff; border-bottom: 1px solid var(--border); padding: 16px 28px;
  display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; z-index: 5;
}
.topbar h1 { font-size: 20px; font-weight: 700; }
.topbar .crumb { color: var(--text-soft); font-size: 13px; }
.content { padding: 28px; flex: 1; }

/* ---------- CARDS / STATS ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px,1fr)); gap: 16px; margin-bottom: 24px; }
.stat {
  background: #fff; border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px 20px; box-shadow: var(--shadow);
}
.stat .top { display: flex; align-items: center; justify-content: space-between; }
.stat .ic { width: 42px; height: 42px; border-radius: 10px; display: grid; place-items: center; }
.stat .ic svg { width: 20px; height: 20px; }
.stat .label { color: var(--text-soft); font-size: 13px; margin-top: 14px; font-weight: 500; }
.stat .value { font-size: 26px; font-weight: 800; margin-top: 2px; }
.ic-blue { background: var(--primary-soft); color: var(--primary); }
.ic-green { background: var(--success-soft); color: var(--success); }
.ic-amber { background: var(--warning-soft); color: var(--warning); }
.ic-red { background: var(--danger-soft); color: var(--danger); }

.card { background: #fff; border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-head { padding: 18px 20px; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.card-head h2 { font-size: 16px; font-weight: 700; }
.card-head .tools { display: flex; align-items: center; gap: 10px; }
.search { position: relative; }
.search svg { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); width: 16px; height: 16px; color: var(--muted); }
.search input { padding-left: 34px; min-width: 240px; }

/* ---------- TABLE ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
thead th {
  text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .4px;
  color: var(--text-soft); font-weight: 600; padding: 12px 20px; border-bottom: 1px solid var(--border);
  background: var(--surface-2); white-space: nowrap;
}
tbody td { padding: 13px 20px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--surface-2); }
.cell-strong { font-weight: 600; }
.cell-sub { color: var(--text-soft); font-size: 12px; }
.row-actions { display: flex; gap: 4px; justify-content: flex-end; }

.badge { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.badge-green { background: var(--success-soft); color: var(--success); }
.badge-gray { background: #f1f5f9; color: var(--text-soft); }
.badge-amber { background: var(--warning-soft); color: var(--warning); }
.badge-blue { background: var(--primary-soft); color: var(--primary); }
.badge-red { background: var(--danger-soft); color: var(--danger); }

.empty { text-align: center; padding: 56px 20px; color: var(--text-soft); }
.empty svg { width: 46px; height: 46px; color: var(--muted); margin-bottom: 12px; }
.empty p { margin-bottom: 4px; font-weight: 600; color: var(--text); }

/* ---------- MODAL ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(15,23,42,.55); backdrop-filter: blur(2px);
  display: grid; place-items: center; padding: 20px; z-index: 50; animation: fade .15s ease;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.modal {
  background: #fff; border-radius: var(--radius); box-shadow: var(--shadow-lg);
  width: 100%; max-width: 720px; max-height: 92vh; display: flex; flex-direction: column;
  animation: pop .15s ease;
}
.modal.modal-lg { max-width: 920px; }
@keyframes pop { from { transform: translateY(8px) scale(.99); opacity: 0; } to { transform: none; opacity: 1; } }
.modal-head { padding: 18px 22px; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; }
.modal-head h3 { font-size: 17px; font-weight: 700; }
.modal-body { padding: 22px; overflow-y: auto; }
.modal-foot { padding: 16px 22px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 10px; background: var(--surface-2); border-radius: 0 0 var(--radius) var(--radius); }

/* tabs */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 20px; }
.tab { padding: 10px 16px; font-weight: 600; color: var(--text-soft); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tab.active { color: var(--primary); border-bottom-color: var(--primary); }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

.section-title { font-weight: 700; font-size: 13px; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); margin: 6px 0 14px; display: flex; align-items: center; gap: 8px; }
.section-title::after { content: ""; flex: 1; height: 1px; background: var(--border); }

/* purchase items editor */
.items-table input { padding: 7px 9px; }
.items-table td { padding: 6px 6px; }
.items-table thead th { padding: 8px 6px; }

/* toast */
#toast-root { position: fixed; top: 18px; right: 18px; z-index: 100; display: flex; flex-direction: column; gap: 10px; }
.toast {
  background: #fff; border: 1px solid var(--border); border-left: 4px solid var(--primary);
  border-radius: var(--radius-sm); padding: 12px 16px; box-shadow: var(--shadow-lg); min-width: 260px;
  display: flex; align-items: center; gap: 10px; animation: slidein .2s ease; font-weight: 500;
}
.toast.ok { border-left-color: var(--success); }
.toast.err { border-left-color: var(--danger); }
@keyframes slidein { from { transform: translateX(20px); opacity: 0; } to { transform: none; opacity: 1; } }

.spinner { width: 18px; height: 18px; border: 2px solid rgba(255,255,255,.4); border-top-color: #fff; border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.page-loader { display: grid; place-items: center; height: 60vh; }
.page-loader .spinner { width: 34px; height: 34px; border-color: rgba(37,99,235,.2); border-top-color: var(--primary); }

.muted-link { color: var(--primary); font-weight: 600; cursor: pointer; }
.hist-meta { display:flex; gap:18px; flex-wrap:wrap; color:var(--text-soft); font-size:13px; margin-bottom:18px; }
.hist-meta b { color: var(--text); }
