/* ============ Theme tokens ============ */
:root {
  --bg: #f4f6fb;
  --bg-2: #eaeefa;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #6366f1;
  --primary-2: #8b5cf6;
  --success: #16a34a;
  --danger: #ef4444;
  --warning: #f59e0b;
  --border: #e5e9f2;
  --shadow: 0 1px 2px rgba(15, 23, 42, .04), 0 10px 30px rgba(15, 23, 42, .07);
  --radius: 18px;
  --ease: cubic-bezier(.2, .8, .2, 1);
}
[data-theme="dark"] {
  --bg: #0b1020;
  --bg-2: #0f1730;
  --card: #131b31;
  --text: #e6ebf5;
  --muted: #94a3b8;
  --border: #22304f;
  --shadow: 0 10px 34px rgba(0, 0, 0, .4);
}

/* ============ Base ============ */
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: radial-gradient(1200px 600px at 10% -10%, var(--bg-2), transparent), var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { margin: 0 0 12px; letter-spacing: -.01em; }
h1 { font-size: 1.6rem; }
h3 { font-size: 1rem; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
code { background: var(--bg-2); padding: 2px 6px; border-radius: 6px; font-size: .85em; }
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: .88rem; }
.small { font-size: .82rem; }
.center { text-align: center; }
.mt { margin-top: 18px; }
.grow { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.income { color: var(--success) !important; }
.expense { color: var(--danger) !important; }
.done-text { text-decoration: line-through; color: var(--muted); }

/* ============ Animations ============ */
@keyframes fadeUp { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes pop { from { opacity: 0; transform: scale(.94); } to { opacity: 1; transform: none; } }
@keyframes slideIn { from { opacity: 0; transform: translateX(-10px); } to { opacity: 1; transform: none; } }
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }
@keyframes grow { from { transform: scaleY(0); } to { transform: scaleY(1); } }
@keyframes fill { from { width: 0; } to { width: var(--w); } }
@keyframes draw { to { stroke-dashoffset: 0; } }
@keyframes seg { from { stroke-dasharray: 0 var(--c); } }
@keyframes shake { 10%, 90% { transform: translateX(-2px); } 20%, 80% { transform: translateX(4px); } 30%, 50%, 70% { transform: translateX(-6px); } 40%, 60% { transform: translateX(6px); } }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes floaty { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-14px); } }

.fade-up { animation: fadeUp .5s var(--ease) both; }
.pop { animation: pop .45s var(--ease) both; }
.row-in { animation: slideIn .35s var(--ease) both; }
.slide-in { animation: slideIn .4s var(--ease) both; }
#view > * { animation: fadeUp .5s var(--ease) both; }
#view > *:nth-child(2) { animation-delay: .05s; }
#view > *:nth-child(3) { animation-delay: .1s; }
#view > *:nth-child(4) { animation-delay: .15s; }
#view > *:nth-child(n+5) { animation-delay: .2s; }
.shake { animation: shake .5s; }
.spinner { width: 14px; height: 14px; border: 2px solid var(--border); border-top-color: var(--primary); border-radius: 50%; animation: spin .7s linear infinite; display: inline-block; }

.skeleton {
  height: 110px; border-radius: var(--radius);
  background: linear-gradient(90deg, var(--card) 25%, var(--border) 50%, var(--card) 75%);
  background-size: 200% 100%; animation: shimmer 1.3s linear infinite;
}
.skeleton-grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }

