:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #eef2f5;
  --text: #1c2630;
  --muted: #5d6b78;
  --border: #d5dde4;
  --primary: #0e5a8a;
  --primary-ink: #ffffff;
  --primary-soft: #e3eff7;
  --accent: #d9731a;
  --danger: #b3261e;
  --danger-soft: #fbe9e7;
  --ok: #1e7a44;
  --warn-soft: #fff4de;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 32, 48, .08), 0 2px 8px rgba(16, 32, 48, .06);
  --font: "Noto Sans Devanagari", "Nirmala UI", "Mangal", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --tab-h: 62px;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 var(--font);
  padding-bottom: calc(var(--tab-h) + env(safe-area-inset-bottom));
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--primary); }
.loading { padding: 40px 16px; text-align: center; color: var(--muted); }

/* ---------- top bar ---------- */
#topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  min-height: 54px; padding: 6px 12px;
  background: var(--primary); color: var(--primary-ink);
  box-shadow: var(--shadow);
}
#topbar h1 { flex: 1; margin: 0; font-size: 18px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#topbar .icon-btn { background: transparent; border: 0; color: inherit; padding: 8px; border-radius: 8px; cursor: pointer; font-size: 15px; }
#topbar .icon-btn:hover { background: rgba(255, 255, 255, .12); }
#topbar .who { font-size: 13px; opacity: .9; text-align: right; line-height: 1.2; }

/* ---------- bottom tabs ---------- */
#tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; height: calc(var(--tab-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--border);
}
#tabbar:empty { display: none; }
#tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  text-decoration: none; color: var(--muted); font-size: 13px;
}
#tabbar a .ico { font-size: 20px; line-height: 1; }
#tabbar a.active { color: var(--primary); font-weight: 600; }

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

