/* =============== THEME =============== */
:root {
  /* Warm palette */
  --warm-1: #cc7722; --warm-2: #a0522d; --warm-3: #d4a373;
  --warm-4: #6b8e23; --warm-5: #2f6f6f; --warm-6: #e0a899; --warm-7: #9c6644;

  /* Surfaces & ink */
  --bg: #f8f5f1;
  --ink: #2b2b2b;
  --card: #ffffff;
  --border: #e6dfd7;
  --shadow: 0 10px 24px rgba(0,0,0,.06), 0 2px 4px rgba(0,0,0,.04);

  /* Typography tokens used by charts (JS reads these) */
  --title-font-size: 1rem;
  --title-font-weight: 700;
  --axis-font-size: 0.95rem;
  --axis-font-weight: 600;
  --axis-color: #4a341f;      /* same tone as chart headers */
  --title-color: #4a341f;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, Noto Sans, Arial, "Apple Color Emoji", "Segoe UI Emoji";
  color: var(--ink);
  background: var(--bg);
}

/* =============== APP SHELL (moved here from base.html inline) =============== */
:root { --sidebar-w: 260px; }

.shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 100dvh;
}

/* Sidebar slightly darker (warm) for contrast */
.sidebar {
  position: sticky;
  top: 0;
  height: 100dvh;
  background: #f3ede6;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.brand {
  display: flex; align-items: center; gap: 10px;
  padding: 16px; border-bottom: 1px solid var(--border);
}
.brand img { width: 32px; height: 32px; object-fit: contain; }
.brand .name { font-weight: 700; color: var(--ink); letter-spacing: .2px; }

.nav { padding: 12px 8px; overflow: auto; }
.nav-group { margin: 10px 8px; }
.nav-title {
  font-size: .78rem; letter-spacing: .06em; text-transform: uppercase;
  color: #6b5a47; margin: 10px 8px 6px;
}
.nav a {
  display: flex; gap: 10px; align-items: center;
  color: var(--ink); text-decoration: none;
  padding: 10px 12px; border-radius: 10px;
}
.nav a:hover { background: #faf6f1; }
.divider { height: 1px; background: var(--border); margin: 8px 10px; }

.sidebar-footer { margin-top: auto; border-top: 1px solid var(--border); padding: 8px; }
.user-menu { position: relative; }
.user-btn {
  width: 100%; text-align: left; background: transparent; border: 0;
  padding: 10px 12px; border-radius: 10px; cursor: pointer; color: var(--ink);
}
.user-btn:hover { background: #faf6f1; }
.user-dropdown {
  position: absolute; left: 8px; right: 8px; bottom: 48px;
  background: var(--card); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: var(--shadow); display: none; overflow: hidden;
}
.user-dropdown a { display: block; padding: 10px 12px; color: var(--ink); text-decoration: none; }
.user-dropdown a:hover { background: #faf6f1; }
.user-menu.open .user-dropdown { display: block; }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 5;
  background: var(--bg); border-bottom: 1px solid var(--border);
  padding: 10px 16px;
}
.topbar h1 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--ink);
}

.content { padding: 12px 0 28px; }

/* Optional small action link near titles */
.report-link {
  margin-left: 12px;
  font-size: 0.9rem;
  color: var(--warm-5);
  text-decoration: none;
  border: 1px solid var(--warm-5);
  padding: 2px 8px;
  border-radius: 6px;
  transition: background .2s, color .2s, border-color .2s;
}
.report-link:hover { background: var(--warm-5); color: #fff; }

/* =============== PAGE LAYOUT (dashboard) =============== */
.container { max-width: 1200px; margin: 24px auto; padding: 0 16px; }
.grid-12 {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  gap: 16px;
  align-items: start;
}

/* Cards */
.chart-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 14px 14px 10px;
  box-shadow: var(--shadow);
  transition: transform .15s ease, box-shadow .15s ease;
}
.chart-card:hover { transform: translateY(-2px); box-shadow: 0 16px 32px rgba(0,0,0,.08); }
.chart-title { margin: 4px 4px 12px; font-size: var(--title-font-size); font-weight: var(--title-font-weight); color: var(--title-color); }

