/* ---------- Tokens ---------- */
:root {
  --bg: #15130f; --bg-2: #181614; --panel: #1f1b16; --panel-2: #26211b; --border: #39332a;
  --text: #f1e9da; --muted: #a89c86; --accent: #e8a33d; --accent-2: #f2c273;
  --green: #6cb86f; --yellow: #e0b25a; --red: #d1554d; --blue: #6fa0c4;
  --radius: 16px; --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; color: var(--text); font-family: var(--font); font-size: 16px; line-height: 1.45; min-height: 100dvh;
  background: radial-gradient(1200px 600px at 50% -200px, #23201a 0%, var(--bg) 60%);
  padding-bottom: env(safe-area-inset-bottom);
}
h1, h2, h3 { margin: 0; font-weight: 700; letter-spacing: -0.01em; }
p { margin: 0.35rem 0; }
button, input, select { font: inherit; color: inherit; }
.vh { position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }
[hidden] { display: none !important; }
.num, .stat-value, .est-value, .progress-scale, .big-input, .table td:nth-child(2), .table td:nth-child(3) { font-variant-numeric: tabular-nums; direction: ltr; unicode-bidi: isolate; }

/* ---------- Layout ---------- */
.wrap { max-width: 640px; margin: 0 auto; padding: 14px 14px 40px; }
.topbar {
  position: sticky; top: 0; z-index: 20; background: rgba(21, 19, 15, 0.9); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border); padding: calc(10px + env(safe-area-inset-top)) 14px 0;
}
.brand { display: flex; align-items: center; gap: 12px; max-width: 640px; margin: 0 auto; }
.brand-mark {
  width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; font-size: 22px; flex: none;
  background: linear-gradient(135deg, #3a2c12, #241d12); border: 1px solid #5a4620; box-shadow: 0 0 0 1px rgba(232, 163, 61, 0.15), 0 6px 18px rgba(232, 163, 61, 0.15);
}
.brand-text { flex: 1; min-width: 0; }
.brand-title { font-weight: 800; font-size: 18px; }
.brand-sub { color: var(--muted); font-size: 12px; }
.lang-btn { border: 1px solid #4b3d24; background: #2a2419; color: var(--accent-2); border-radius: 999px; padding: 6px 12px; font-weight: 700; font-size: 14px; cursor: pointer; white-space: nowrap; }
.tabs { display: flex; gap: 6px; max-width: 640px; margin: 10px auto 0; }
.tab { flex: 1; border: 0; background: transparent; color: var(--muted); padding: 10px 6px 12px; font-weight: 700; font-size: 15px; border-bottom: 3px solid transparent; cursor: pointer; }
.tab.is-active { color: var(--accent-2); border-bottom-color: var(--accent); }

/* ---------- Cards ---------- */
.card {
  position: relative; background: linear-gradient(180deg, var(--panel), var(--bg-2)); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; margin: 12px 0; box-shadow: var(--shadow); overflow: hidden;
}
.card::before { content: ""; position: absolute; left: 0; right: 0; top: 0; height: 2px; background: linear-gradient(90deg, transparent, var(--accent), transparent); opacity: 0.85; }
.card-title { font-size: 17px; margin-bottom: 6px; }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.hint { color: var(--muted); font-size: 13px; margin-top: 8px; }
.notice { padding: 10px 12px; border-radius: 12px; background: #1a1c22; border: 1px solid #2c3442; color: #b9cbe0; font-size: 14px; margin-top: 10px; }

/* ---------- Buttons & forms ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 11px 16px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--panel-2); color: var(--text); font-weight: 600; cursor: pointer; text-decoration: none; min-height: 44px; user-select: none;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: linear-gradient(180deg, var(--accent-2), var(--accent)); color: #1c1407; border-color: #b57a22; }
.btn-secondary { background: #2a2419; border-color: #4b3d24; color: var(--accent-2); }
.row-actions { display: flex; gap: 8px; margin-top: 4px; }
.row-actions.wrap-actions { flex-wrap: wrap; }
.row-actions > .btn { flex: 1 1 auto; }
.link-row { display: flex; flex-wrap: wrap; gap: 6px 18px; margin-top: 12px; }
.link-btn { background: none; border: 0; padding: 4px 0; color: var(--accent-2); text-decoration: underline; cursor: pointer; font-size: 14px; }
.link-btn.danger { color: #e58a84; }
.form-grid { display: grid; gap: 10px; margin-top: 10px; }
.field { display: grid; gap: 5px; font-size: 13px; color: var(--muted); }
.field input, .big-input { width: 100%; padding: 11px 12px; border-radius: 10px; background: #120f0b; border: 1px solid var(--border); color: var(--text); font-size: 16px; }
.field input:focus { outline: 2px solid rgba(232, 163, 61, 0.55); border-color: var(--accent); }
.field .big-input { font-size: 22px; font-weight: 800; letter-spacing: 0.04em; padding: 8px 12px; }
.add-form { display: grid; grid-template-columns: 1fr 1.4fr; gap: 10px; margin-top: 10px; align-items: end; }
.add-form .btn { grid-column: 1 / -1; }
.btn-ghost { background: transparent; }
.photo-row { grid-column: 1 / -1; display: flex; gap: 8px; }
.photo-row .small-btn { flex: 1; min-height: 40px; padding: 8px 10px; font-size: 14px; font-weight: 600; color: var(--accent-2); border-color: #4b3d24; }
.photo-row .small-btn.busy { opacity: 0.6; pointer-events: none; }
.photo-result { grid-column: 1 / -1; display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 10px; background: #120f0b; border: 1px solid #4b3d24; }
.photo-result img { width: 56px; height: 56px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); flex: none; }
.photo-result .ok { color: var(--green); font-weight: 700; }
.photo-result .warn { color: var(--yellow); font-weight: 700; }
.photo-result .spin { display: inline-block; width: 14px; height: 14px; border-radius: 50%; border: 2px solid rgba(232,163,61,.3); border-top-color: var(--accent); animation: spin .8s linear infinite; vertical-align: -2px; margin-right: 6px; }
@keyframes spin { to { transform: rotate(360deg); } }
.bill-form { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 10px; align-items: end; }
.bill-form .btn { grid-column: 1 / -1; }
@media (min-width: 480px) { .bill-form { grid-template-columns: 1fr 1fr 1fr auto; } .bill-form .btn { grid-column: auto; } }

/* ---------- Stats / estimates ---------- */
.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-top: 8px; }
@media (min-width: 480px) { .stats { grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); } }
.stat { background: #120f0b; border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px; }
.stat-label { color: var(--muted); font-size: 12px; }
.stat-value { font-size: 22px; font-weight: 800; margin-top: 2px; }
.stat-value small { font-size: 12px; color: var(--muted); font-weight: 600; margin-left: 4px; }
.estimates { margin-top: 12px; display: grid; gap: 8px; }
.est { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 12px; background: #1a1611; border: 1px solid var(--border); }
.est.highlight { border-color: #4b3d24; background: linear-gradient(90deg, #2a2113, #1a1611); }
.est-label { color: var(--muted); font-size: 13px; }
.est-label strong { color: var(--text); display: block; font-size: 14px; }
.est-value { font-size: 20px; font-weight: 800; white-space: nowrap; color: var(--blue); }
.est-value.amber { color: var(--accent-2); }

/* ---------- 200-unit band ---------- */
.band { margin-top: 12px; padding: 10px 12px; border-radius: 12px; background: #120f0b; border: 1px solid var(--band, var(--border)); }
.band-text { font-weight: 700; color: var(--band, var(--text)); font-size: 14px; }
.band-green { --band: var(--green); } .band-yellow { --band: var(--yellow); } .band-red { --band: var(--red); }
.progress { position: relative; height: 10px; margin-top: 10px; border-radius: 999px; background: #1f1b16; border: 1px solid var(--border); }
.progress-bar { height: 100%; width: 0; border-radius: 999px; background: var(--band, var(--green)); transition: width 0.4s ease; }
.progress-marker { position: absolute; top: -4px; bottom: -4px; width: 2px; background: var(--yellow); opacity: 0.8; }
.progress-scale { display: flex; justify-content: space-between; color: var(--muted); font-size: 11px; margin-top: 4px; }

/* ---------- Chart ---------- */
.chart-wrap { margin-top: 12px; }
.chart-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; margin-bottom: 4px; }
#chart svg { width: 100%; height: auto; display: block; }
.chart-bar { fill: var(--accent); } .chart-bar.hi { fill: var(--red); } .chart-bar.mid { fill: var(--yellow); }
.chart-avg { stroke: var(--blue); stroke-width: 1.5; stroke-dasharray: 4 3; }
.chart-label { fill: var(--muted); font-size: 10px; font-family: var(--font); }
.chart-val { fill: var(--text); font-size: 10.5px; font-weight: 700; font-family: var(--font); }

/* ---------- Table ---------- */
.table-wrap { overflow-x: auto; margin-top: 8px; }
.table { width: 100%; border-collapse: collapse; font-size: 15px; }
.table th, .table td { padding: 9px 8px; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
.table th { color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
.table th.num, .table td.num { text-align: right; }
.table tr:last-child td { border-bottom: 0; }
.icon-btn { background: transparent; border: 0; cursor: pointer; font-size: 16px; padding: 4px 6px; opacity: 0.8; color: var(--muted); }
.icon-btn:hover { opacity: 1; }
.used-pos { color: var(--accent-2); }

/* ---------- Bills ---------- */
.bills-list { display: grid; gap: 6px; margin-top: 8px; }
.bill-row { display: grid; grid-template-columns: 24px 1fr 1fr 1fr auto; align-items: center; gap: 8px; padding: 8px 10px; border-radius: 10px; background: #120f0b; border: 1px solid var(--border); font-size: 14px; }
.bill-idx { width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; background: #2a2419; color: var(--accent-2); font-size: 12px; font-weight: 700; }

/* ---------- Misc ---------- */
.disclaimer { margin-top: 18px; padding: 10px 12px; border-radius: 12px; font-size: 13px; color: var(--yellow); background: rgba(224, 178, 90, 0.08); border: 1px solid rgba(224, 178, 90, 0.35); text-align: center; }
.toasts { position: fixed; left: 50%; bottom: 18px; transform: translateX(-50%); z-index: 200; display: grid; gap: 8px; width: min(92vw, 520px); }
.toast { padding: 12px 14px; border-radius: 12px; background: #221d16; border: 1px solid var(--border); color: var(--text); font-size: 14px; box-shadow: var(--shadow); }
.toast.err { border-color: #7a3a36; background: #2a1815; color: #f3c9c5; }
.toast.ok { border-color: #3a5a3c; background: #172118; color: #cfe9d0; }

/* ---------- Urdu / RTL ---------- */
html[dir="rtl"] body { font-family: "Noto Sans Arabic", "Noto Nastaliq Urdu", var(--font); line-height: 1.7; }
html[dir="rtl"] .card-title, html[dir="rtl"] .brand-title, html[dir="rtl"] .btn, html[dir="rtl"] .tab { font-family: "Noto Nastaliq Urdu", "Noto Sans Arabic", var(--font); line-height: 1.9; }
html[dir="rtl"] .card-title { font-size: 18px; }
html[dir="rtl"] input[type="number"], html[dir="rtl"] input[type="date"] { direction: ltr; }
html[dir="rtl"] .table th, html[dir="rtl"] .table td { text-align: right; }
html[dir="rtl"] .table th.num, html[dir="rtl"] .table td.num { text-align: left; }
html[dir="rtl"] .progress-marker { left: auto; right: 92.5%; }
html[dir="rtl"] .progress-scale { direction: rtl; }
html[dir="rtl"] .field, html[dir="rtl"] .hint, html[dir="rtl"] .muted { font-size: 14px; }
