:root {
  --bg: #f4efe4;
  --bg-2: #efe4cc;
  --surface: #fffaf1;
  --surface-2: #f7edd8;
  --ink: #1d1408;
  --muted: #6d5a3a;
  --line: #d9c7a0;
  --gold: #c1962c;
  --gold-2: #8d6a12;
  --accent: #2e6b4a;
  --danger: #9b2c2c;
  --ok: #1f7a45;
  --wait: #b7791f;
  --shadow: 0 10px 28px rgba(29, 20, 8, 0.12);
  --sidebar: #24180b;
  --sidebar-ink: #f6e7c3;
  --print-line: #222;
}

html[data-theme="dark"] {
  --bg: #16110b;
  --bg-2: #21180f;
  --surface: #24190f;
  --surface-2: #2d2013;
  --ink: #f6ead0;
  --muted: #cbb48a;
  --line: #4a3a22;
  --gold: #e0b64a;
  --gold-2: #f1d27a;
  --accent: #6dcaa0;
  --danger: #f08a8a;
  --ok: #7ad39a;
  --wait: #e0b64a;
  --shadow: 0 10px 28px rgba(0, 0, 0, 0.35);
  --sidebar: #100c08;
  --sidebar-ink: #f6e7c3;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; min-height: 100%; }
[hidden] { display: none !important; }
body {
  font-family: "Cairo", Tahoma, sans-serif;
  background: radial-gradient(circle at top, var(--bg-2), var(--bg));
  color: var(--ink);
}
body[data-view="login"] #appView,
body[data-view="login"] #printView,
body[data-view="app"] #loginView,
body[data-view="app"] #printView,
body[data-view="print"] #loginView,
body[data-view="print"] #appView { display: none !important; }

button, input, select, textarea { font-family: inherit; }
input, select, textarea {
  width: 100%;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
}
label { display: block; font-size: 13px; font-weight: 700; margin-bottom: 10px; }
h1, h2, h3, p { margin-top: 0; }

.btn {
  border: 1px solid transparent;
  border-radius: 9px;
  padding: 8px 13px;
  font-weight: 700;
  cursor: pointer;
  background: var(--surface-2);
  color: var(--ink);
}
.btn.gold { background: var(--gold); color: #1d1408; }
.btn.ghost { background: transparent; border-color: currentColor; }
.btn.danger { background: var(--danger); color: #fff; }
.btn.ok { background: var(--ok); color: #fff; }
.btn.block { width: 100%; }
.btn.small { padding: 5px 9px; font-size: 12px; }
.btn.icon { min-width: 42px; }

.toast {
  position: fixed;
  top: 16px;
  inset-inline-start: 50%;
  transform: translateX(-50%);
  background: var(--sidebar);
  color: var(--sidebar-ink);
  padding: 10px 16px;
  border-radius: 10px;
  z-index: 50;
  box-shadow: var(--shadow);
}

.login-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
}
.login-card {
  width: min(420px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 28px;
  box-shadow: var(--shadow);
  text-align: center;
}
.emblem {
  border-radius: 50%;
  overflow: hidden;
  flex-shrink: 0;
  box-shadow: 0 0 0 3px #2a1c0a, 0 0 0 5px var(--gold), 0 8px 18px rgba(0,0,0,.28);
  background: #1a1208;
}
.emblem img { display: block; width: 100%; height: 100%; }
.emblem-lg { width: 196px; height: 196px; margin: 0 auto 14px; }
.emblem-sm { width: 92px; height: 92px; }
.emblem-print { width: 102px; height: 102px; }
.login-card h1 { font-family: "Amiri", serif; margin-bottom: 4px; }
.motto {
  margin: 0 0 8px;
  color: var(--gold);
  font-weight: 800;
  letter-spacing: 0.6px;
  font-size: 14px;
}
.login-sub, .hint { color: var(--muted); font-size: 13px; }
.login-card label { text-align: right; }

.app { display: grid; grid-template-columns: 250px 1fr; min-height: 100vh; }
.sidebar {
  background: var(--sidebar);
  color: var(--sidebar-ink);
  display: flex;
  flex-direction: column;
  padding: 16px 12px;
}
.side-brand { display: flex; gap: 10px; align-items: center; margin-bottom: 18px; }
.side-brand .emblem { margin: 0; }
.side-brand small { display: block; color: #d8c394; }
.side-brand .motto { color: var(--gold); margin-top: 2px; font-size: 12px; }
.title-block { flex: 1; }
.title-block h2 { margin: 0; }
.title-block .motto { margin: 2px 0 0; font-size: 12px; }
#mainNav { display: flex; flex-direction: column; gap: 6px; flex: 1; }
#mainNav button {
  background: transparent;
  color: inherit;
  border: 0;
  text-align: right;
  padding: 10px 12px;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 700;
}
#mainNav button.active, #mainNav button:hover { background: rgba(193, 150, 44, 0.22); }
.side-foot { font-size: 12px; }
.side-foot span { display: block; margin-bottom: 8px; color: #d8c394; }

.workspace { min-width: 0; }
.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface) 86%, transparent);
  position: sticky;
  top: 0;
  z-index: 5;
}
.topbar h2 { margin: 0; flex: 1; font-size: 20px; }
#btnMenu { display: none; }
.pages { padding: 18px; }

.grid { display: grid; gap: 12px; }
.grid.kpis { grid-template-columns: repeat(4, 1fr); }
.grid.two { grid-template-columns: 1fr 1fr; }
.grid.three { grid-template-columns: 1fr 1fr 1fr; }
.card, .table-wrap {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px;
  box-shadow: var(--shadow);
}
.kpi b { display: block; font-size: 24px; margin-top: 6px; }
.kpi span { color: var(--muted); font-size: 13px; }

.toolbar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar input, .toolbar select { width: auto; min-width: 160px; }

table.data { width: 100%; border-collapse: collapse; font-size: 13px; }
table.data th, table.data td {
  border-bottom: 1px solid var(--line);
  padding: 8px 6px;
  text-align: right;
}
table.data th { color: var(--muted); }
.badge {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
}
.badge.wait { background: #f6e2b8; color: #7a4d07; }
.badge.work { background: #d7e8ff; color: #1d4f91; }
.badge.done { background: #d8f3e3; color: #146338; }

.modal-bg {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.45);
  display: grid;
  place-items: center;
  z-index: 30;
  padding: 16px;
}
.modal {
  width: min(720px, 100%);
  max-height: 90vh;
  overflow: auto;
  background: var(--surface);
  border-radius: 14px;
  padding: 16px;
}
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }

.invoice-form .section { margin-bottom: 14px; }
.money { font-variant-numeric: tabular-nums; font-weight: 800; }
.warn { color: var(--danger); font-size: 13px; }

body[data-view="print"] {
  background: #3a2a14;
  min-height: 100vh;
  padding: 16px 12px 28px;
}
.print-toolbar {
  width: min(210mm, 100%);
  margin: 0 auto 12px;
  display: flex;
  gap: 8px;
  justify-content: center;
}
.print-toolbar .btn.ghost { color: #f6e7c3; border-color: #f6e7c3; }
.print-sheet {
  width: min(210mm, 100%);
  min-height: 297mm;
  margin: 0 auto;
  background: #fff;
  color: #111;
  padding: 9mm 8mm;
  font-size: 11px;
  box-shadow: 0 16px 40px rgba(0,0,0,.4);
  border: 1px solid #c9ae6c;
}
.print-head {
  display: grid;
  grid-template-columns: 1.1fr auto 1.1fr;
  gap: 6px;
  align-items: center;
  border-bottom: 2px solid #8d6a12;
  padding-bottom: 6px;
}
.print-meta {
  display: flex;
  gap: 8px;
  align-items: center;
  font-size: 11px;
}
.print-meta strong { display: block; font-size: 13px; }
.print-meta .motto { display: block; color: #8d6a12; font-size: 10px; font-weight: 800; }
.emblem-print { width: 54px; height: 54px; }
.print-title { text-align: center; }
.print-head h1 {
  margin: 0;
  font-family: "Amiri", serif;
  font-size: 22px;
  line-height: 1.1;
}
.print-no {
  display: inline-block;
  margin-top: 3px;
  background: #1d1408;
  color: #f6e2a8;
  border-radius: 999px;
  padding: 1px 10px;
  font-size: 11px;
  font-weight: 800;
}
.print-fr { text-align: left; direction: ltr; font-size: 10px; line-height: 1.35; }
.print-box {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 0;
  border: 1px solid #111;
  margin: 7px 0;
}
.print-box div {
  padding: 4px 6px;
  border-inline-start: 1px solid #d9c7a0;
}
.print-box span { display: block; color: #6d5a3a; font-size: 9px; }
.print-box b { display: block; font-size: 11px; }
.print-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.print-table { width: 100%; border-collapse: collapse; }
.print-table th, .print-table td { border: 1px solid #111; padding: 3px 6px; font-size: 11px; }
.print-table th { background: #efe2c4; font-size: 11px; }
.print-table td:last-child { text-align: left; font-variant-numeric: tabular-nums; font-weight: 800; width: 34%; }
.share-table th, .share-table td { text-align: center; }
.share-table .share-name { text-align: right; font-weight: 700; }
.share-table .share-pct {
  width: 22%;
  font-weight: 800;
  font-size: 12px;
  letter-spacing: 0.3px;
}
.share-table .share-amt { width: 30%; }
.print-bar, .print-totals {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  border: 1px solid #111;
  margin-top: 6px;
}
.print-totals { grid-template-columns: 1fr 1fr; }
.print-bar div, .print-totals div {
  padding: 5px 7px;
  border-inline-start: 1px solid #111;
}
.print-bar span, .print-totals span { display: block; font-size: 9px; color: #6d5a3a; }
.print-bar b, .print-totals b { font-size: 13px; }
.print-totals .net { background: #efe2c4; }
.print-foot {
  text-align: center;
  margin: 8px 0 0;
  font-size: 10px;
  border-top: 1px dashed #c9ae6c;
  padding-top: 6px;
}

@media (max-width: 980px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 250px; transform: translateX(110%); transition: .2s; z-index: 20; }
  html[dir="rtl"] .sidebar { inset: 0 0 0 auto; }
  .sidebar.open { transform: none; }
  #btnMenu { display: inline-block; }
  .grid.kpis, .grid.three, .grid.two { grid-template-columns: 1fr 1fr; }
  body[data-view="print"] .print-grid,
  body[data-view="print"] .print-box,
  body[data-view="print"] .print-head { grid-template-columns: 1fr; }
}

@media (max-width: 640px) {
  .grid.kpis, .grid.three, .grid.two { grid-template-columns: 1fr; }
  .pages { padding: 10px; }
}

@page { size: A4 portrait; margin: 8mm; }
@media print {
  * {
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
    box-shadow: none !important;
    text-shadow: none !important;
  }
  html, body {
    background: #fff !important;
    color: #000 !important;
    padding: 0 !important;
    margin: 0 !important;
  }
  body[data-view="print"] { background: #fff !important; padding: 0 !important; }
  #loginView, #appView, .toast, .print-toolbar { display: none !important; }
  #printView { display: block !important; }
  .print-sheet {
    box-shadow: none !important;
    border: 2px solid #000;
    width: auto;
    min-height: auto;
    padding: 4mm;
    margin: 0;
    color: #000;
    background: #fff;
    font-size: 11px;
  }
  .print-head {
    grid-template-columns: 1.15fr auto 1.15fr !important;
    border-bottom: 2px solid #000;
    align-items: center;
    padding-bottom: 4px;
    margin-bottom: 4px;
  }
  .print-meta strong, .print-meta .motto, .print-head h1, .print-fr, .print-box span, .print-bar span, .print-totals span, .print-foot {
    color: #000 !important;
  }
  .print-meta .motto { font-weight: 800; }
  .print-head h1 { font-size: 20px; }
  .print-no {
    background: #fff !important;
    color: #000 !important;
    border: 1.5px solid #000;
    border-radius: 0;
    padding: 1px 8px;
  }
  .emblem, .emblem-print {
    box-shadow: none !important;
    border: 1.5px solid #000;
    background: #fff;
    filter: grayscale(1) contrast(1.35);
  }
  .print-box {
    grid-template-columns: repeat(5, 1fr) !important;
    border: 1.5px solid #000;
    margin: 5px 0;
  }
  .print-box div { border-inline-start: 1px solid #000; padding: 3px 5px; }
  .print-grid { grid-template-columns: 1fr 1fr !important; gap: 5px; }
  .print-table th, .print-table td {
    border: 1px solid #000;
    padding: 2px 5px;
    background: #fff !important;
    color: #000 !important;
  }
  .print-table th {
    background: #fff !important;
    font-weight: 800;
    border-bottom: 2px solid #000;
  }
  .print-bar {
    border: 1.5px solid #000;
    margin-top: 5px;
    grid-template-columns: repeat(3, 1fr) !important;
  }
  .print-totals {
    border: 1.5px solid #000;
    margin-top: 5px;
    grid-template-columns: 1fr 1fr !important;
  }
  .share-table .share-pct {
    font-size: 12px;
    font-weight: 800;
  }
  .print-bar div, .print-totals div {
    border-inline-start: 1px solid #000;
    padding: 4px 6px;
    background: #fff !important;
  }
  .print-totals .net {
    background: #fff !important;
    outline: 2px solid #000;
    outline-offset: -2px;
    font-weight: 800;
  }
  .print-foot {
    border-top: 1px solid #000;
    margin-top: 6px;
    padding-top: 4px;
    color: #000;
  }
}