/* ============ Login ============ */
.auth {
  position: fixed; inset: 0; display: grid; place-items: center; padding: 20px; z-index: 100;
  background: linear-gradient(135deg, #312e81, #6d28d9 60%, #0ea5e9);
  overflow: hidden;
}
.auth-bg span {
  position: absolute; border-radius: 50%; background: rgba(255, 255, 255, .08);
  animation: floaty 9s ease-in-out infinite;
}
.auth-bg span:nth-child(1) { width: 320px; height: 320px; left: -80px; top: 10%; }
.auth-bg span:nth-child(2) { width: 220px; height: 220px; right: -60px; bottom: 8%; animation-delay: -3s; }
.auth-bg span:nth-child(3) { width: 140px; height: 140px; right: 20%; top: -40px; animation-delay: -6s; }
.auth-card {
  position: relative; width: 100%; max-width: 380px; background: var(--card);
  border-radius: 24px; padding: 30px; box-shadow: 0 30px 80px rgba(0, 0, 0, .35);
  display: grid; gap: 12px;
}
.auth-card h1 { margin: 0; text-align: center; }
.auth-card > p { margin: 0 0 4px; text-align: center; }
.auth-logo { font-size: 2.6rem; text-align: center; animation: floaty 4s ease-in-out infinite; }
.auth-card label { display: grid; gap: 6px; font-size: .9rem; color: var(--muted); }

/* ============ Shell ============ */
.app { min-height: 100vh; }
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; justify-content: space-between; align-items: center;
  padding: 12px 22px; background: color-mix(in srgb, var(--card) 85%, transparent);
  backdrop-filter: saturate(180%) blur(12px); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 1.05rem; }
.logo { display: inline-grid; place-items: center; width: 34px; height: 34px; border-radius: 10px;
  background: linear-gradient(135deg, var(--primary), var(--primary-2)); font-size: 1.1rem;
  box-shadow: 0 6px 18px rgba(99, 102, 241, .35); }
.top-actions { display: flex; align-items: center; gap: 10px; }
.clock { color: var(--muted); font-size: .85rem; font-variant-numeric: tabular-nums; }
.conn-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); transition: background .3s; }
.conn-dot.ok { background: var(--success); box-shadow: 0 0 0 0 rgba(22, 163, 74, .5); animation: ping 2.4s infinite; }
.conn-dot.bad { background: var(--danger); }
@keyframes ping { 0% { box-shadow: 0 0 0 0 rgba(22, 163, 74, .5); } 70% { box-shadow: 0 0 0 8px rgba(22, 163, 74, 0); } 100% { box-shadow: 0 0 0 0 rgba(22, 163, 74, 0); } }

