:root {
  --bg: #0f172a;
  --bg-soft: #111827;
  --card: #1f2937;
  --line: #334155;
  --text: #e5e7eb;
  --muted: #94a3b8;
  --primary: #22d3ee;
  --green: #22c55e;
  --yellow: #eab308;
  --red: #ef4444;
}
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  background: radial-gradient(circle at 10% 0%, #172554, var(--bg));
  color: var(--text);
}
.topbar {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: start;
  padding: 1rem 2rem;
  position: sticky;
  top: 0;
  z-index: 40;
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(10px);
  background: rgba(15, 23, 42, 0.9);
}
h1 { margin: 0; }
.subtitle { margin: 0.2rem 0 0; color: var(--muted); }
.global-search-wrap { position: relative; width: min(56vw, 700px); }
.top-actions { display: flex; align-items: center; gap: .5rem; }
#authBox { display: flex; align-items: center; gap: .4rem; }
.denied { border: 1px solid #7f1d1d; background: #1f1117; color: #fecaca; }
input, select, button, textarea {
  width: 100%;
  border: 1px solid var(--line);
  background: #0b1223;
  color: var(--text);
  border-radius: 10px;
  padding: 0.55rem 0.7rem;
}
#globalSearch { padding: .72rem .9rem; font-size: 1rem; }
input:focus, select:focus, button:focus, textarea:focus {
  outline: 2px solid var(--primary);
}
button {
  width: auto;
  cursor: pointer;
  background: linear-gradient(120deg, #0891b2, #2563eb);
  border: none;
  transition: transform .15s ease;
}
button:hover { transform: translateY(-1px); }
main { padding: 1rem 2rem 2rem; }
.tabs {
  display: flex;
  gap: .45rem;
  padding: .8rem 2rem;
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 84px;
  z-index: 25;
  background: rgba(15, 23, 42, 0.95);
}
.tab {
  padding: .45rem .7rem;
  border: 1px solid #38bdf8;
  border-radius: 999px;
  cursor: pointer;
  color: #e0f2fe;
  background: #0b2545;
  font-weight: 600;
}
.tab.active {
  color: var(--text);
  border-color: var(--primary);
  box-shadow: 0 0 20px #22d3ee55 inset;
}
.panel { display: none; }
.panel.active { display: block; animation: enter .35s ease; }
@keyframes enter { from { opacity: 0; transform: translateY(8px);} }
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: .8rem;
}
.card {
  background: linear-gradient(165deg, #111827, #1e293b);
  border: 1px solid #334155;
  border-radius: 14px;
  padding: .8rem;
}
.stat .value { font-size: 1.35rem; font-weight: 700; margin: .2rem 0; }
.muted { color: var(--muted); }
.row { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; }
.field-block { display: flex; flex-direction: column; gap: .25rem; min-width: 180px; flex: 1; }
.field-block label { font-size: .8rem; color: var(--muted); }
.table-wrap { overflow-x: auto; }
table {
  width: 100%;
  border-collapse: collapse;
  margin-top: .7rem;
  min-width: 780px;
}
th, td {
  text-align: left;
  padding: .55rem;
  border-bottom: 1px solid #334155;
  vertical-align: top;
}
th { color: #cbd5e1; }
.badge { padding: .15rem .5rem; border-radius: 999px; font-size: .8rem; }
.badge.green { background: #14532d; color: #bbf7d0; }
.badge.yellow { background: #713f12; color: #fef08a; }
.badge.red { background: #7f1d1d; color: #fecaca; }
.two-col {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: .8rem;
}
@media (max-width: 960px) {
  .two-col { grid-template-columns: 1fr; }
  .topbar { flex-direction: column; }
  .tabs { top: 114px; overflow-x:auto; }
  .global-search-wrap { width: 100%; }
  .top-actions { width: 100%; justify-content: space-between; }
  main { padding: .9rem 1rem 1.2rem; }
  .tabs { padding: .7rem 1rem; }
  .row > input,
  .row > select,
  .row > textarea,
  .row > button,
  .row > .field-block,
  .row > div[style*="min-width"] {
    min-width: 100% !important;
    width: 100%;
    flex: 1 1 100%;
  }
  .actions button { width: 100%; }
  table { min-width: 640px; }
}

@media (max-width: 640px) {
  .topbar { padding: .85rem 1rem; }
  .tabs { top: 152px; }
  h1 { font-size: 1.25rem; }
  .subtitle { font-size: .86rem; }
  #globalSearch { font-size: .95rem; padding: .62rem .78rem; }
  .tab { white-space: nowrap; }
  .card { padding: .65rem; }
  th, td { padding: .45rem; font-size: .88rem; }
  table { min-width: 560px; }
  .suggestion-item { grid-template-columns: 78px 1fr; }
  .modal-box { width: min(96vw, 98vw); padding: .75rem; }
}
.suggestions {
  position: absolute;
  top: 105%;
  left: 0;
  right: 0;
  background: #0f172a;
  border: 1px solid #334155;
  border-radius: 14px;
  max-height: 260px;
  overflow: auto;
  display: none;
}
.suggestions.show { display: block; }
.suggestion-item {
  padding: .65rem .75rem;
  border-bottom: 1px solid #1e293b;
  display: grid;
  grid-template-columns: 95px 1fr;
  gap: .55rem;
  align-items: center;
}
.suggestion-item:hover { background: #0b1223; }
.suggestion-item:last-child { border-bottom: none; }
.s-type { color: #7dd3fc; font-weight: 700; text-transform: capitalize; }
.s-main { color: #e2e8f0; font-weight: 600; }
.s-sub { color: #94a3b8; font-size: .82rem; }
.multi-trend-chart {
  position: relative;
  height: 300px;
  padding: .5rem .7rem .4rem;
  background: linear-gradient(180deg, #0b1223, #0f172a);
  border: 1px solid #334155;
  border-radius: 14px;
  overflow: hidden;
}
.multi-trend-chart svg { width: 100%; height: 245px; }
.trend-grid { stroke: #334155; stroke-width: .45; opacity: .7; }
.trend-line {
  fill: none;
  stroke: var(--line-color, #a855f7);
  stroke-width: 1.1;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: var(--line-len, 160);
  stroke-dashoffset: var(--line-len, 160);
  animation: trendDraw 3.2s cubic-bezier(.16,1,.3,1) forwards;
  animation-delay: calc(var(--idx, 0) * .2s);
}
.trend-line[data-idx="0"] { --idx: 0; }
.trend-line[data-idx="1"] { --idx: 1; }
.trend-line[data-idx="2"] { --idx: 2; }
.trend-line[data-idx="3"] { --idx: 3; }
.trend-line[data-idx="4"] { --idx: 4; }
.trend-point {
  fill: var(--point-color, #fff);
  stroke: #0f172a;
  stroke-width: .35;
  opacity: 0;
  animation: trendPointIn .8s ease forwards;
  animation-delay: 2.2s;
}
.trend-point:hover { r: 1.4; }
.trend-x-axis {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: .3rem;
  margin-top: -.1rem;
  padding: 0 .3rem;
}
.trend-x-axis span { text-align: center; font-size: .74rem; color: var(--muted); }
.trend-legend {
  display: flex;
  gap: .7rem;
  flex-wrap: wrap;
  margin-top: .45rem;
  padding: 0 .2rem;
  font-size: .75rem;
  color: var(--muted);
}
.trend-legend i {
  display: inline-block;
  width: .62rem;
  height: .62rem;
  border-radius: 50%;
  margin-right: .35rem;
  vertical-align: -1px;
  background: var(--dot, #a855f7);
}
@keyframes trendDraw { to { stroke-dashoffset: 0; } }
@keyframes trendPointIn { from { opacity: 0; transform: scale(.7); } to { opacity: 1; transform: scale(1); } }
.pie {
  --pct: 50;
  width: 200px;
  aspect-ratio: 1;
  border-radius: 50%;
  background: conic-gradient(#22c55e calc(var(--pct) * 1%), #ef4444 0);
  margin-inline: auto;
  position: relative;
  transform: rotate(-90deg);
  animation: pieLoad 2s ease forwards;
}
.mini-pie-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: .6rem; }
.mini-pie-card { text-align: center; }
.mini-pie { width: 86px; aspect-ratio: 1; border-radius: 50%; margin: 0 auto .3rem; background: conic-gradient(var(--pie-color, #22c55e) calc(var(--pct) * 1%), var(--pie-rest, #1e293b) 0); position: relative; transform: rotate(-90deg); animation: pieLoad 2.2s ease forwards; }
.mini-pie::after { content: ""; position: absolute; inset: 24%; border-radius: 50%; background: #0f172a; }
.mini-pie-label { font-size: .75rem; color: var(--muted); }

.bar-chart { display: grid; grid-template-columns: repeat(7, 1fr); gap: .35rem; align-items: end; height: 130px; margin-top: .5rem; }
.bar { background: linear-gradient(180deg,#22d3ee,#3b82f6); border-radius: 8px 8px 2px 2px; position: relative; min-height: 8px; }
.bar span { position: absolute; bottom: -1.1rem; left: 50%; transform: translateX(-50%); font-size: .68rem; color: var(--muted); }
.pie::after {
  content: "";
  position: absolute;
  inset: 22%;
  border-radius: 50%;
  background: #0f172a;
}
@keyframes pieLoad {
  from { clip-path: circle(0% at 50% 50%); opacity:.35; }
  to { clip-path: circle(75% at 50% 50%); opacity:1; }
}
.chart-tooltip {
  position: absolute;
  pointer-events: none;
  background: rgba(15, 23, 42, .95);
  border: 1px solid #334155;
  color: #e2e8f0;
  padding: .25rem .45rem;
  border-radius: 8px;
  font-size: .75rem;
  display: none;
  z-index: 2;
}
.chart-tooltip.show { display: block; }
.small { font-size: .85rem; }
hr { border: none; border-top: 1px solid #334155; margin: 1rem 0; }
.actions { display: flex; gap: .4rem; flex-wrap: wrap; }
.print-area { background: white; color: black; padding: 1rem; margin-top: .6rem; border-radius: 8px; }
.inline-input { max-width: 140px; }
.link-btn {
  background: transparent;
  border: 1px solid #3b82f6;
  color: #93c5fd;
  padding: .25rem .55rem;
}

#modalRoot { position: fixed; inset: 0; pointer-events: none; z-index: 120; }
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(2, 6, 23, .65);
  backdrop-filter: blur(3px);
  display: grid;
  place-items: center;
  pointer-events: all;
}
.modal-box {
  width: min(95vw, 980px);
  background: linear-gradient(165deg, #0f172a, #1e293b);
  border: 1px solid #475569;
  border-radius: 16px;
  padding: 1rem;
  box-shadow: 0 20px 45px rgba(2, 6, 23, .5);
}
.modal-large #modalBody { max-height: 72vh; overflow: auto; }
.modal-title { margin: 0 0 .4rem; }
.modal-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: .8rem; }
.btn-ghost { background: #1e293b; border: 1px solid #475569; }

.invoice-template {
  width: 210mm;
  background: white;
  color: #0f172a;
  border-radius: 12px;
  padding: 12mm;
  margin-top: .7rem;
}
.invoice-template h2 { margin-top: 0; }
.invoice-template .meta { display: flex; justify-content: space-between; gap: .7rem; flex-wrap: wrap; }
.invoice-template table { min-width: 100%; margin-top: .5rem; }
.invoice-template th, .invoice-template td { border: 1px solid #cbd5e1; }
.invoice-template-modern {
  position: relative;
  overflow: hidden;
  border-radius: 10px;
  color: #1e293b;
  -webkit-print-color-adjust: exact;
  print-color-adjust: exact;
}
.invoice-head,
.invoice-meta-row,
.invoice-lines,
.invoice-summary-grid,
.invoice-signature-row,
.invoice-note {
  position: relative;
  z-index: 1;
}
.invoice-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
}
.invoice-brand-name {
  margin: 0 0 5px;
  font-size: 24px;
  color: #0284c7;
}
.invoice-doc-title-wrap { text-align: right; }
.invoice-doc-title {
  margin: 0 0 6px;
  color: #0284c7;
  font-size: 44px;
  letter-spacing: 1.5px;
  line-height: 1;
}
.invoice-meta-row {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  margin-top: 20px;
}
.invoice-meta-right { text-align: right; }
.invoice-lines {
  width: 100%;
  border-collapse: collapse;
  margin-top: 16px;
}
.invoice-lines th,
.invoice-lines td {
  padding: 10px 8px;
  border: none;
  border-bottom: 1px solid #cbd5e1;
  font-size: 13px;
  text-align: left;
}
.invoice-lines th {
  color: #0284c7;
  font-size: 12px;
  letter-spacing: .3px;
  text-transform: uppercase;
}
.invoice-summary-grid {
  display: grid;
  grid-template-columns: 1fr 230px;
  gap: 18px;
  margin-top: 20px;
}
.invoice-totals {
  border: 1px solid #cbd5e1;
  padding: 10px 12px;
  border-radius: 8px;
  background: #f8fafc;
}
.invoice-totals div {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}
.invoice-totals .grand {
  font-size: 18px;
  border-top: 1px solid #cbd5e1;
  padding-top: 8px;
  margin-top: 8px;
  margin-bottom: 0;
}
.invoice-signature-row {
  display: flex;
  justify-content: space-between;
  margin-top: 26px;
  font-size: 13px;
}
.invoice-sign-text {
  color: #0284c7;
  font-weight: 600;
}
@media (max-width: 900px) {
  .invoice-doc-title { font-size: 36px; }
  .invoice-summary-grid { grid-template-columns: 1fr; }
  .invoice-meta-right { text-align: left; }
}

.flash { animation: flashRow 1.2s ease; }
@keyframes flashRow { 0%{background:#0ea5e955;} 100%{background:transparent;} }

body.light-mode {
  --bg: #f7f9fc;
  --bg-soft: #eef3fa;
  --card: #ffffff;
  --line: #d7e0ee;
  --text: #111827;
  --muted: #475569;
  --primary: #3b82f6;
  --green: #16a34a;
  --yellow: #ca8a04;
  --red: #dc2626;
  background: radial-gradient(circle at 10% 0%, #f0f5ff, #f8fbff 48%, #f3f6fb 100%);
}
body.light-mode .topbar,
body.light-mode .tabs {
  background: rgba(255, 255, 255, 0.92);
  border-color: #d9e3f2;
  box-shadow: 0 8px 20px rgba(59, 130, 246, 0.06);
}
body.light-mode .tab { color: #64748b; border-color: #d9e3f2; background: #f8fbff; }
body.light-mode .tab.active { color: #0f172a; border-color: #93c5fd; box-shadow: 0 0 0 1px #bfdbfe inset; }
body.light-mode input,
body.light-mode textarea,
body.light-mode select {
  background: #ffffff;
  color: #111827;
  border-color: #cfd9e8;
}
body.light-mode button { background: linear-gradient(120deg, #3b82f6, #2563eb); color: #fff; }
body.light-mode .btn-ghost,
body.light-mode .link-btn { background: #eef4ff; color: #1e3a8a; border: 1px solid #bfdbfe; }
body.light-mode .card {
  background: linear-gradient(165deg, #ffffff, #f4f7fc);
  border-color: #d7e0ee;
  box-shadow: 0 10px 24px rgba(30, 64, 175, 0.08);
}
body.light-mode th { color: #334155; }
body.light-mode td, body.light-mode th, body.light-mode hr { border-color: #d7e0ee; }
body.light-mode .multi-trend-chart { background: linear-gradient(180deg, #f8fbff, #eef4ff); border-color: #cfd9e8; }
body.light-mode .trend-grid { stroke: #cbd5e1; opacity: .9; }
body.light-mode .trend-point { stroke: #ffffff; }
body.light-mode .mini-pie::after,
body.light-mode .pie::after { background: #ffffff; }
body.light-mode .suggestions { background: #ffffff; border-color: #d7e0ee; box-shadow: 0 12px 28px rgba(15, 23, 42, 0.08); }
body.light-mode .suggestion-item { border-bottom-color: #e2e8f0; }
body.light-mode .suggestion-item:hover { background: #f1f5ff; }
body.light-mode .chart-tooltip { background: rgba(255, 255, 255, 0.98); color: #0f172a; border-color: #cbd5e1; }
body.light-mode .badge.green { background: #dcfce7; color: #166534; }
body.light-mode .badge.yellow { background: #fef9c3; color: #854d0e; }
body.light-mode .badge.red { background: #fee2e2; color: #991b1b; }
body.light-mode .modal-backdrop { background: rgba(15, 23, 42, 0.25); }
body.light-mode .modal-box {
  background: linear-gradient(165deg, #ffffff, #f4f7fc);
  color: #0f172a;
  border-color: #d7e0ee;
  box-shadow: 0 18px 40px rgba(15, 23, 42, 0.16);
}

#allyRoot {
  position: fixed;
  right: 18px;
  top: 86px;
  z-index: 110;
}
.ally-toggle {
  width: 56px;
  height: 56px;
  border-radius: 999px;
  border: 1px solid rgba(148, 163, 184, .45);
  padding: 0;
  display: grid;
  place-items: center;
  background: radial-gradient(circle at center, #0f172a 35%, #020617 100%);
  box-shadow: 0 14px 26px rgba(2, 132, 199, .35);
  transition: transform .32s ease, box-shadow .32s ease;
  cursor: pointer;
}
.maze-icon {
  width: 36px;
  height: 36px;
  border-radius: 999px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Cg fill='none' stroke='%23d1d5db' stroke-width='6' stroke-linecap='round'%3E%3Ccircle cx='50' cy='50' r='45'/%3E%3Ccircle cx='50' cy='50' r='30'/%3E%3Cpath d='M50 5v16M95 50H79M5 50h16M50 95V79M26 26l8 8M74 26l-8 8M74 74l-8-8M26 74l8-8'/%3E%3C/g%3E%3Cg fill='none' stroke='%2394a3b8' stroke-width='5'%3E%3Cpath d='M21 14h20M59 14h20M85 22v18M85 60v20M79 86H58M42 86H21M14 78V60M14 40V21M35 35h30M65 35v16M65 66H35M35 66V52M35 46h10'/%3E%3C/g%3E%3C/svg%3E");
  background-size: cover;
  box-shadow: 0 0 18px rgba(14, 165, 233, .25);
}
.ally-avatar { flex: 0 0 auto; }
.ally-toggle:hover {
  transform: translateY(-2px) scale(1.04);
  box-shadow: 0 18px 30px rgba(2, 132, 199, .45);
}
.ally-toggle:hover .maze-icon {
  box-shadow: 0 0 26px rgba(56, 189, 248, .55), 0 0 46px rgba(14, 165, 233, .3);
  filter: brightness(1.12);
}
.ally-panel {
  position: fixed;
  right: 14px;
  top: 146px;
  bottom: 14px;
  width: min(95vw, 560px);
  background: linear-gradient(165deg, #111827, #1e293b);
  border: 1px solid #334155;
  border-radius: 18px;
  padding: .75rem;
  display: none;
  box-shadow: 0 22px 50px rgba(2, 6, 23, .55);
  overflow: hidden;
}
.ally-panel.open { display: flex; flex-direction: column; }
.ally-head { display: flex; justify-content: space-between; gap: .5rem; align-items: flex-start; margin-bottom: .6rem; }
.ally-title { display: flex; gap: .55rem; align-items: center; }
.ally-head .btn-ghost { padding: .15rem .5rem; min-width: auto; }
.ally-messages {
  flex: 1;
  min-height: 260px;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: .5rem;
  padding: .5rem;
  border: 1px solid #334155;
  border-radius: 12px;
  background: #0b1223;
}
.ally-msg {
  font-size: .93rem;
  line-height: 1.42;
  padding: .58rem .72rem;
  border-radius: 14px;
  max-width: 88%;
  border: 1px solid transparent;
}
.ally-line { margin-bottom: .2rem; }
.ally-msg-user {
  background: #082f49;
  border-color: rgba(125, 211, 252, .28);
  align-self: flex-end;
  text-align: left;
}
.ally-msg-ally {
  background: #1f2937;
  border-color: rgba(148, 163, 184, .22);
  align-self: flex-start;
}
.ally-action-box {
  margin-top: .45rem;
  padding-top: .4rem;
  border-top: 1px dashed rgba(148, 163, 184, .35);
}
.ally-form { display: grid; grid-template-columns: 1fr auto; gap: .5rem; margin-top: .65rem; }
.ally-form input { padding: .7rem .8rem; }
@media (max-width: 920px) {
  #allyRoot { top: 74px; right: 12px; }
  .ally-panel {
    right: 8px;
    left: 8px;
    top: 134px;
    bottom: 8px;
    width: auto;
  }
  .ally-messages { min-height: 120px; }
}
body.light-mode .ally-panel { background: linear-gradient(165deg, #ffffff, #f4f7fc); border-color: #d7e0ee; }
body.light-mode .ally-messages { background: #ffffff; border-color: #d7e0ee; }
body.light-mode .ally-msg-user { background: #e0f2fe; }
body.light-mode .ally-msg-ally { background: #eef2ff; }


.invoice-item-desc { font-size: .8em; font-style: italic; color: #475569; margin-top: 2px; }
.item-desc-preview { font-size: .8em; font-style: italic; color: var(--muted); margin-top: 2px; }
