/* ==========================================================================
   FM Billing — design system (shadcn-like, vanilla CSS)
   ========================================================================== */

:root {
  /* Neutral (zinc) palette as HSL channels — shadcn convention */
  --background: 0 0% 100%;
  --foreground: 240 10% 3.9%;
  --card: 0 0% 100%;
  --card-foreground: 240 10% 3.9%;
  --popover: 0 0% 100%;
  --popover-foreground: 240 10% 3.9%;
  --primary: 240 5.9% 10%;
  --primary-foreground: 0 0% 98%;
  --secondary: 240 4.8% 95.9%;
  --secondary-foreground: 240 5.9% 10%;
  --muted: 240 4.8% 95.9%;
  --muted-foreground: 240 3.8% 46.1%;
  --accent: 240 4.8% 95.9%;
  --accent-foreground: 240 5.9% 10%;
  --destructive: 0 72% 51%;
  --destructive-foreground: 0 0% 98%;
  --success: 142 71% 45%;
  --success-foreground: 0 0% 98%;
  --warning: 38 92% 50%;
  --border: 240 5.9% 90%;
  --input: 240 5.9% 90%;
  --ring: 240 5.9% 10%;
  --radius: 0.4rem;

  --sidebar-width: 212px;
  --header-height: 46px;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 13px;
  line-height: 1.45;
  color: hsl(var(--foreground));
  background: hsl(var(--muted));
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4, h5, h6 { margin: 0; font-weight: 600; line-height: 1.25; }
p { margin: 0 0 0.75rem; }
a { color: inherit; text-decoration: none; }

/* ---------- Layout: app shell ---------------------------------------------- */
.app { display: flex; min-height: 100vh; }

.sidebar {
  position: fixed; inset: 0 auto 0 0;
  width: var(--sidebar-width);
  background: hsl(var(--background));
  border-right: 1px solid hsl(var(--border));
  display: flex; flex-direction: column;
  z-index: 40;
}
.sidebar__brand {
  height: var(--header-height);
  display: flex; align-items: center; gap: 0.55rem;
  padding: 0 0.85rem;
  border-bottom: 1px solid hsl(var(--border));
  font-weight: 700; font-size: 0.9rem;
}
.sidebar__brand .logo {
  width: 25px; height: 25px; border-radius: 7px;
  background: hsl(var(--primary)); color: hsl(var(--primary-foreground));
  display: grid; place-items: center; font-weight: 800; font-size: 0.72rem;
}
.sidebar__nav { padding: 0.5rem; overflow-y: auto; flex: 1; }
.nav-group__label {
  font-size: 0.66rem; text-transform: uppercase; letter-spacing: 0.06em;
  color: hsl(var(--muted-foreground)); padding: 0.55rem 0.55rem 0.18rem;
}
.nav-link {
  display: flex; align-items: center; gap: 0.55rem;
  padding: 0.36rem 0.55rem; border-radius: calc(var(--radius) - 2px);
  color: hsl(var(--muted-foreground)); font-weight: 500; font-size: 0.82rem;
  margin-bottom: 1px; cursor: pointer;
}
.nav-link:hover { background: hsl(var(--accent)); color: hsl(var(--accent-foreground)); }
.nav-link.active { background: hsl(var(--primary)); color: hsl(var(--primary-foreground)); }
.nav-link svg { width: 16px; height: 16px; flex: none; }
.nav-link i { font-size: 1.02rem; width: 18px; text-align: center; flex: none; line-height: 1; }

.main { flex: 1; margin-left: var(--sidebar-width); display: flex; flex-direction: column; min-width: 0; }

.topbar {
  height: var(--header-height); position: sticky; top: 0; z-index: 30;
  background: hsl(var(--background) / 0.9); backdrop-filter: blur(8px);
  border-bottom: 1px solid hsl(var(--border));
  display: flex; align-items: center; gap: 0.6rem; padding: 0 1rem;
}
.topbar__title { font-weight: 600; font-size: 0.88rem; }
.topbar__spacer { flex: 1; }
.sidebar-toggle { display: none; background: none; border: 0; cursor: pointer; padding: 6px; border-radius: 6px; }
.sidebar-toggle:hover { background: hsl(var(--accent)); }

.content { padding: 1rem 1.25rem; max-width: 1360px; width: 100%; }

.page-header { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: 0.9rem; flex-wrap: wrap; }
.page-header h1 { font-size: 1.2rem; }
.page-header .muted { color: hsl(var(--muted-foreground)); font-size: 0.8rem; margin-top: 1px; }

/* ---------- Buttons ------------------------------------------------------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 0.4rem;
  height: 32px; padding: 0 0.7rem; border-radius: var(--radius);
  font-size: 0.82rem; font-weight: 500; line-height: 1; cursor: pointer;
  border: 1px solid transparent; background: hsl(var(--primary)); color: hsl(var(--primary-foreground));
  transition: background-color .15s, opacity .15s, border-color .15s; white-space: nowrap;
}
.btn:hover { opacity: 0.9; }
.btn:focus-visible { outline: 2px solid hsl(var(--ring)); outline-offset: 2px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn svg { width: 16px; height: 16px; }
.btn i { font-size: 1.05rem; line-height: 1; }
.btn--outline { background: hsl(var(--background)); color: hsl(var(--foreground)); border-color: hsl(var(--border)); }
.btn--outline:hover { background: hsl(var(--accent)); opacity: 1; }
.btn--ghost { background: transparent; color: hsl(var(--foreground)); }
.btn--ghost:hover { background: hsl(var(--accent)); opacity: 1; }
.btn--secondary { background: hsl(var(--secondary)); color: hsl(var(--secondary-foreground)); }
.btn--secondary:hover { opacity: 1; background: hsl(var(--border)); }
.btn--destructive { background: hsl(var(--destructive)); color: hsl(var(--destructive-foreground)); }
.btn--sm { height: 27px; padding: 0 0.5rem; font-size: 0.76rem; }
.btn--icon { width: 32px; padding: 0; }
.btn--icon.btn--sm { width: 27px; }
.btn--block { width: 100%; }

/* ---------- Forms --------------------------------------------------------- */
.field { display: flex; flex-direction: column; gap: 0.3rem; margin-bottom: 0.7rem; }
.label { font-size: 0.8rem; font-weight: 500; }
.label .req { color: hsl(var(--destructive)); }
.input, .select, .textarea {
  width: 100%; height: 32px; padding: 0 0.6rem; font-size: 0.82rem; font-family: inherit;
  color: hsl(var(--foreground)); background: hsl(var(--background));
  border: 1px solid hsl(var(--input)); border-radius: var(--radius);
  transition: border-color .15s, box-shadow .15s;
}
.textarea { height: auto; padding: 0.45rem 0.6rem; resize: vertical; min-height: 60px; }
.input:focus, .select:focus, .textarea:focus {
  outline: none; border-color: hsl(var(--ring)); box-shadow: 0 0 0 3px hsl(var(--ring) / 0.12);
}
.input::placeholder, .textarea::placeholder { color: hsl(var(--muted-foreground)); }
.input:disabled, .select:disabled { background: hsl(var(--muted)); cursor: not-allowed; }
.input[readonly] { background: hsl(var(--muted)); cursor: default; }
.input[readonly]:focus { border-color: hsl(var(--input)); box-shadow: none; }
.input--invalid { border-color: hsl(var(--destructive)); }
.field__hint { font-size: 0.78rem; color: hsl(var(--muted-foreground)); }
.field__error { font-size: 0.78rem; color: hsl(var(--destructive)); }
.select { appearance: none; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23888' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 0.6rem center; padding-right: 2rem; }
.input--right { text-align: right; }

.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0 1rem; }
.form-grid--3 { grid-template-columns: repeat(3, 1fr); }
.col-span-2 { grid-column: span 2; }
.col-span-full { grid-column: 1 / -1; }

/* ---------- Card ---------------------------------------------------------- */
.card {
  background: hsl(var(--card)); color: hsl(var(--card-foreground));
  border: 1px solid hsl(var(--border)); border-radius: var(--radius);
  box-shadow: 0 1px 2px 0 hsl(240 5% 10% / 0.04);
}
.card__header { padding: 0.7rem 0.9rem; border-bottom: 1px solid hsl(var(--border)); }
.card__title { font-size: 0.92rem; font-weight: 600; }
.card__desc { font-size: 0.78rem; color: hsl(var(--muted-foreground)); margin-top: 1px; }
.card__body { padding: 0.9rem; }
.card__footer { padding: 0.65rem 0.9rem; border-top: 1px solid hsl(var(--border)); display: flex; gap: 0.5rem; justify-content: flex-end; }
a.card { transition: border-color .15s, box-shadow .15s, transform .15s; }
a.card:hover { border-color: hsl(var(--ring) / 0.35); box-shadow: 0 6px 20px -10px hsl(240 10% 10% / 0.18); }

/* ---------- Stat (KPI) ---------------------------------------------------- */
.stat { display: flex; align-items: center; gap: 0.85rem; }
.stat__icon {
  width: 36px; height: 36px; border-radius: 9px; flex: none;
  display: grid; place-items: center; font-size: 1.1rem;
  background: hsl(var(--accent)); color: hsl(var(--foreground));
}
.stat__icon--primary { background: hsl(var(--primary)); color: hsl(var(--primary-foreground)); }
.stat__icon--success { background: hsl(var(--success) / 0.14); color: hsl(142 71% 30%); }
.stat__icon--info { background: hsl(217 91% 60% / 0.14); color: hsl(217 91% 45%); }
.stat__icon--warning { background: hsl(var(--warning) / 0.16); color: hsl(32 81% 36%); }
.stat__label { font-size: 0.74rem; color: hsl(var(--muted-foreground)); margin-bottom: 1px; }
.stat__value { font-size: 1.2rem; font-weight: 700; line-height: 1.15; font-variant-numeric: tabular-nums; }
.tile-icon { width: 34px; height: 34px; border-radius: 9px; flex: none; display: grid; place-items: center; font-size: 1.1rem; background: hsl(var(--accent)); color: hsl(var(--foreground)); }
.li-remove { color: hsl(var(--muted-foreground)); }
.li-remove:hover { color: hsl(var(--destructive)); background: hsl(var(--destructive) / 0.08); }

/* ---------- Table --------------------------------------------------------- */
.table-wrap { overflow-x: auto; border: 1px solid hsl(var(--border)); border-radius: var(--radius); background: hsl(var(--card)); }
table.table { width: 100%; border-collapse: collapse; font-size: 0.8rem; }
.table thead th {
  text-align: left; font-weight: 500; color: hsl(var(--muted-foreground));
  padding: 0.4rem 0.65rem; border-bottom: 1px solid hsl(var(--border)); white-space: nowrap;
  background: hsl(var(--muted) / 0.4); font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.02em;
}
.table tbody td { padding: 0.4rem 0.65rem; border-bottom: 1px solid hsl(var(--border)); vertical-align: middle; }
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: hsl(var(--muted) / 0.35); }
.table tfoot td {
  padding: 0.5rem 0.65rem; border-top: 2px solid hsl(var(--border));
  background: hsl(var(--muted)); font-weight: 700; font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.table tr.row-strong td { background: hsl(var(--muted)); font-weight: 700; border-top: 2px solid hsl(var(--border)); }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.table__actions { display: flex; gap: 0.35rem; justify-content: flex-end; }
.table-empty { padding: 2rem 1rem; text-align: center; color: hsl(var(--muted-foreground)); }

/* ---------- Badge --------------------------------------------------------- */
.badge {
  display: inline-flex; align-items: center; gap: 0.25rem; height: 19px; padding: 0 0.45rem;
  font-size: 0.7rem; font-weight: 500; border-radius: 999px;
  background: hsl(var(--secondary)); color: hsl(var(--secondary-foreground));
}
.badge--success { background: hsl(var(--success) / 0.14); color: hsl(142 71% 30%); }
.badge--warning { background: hsl(var(--warning) / 0.16); color: hsl(32 81% 36%); }
.badge--destructive { background: hsl(var(--destructive) / 0.12); color: hsl(var(--destructive)); }
.badge--outline { background: transparent; border: 1px solid hsl(var(--border)); color: hsl(var(--muted-foreground)); }

/* ---------- Dialog / modal ------------------------------------------------ */
.dialog-overlay {
  position: fixed; inset: 0; background: hsl(240 10% 4% / 0.45);
  display: none; align-items: flex-start; justify-content: center; z-index: 60;
  padding: 5vh 1rem; overflow-y: auto;
}
.dialog-overlay.open { display: flex; }
.dialog {
  background: hsl(var(--background)); border-radius: var(--radius);
  border: 1px solid hsl(var(--border)); box-shadow: 0 20px 50px -12px hsl(240 10% 4% / 0.3);
  width: 100%; max-width: 520px; animation: dialog-in .15s ease-out;
}
.dialog--lg { max-width: 760px; }
@keyframes dialog-in { from { opacity: 0; transform: translateY(-8px) scale(.98); } to { opacity: 1; } }
.dialog__header { padding: 1.1rem 1.25rem 0.5rem; }
.dialog__title { font-size: 1.05rem; font-weight: 600; }
.dialog__desc { font-size: 0.85rem; color: hsl(var(--muted-foreground)); margin-top: 3px; }
.dialog__body { padding: 1rem 1.25rem; }
.dialog__footer { padding: 0.75rem 1.25rem 1.25rem; display: flex; gap: 0.6rem; justify-content: flex-end; }
.dialog__close { position: absolute; }

/* ---------- Toast --------------------------------------------------------- */
.toast-region { position: fixed; bottom: 1.25rem; right: 1.25rem; z-index: 80; display: flex; flex-direction: column; gap: 0.6rem; }
.toast {
  background: hsl(var(--foreground)); color: hsl(var(--background));
  padding: 0.7rem 1rem; border-radius: var(--radius); font-size: 0.85rem;
  box-shadow: 0 10px 30px -10px hsl(240 10% 4% / 0.4); min-width: 240px; max-width: 360px;
  display: flex; align-items: center; gap: 0.6rem; animation: toast-in .2s ease-out;
}
.toast--success { background: hsl(142 71% 35%); color: #fff; }
.toast--error { background: hsl(var(--destructive)); color: #fff; }
@keyframes toast-in { from { opacity: 0; transform: translateY(10px); } }

/* ---------- Dropdown ------------------------------------------------------ */
.dropdown { position: relative; display: inline-block; }
.dropdown__menu {
  position: absolute; right: 0; top: calc(100% + 4px); min-width: 180px; z-index: 50;
  background: hsl(var(--popover)); border: 1px solid hsl(var(--border)); border-radius: var(--radius);
  box-shadow: 0 12px 30px -10px hsl(240 10% 4% / 0.25); padding: 4px; display: none;
}
.dropdown.open .dropdown__menu { display: block; }
.dropdown__item {
  display: flex; align-items: center; gap: 0.5rem; padding: 0.45rem 0.6rem;
  border-radius: calc(var(--radius) - 3px); font-size: 0.85rem; cursor: pointer; width: 100%;
  background: none; border: 0; text-align: left; color: inherit;
}
.dropdown__item:hover { background: hsl(var(--accent)); }
.dropdown__item--danger { color: hsl(var(--destructive)); }
.dropdown__sep { height: 1px; background: hsl(var(--border)); margin: 4px 0; }

/* ---------- Combobox / autocomplete --------------------------------------- */
.combobox { position: relative; }
.combobox__list {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0; z-index: 55;
  background: hsl(var(--popover)); border: 1px solid hsl(var(--border)); border-radius: var(--radius);
  box-shadow: 0 12px 30px -10px hsl(240 10% 4% / 0.25); max-height: 260px; overflow-y: auto;
  padding: 4px; display: none;
}
.combobox.open .combobox__list { display: block; }
.combobox__option { padding: 0.5rem 0.6rem; border-radius: calc(var(--radius) - 3px); cursor: pointer; font-size: 0.85rem; }
.combobox__option small { color: hsl(var(--muted-foreground)); display: block; }
.combobox__option:hover, .combobox__option.active { background: hsl(var(--accent)); }
.combobox__empty { padding: 0.7rem; color: hsl(var(--muted-foreground)); font-size: 0.83rem; text-align: center; }

/* ---------- TomSelect overrides (match .input / .popover) ----------------- */
.ts-wrapper { font-size: 0.875rem; }
.ts-wrapper.single .ts-control,
.ts-wrapper.multi .ts-control {
  min-height: 32px; padding: 1px 1.8rem 1px 0.6rem; border: 1px solid hsl(var(--input)); border-radius: var(--radius);
  background: hsl(var(--background)); box-shadow: none; color: hsl(var(--foreground));
}
.ts-wrapper.single .ts-control { display: flex; align-items: center; flex-wrap: nowrap; }
.ts-control { font-family: inherit; line-height: 1.3; }
.ts-control > * { margin: 0 !important; vertical-align: middle; }
.ts-wrapper.single .ts-control > .item { line-height: 1.3; padding: 0; }
.ts-control input { margin: 0 !important; min-height: 0; height: auto; line-height: 1.3; }
.ts-wrapper.focus .ts-control { border-color: hsl(var(--ring)); box-shadow: 0 0 0 3px hsl(var(--ring) / 0.12); }
.ts-wrapper.single .ts-control input { color: hsl(var(--foreground)); }
.ts-control input::placeholder { color: hsl(var(--muted-foreground)); }
.ts-wrapper.single.input-active .ts-control { background: hsl(var(--background)); }
.ts-wrapper.single .ts-control:not(.rtl)::after {
  position: absolute; top: 50%; right: 0.7rem; margin: 0; transform: translateY(-25%);
  border-color: hsl(var(--muted-foreground)) transparent transparent;
}
.ts-wrapper.single .ts-control { position: relative; }
.ts-wrapper.disabled { opacity: 0.7; }
.ts-dropdown {
  background: hsl(var(--popover)); border: 1px solid hsl(var(--border)); border-radius: var(--radius);
  box-shadow: 0 12px 30px -10px hsl(240 10% 4% / 0.25); margin-top: 4px; padding: 4px; font-size: 0.875rem;
}
.ts-dropdown .option { border-radius: calc(var(--radius) - 3px); padding: 0.5rem 0.6rem; }
.ts-dropdown .active { background: hsl(var(--accent)); color: hsl(var(--accent-foreground)); }
.ts-dropdown .no-results { padding: 0.6rem; color: hsl(var(--muted-foreground)); }
.ts-wrapper.disabled .ts-control { background: hsl(var(--muted)); }

/* ---------- Auth page ----------------------------------------------------- */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem; background: hsl(var(--muted)); }
.auth-card { width: 100%; max-width: 380px; }
.auth-card .logo-lg { width: 48px; height: 48px; border-radius: 12px; background: hsl(var(--primary)); color: hsl(var(--primary-foreground)); display: grid; place-items: center; font-weight: 800; margin: 0 auto 1rem; }
.logo-lg { width: 48px; height: 48px; border-radius: 12px; background: hsl(var(--primary)); color: hsl(var(--primary-foreground)); display: inline-grid; place-items: center; font-weight: 800; font-size: 1.05rem; }

/* Onboarding step indicator */
.step-pill { display: inline-flex; align-items: center; gap: 0.4rem; color: hsl(var(--muted-foreground)); font-weight: 500; white-space: nowrap; }
.step-pill .step-num { width: 20px; height: 20px; border-radius: 50%; background: hsl(var(--muted)); display: grid; place-items: center; font-size: 0.72rem; font-weight: 700; }
.step-pill.active { color: hsl(var(--foreground)); }
.step-pill.active .step-num { background: hsl(var(--primary)); color: hsl(var(--primary-foreground)); }

/* ---------- Utilities ----------------------------------------------------- */
.muted { color: hsl(var(--muted-foreground)); }
.text-sm { font-size: 0.83rem; }
.text-right { text-align: right; }
.num { font-variant-numeric: tabular-nums; }
.flex { display: flex; } .items-center { align-items: center; } .justify-between { justify-content: space-between; }
.gap-1 { gap: 0.35rem; } .gap-2 { gap: 0.6rem; } .gap-3 { gap: 1rem; }
.mt-1 { margin-top: 0.35rem; } .mt-2 { margin-top: 0.6rem; } .mt-3 { margin-top: 1rem; } .mt-4 { margin-top: 1.5rem; }
.mb-0 { margin-bottom: 0; } .mb-2 { margin-bottom: 0.6rem; } .mb-3 { margin-bottom: 1rem; }
.w-full { width: 100%; }
.alert { padding: 0.7rem 0.9rem; border-radius: var(--radius); font-size: 0.85rem; margin-bottom: 1rem; border: 1px solid transparent; }
.alert--error { background: hsl(var(--destructive) / 0.08); border-color: hsl(var(--destructive) / 0.3); color: hsl(var(--destructive)); }
.alert--success { background: hsl(var(--success) / 0.1); border-color: hsl(var(--success) / 0.3); color: hsl(142 71% 30%); }
.hidden { display: none !important; }

/* ---------- Print --------------------------------------------------------- */
@media print {
  .sidebar, .topbar, .sidebar-backdrop, .no-print { display: none !important; }
  .main { margin-left: 0 !important; }
  .content { padding: 0 !important; max-width: none !important; }
  body { background: #fff; }
  .card { box-shadow: none; border-color: #ccc; }
  .table thead th { background: #f3f3f3 !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  a { color: #000; text-decoration: none; }
}

/* ---------- Responsive ---------------------------------------------------- */
@media (max-width: 880px) {
  .sidebar { transform: translateX(-100%); transition: transform .2s; box-shadow: 0 0 40px hsl(240 10% 4% / 0.2); }
  .sidebar.open { transform: translateX(0); }
  .main { margin-left: 0; }
  .sidebar-toggle { display: inline-flex; }
  .form-grid, .form-grid--3 { grid-template-columns: 1fr; }
  .col-span-2 { grid-column: auto; }
  .sidebar-backdrop { position: fixed; inset: 0; background: hsl(240 10% 4% / 0.4); z-index: 35; display: none; }
  .sidebar-backdrop.open { display: block; }
}