/* Chart wrappers */
.chart-wrap { width: 100%; height: auto; }
.aspect-16-9 { aspect-ratio: 16 / 9; }
.clamp-ht { max-height: min(520px, 60vh); }

/* Bottom row equal height for donut & bar */
.bottom-equal {
  aspect-ratio: 16 / 9;
  max-height: min(520px, 60vh);
  position: relative;
}

/* Donut perfect circle within equal-height wrapper */
.square-fit {
  height: 100%;
  aspect-ratio: 1 / 1;
  width: auto;
  margin: 0 auto;
  display: block;
}

/* Canvas sizing */
.chart-wrap canvas,
.square-fit > canvas {
  width: 100% !important;
  height: 100% !important;
  display: block;
}

/* Grid spans */
.span-12 { grid-column: span 12; }
.span-6 { grid-column: span 6; }

/* Responsive */
@media (max-width: 900px) {
  .span-6 { grid-column: span 12; }
  .clamp-ht, .bottom-equal { max-height: unset; }
  .container { margin: 16px auto; }
}

/* =============== Toast Notifications =============== */
.toast-container {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.toast {
  position: relative;
  background: #d9534f; /* default red for error */
  color: #fff;
  padding: 10px 16px;
  padding-right: 28px; /* space for close */
  border-radius: 8px;
  box-shadow: 0 6px 12px rgba(0,0,0,0.2);
  font-size: 0.9rem;
  opacity: 0;
  transform: translateX(120%);
  transition: opacity 0.4s ease, transform 0.4s ease;
}
.toast.show { opacity: 1; transform: translateX(0); }
.toast.success { background: #5cb85c; }
.toast.warn    { background: #f0ad4e; }
.toast.info    { background: #0275d8; }

/* Close button */
.toast-close {
  position: absolute;
  top: 6px;
  right: 8px;
  cursor: pointer;
  font-weight: bold;
  color: #fff;
  opacity: 0.8;
  transition: opacity 0.2s;
}
.toast-close:hover { opacity: 1; }

/* ===== Donut: legend on the RIGHT, keep donut large ===== */
.legend-right-layout .chart-wrap {
  display: flex;
  gap: 16px;
  align-items: center;
}
.legend-right-layout .square-fit {
  flex: 1 1 auto;          /* canvas takes remaining width */
  min-width: 260px;
}
.legend-right {
  min-width: 240px;
  max-width: 320px;
}
.legend-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.legend-item {
  display: grid;
  grid-template-columns: 14px 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 6px 4px;
  border-radius: 8px;
}
.legend-item:hover { background: #faf6f1; }
.legend-item.muted { opacity: .45; }
.legend-dot {
  width: 12px; height: 12px; border-radius: 50%;
  display: inline-block;
}
.legend-text {
  color: var(--ink);
  font-size: 0.95rem;
}
.legend-pct {
  color: var(--ink);
  font-weight: 600;
  font-size: 0.95rem;
}

/* Mobile: stack legend under donut */
@media (max-width: 900px) {
  .legend-right-layout .chart-wrap {
    flex-direction: column;
    align-items: stretch;
  }
  .legend-right {
    min-width: 0;
  }
}

/* --- Report layout fixes (overflow / notes / buttons) --- */
.report-container .report-toprow{
  display:flex; align-items:center; justify-content:space-between;
  margin: 4px 0 14px;
}
.page-heading{ margin:0; font-size:1.1rem; color:#4a341f; }

.btn-report{
  appearance:none; border:1px solid var(--border); background:#faf6f1;
  color:#4a341f; font-weight:600; padding:8px 14px; border-radius:10px;
  box-shadow: var(--shadow); text-decoration:none; display:inline-flex; align-items:center; gap:8px;
}
.btn-report:hover{ transform: translateY(-1px); box-shadow:0 12px 20px rgba(0,0,0,.06); }

.report-table-wrap{
  width:100%; overflow-x:auto; padding-bottom:6px; margin-top:10px;
}
.report-table{ width:max-content; min-width:100%; border:1px solid var(--border); border-radius:10px; overflow:hidden; border-collapse:separate; border-spacing:0; }
.report-table th, .report-table td { white-space:nowrap; }

.note-form{ margin-top:10px; display:grid; grid-template-columns: 100px 1fr auto; gap:10px; align-items:start; }
.note-label{ color:#4a341f; font-weight:600; padding-top:6px; }
.note-textarea{
  width:100%; min-height:100px; resize:vertical;
  border:1px solid var(--border); border-radius:10px; padding:10px; background:#fffdf9;
  font: inherit; color: var(--ink);
}
.btn-save-note{
  align-self:start; padding:8px 14px; border-radius:10px; border:0; background:#faf6f1; color:#4a341f; font-weight:600;
  box-shadow: var(--shadow); cursor:pointer;
}
.btn-save-note:hover{ filter:brightness(0.95); }
/* Center table content, prevent spill */
.report-table.center th,
.report-table.center td {
  text-align: center;
  vertical-align: middle;
  white-space: nowrap;
}

/* Remove vertical borders, keep subtle row separators */
.report-table-wrap.no-vert-borders .report-table {
  border-collapse: separate;
  border-spacing: 0;
}
.report-table-wrap.no-vert-borders .report-table th,
.report-table-wrap.no-vert-borders .report-table td {
  border-left: none;
  border-right: none;
  border-top: none;
  border-bottom: 1px solid var(--border);
}

/* Keep first column readable & sticky for wide tables */
.report-table .sticky-left {
  position: sticky;
  left: 0;
  background: var(--card);
  z-index: 1;
  text-align: left; /* row label should read well */
  white-space: nowrap;
}

/* Keep your existing sizes: */
.donut-compact {
  display: grid;
  grid-template-columns: 1fr auto; /* chart + legend */
  align-items: center;
  gap: 12px;
  min-height: 180px;
  max-height: 240px;
  padding: 8px 0;
}
.donut-compact .square-fit {
  height: 100%;
  aspect-ratio: 1 / 1;
  max-height: 220px;
  margin: 0 auto;
}

/* NEW/UPDATED */
.donut-compact .legend-right {
  align-self: stretch;
  display: flex;
  align-items: stretch;
  max-width: 220px;        /* constrain legend column */
  overflow: auto;          /* scroll if still too tall */
}

/* Make list vertical with small gaps so it doesn’t bunch horizontally */
.legend-right .legend-list{
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 0;
  padding: 0;
  list-style: none;
}

/* Optional: reduce text size slightly to avoid wrapping too soon */
.legend-right .legend-item .legend-text{
  font-size: .9rem;
}

.legend-right {
  display: flex;
  flex-direction: column;
  justify-content: center;  /* centers vertically */
}

/* Mobile: legend under the chart and centered */
@media (max-width: 900px) {
  .donut-compact { grid-template-columns: 1fr; }
  .donut-compact .legend-right {
    justify-content: center;
    max-width: none;
  }
}

/* Tighter table look + centered values (without vertical borders) */
.report-table-wrap.no-vert-borders .report-table {
  border-collapse: separate;
  border-spacing: 0;
}
.report-table-wrap.no-vert-borders .report-table th,
.report-table-wrap.no-vert-borders .report-table td {
  border-left: none;
  border-right: none;
  border-top: none;
  border-bottom: 1px solid var(--border);
}

/* Center cell content, keep labels readable, prevent wrapping */
.report-table.center th,
.report-table.center td {
  text-align: center;
  vertical-align: middle;
  white-space: nowrap; /* keep first/last columns from wrapping */
}
.report-table .sticky-left {
  position: sticky;
  left: 0;
  background: var(--card);
  z-index: 1;
  text-align: left;
}
.report-table .num { text-align: center; }

/* Slightly wider last column to avoid clipping totals */
.report-table th:last-child,
.report-table td:last-child { padding-right: 14px; }

/* --- Donut area: reserve height & spacing so it never overlaps the table --- */
.donut-section { margin-bottom: 8px; }

.donut-compact {
  display: grid;
  grid-template-columns: 1fr auto; /* chart + legend */
  align-items: center;
  gap: 14px;
  min-height: 300px;               /* reserve enough vertical room */
  max-height: 340px;
  padding: 8px 0 4px;
}

.donut-compact .square-fit {
  height: 100%;
  aspect-ratio: 1 / 1;
  max-height: 320px;
  margin: 0 auto;
}

.donut-compact .legend-right {
  align-self: center;              /* center legend vertically */
  display: flex;
  align-items: center;
}

/* Narrow screens: stack donut & legend and center legend */
@media (max-width: 900px) {
  .donut-compact { grid-template-columns: 1fr; }
  .donut-compact .legend-right { justify-content: center; }
}

/* --- Ensure a gap between chart and table everywhere --- */
.report-table-wrap { margin-top: 18px !important; }

/* --- Table theme consistency (no vertical borders, centered numbers) --- */
.report-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;   /* stop spill; cells wrap nicely */
}
.report-table th, .report-table td {
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 10px 8px;
  vertical-align: middle;
  word-break: break-word;
}
.report-table th:first-child,
.report-table td:first-child {
  text-align: left;
}
.report-table th:not(:first-child),
.report-table td:not(:first-child) {
  text-align: center;    /* center numeric columns */
}
.report-table thead th {
  background: #f7efe7;   /* same warm card tint */
  color: #4a341f;
  font-weight: 700;
}
/* Donut table: allow header text to wrap cleanly */
.report-table th {
  white-space: normal;        /* allow wrapping */
  word-break: break-word;     /* break very long words */
  hyphens: auto;              /* soft hyphenation where supported */
  line-height: 1.2;
}
/* Donut area centering */
.donut-compact {
  grid-template-columns: minmax(320px, 1fr) auto; /* left column wide enough, then fill */
  justify-items: center;      /* center items in each grid column */
  align-items: center;        /* vertical centering */
}
/* Let menus escape table/card clipping */
.table-responsive { overflow: visible; }        /* Bootstrap sets overflow-x:auto */
.card, .card-body { overflow: visible; }

.dropdown, .btn-group { position: static; }     /* so Popper can place menu freely */
.dropdown-menu { z-index: 1060; }               /* above card shadows/borders */
/* Let menus escape table/card clipping */
.table-actions .table-responsive { overflow: visible; }

.btn-brown{ background:#7a5a3a; color:#fff; border:1px solid #6d5033; }
.btn-brown:hover{ filter:brightness(0.95); }
.btn-outline-brown{ color:#7a5a3a; border:1px solid #7a5a3a; background:transparent; }
.btn-outline-brown:hover{ background:#efe7df; }
/* Money bag icon styling */
.icon-money{ width:22px; height:22px; vertical-align:-3px; filter: drop-shadow(0 1px 0 rgba(0,0,0,.05)); }
.sidebar .nav a .icon-money{ margin-right:6px; }
/* ==== Donut: split legend, centered chart ==== */
.donut-split {
  display: grid;
  grid-template-columns: 1fr minmax(260px, 360px) 1fr; /* L legend | donut | R legend */
  align-items: center;
  gap: 12px;
  padding: 8px 0;
  min-height: 280px;
}
.donut-split .square-fit {
  height: 100%;
  aspect-ratio: 1 / 1;
  width: 100%;
  max-width: 340px;
  margin-inline: auto;
}
.donut-split .legend-col {
  align-self: center;
}
.legend-list {
  list-style: none;
  margin: 0; padding: 0;
  display: grid;
  grid-auto-rows: minmax(24px, auto);
  gap: 10px;
}
.legend-item {
  display: grid;
  grid-template-columns: 16px 1fr auto;
  align-items: center;
  gap: 10px;
  font-size: 0.95rem;
  color: var(--ink);
}
.legend-dot {
  width: 10px; height: 10px;
  border-radius: 999px;
  display: inline-block;
}
.legend-text { white-space: normal; line-height: 1.2; }
.legend-pct  { font-weight: 700; color: #4a341f; }

/* Dashboard (right legend): keep wrapping so it stays inside card */
.legend-right { max-width: 380px; }
.legend-right .legend-text { white-space: normal; }

/* Report tables – small refinements */
.report-table th, .report-table td { text-align: center; }
.report-table th { white-space: normal; } /* allow header wrapping */

/* ensure canvases fill their containers */
.chart-wrap canvas,
.square-fit > canvas{ width:100% !important; height:100% !important; display:block; }
.square-fit{ aspect-ratio:1/1; height:100%; margin:0 auto; }
.bottom-equal{ aspect-ratio:16/9; max-height:min(520px,60vh); }
@media (max-width:1024px){ .bottom-equal{ max-height:unset; } }
