* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
        Arial, "Apple Color Emoji", "Segoe UI Emoji", sans-serif;
  background: #f5f6f8;
  color: #1f2330;
}
code { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 1rem;
  padding: .75rem 1.25rem;
  background: #1f2330; color: #fff;
  border-bottom: 1px solid #0d0f15;
}
.topbar .brand { font-size: 1.05rem; }
.topbar .brand strong { color: #4cc2ff; letter-spacing: .5px; }
.tabs { display: flex; gap: .25rem; margin-left: 1rem; }
.tabs .tab {
  background: transparent; border: 0; color: #c7cbd6;
  padding: .45rem .85rem; border-radius: 6px; cursor: pointer;
  font: inherit;
}
.tabs .tab:hover { background: #2d3142; color: #fff; }
.tabs .tab.active { background: #4cc2ff; color: #0d0f15; font-weight: 600; }
.stats {
  margin-left: auto; font-size: .85rem; color: #c7cbd6;
  display: flex; gap: 1rem;
}
.stats span b { color: #fff; }

main { padding: 1.25rem; max-width: 1500px; margin: 0 auto; }

.panel {
  background: #fff; border: 1px solid #e2e6ee; border-radius: 8px;
  padding: 1.25rem; margin-bottom: 1.25rem;
  box-shadow: 0 1px 2px rgba(20, 30, 50, .03);
}
.panel.hidden { display: none; }
.panel-head { margin-bottom: 1rem; }
.panel-head h2 { margin: 0 0 .25rem; font-size: 1.15rem; }
.muted { color: #71788a; }

.row {
  display: flex; align-items: center; gap: .75rem; flex-wrap: wrap;
  margin-bottom: .75rem;
}
.row input[type="text"], .row input:not([type]) {
  padding: .4rem .6rem; border: 1px solid #d0d4de; border-radius: 6px;
  min-width: 320px; font: inherit;
}
.check { display: flex; align-items: center; gap: .35rem; }

input[type="text"], input:not([type]), input[type="file"] {
  font: inherit;
}

button {
  font: inherit; cursor: pointer;
  background: #4cc2ff; color: #0d0f15; border: 0;
  border-radius: 6px; padding: .45rem .9rem; font-weight: 600;
}
button:hover { background: #29b1ff; }
button.danger { background: #fff; color: #b3261e; border: 1px solid #f1c5c1; font-weight: 500; }
button.danger:hover { background: #fdecea; }

.table-scroll { overflow: auto; border: 1px solid #e2e6ee; border-radius: 6px; }
.lookup-scroll { max-height: 60vh; }
table { border-collapse: collapse; width: 100%; font-size: .87rem; }
th, td {
  text-align: left; padding: .4rem .6rem; border-bottom: 1px solid #eef0f4;
  white-space: nowrap;
}
th {
  position: sticky; top: 0; background: #f7f8fb; z-index: 1;
  font-weight: 600; color: #2c3142;
}
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tr:hover td { background: #fafbfd; }

.pivot th, .pivot td { white-space: nowrap; }
.pivot td.row-label, .pivot th:first-child { font-weight: 600; background: #f7f8fb; position: sticky; left: 0; z-index: 2; }
.pivot td.num { font-variant-numeric: tabular-nums; }
.pivot tr.total td { background: #eef5fb; font-weight: 600; }
.pivot th.year { background: #1f2330; color: #fff; }
.pivot th.month { background: #2d3142; color: #c7cbd6; font-weight: 500; }
.pivot th.total { background: #4cc2ff; color: #0d0f15; }
.pivot td.total { background: #eef5fb; font-weight: 600; }

/* Visually separate years: thicker left border at year-start cells,
   plus a subtle tint on every other year's columns. */
.pivot th.year-start,
.pivot th.month.year-start,
.pivot td.year-start {
  border-left: 2px solid #1f2330 !important;
}
.pivot th.month.year-tint { background: #3a4358; }
.pivot tbody td.year-tint { background: #f4f6fb; }
.pivot tbody tr:hover td.year-tint { background: #eaeef7; }
.pivot tbody tr.total td.year-tint { background: #dde8f3; }
.pivot th.total.year-tint { background: #29b1ff; }

.upload {
  display: flex; gap: .75rem; align-items: center;
  padding: .9rem 1rem; border: 1px dashed #b9c0cc; border-radius: 8px;
  background: #fafbfd;
}
.upload button[disabled] { opacity: .5; cursor: not-allowed; }
.result { margin-top: .75rem; }
.result.ok    { color: #155724; background: #d4edda; padding: .6rem .8rem; border-radius: 6px; }
.result.err   { color: #842029; background: #f8d7da; padding: .6rem .8rem; border-radius: 6px; }
.result.progress-block { background: #f7f9fc; padding: .85rem 1rem; border: 1px solid #e2e6ee; border-radius: 8px; color: #1f2330; }

.progress {
  position: relative;
  width: 100%;
  background: #eaecef;
  border-radius: 999px;
  height: 22px;
  overflow: hidden;
  box-shadow: inset 0 1px 2px rgba(0,0,0,.05);
}
.progress-bar {
  background: linear-gradient(90deg, #4cc2ff, #29b1ff);
  height: 100%;
  width: 0;
  transition: width 0.18s ease-out;
}
.progress-bar.err { background: linear-gradient(90deg, #f5c2c0, #ee6e62); }
.progress-bar.ok  { background: linear-gradient(90deg, #6fdc8c, #2faa56); }
.progress-label {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: .82rem; font-weight: 600; color: #1f2330;
  letter-spacing: .2px;
  pointer-events: none;
}
.progress-meta {
  display: flex; justify-content: space-between;
  margin-top: .35rem; font-size: .8rem; color: #71788a;
  font-variant-numeric: tabular-nums;
}

.lookup-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1.5rem; }
@media (max-width: 1100px) { .lookup-grid { grid-template-columns: 1fr; } }
.lookup-form {
  display: grid; grid-template-columns: 2fr 1fr 1fr 1fr auto; gap: .5rem;
  margin-bottom: .75rem;
}
.lookup-form input {
  padding: .35rem .55rem; border: 1px solid #d0d4de; border-radius: 6px;
}
.badge {
  display: inline-block; padding: .05rem .45rem; border-radius: 10px;
  font-size: .75rem; font-weight: 600;
}
.badge.yes { background: #d4edda; color: #155724; }

/* ---- Formula columns (Excel cols AL..BC) ---- */
th.fcol {
  background: #fdf6dd !important;
  color: #5b4a08;
  white-space: nowrap;
}
th.fcol .fbadge {
  display: inline-block;
  background: #b8860b;
  color: #fff;
  font: 700 .68rem ui-monospace, "SF Mono", Menlo, monospace;
  padding: 1px 5px;
  border-radius: 3px;
  margin-right: 5px;
  letter-spacing: .3px;
  vertical-align: 1px;
}
td.fcol {
  background: #fffbe6;
}
tr:hover td.fcol { background: #fff5b8; }
tr.expanded td.fcol { background: #fff2a8; }

td.expander {
  width: 24px; text-align: center;
  cursor: pointer; user-select: none;
  color: #71788a;
}
td.expander::before {
  content: "\25B8"; /* ▸ */
  display: inline-block;
  transition: transform .15s ease;
  font-size: .9rem;
}
tr.expanded td.expander::before {
  transform: rotate(90deg);
  color: #b8860b;
}

tr.formula-row td {
  background: #f6f8fc;
  padding: .9rem 1.1rem;
  border-bottom: 2px solid #d8dde7;
}
.fdetails {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: .55rem .85rem;
}
.fcard {
  background: #fff;
  border: 1px solid #e2e6ee;
  border-left: 3px solid #b8860b;
  border-radius: 4px;
  padding: .45rem .6rem .55rem;
  display: flex; flex-direction: column; gap: .15rem;
}
.fcard .fhead {
  display: flex; align-items: center; gap: .4rem;
}
.fcard .fxl {
  background: #b8860b; color: #fff;
  font: 700 .65rem ui-monospace, "SF Mono", Menlo, monospace;
  padding: 1px 5px; border-radius: 3px;
  letter-spacing: .3px;
}
.fcard .flabel {
  font-weight: 600; font-size: .82rem; color: #1f2330;
}
.fcard .fvalue {
  font: 700 .9rem ui-monospace, "SF Mono", Menlo, monospace;
  color: #0d0f15;
  word-break: break-all;
  margin-top: .1rem;
}
.fcard .fvalue.empty { color: #b9c0cc; font-weight: 500; font-style: italic; }
.fcard .fdesc {
  font-size: .73rem; color: #71788a; line-height: 1.35;
}
.fcard .fexpr {
  margin-top: .25rem;
  font: .7rem/1.4 ui-monospace, "SF Mono", Menlo, monospace;
  color: #455161;
  background: #eef0f4;
  padding: 3px 6px; border-radius: 3px;
  white-space: pre-wrap;
  word-break: break-word;
}

/* ---- Orders controls: view toggle, filter row, pager, popover ---- */
.orders-controls { gap: .6rem; align-items: center; }
.view-toggle {
  display: inline-flex;
  border: 1px solid #d0d4de; border-radius: 6px; overflow: hidden;
}
.view-toggle button {
  background: #fff; color: #1f2330; font-weight: 500;
  border: 0; padding: .35rem .8rem; border-radius: 0;
}
.view-toggle button.active { background: #1f2330; color: #fff; }
.view-toggle button + button { border-left: 1px solid #d0d4de; }

.formula-help-btn {
  background: #fdf6dd; color: #5b4a08; border: 1px solid #e6d896;
  font-weight: 600; cursor: pointer;
}
.formula-help-btn .fbadge {
  background: #b8860b; color: #fff;
  font: 700 .72rem ui-monospace, "SF Mono", Menlo, monospace;
  padding: 1px 5px; border-radius: 3px; margin-right: 5px;
}
.muted-btn {
  background: #f7f8fb; color: #455161; border: 1px solid #d0d4de;
  font-weight: 500;
}

.formula-popover {
  position: fixed;
  z-index: 50;
  width: min(820px, 92vw);
  max-height: min(70vh, 600px);
  overflow: auto;
  background: #fff;
  border: 1px solid #d0d4de;
  border-radius: 8px;
  box-shadow: 0 12px 28px rgba(13,15,21,.18), 0 4px 8px rgba(13,15,21,.06);
  padding: .85rem 1rem;
}
.formula-popover.hidden { display: none; }
.formula-popover-head {
  display: flex; align-items: baseline; justify-content: space-between;
  margin-bottom: .55rem;
  border-bottom: 1px solid #eef0f4; padding-bottom: .4rem;
}

/* Sortable header */
th.sortable { cursor: pointer; user-select: none; position: sticky; }
th.sortable:hover { background: #eef2fb; }
th.sortable .sort-mark {
  font-size: .75rem; color: #b9c0cc; margin-left: 4px;
  display: inline-block;
}
th.sortable.sort-asc  .sort-mark::after  { content: "▲"; color: #1f2330; }
th.sortable.sort-desc .sort-mark::after  { content: "▼"; color: #1f2330; }
th.sortable.sort-none .sort-mark::after  { content: "↕"; color: #b9c0cc; }
th.fcol.sortable:hover { background: #f9f0c4; }

/* Filter row in thead */
tr.filter-row th {
  background: #f7f8fb;
  padding: .25rem .35rem;
  font-weight: 400;
  border-bottom: 1px solid #e2e6ee;
  position: sticky; top: 32px;  /* below the header row */
  z-index: 1;
}
tr.filter-row th.fcol  { background: #fcf2c7; }
tr.filter-row .filter-input,
tr.filter-row select {
  width: 100%;
  font: inherit; font-size: .78rem;
  padding: 2px 4px;
  border: 1px solid #d8dde7;
  border-radius: 3px;
  background: #fff;
}
tr.filter-row .date-filter {
  display: flex; gap: 3px;
}
tr.filter-row .date-filter select {
  flex: 1; min-width: 0; padding: 2px;
}
tr.filter-row .num-filter::placeholder { color: #b9c0cc; }
tr.filter-row .filter-clear {
  font: 700 .7rem ui-monospace, monospace;
  color: #b9c0cc; cursor: pointer;
  border: 0; background: transparent; padding: 0 2px;
}
tr.filter-row .filter-clear:hover { color: #b3261e; }

/* Pager */
.pager {
  display: flex; align-items: center; gap: .4rem;
  margin-top: .85rem; padding: .55rem .75rem;
  border: 1px solid #e2e6ee; border-radius: 6px;
  background: #fafbfd;
  flex-wrap: wrap;
}
.pager button {
  background: #fff; color: #1f2330; border: 1px solid #d0d4de;
  padding: .3rem .55rem; font-weight: 500; min-width: 30px;
}
.pager button:disabled { opacity: .4; cursor: not-allowed; }
.pager .page-input {
  width: 60px; text-align: center;
  padding: .3rem .4rem; border: 1px solid #d0d4de; border-radius: 6px;
  font: inherit; font-variant-numeric: tabular-nums;
}
.pager .page-info { color: #455161; }
.pager .pager-spacer { flex: 1; }
.pager select {
  padding: .3rem .4rem; border: 1px solid #d0d4de; border-radius: 6px;
  font: inherit;
}

/* Refund + revenue badges in dynamic rows */
td .num-cell { font-variant-numeric: tabular-nums; }
.badge.no  { background: #f8d7da; color: #842029; }
.badge.refund { background: #fff3cd; color: #664d03; }
