:root {
  --brand: #c1272d;
  --brand-dark: #8f1b20;
  --ink: #1c1f26;
  --ink-soft: #565c66;
  --line: #e4e6ea;
  --bg: #f4f5f7;
  --card: #ffffff;
  --good: #1f8a4c;
  --warn: #b8860b;
  --bad: #c1272d;
  --radius: 12px;
  font-family: "Aptos", "Calibri", "Segoe UI", system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }
/* The HTML `hidden` attribute must always win over any component's own
   `display` rule (e.g. .modal-host / .app-shell use display:flex), otherwise
   a hidden modal/panel still renders as a blank overlay on top of the page. */
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: inherit;
  -webkit-font-smoothing: antialiased;
}

.muted { color: var(--ink-soft); }
code { background: #eee; padding: 1px 5px; border-radius: 4px; font-size: 0.85em; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%) translateY(-20px);
  background: var(--ink);
  color: #fff;
  padding: 10px 18px;
  border-radius: 10px;
  font-size: 14px;
  z-index: 999;
  opacity: 0;
  pointer-events: none;
  transition: all 0.25s ease;
  max-width: min(90vw, 480px);
  text-align: center;
  box-shadow: 0 8px 24px rgba(0,0,0,0.2);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { background: var(--bad); }
.toast.success { background: var(--good); }

/* ---------- Buttons ---------- */
.btn {
  font: inherit;
  font-weight: 600;
  font-size: 14px;
  padding: 10px 16px;
  border-radius: 9px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: transform 0.05s ease, background 0.15s ease, opacity 0.15s ease;
  white-space: nowrap;
}
.btn:active { transform: scale(0.98); }
.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-dark); }
.btn-ghost { background: transparent; color: var(--ink); border-color: var(--line); }
.btn-ghost:hover { background: #eee; }
.btn-danger { background: #fff; color: var(--bad); border-color: #f0c4c6; }
.btn-danger:hover { background: #fdecec; }
.btn-sm { padding: 6px 11px; font-size: 12.5px; }
.btn-block { width: 100%; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---------- Login ---------- */
.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: radial-gradient(circle at top, #fff 0%, var(--bg) 60%);
}
.login-card {
  width: 100%;
  max-width: 420px;
  background: var(--card);
  border-radius: 18px;
  padding: 32px;
  box-shadow: 0 20px 60px rgba(20, 20, 30, 0.12);
  border: 1px solid var(--line);
}
.login-brand { display: flex; align-items: center; gap: 14px; margin-bottom: 22px; }
.login-brand h1 { font-size: 19px; margin: 0 0 2px; }
.login-brand p { margin: 0; font-size: 13px; }
.brand-mark {
  width: 46px; height: 46px; border-radius: 12px;
  background: linear-gradient(145deg, var(--brand), var(--brand-dark));
  color: #fff; font-weight: 800; font-size: 22px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.brand-mark.small { width: 34px; height: 34px; font-size: 16px; border-radius: 9px; }

.role-tabs {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-bottom: 20px;
}
.role-tab {
  font: inherit; font-size: 13px; font-weight: 600;
  padding: 9px 6px; border-radius: 8px;
  border: 1px solid var(--line); background: #fafafa; color: var(--ink-soft);
  cursor: pointer;
}
.role-tab.active { background: var(--brand); color: #fff; border-color: var(--brand); }

.field { margin-bottom: 14px; }
.field label { display: block; font-size: 12.5px; font-weight: 600; margin-bottom: 5px; color: var(--ink-soft); }
.field input, .field select {
  width: 100%; font: inherit; font-size: 14.5px;
  padding: 10px 12px; border-radius: 9px; border: 1px solid var(--line);
  background: #fff;
}
.field input:focus, .field select:focus { outline: 2px solid var(--brand); outline-offset: 1px; }

.form-error {
  background: #fdecec; color: var(--bad); border: 1px solid #f2c4c6;
  padding: 9px 12px; border-radius: 8px; font-size: 13px; margin-bottom: 14px;
}
.hint { font-size: 11.5px; color: var(--ink-soft); margin: 12px 0 0; text-align: center; }

/* ---------- App shell ---------- */
.app-shell { min-height: 100vh; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  background: #fff; border-bottom: 1px solid var(--line);
  padding: 10px 20px; gap: 12px; flex-wrap: wrap;
  position: sticky; top: 0; z-index: 50;
}
.topbar-left { display: flex; align-items: center; gap: 12px; min-width: 0; }
.topbar-title { font-weight: 800; font-size: 15px; }
.topbar-sub { font-size: 12px; color: var(--ink-soft); }
.topbar-right { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.period-select {
  font: inherit; font-size: 13px; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--line);
}

.main-area { flex: 1; padding: 20px; max-width: 1280px; width: 100%; margin: 0 auto; }

/* ---------- Cards / layout ---------- */
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px; margin-bottom: 16px;
}
.card h2, .card h3 { margin-top: 0; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
@media (max-width: 760px) {
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
}

.stat { text-align: left; }
.stat .label { font-size: 12px; color: var(--ink-soft); text-transform: uppercase; letter-spacing: 0.03em; }
.stat .value { font-size: 26px; font-weight: 800; margin-top: 2px; }

.bar-track { height: 10px; border-radius: 6px; background: #eee; overflow: hidden; margin-top: 10px; }
.bar-fill { height: 100%; border-radius: 6px; background: var(--good); transition: width 0.3s ease; }
.bar-fill.warn { background: var(--warn); }
.bar-fill.bad { background: var(--bad); }

.badge { display: inline-block; padding: 3px 9px; border-radius: 20px; font-size: 11.5px; font-weight: 700; }
.badge.good { background: #e5f6ec; color: var(--good); }
.badge.warn { background: #fdf3dd; color: var(--warn); }
.badge.bad { background: #fdeaea; color: var(--bad); }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { color: var(--ink-soft); font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.03em; font-weight: 700; }
tr:hover td { background: #fafafa; }
td.wrap { white-space: normal; }
.row-actions { display: flex; gap: 6px; }

.search-row { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.search-row input, .search-row select { font: inherit; font-size: 13.5px; padding: 8px 11px; border-radius: 8px; border: 1px solid var(--line); }
.search-row input { flex: 1; min-width: 160px; }

/* ---------- Store list (AM/OM/Admin) ---------- */
.store-list { display: flex; flex-direction: column; gap: 8px; max-height: 560px; overflow-y: auto; }
.store-item {
  display: flex; justify-content: space-between; align-items: center; gap: 10px;
  padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; cursor: pointer; background: #fff;
}
.store-item:hover { border-color: var(--brand); }
.store-item.active { border-color: var(--brand); background: #fdf1f1; }
.store-item .code { font-weight: 700; font-size: 13.5px; }
.store-item .meta { font-size: 11.5px; color: var(--ink-soft); }
.store-item .mini-bar { width: 60px; height: 6px; border-radius: 4px; background: #eee; overflow: hidden; }
.store-item .mini-bar > div { height: 100%; background: var(--good); }
.store-item .mini-bar > div.warn { background: var(--warn); }
.store-item .mini-bar > div.bad { background: var(--bad); }

.layout-with-side { display: grid; grid-template-columns: 300px 1fr; gap: 16px; align-items: start; }
@media (max-width: 900px) {
  .layout-with-side { grid-template-columns: 1fr; }
}

.empty-state { text-align: center; padding: 40px 16px; color: var(--ink-soft); }
.tabs { display: flex; gap: 6px; margin-bottom: 14px; flex-wrap: wrap; }
.tab-btn {
  font: inherit; font-size: 13px; font-weight: 600; padding: 8px 14px;
  border-radius: 8px; border: 1px solid var(--line); background: #fff; cursor: pointer; color: var(--ink-soft);
}
.tab-btn.active { background: var(--ink); color: #fff; border-color: var(--ink); }

/* ---------- Modal ---------- */
.modal-host { position: fixed; inset: 0; z-index: 200; display: flex; align-items: center; justify-content: center; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(20, 20, 24, 0.45); }
.modal {
  position: relative; background: #fff; border-radius: 16px; padding: 22px;
  width: min(92vw, 480px); max-height: 88vh; overflow-y: auto;
  box-shadow: 0 24px 70px rgba(0,0,0,0.3);
}
.modal h3 { margin-top: 0; }
.modal-close { position: absolute; top: 14px; right: 14px; background: none; border: none; font-size: 18px; cursor: pointer; color: var(--ink-soft); }
.modal-actions { display: flex; gap: 10px; margin-top: 18px; }
.modal-actions .btn { flex: 1; }
.preview-box { background: #f8f8f9; border: 1px dashed var(--line); border-radius: 10px; padding: 10px 12px; font-size: 13px; margin: 10px 0; }
.preview-box .row { display: flex; justify-content: space-between; padding: 2px 0; }

@media (max-width: 520px) {
  .topbar { flex-direction: column; align-items: stretch; }
  .topbar-right { justify-content: space-between; }
  .main-area { padding: 14px; }
}
