/* ==========================================================================
   Kongu Mess - application styles (screen). Print styles live in print.css.
   ========================================================================== */
:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --border: #e2e5ea;
  --border-strong: #cfd4dc;
  --text: #1d232b;
  --muted: #687280;
  --primary: #a3401c;
  --primary-hover: #8a3517;
  --primary-soft: #fbefe9;
  --sidebar: #1e2329;
  --sidebar-text: #c7ccd3;
  --green: #1f7a4a;  --green-soft: #e6f4ec;
  --amber: #9a6200;  --amber-soft: #fdf3dc;
  --red:   #b42318;  --red-soft:   #fdecea;
  --grey:  #5d6570;  --grey-soft:  #eef0f3;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(16,24,40,.06);
  --font: "Segoe UI", system-ui, -apple-system, Roboto, "Noto Sans", "Noto Sans Tamil", Arial, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { font: 14px/1.5 var(--font); color: var(--text); background: var(--bg); }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0 0 .5rem; line-height: 1.25; }
h1 { font-size: 22px; } h2 { font-size: 17px; } h3 { font-size: 15px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.center { text-align: center; }
.mt-0 { margin-top: 0; } .mt { margin-top: 16px; } .mb { margin-bottom: 16px; }
.pre { white-space: pre-line; }

/* ---------- Layout ---------- */
.layout { display: flex; min-height: 100vh; }
.sidebar { width: 236px; flex: 0 0 236px; background: var(--sidebar); color: var(--sidebar-text);
  position: sticky; top: 0; height: 100vh; overflow-y: auto; display: flex; flex-direction: column; }
.brand { display: flex; gap: 10px; align-items: center; padding: 18px 16px; border-bottom: 1px solid #2c333b; }
.brand-mark { width: 36px; height: 36px; border-radius: 8px; background: var(--primary); color: #fff;
  display: grid; place-items: center; font-weight: 700; letter-spacing: .5px; }
.brand-name { color: #fff; font-weight: 600; font-size: 15px; }
.brand-sub { font-size: 11px; color: #8d959f; }
.nav { padding: 10px 8px; display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav a, .nav-logout button { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 6px;
  color: var(--sidebar-text); font: inherit; text-decoration: none; }
.nav a:hover, .nav-logout button:hover { background: #2a3139; color: #fff; }
.nav a.active { background: var(--primary); color: #fff; }
.nav-logout { margin-top: auto; border-top: 1px solid #2c333b; padding-top: 8px; }
.nav-logout button { width: 100%; background: none; border: 0; cursor: pointer; text-align: left; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 12px; padding: 10px 24px;
  background: var(--surface); border-bottom: 1px solid var(--border); }
.menu-toggle { display: none; background: none; border: 1px solid var(--border); border-radius: 6px; padding: 4px 10px; font-size: 18px; cursor: pointer; }
.top-search { flex: 1; max-width: 460px; }
.top-search input { width: 100%; }
.top-actions { margin-left: auto; display: flex; gap: 8px; align-items: center; }
.user-chip { padding: 4px 10px; background: var(--grey-soft); border-radius: 999px; font-size: 12px; color: var(--grey); }
.content { padding: 22px 24px 40px; flex: 1; }
.app-footer { padding: 12px 24px; font-size: 12px; color: var(--muted); border-top: 1px solid var(--border); background: var(--surface); }

.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.page-head .sub { color: var(--muted); margin-top: 2px; }
.page-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- Cards ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 16px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.card-head h2 { margin: 0; font-size: 15px; }
.card-body { padding: 16px; }
.card-body.flush { padding: 0; }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(165px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.stat .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .4px; }
.stat .value { font-size: 24px; font-weight: 650; margin-top: 4px; font-variant-numeric: tabular-nums; }
.stat .hint { font-size: 12px; color: var(--muted); }
.stat.accent { border-left: 3px solid var(--primary); }
.stat.warn { border-left: 3px solid var(--amber); }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
table.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: 9px 12px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
.table th { background: #fafbfc; font-size: 12px; text-transform: uppercase; letter-spacing: .3px; color: var(--muted); font-weight: 600; white-space: nowrap; }
.table tbody tr:hover { background: #fcfcfd; }
.table td.num, .table th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table tfoot td { font-weight: 600; background: #fafbfc; }
.th-sort { color: inherit; }
.addr-cell { max-width: 260px; white-space: pre-line; font-size: 13px; }
.empty { padding: 36px 16px; text-align: center; color: var(--muted); }
.empty strong { display: block; color: var(--text); margin-bottom: 4px; }
.actions { display: flex; flex-wrap: wrap; gap: 4px; }
.row-deleted td { opacity: .55; }

/* ---------- Badges ---------- */
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.badge-green { background: var(--green-soft); color: var(--green); }
.badge-amber { background: var(--amber-soft); color: var(--amber); }
.badge-red { background: var(--red-soft); color: var(--red); }
.badge-grey { background: var(--grey-soft); color: var(--grey); }

/* ---------- Buttons ---------- */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 7px 14px; border: 1px solid var(--border-strong); border-radius: 6px;
  background: #fff; color: var(--text); font: inherit; font-weight: 500; cursor: pointer; text-decoration: none; white-space: nowrap; line-height: 1.3; }
.btn:hover { background: #f4f5f7; text-decoration: none; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger { color: var(--red); border-color: #f1c4bf; }
.btn-danger:hover { background: var(--red-soft); }
.btn-sm { padding: 4px 10px; font-size: 13px; }
.btn-xs { padding: 2px 8px; font-size: 12px; }
.btn[disabled], .btn.disabled { opacity: .5; pointer-events: none; }
.inline-form { display: inline; margin: 0; }

/* ---------- Forms ---------- */
label { display: block; font-weight: 600; font-size: 13px; margin-bottom: 4px; }
label .req { color: var(--red); }
input[type=text], input[type=search], input[type=number], input[type=date], input[type=tel], input[type=email],
input[type=password], select, textarea {
  width: 100%; padding: 8px 10px; border: 1px solid var(--border-strong); border-radius: 6px; background: #fff;
  font: inherit; color: var(--text); }
textarea { min-height: 70px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid #f0b9a3; outline-offset: 0; border-color: var(--primary); }
input[readonly] { background: #f7f8fa; }
.field { margin-bottom: 14px; }
.field .help { font-size: 12px; color: var(--muted); margin-top: 3px; font-weight: normal; }
.form-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0 16px; }
.form-grid .span-2 { grid-column: span 2; } .form-grid .span-3 { grid-column: 1 / -1; }
.form-actions { display: flex; gap: 8px; padding-top: 6px; flex-wrap: wrap; }
.filters { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.filters .f { min-width: 150px; }
.filters .f label { font-size: 12px; color: var(--muted); }
.filters input, .filters select { padding: 6px 8px; }
.invalid { border-color: var(--red) !important; }

/* Line items editor */
.items-table input, .items-table select { padding: 6px 8px; }
.items-table td { vertical-align: middle; }
.items-table .col-qty { width: 110px; } .items-table .col-price { width: 130px; } .items-table .col-amt { width: 130px; }
.items-table .col-del { width: 44px; } .items-table .col-sn { width: 40px; color: var(--muted); }
.total-row { font-size: 16px; }

/* ---------- Searchable select ---------- */
.ss { position: relative; }
.ss-input { width: 100%; }
.ss-list { position: absolute; z-index: 50; left: 0; right: 0; top: 100%; margin-top: 2px; max-height: 260px; overflow-y: auto;
  background: #fff; border: 1px solid var(--border-strong); border-radius: 6px; box-shadow: 0 8px 24px rgba(16,24,40,.12); display: none; }
.ss.open .ss-list { display: block; }
.ss-opt { padding: 7px 10px; cursor: pointer; }
.ss-opt small { color: var(--muted); margin-left: 6px; }
.ss-opt.hl, .ss-opt:hover { background: var(--primary-soft); }
.ss-empty { padding: 8px 10px; color: var(--muted); }

/* ---------- Alerts ---------- */
.alert { position: relative; padding: 10px 36px 10px 14px; border-radius: 6px; margin-bottom: 14px; border: 1px solid; }
.alert-success { background: var(--green-soft); border-color: #b9e0c9; color: #14532d; }
.alert-error { background: var(--red-soft); border-color: #f4c7c2; color: #7a1a12; }
.alert-info { background: #eef4fd; border-color: #c9dbf6; color: #1e3a6b; }
.alert-warning { background: var(--amber-soft); border-color: #f1dca5; color: #6b4500; }
.alert ul { margin: 4px 0 0 18px; padding: 0; }
.alert-close { position: absolute; right: 8px; top: 6px; border: 0; background: none; font-size: 18px; cursor: pointer; color: inherit; opacity: .6; }

/* ---------- Detail views ---------- */
.dl { display: grid; grid-template-columns: 150px 1fr; gap: 6px 12px; margin: 0; }
.dl dt { color: var(--muted); }
.dl dd { margin: 0; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; flex-wrap: wrap; }
.tabs a { padding: 8px 14px; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a.active { color: var(--primary); border-color: var(--primary); font-weight: 600; }
.tabs a:hover { text-decoration: none; color: var(--text); }

/* Address editor on customer form */
.addr-row { display: grid; grid-template-columns: 150px 1fr auto auto; gap: 8px; align-items: start; padding: 10px; border: 1px solid var(--border); border-radius: 6px; margin-bottom: 8px; background: #fcfcfd; }
.addr-row textarea { min-height: 58px; }
.addr-row .def { font-weight: normal; font-size: 12px; display: flex; gap: 4px; align-items: center; margin-top: 8px; white-space: nowrap; }

/* ---------- Loading ---------- */
.loading-overlay { position: fixed; inset: 0; background: rgba(255,255,255,.6); display: grid; place-items: center; z-index: 100; }
.loading-overlay[hidden] { display: none; }
.spinner { width: 38px; height: 38px; border: 4px solid #eadbd4; border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Pagination ---------- */
.pager { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; padding: 10px 16px; }
.pager-info { color: var(--muted); font-size: 13px; margin-right: auto; padding: 10px 16px; }
.pager .pager-info { padding: 0; }
.pg { padding: 4px 10px; border: 1px solid var(--border); border-radius: 6px; color: var(--text); background: #fff; font-size: 13px; }
.pg.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.pg:hover { text-decoration: none; background: #f4f5f7; }
.pg.active:hover { background: var(--primary-hover); }
.pg-gap { padding: 0 4px; color: var(--muted); }

/* ---------- Login ---------- */
.auth-page { min-height: 100vh; display: grid; place-items: center; background: var(--bg); padding: 20px; }
.auth-card { width: 100%; max-width: 380px; background: #fff; border: 1px solid var(--border); border-radius: 12px; padding: 28px; box-shadow: 0 10px 30px rgba(16,24,40,.06); }
.auth-card .brand-mark { margin-bottom: 12px; }
.auth-card .btn { width: 100%; justify-content: center; padding: 10px; }

/* ---------- Print preview frame (screen) ---------- */
.preview-toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 14px; }
.preview-stage { background: #d9dce1; padding: 24px; border-radius: var(--radius); overflow: auto; }

/* ---------- Responsive ---------- */
@media (max-width: 1100px) {
  .grid-3 { grid-template-columns: 1fr 1fr; }
  .form-grid { grid-template-columns: 1fr 1fr; }
}
/* ---------- Mobile app layout (phones & small tablets) ---------- */
.tabbar, .top-brand, .search-toggle, .drawer-backdrop { display: none; }
@media (max-width: 860px) {
  body { font-size: 15px; background: #f2f3f5; }
  .layout { display: block; }
  /* Drawer menu ("More") */
  .sidebar { position: fixed; left: -280px; top: 0; z-index: 60; width: 270px; height: 100%; transition: left .2s ease; }
  .sidebar.open { left: 0; box-shadow: 4px 0 24px rgba(0,0,0,.25); }
  .drawer-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 55; }
  body.drawer-open .drawer-backdrop { display: block; }
  .nav a, .nav-logout button { padding: 12px 12px; font-size: 15px; }

  /* App bar */
  .topbar { padding: 8px 12px; gap: 8px; padding-top: calc(8px + env(safe-area-inset-top)); background: #1e2329; border-bottom: 0; flex-wrap: wrap; }
  .menu-toggle { display: none; }
  .top-brand { display: flex; align-items: center; gap: 10px; color: #fff; font-weight: 600; font-size: 17px; flex: 1; min-width: 0; text-decoration: none; }
  .top-brand .brand-mark { width: 30px; height: 30px; font-size: 12px; flex: 0 0 30px; }
  .top-title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .top-brand:hover { text-decoration: none; }
  .search-toggle { display: inline-grid; place-items: center; width: 38px; height: 38px; border: 0; border-radius: 50%; background: transparent; color: #fff; cursor: pointer; }
  .top-search { display: none; order: 5; flex: 1 0 100%; max-width: none; }
  .search-open .top-search { display: block; }
  .top-actions { display: none; }

  /* Content */
  .content { padding: 12px 12px calc(84px + env(safe-area-inset-bottom)); }
  .app-footer { display: none; }
  .page-head { margin-bottom: 12px; }
  .page-head h1 { font-size: 20px; }
  .page-actions { width: 100%; }
  .page-actions .btn, .page-actions form, .page-actions .inline-form { flex: 1 1 auto; }
  .page-actions .btn { justify-content: center; padding: 10px 12px; }
  .page-actions form .btn { width: 100%; }
  .card { border-radius: 12px; }
  .card-head { padding: 12px 14px; flex-wrap: wrap; }
  .card-body { padding: 14px; }
  .grid-2, .grid-3, .form-grid { grid-template-columns: 1fr; }
  .form-grid .span-2, .form-grid .span-3 { grid-column: auto; }
  .stats { grid-template-columns: 1fr 1fr; gap: 10px; }
  .stat { padding: 12px; border-radius: 12px; }
  .stat .value { font-size: 20px; }
  .dl { grid-template-columns: 110px 1fr; }

  /* Touch-friendly inputs (16px stops iPhone zooming in) */
  input[type=text], input[type=search], input[type=number], input[type=date], input[type=tel], input[type=email],
  input[type=password], select, textarea { font-size: 16px; padding: 10px 12px; min-height: 44px; }
  .btn { min-height: 40px; }
  .btn-xs, .btn-sm { min-height: 34px; padding: 6px 12px; font-size: 14px; }
  .form-actions { position: sticky; bottom: calc(64px + env(safe-area-inset-bottom)); background: #f2f3f5; padding: 10px 0; z-index: 5; }
  .form-actions .btn { flex: 1 1 auto; justify-content: center; }
  .filters { flex-direction: column; align-items: stretch; }
  .filters .f { min-width: 0 !important; }
  .filters .btn { justify-content: center; }
  .print-day-form { flex-direction: column; align-items: stretch; }
  .print-day-form .btn { justify-content: center; }
  .tabs { overflow-x: auto; flex-wrap: nowrap; }
  .tabs a { white-space: nowrap; }
  .addr-row { grid-template-columns: 1fr; }
  .pager { justify-content: center; }
  .pager .pager-info { width: 100%; text-align: center; margin: 0 0 6px; }

  /* Tables become cards: each row is a card, each cell shows its column name */
  table.table.cards thead { display: none; }
  table.table.cards, table.table.cards tbody, table.table.cards tfoot, table.table.cards tr, table.table.cards td { display: block; width: 100%; }
  table.table.cards tr { padding: 10px 14px; border-bottom: 1px solid var(--border); }
  table.table.cards tbody tr:hover { background: transparent; }
  table.table.cards td { border: 0; padding: 3px 0; display: flex; gap: 12px; justify-content: space-between; text-align: right; }
  table.table.cards td::before { content: attr(data-label); color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .3px; text-align: left; flex: 0 0 auto; padding-top: 2px; }
  table.table.cards td[data-label=""]::before { content: none; }
  table.table.cards td:first-child { font-size: 16px; font-weight: 600; }
  table.table.cards td.addr-cell { max-width: none; }
  table.table.cards td .cv { min-width: 0; overflow-wrap: anywhere; }
  table.table.cards td .nowrap, table.table.cards td.nowrap { white-space: normal; }
  table.table.cards td .addr-cell, table.table.cards td.addr-cell .cv { white-space: pre-line; }
  table.table.cards td .actions { justify-content: flex-end; }
  table.table.cards td:empty, table.table.cards td:has(> .cv:empty) { display: none; }
  table.table.cards tfoot tr { background: #fafbfc; }
  .table-wrap { overflow-x: visible; }

  /* Line-item editor: one product per block */
  .items-table thead { display: none; }
  .items-table, .items-table tbody, .items-table tfoot, .items-table tr, .items-table td { display: block; width: 100% !important; }
  .items-table tbody tr { display: grid; grid-template-columns: 1fr 1fr 1fr 44px; gap: 6px 8px; padding: 12px; border-bottom: 1px solid var(--border); }
  .items-table tbody td { padding: 0; border: 0; }
  .items-table tbody td.col-sn { display: none; }
  .items-table tbody td:nth-child(2) { grid-column: 1 / -1; }
  .items-table tbody td.col-qty::before { content: "Qty"; }
  .items-table tbody td.col-price::before { content: "Rate"; }
  .items-table tbody td.col-amt::before { content: "Amount"; }
  .items-table tbody td.col-qty::before, .items-table tbody td.col-price::before, .items-table tbody td.col-amt::before { display: block; font-size: 11px; color: var(--muted); text-transform: uppercase; margin-bottom: 2px; }
  .items-table tbody td.col-del { align-self: end; }
  .items-table tbody td.col-del .btn { width: 44px; height: 44px; justify-content: center; }
  .items-table tfoot tr { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 12px; }
  .items-table tfoot td { border: 0; padding: 0; width: auto !important; }
  .items-table tfoot td:empty { display: none; }
  .items-table tfoot td:nth-child(2) { order: 3; width: 100% !important; }
  .items-table tfoot td:nth-child(2) .btn { width: 100%; justify-content: center; min-height: 44px; }
  .items-table tfoot td.right { flex: 1; text-align: left; }
  .ss-list { max-height: 50vh; }
  .ss-opt { padding: 11px 12px; }

  /* Bottom tab bar */
  .tabbar { display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 50; background: #fff; border-top: 1px solid var(--border);
    padding-bottom: env(safe-area-inset-bottom); box-shadow: 0 -2px 12px rgba(16,24,40,.06); }
  .tab { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 7px 0 6px; color: var(--muted); font-size: 11px; text-decoration: none; }
  .tab:hover { text-decoration: none; }
  .tab.active { color: var(--primary); font-weight: 600; }
  .tab-new .plus { width: 40px; height: 40px; margin-top: -18px; border-radius: 50%; background: var(--primary); color: #fff; display: grid; place-items: center;
    font-size: 26px; line-height: 1; box-shadow: 0 4px 12px rgba(163,64,28,.35); border: 3px solid #fff; }
}
@media (max-width: 380px) {
  .stats { grid-template-columns: 1fr; }
}
@media print {
  .sidebar, .topbar, .app-footer, .page-actions, .filters, .pager, .no-print, .alert { display: none !important; }
  .content { padding: 0; }
  body { background: #fff; }
  .card { border: 0; box-shadow: none; }
}
a.stat { display: block; color: inherit; text-decoration: none; }
a.stat:hover { border-color: var(--border-strong); text-decoration: none; }
.print-day-form { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.print-day-form .f { min-width: 160px; }
.print-day-form label { font-size: 12px; color: var(--muted); }
.day-chips { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.day-chip { padding: 6px 12px; border: 1px solid var(--border-strong); border-radius: 999px; color: var(--text); background: #fff; font-size: 13px; }
.day-chip:hover { background: var(--primary-soft); text-decoration: none; }
.day-chip.today { border-color: var(--primary); background: var(--primary-soft); }
@media (max-width: 860px) {
  .print-day-form { flex-direction: column; align-items: stretch; }
  .print-day-form .f { min-width: 0; }
  .print-day-form .btn { justify-content: center; }
}
/* Shipping address chooser (challan / bill pages) */
.addr-current { display: flex; gap: 10px; align-items: flex-start; padding: 10px 12px; background: var(--green-soft); border-radius: 8px; margin-bottom: 12px; }
.addr-options { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 8px; margin-bottom: 10px; }
.addr-option { display: flex; gap: 10px; align-items: flex-start; padding: 10px 12px; border: 1px solid var(--border-strong); border-radius: 8px; font-weight: normal; cursor: pointer; background: #fff; }
.addr-option:hover { border-color: var(--primary); }
.addr-option input { margin-top: 3px; width: 18px; height: 18px; flex: 0 0 auto; }
.addr-option.current { border-color: var(--green); }
.addr-option:has(input:checked) { border-color: var(--primary); background: var(--primary-soft); }
.d-block { display: block; }