/* ---------- basics ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; box-shadow: var(--shadow); }
.card h2 { margin: 0 0 10px; font-size: 17px; }
.card h3 { margin: 12px 0 6px; font-size: 15px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row > .grow { flex: 1 1 160px; min-width: 0; }
.stack > * + * { margin-top: 10px; }
.right { text-align: right; }
.num { font-variant-numeric: tabular-nums; text-align: right; }
.hidden { display: none !important; }

label.field { display: block; }
label.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
input[type=text], input[type=email], input[type=tel], input[type=number], input[type=password], input[type=search], input[type=date], select, textarea {
  width: 100%; min-height: 44px; padding: 8px 10px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: var(--primary); }
input[readonly] { background: var(--surface-2); }
textarea { min-height: 70px; resize: vertical; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 8px 16px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  text-decoration: none; font-weight: 500;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); border-color: #e6b8b4; }
.btn.small { min-height: 36px; padding: 4px 10px; font-size: 14px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }

.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 6px 12px; cursor: pointer; font-size: 14px; }
.chip.on { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }

.seg { display: flex; border: 1px solid var(--primary); border-radius: 8px; overflow: hidden; }
.seg button { flex: 1; border: 0; background: var(--surface); padding: 10px 6px; cursor: pointer; min-height: 44px; }
.seg button + button { border-left: 1px solid var(--primary); }
.seg button.on { background: var(--primary); color: var(--primary-ink); font-weight: 600; }

.badge { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.badge.def { background: #e6f4ea; color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: #8a5a00; }
.badge.edit { background: #fdeede; color: var(--accent); }

.empty { text-align: center; color: var(--muted); padding: 30px 10px; }
.notice { background: var(--warn-soft); border: 1px solid #f0d49a; border-radius: 8px; padding: 10px 12px; font-size: 14px; }
.error-text { color: var(--danger); font-size: 14px; }

/* ---------- lists ---------- */
.list { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.list-item { display: flex; gap: 10px; align-items: center; padding: 11px 12px; border-bottom: 1px solid var(--border); text-decoration: none; color: inherit; cursor: pointer; }
.list-item:last-child { border-bottom: 0; }
.list-item:hover, .list-item.hl { background: var(--primary-soft); }
.list-item .main { flex: 1; min-width: 0; }
.list-item .title { font-weight: 500; }
.list-item .sub { font-size: 13px; color: var(--muted); }
.list-item .amt { font-weight: 600; white-space: nowrap; }

/* ---------- quotation editor ---------- */
.q-head { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.q-head .full { grid-column: 1 / -1; }
.profit-box { margin-top: 10px; padding: 8px 10px; border: 1px dashed var(--border); border-radius: 8px; }
.profit-box.on { border: 1px solid var(--accent); background: #fff7ef; }
.billed { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; background: #e6f4ea; border: 1px solid #b7dcc3;
  color: var(--ok); border-radius: 8px; padding: 8px 12px; margin-bottom: 12px; font-weight: 600; }
.paper { position: relative; }
.paper .stamp {
  position: absolute; top: 38%; left: 50%; transform: translate(-50%, -50%) rotate(-18deg);
  border: 4px solid #b3261e; color: #b3261e; padding: 6px 18px; font-size: 34px; font-weight: 800;
  letter-spacing: 2px; opacity: .35; pointer-events: none; white-space: nowrap;
}
.paper .cancel-note { border: 2px solid #b3261e; color: #b3261e; padding: 6px 10px; margin-bottom: 10px; font-size: 14px; font-weight: 600; }
.cancelled-bar { background: var(--danger-soft); border: 1px solid #e6b8b4; color: var(--danger); border-radius: 8px; padding: 8px 12px; font-weight: 600; }
.list-item.cancelled .amt { text-decoration: line-through; color: var(--muted); }
.badge.cancel { background: var(--danger-soft); color: var(--danger); font-weight: 600; }
.sum-bar { display: flex; justify-content: space-between; gap: 10px; flex-wrap: wrap; font-size: 14px; margin: 0 0 10px; }
@media print { .paper .stamp { opacity: .5; } }
.paper .terms { margin-top: 14px; border-top: 1px solid #333; padding-top: 6px; font-size: 13px; line-height: 1.5; }
.search-wrap { position: sticky; top: 54px; z-index: 10; background: var(--bg); padding: 8px 0; }
.search-wrap input { font-size: 17px; min-height: 48px; }
.results { margin-top: 6px; max-height: 55vh; overflow-y: auto; }
.results .rate { font-weight: 600; white-space: nowrap; }

.lines { counter-reset: ln; }
.line { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; margin-bottom: 8px; }
.line.flash { animation: flash 1.2s; }
@keyframes flash { from { background: #fff1c7; } to { background: var(--surface); } }
.line-top { display: flex; gap: 8px; align-items: flex-start; }
.line-top .sr { color: var(--muted); min-width: 22px; }
.line-top .name { flex: 1; font-weight: 500; }
.line-top .del { border: 0; background: transparent; color: var(--danger); font-size: 20px; cursor: pointer; padding: 0 4px; line-height: 1; }
.line-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(90px, 1fr)); gap: 8px; margin-top: 8px; align-items: end; }
.line-grid label > span { font-size: 12px; color: var(--muted); display: block; }
.line-grid input { min-height: 42px; text-align: right; }
.line-grid .amount { text-align: right; font-weight: 600; padding-bottom: 8px; }
.line .note { font-size: 12px; margin-top: 4px; }

.q-footer {
  position: sticky; bottom: calc(var(--tab-h) + env(safe-area-inset-bottom)); z-index: 10;
  display: flex; align-items: center; gap: 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 12px; box-shadow: 0 -2px 10px rgba(16, 32, 48, .08); margin-top: 12px;
}
.q-footer .total { flex: 1; }
.q-footer .total b { font-size: 20px; }

/* ---------- tables (settings, items) ---------- */
.table-wrap { overflow-x: auto; }
table.grid { width: 100%; border-collapse: collapse; font-size: 14px; }
table.grid th, table.grid td { padding: 8px 6px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
table.grid th { font-size: 12px; color: var(--muted); font-weight: 600; background: var(--surface-2); }
table.grid input { min-height: 38px; padding: 4px 8px; }
table.grid td.num input { text-align: right; max-width: 110px; }

.rate-preview { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.rate-preview div { background: var(--surface-2); border-radius: 8px; padding: 8px; text-align: center; }
.rate-preview b { display: block; font-size: 17px; }

/* ---------- printable quotation ---------- */
.paper { background: #fff; color: #000; border: 1px solid var(--border); border-radius: 6px; padding: 22px 22px 30px; max-width: 800px; margin: 0 auto; }
.paper .shop { text-align: center; border-bottom: 2px solid #000; padding-bottom: 8px; margin-bottom: 8px; }
.paper .shop h2 { margin: 0; font-size: 24px; }
.paper .shop div { font-size: 14px; }
.paper .doc-title { text-align: center; font-size: 19px; font-weight: 700; margin: 6px 0 10px; letter-spacing: 1px; }
.paper .meta { display: flex; justify-content: space-between; gap: 12px; font-size: 15px; margin-bottom: 10px; flex-wrap: wrap; }
.paper table { width: 100%; border-collapse: collapse; font-size: 15px; }
.paper th, .paper td { border: 1px solid #333; padding: 5px 7px; }
.paper th { background: #eee; font-weight: 700; }
.paper td.c { text-align: center; }
.paper th { white-space: nowrap; }
.paper td.r, .paper th.r { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.paper tfoot td { font-weight: 700; font-size: 16px; }
.paper .foot { margin-top: 12px; font-size: 13px; display: flex; justify-content: space-between; gap: 12px; }
.paper .sign { margin-top: 40px; text-align: right; font-size: 14px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--tab-h) + 16px); transform: translateX(-50%) translateY(20px);
  background: #1c2630; color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 15px;
  opacity: 0; pointer-events: none; transition: .2s; z-index: 50; max-width: calc(100% - 32px);
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.error { background: var(--danger); }
#toast.ok { background: var(--ok); }

.login-box { max-width: 380px; margin: 30px auto; }
.user-pick { display: grid; gap: 8px; }
.user-pick button { text-align: left; }
.user-pick button.on { border-color: var(--primary); background: var(--primary-soft); font-weight: 600; }
.pin-input { font-size: 24px !important; letter-spacing: 8px; text-align: center; }

@media (max-width: 560px) {
  .q-head { grid-template-columns: 1fr; }
  .rate-preview { grid-template-columns: repeat(2, 1fr); }
  .paper { padding: 12px; }
  .paper th, .paper td { padding: 4px 4px; font-size: 13px; }
}

@media print {
  @page { size: A4; margin: 12mm; }
  body { background: #fff; padding: 0; }
  .no-print, #topbar, #tabbar, #toast, .actions { display: none !important; }
  main { max-width: none; padding: 0; }
  .paper { border: 0; padding: 0; max-width: none; }
  .paper th, .paper td { font-size: 13px; }
  .paper tr { break-inside: avoid; }
}