.offline-banner { background: var(--danger); color: #fff; text-align: center; padding: 10px; font-weight: 600; animation: slideIn .4s var(--ease); }

.layout { display: flex; min-height: calc(100vh - 62px); }
.sidebar {
  width: 220px; padding: 16px 12px; display: flex; flex-direction: column; gap: 4px;
  border-right: 1px solid var(--border); background: color-mix(in srgb, var(--card) 70%, transparent);
  position: sticky; top: 62px; height: calc(100vh - 62px); overflow-y: auto;
}
.sidebar button {
  display: flex; align-items: center; gap: 10px; text-align: left; background: none; border: none;
  padding: 11px 12px; border-radius: 12px; font: inherit; font-size: .95rem; color: var(--text);
  cursor: pointer; transition: background .2s, transform .2s var(--ease);
}
.sidebar button span { width: 22px; text-align: center; }
.sidebar button:hover { background: var(--bg-2); transform: translateX(3px); }
.sidebar button.active { background: linear-gradient(135deg, var(--primary), var(--primary-2)); color: #fff; box-shadow: 0 8px 20px rgba(99, 102, 241, .3); }

main#view { flex: 1; padding: 26px; min-width: 0; max-width: 1300px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; flex-wrap: wrap; gap: 12px; margin-bottom: 18px; }
.page-head h1 { margin: 0; }
.page-head p { margin: 4px 0 0; }
.page-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ============ Cards & grids ============ */
.card {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 20px; box-shadow: var(--shadow); margin-bottom: 16px;
  transition: transform .25s var(--ease), box-shadow .25s var(--ease);
}
.card:hover { transform: translateY(-2px); box-shadow: 0 16px 40px rgba(15, 23, 42, .1); }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 8px; }
.card-head h3, .card-head h2 { margin: 0; }
.kpis { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); margin-bottom: 16px; }
.kpi { margin: 0; position: relative; overflow: hidden; }
.kpi::after { content: ""; position: absolute; right: -30px; top: -30px; width: 90px; height: 90px; border-radius: 50%; background: var(--primary); opacity: .07; }
.kpi-label { color: var(--muted); font-size: .82rem; text-transform: uppercase; letter-spacing: .05em; }
.kpi-value { font-size: 1.75rem; font-weight: 800; margin-top: 6px; font-variant-numeric: tabular-nums; }
.kpi-value.accent { color: var(--primary); }
.big-num { font-size: 2rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.grid-2, .grid-3, .grid-4 { display: grid; gap: 16px; margin-bottom: 0; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); }
.grid-4 { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.grid-2 > .card, .grid-3 > .card, .grid-4 > .card { margin-bottom: 0; }
.cards-grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
.cards-grid > .card { margin-bottom: 0; }
.split { display: flex; gap: 22px; align-items: center; flex-wrap: wrap; }
.split > .legend { flex: 1; min-width: 220px; }
.empty { text-align: center; padding: 26px 10px; }
.empty-icon { font-size: 2.2rem; animation: floaty 3.5s ease-in-out infinite; }
.alert { background: #fff7ed; border: 1px solid #fed7aa; color: #9a3412; padding: 11px 14px; border-radius: 12px; margin: 0 0 12px; font-size: .92rem; }
.alert.red { background: #fef2f2; border-color: #fecaca; color: #991b1b; }
[data-theme="dark"] .alert { background: #2a1d10; border-color: #5a3a14; color: #fdba74; }
[data-theme="dark"] .alert.red { background: #2a1214; border-color: #5a1f25; color: #fca5a5; }
.insights { display: grid; gap: 10px; }
.insight { padding: 12px 14px; border-radius: 12px; background: var(--bg-2); font-size: .95rem; }
.badge { font-size: .75rem; padding: 2px 8px; border-radius: 999px; font-weight: 700; white-space: nowrap; }
.badge.good { background: #dcfce7; color: #166534; }
.badge.bad { background: #fee2e2; color: #991b1b; }
[data-theme="dark"] .badge.good { background: #123524; color: #86efac; }
[data-theme="dark"] .badge.bad { background: #3a1517; color: #fca5a5; }
.link { font-size: .88rem; font-weight: 600; }
.rings { display: flex; gap: 18px; justify-content: space-around; flex-wrap: wrap; margin: 6px 0; }
.ring-item { text-align: center; display: grid; gap: 4px; justify-items: center; font-weight: 600; }
.ring-item .muted { font-weight: 400; }
.center-text { text-align: center; }
.big-list { margin-top: 10px; }

/* ============ Forms ============ */
input, select, textarea {
  font: inherit; padding: 10px 12px; border: 1px solid var(--border); border-radius: 12px;
  background: var(--card); color: var(--text); min-width: 0; transition: border-color .2s, box-shadow .2s;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 4px rgba(99, 102, 241, .18); }
textarea { width: 100%; min-height: 110px; resize: vertical; }
.form-grid { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.form-grid.stack { flex-direction: column; align-items: stretch; }
.form-grid.compact input, .form-grid.compact select { flex: 1; min-width: 120px; }
.form-stack { display: grid; gap: 10px; margin-bottom: 10px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn {
  background: linear-gradient(135deg, var(--primary), var(--primary-2)); color: #fff; border: none;
  padding: 10px 18px; border-radius: 12px; font: inherit; font-weight: 700; cursor: pointer;
  box-shadow: 0 6px 16px rgba(99, 102, 241, .25); transition: transform .15s var(--ease), box-shadow .2s, opacity .2s;
}
.btn:hover { transform: translateY(-1px); box-shadow: 0 10px 22px rgba(99, 102, 241, .32); }
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .6; cursor: wait; transform: none; }
.btn.small { padding: 7px 12px; font-size: .85rem; }
.btn.block { width: 100%; }
.btn.ghost { background: transparent; color: var(--primary); box-shadow: none; border: 1px solid var(--primary); }
.btn.ghost:hover { background: color-mix(in srgb, var(--primary) 10%, transparent); }
.btn.danger { background: var(--danger); box-shadow: none; }
.btn.danger-text { background: none; color: var(--danger); box-shadow: none; border: 1px solid var(--danger); }
.icon-btn {
  width: 38px; height: 38px; border-radius: 12px; border: 1px solid var(--border); background: var(--card);
  color: var(--text); cursor: pointer; font-size: 1rem; display: inline-grid; place-items: center;
  transition: transform .15s var(--ease), background .2s;
}
.icon-btn:hover { transform: rotate(-8deg) scale(1.06); background: var(--bg-2); }
.icon-btn.danger-hover:hover { background: #fee2e2; }
.seg-toggle { display: inline-flex; background: var(--bg-2); padding: 4px; border-radius: 12px; }
.seg-toggle label { position: relative; }
.seg-toggle input { position: absolute; opacity: 0; pointer-events: none; }
.seg-toggle span { display: inline-block; padding: 8px 14px; border-radius: 9px; cursor: pointer; font-weight: 600; color: var(--muted); transition: all .25s var(--ease); }
.seg-toggle input:checked + span { background: var(--card); color: var(--primary); box-shadow: var(--shadow); }
.toolbar { display: flex; gap: 10px; margin-bottom: 12px; }
.toolbar input { flex: 1; }

/* ============ Lists ============ */
ul.list, .legend { list-style: none; padding: 0; margin: 0; }
ul.list li, .legend li {
  display: flex; align-items: center; gap: 12px; padding: 11px 4px; border-bottom: 1px solid var(--border);
  transition: background .2s;
}
ul.list li:last-child, .legend li:last-child { border-bottom: none; }
ul.list li:hover { background: color-mix(in srgb, var(--primary) 4%, transparent); border-radius: 10px; }
.tag { font-size: .75rem; padding: 3px 9px; border-radius: 999px; background: var(--bg-2); border: 1px solid var(--border); white-space: nowrap; color: var(--muted); }
.tag.warn, .tag.pending { background: #fef3c7; color: #92400e; border-color: #fde68a; }
.tag.high { background: #fee2e2; color: #991b1b; border-color: #fecaca; }
.tag.medium { background: #e0e7ff; color: #3730a3; border-color: #c7d2fe; }
.tag.low { background: #dcfce7; color: #166534; border-color: #bbf7d0; }
[data-theme="dark"] .tag.warn, [data-theme="dark"] .tag.pending { background: #3a2a0d; color: #fcd34d; border-color: #5a4212; }
[data-theme="dark"] .tag.high { background: #3a1517; color: #fca5a5; border-color: #5a1f25; }
[data-theme="dark"] .tag.medium { background: #1e1b4b; color: #c7d2fe; border-color: #312e81; }
[data-theme="dark"] .tag.low { background: #123524; color: #86efac; border-color: #14532d; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }

/* Checkbox styled as a round tick */
.check { position: relative; width: 22px; height: 22px; flex-shrink: 0; cursor: pointer; }
.check input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.check span { position: absolute; inset: 0; border: 2px solid var(--primary); border-radius: 8px; transition: all .25s var(--ease); }
.check input:checked + span { background: var(--primary); transform: scale(1.08); }
.check input:checked + span::after { content: "✓"; color: #fff; position: absolute; inset: 0; display: grid; place-items: center; font-size: 13px; font-weight: 800; animation: pop .3s var(--ease); }
.is-done .grow { opacity: .6; }
details.card summary { cursor: pointer; font-weight: 700; }
details.card[open] summary { margin-bottom: 10px; }

/* ============ Progress ============ */
.bar { height: 10px; background: var(--bg-2); border-radius: 999px; overflow: hidden; margin: 10px 0 6px; }
.bar.big { height: 14px; }
.bar > span {
  display: block; height: 100%; width: var(--w); border-radius: 999px;
  background: linear-gradient(90deg, var(--success), #4ade80); animation: fill 1s var(--ease) both;
}
.bar > span.warn { background: linear-gradient(90deg, #f59e0b, #fbbf24); }
.bar > span.over { background: linear-gradient(90deg, #ef4444, #f87171); }
.progress-head { display: flex; justify-content: space-between; margin-bottom: 6px; font-weight: 600; }
.usage { margin-top: 10px; }

/* Tabs */
.tabs { display: flex; gap: 8px; margin-bottom: 16px; flex-wrap: wrap; }
.tabs button {
  border: 1px solid var(--border); background: var(--card); padding: 8px 14px; border-radius: 999px;
  cursor: pointer; font: inherit; font-weight: 600; color: var(--muted); transition: all .25s var(--ease);
}
.tabs button:hover { color: var(--text); transform: translateY(-1px); }
.tabs button.active { background: linear-gradient(135deg, var(--primary), var(--primary-2)); color: #fff; border-color: transparent; box-shadow: 0 6px 16px rgba(99, 102, 241, .3); }
.tabs.small button { padding: 6px 12px; font-size: .85rem; }

/* Pills */
.pill { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: .8rem; font-weight: 700; }
.pill.ok { background: #dcfce7; color: #166534; }
.pill.bad { background: #fee2e2; color: #991b1b; }
[data-theme="dark"] .pill.ok { background: #123524; color: #86efac; }
[data-theme="dark"] .pill.bad { background: #3a1517; color: #fca5a5; }

/* Check list (system check) */
.check-row { display: flex; align-items: center; gap: 12px; padding: 11px 2px; border-bottom: 1px solid var(--border); }
.check-row.pop { animation: pop .4s var(--ease); }
.check-row:last-child { border-bottom: none; }

/* Export grid */
.export-grid { display: flex; gap: 8px; flex-wrap: wrap; margin: 8px 0 12px; }

/* ============ Charts ============ */
.chart { width: 100%; height: auto; overflow: visible; }
.chart .grid-line { stroke: var(--border); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 13px; }
.chart .bar { transform-box: fill-box; transform-origin: bottom; animation: grow .8s var(--ease) both; }
.chart .area { opacity: .12; animation: fadeIn 1s ease .6s both; }
.chart .draw { stroke-dasharray: 1; stroke-dashoffset: 1; animation: draw 1.3s var(--ease) .1s forwards; }
.chart .pt { opacity: 0; animation: pop .35s var(--ease) forwards; transform-box: fill-box; transform-origin: center; }
.donut { width: 200px; height: 200px; max-width: 100%; }
.donut .seg { animation: seg .9s var(--ease) both; }
.donut-wrap { position: relative; display: inline-block; }
.donut-center { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; pointer-events: none; }
.donut-center b { font-size: 1.15rem; }
.ring { display: block; margin: 0 auto; }
.ring-arc { animation: seg 1.1s var(--ease) both; }
.ring-text { fill: var(--text); font-size: 17px; font-weight: 800; }
.legend-row { display: flex; gap: 14px; font-size: .85rem; color: var(--muted); margin-top: 6px; }
.legend-row i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; }
.cal-card { padding: 16px; }
.cal { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; }
.cal .dow { font-size: .75rem; color: var(--muted); text-align: center; font-weight: 700; padding: 4px 0; }
.cal .day {
  position: relative; min-height: 58px; border: 1px solid var(--border); border-radius: 12px; padding: 6px;
  background: var(--bg); color: var(--text); font: inherit; font-size: .9rem; cursor: pointer; text-align: left;
  transition: transform .2s var(--ease), border-color .2s, background .2s;
}
.cal .day:hover { transform: translateY(-2px); border-color: var(--primary); }
.cal .day.today { border: 2px solid var(--primary); font-weight: 800; }
.cal .day .dot { position: absolute; bottom: 7px; left: 7px; width: 7px; height: 7px; background: var(--primary-2); }
.big-list li .muted { font-size: .8rem; }

/* ============ Toast ============ */
.toast {
  position: fixed; left: 50%; bottom: 26px; transform: translate(-50%, 30px); opacity: 0; pointer-events: none;
  background: #0f172a; color: #fff; padding: 12px 20px; border-radius: 14px; z-index: 200; font-weight: 600;
  box-shadow: 0 16px 40px rgba(0, 0, 0, .25); transition: opacity .35s var(--ease), transform .35s var(--ease);
  max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: #b91c1c; }

/* ============ Responsive ============ */
@media (max-width: 860px) {
  .layout { flex-direction: column; }
  .sidebar {
    width: 100%; height: auto; position: sticky; top: 62px; flex-direction: row; overflow-x: auto;
    padding: 8px; border-right: none; border-bottom: 1px solid var(--border); z-index: 15; backdrop-filter: blur(10px);
  }
  .sidebar button { white-space: nowrap; padding: 9px 12px; }
  .sidebar button:hover { transform: none; }
  main#view { padding: 16px; }
  .clock { display: none; }
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
}
@media (max-width: 480px) {
  .kpi-value { font-size: 1.4rem; }
  .brand span:last-child { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
