:root {
  --primary: #6c5ce7;
  --primary-dark: #5849c2;
  --primary-light: #efebff;
  --accent: #00b894;
  --danger: #e74c3c;
  --warning: #f0932b;
  --bg: #f5f6fb;
  --card-bg: #ffffff;
  --text: #1e1e2e;
  --text-muted: #6b7280;
  --border: #e6e8f0;
  --radius: 14px;
  --shadow: 0 2px 10px rgba(30, 30, 46, 0.06), 0 1px 2px rgba(30, 30, 46, 0.04);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14141f;
    --card-bg: #1d1d2c;
    --text: #eceefb;
    --text-muted: #9698b3;
    --border: #2c2c3f;
    --primary-light: #262244;
    --shadow: 0 2px 10px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; overflow-x: hidden; }
body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  width: 100%;
}
a { color: var(--primary); text-decoration: none; }
h1, h2, h3 { margin: 0 0 8px; line-height: 1.25; }
p { margin: 0 0 12px; }

/* Topbar */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 1440px; margin: 0 auto;
  display: flex; align-items: center; gap: 12px;
  padding: 12px 24px;
}
.brand { font-weight: 800; font-size: 18px; color: var(--text); display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.user-menu { display: flex; align-items: center; gap: 12px; flex-shrink: 0; margin-left: auto; }
.user-name { font-size: 14px; font-weight: 600; display: flex; align-items: center; gap: 8px; color: var(--text); }
.user-name:hover { color: var(--primary); }
.role-badge {
  background: var(--primary-light); color: var(--primary);
  padding: 2px 8px; border-radius: 999px; font-weight: 700; font-size: 11px;
}

/* App switcher */
.app-switcher { position: relative; flex-shrink: 0; }
.app-switcher-toggle {
  color: var(--primary); font-weight: 700; font-size: 14px;
  padding: 8px 14px; border-radius: 999px; white-space: nowrap;
  background: var(--primary-light); border: none; cursor: pointer; font-family: inherit;
}
.app-switcher-toggle:hover { background: var(--primary); color: #fff; }
.app-switcher-menu {
  display: none; position: absolute; top: calc(100% + 6px); left: 0; z-index: 30;
  background: var(--card-bg); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: var(--shadow); padding: 6px; min-width: 190px; flex-direction: column; gap: 2px;
}
.app-switcher.open .app-switcher-menu { display: flex; }
.app-switcher-menu a { padding: 9px 12px; border-radius: 8px; font-size: 14px; font-weight: 600; color: var(--text); display: flex; align-items: center; gap: 8px; }
.app-switcher-menu a:hover { background: var(--primary-light); color: var(--primary); }
.app-switcher-menu a.active { background: var(--primary); color: #fff; }
.app-switcher-divider { height: 1px; background: var(--border); margin: 6px 4px; }

/* Budget subnav (second row, only on budget pages) */
.subnav {
  display: flex; gap: 4px; overflow-x: auto; scrollbar-width: thin;
  max-width: 1440px; margin: 0 auto; padding: 0 24px 10px;
}
.subnav a {
  color: var(--text-muted); font-weight: 600; font-size: 13px;
  padding: 6px 12px; border-radius: 999px; white-space: nowrap; flex-shrink: 0;
  background: var(--bg);
}
.subnav a:hover { background: var(--primary-light); color: var(--primary); }
.subnav a.active { background: var(--primary); color: #fff; }

@media (max-width: 900px) {
  .topbar-inner { flex-wrap: wrap; }
  .app-switcher-menu {
    position: fixed; top: 62px; left: 12px; right: 12px;
    width: auto; min-width: 0; max-width: none;
  }
}

/* Layout */
main.page { max-width: 1200px; margin: 0 auto; padding: 28px 24px 60px; }
main.page.centered {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  padding: 24px;
}
.page-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; gap: 12px; flex-wrap: wrap; }
.page-header h1 { font-size: 24px; }

/* Cards & grid */
.card { background: var(--card-bg); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 22px; min-width: 0; }
.grid { display: grid; gap: 18px; min-width: 0; }
.grid > * { min-width: 0; }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
@media (max-width: 900px) { .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr 1fr; } }
@media (max-width: 600px) { .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; } }

/* Stat tiles */
.stat-tile { display: flex; flex-direction: column; gap: 4px; }
.stat-tile .label { font-size: 13px; color: var(--text-muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.stat-tile .value { font-size: 28px; font-weight: 800; }
.stat-tile .value.income { color: var(--accent); }
.stat-tile .value.expense { color: var(--danger); }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: none; border-radius: 10px; padding: 10px 18px; font-weight: 600; font-size: 14px;
  cursor: pointer; text-decoration: none; transition: transform .05s ease, opacity .15s ease;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-secondary { background: var(--primary-light); color: var(--primary); }
.btn-ghost { background: transparent; color: var(--text-muted); border: 1px solid var(--border); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-sm { padding: 6px 12px; font-size: 13px; }
.btn-block { width: 100%; }
.icon-btn { background: none; border: none; cursor: pointer; font-size: 15px; color: var(--text-muted); padding: 4px 6px; }
.icon-btn:hover { color: var(--danger); }

/* Forms */
label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; color: var(--text-muted); }
input, select, textarea {
  width: 100%; min-width: 0; max-width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font-size: 14px; font-family: inherit;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary-light); border-color: var(--primary); }
.form-group { margin-bottom: 16px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 14px; min-width: 0; }
.form-grid > * { min-width: 0; }
@media (max-width: 480px) {
  .form-grid { grid-template-columns: 1fr; }
}
.form-actions { display: flex; gap: 10px; margin-top: 8px; flex-wrap: wrap; }
.checkbox-row { display: flex; align-items: center; gap: 8px; }
.checkbox-row input { width: auto; }

/* Tables */
.table-wrap { overflow-x: auto; min-width: 0; max-width: 100%; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 12px 14px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--text-muted); font-weight: 700; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
tbody tr:hover { background: var(--primary-light); }

/* Badges */
.badge { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 700; }
.badge-income { background: rgba(0,184,148,.14); color: var(--accent); }
.badge-expense { background: rgba(231,76,60,.12); color: var(--danger); }

/* Progress */
.progress { background: var(--border); border-radius: 999px; height: 10px; overflow: hidden; }
.progress > div { height: 100%; background: var(--primary); border-radius: 999px; }
.progress.over > div { background: var(--danger); }
.progress-row { display: flex; justify-content: space-between; font-size: 13px; color: var(--text-muted); margin-bottom: 6px; }

/* Flash messages */
.flash { padding: 12px 16px; border-radius: 10px; margin-bottom: 16px; font-size: 14px; font-weight: 600; }
.flash-success { background: rgba(0,184,148,.12); color: var(--accent); }
.flash-error { background: rgba(231,76,60,.1); color: var(--danger); }
.flash-info { background: var(--primary-light); color: var(--primary); }

/* Auth pages */
.auth-card { width: 100%; max-width: 400px; }
.auth-card .brand { justify-content: center; font-size: 22px; margin-bottom: 6px; }
.auth-subtitle { text-align: center; color: var(--text-muted); font-size: 14px; margin-bottom: 20px; }
.auth-footer { text-align: center; margin-top: 16px; font-size: 14px; color: var(--text-muted); }

/* Empty state */
.empty-state { text-align: center; padding: 40px 20px; color: var(--text-muted); }
.empty-state .emoji { font-size: 40px; display: block; margin-bottom: 10px; }

.muted { color: var(--text-muted); }
.text-right { text-align: right; }
.flex { display: flex; align-items: center; }
.flex-between { display: flex; align-items: center; justify-content: space-between; }
.gap-8 { gap: 8px; }
.mt-0 { margin-top: 0; }
.mb-0 { margin-bottom: 0; }

.chart-box { position: relative; height: 300px; }

.modal-backdrop {
  display: none; position: fixed; inset: 0; background: rgba(20,20,31,.5); z-index: 50;
  align-items: center; justify-content: center; padding: 20px;
}
.modal-backdrop.open { display: flex; }
.modal { background: var(--card-bg); border-radius: var(--radius); padding: 24px; width: 100%; max-width: 480px; max-height: 90vh; overflow-y: auto; min-width: 0; box-sizing: border-box; }

/* Avatars */
.avatar { border-radius: 50%; object-fit: cover; flex-shrink: 0; }
.avatar-fallback {
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 50%; background: var(--primary); color: #fff; font-weight: 700; flex-shrink: 0;
}
.profile-avatar-row { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; min-width: 0; }
.avatar-form { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; min-width: 0; }
.avatar-form input[type="file"] { width: auto; max-width: 100%; min-width: 0; }

/* Chat & comments */
.chat-card { display: flex; flex-direction: column; }
.chat-messages {
  height: 420px; overflow-y: auto; display: flex; flex-direction: column; gap: 10px;
  padding: 8px 4px; margin-bottom: 14px; border: 1px solid var(--border); border-radius: 10px;
}
.chat-msg { background: var(--bg); border-radius: 10px; padding: 8px 12px; max-width: 85%; align-self: flex-start; }
.chat-msg.own { background: var(--primary-light); align-self: flex-end; }
.chat-msg-meta { font-size: 12px; color: var(--text-muted); margin-bottom: 2px; }
.chat-msg-text { font-size: 14px; white-space: pre-wrap; word-break: break-word; }
.chat-form { display: flex; gap: 10px; align-items: flex-end; min-width: 0; }
.chat-form textarea { resize: vertical; flex: 1; min-width: 0; }
.chat-form button { flex-shrink: 0; }
.comments-btn { font-size: 13px; white-space: nowrap; }

/* Floating chat widget */
.chat-fab {
  position: fixed; bottom: 24px; right: 24px; z-index: 45;
  width: 56px; height: 56px; border-radius: 50%; background: var(--primary); color: #fff;
  border: none; font-size: 24px; cursor: pointer; box-shadow: 0 6px 18px rgba(30,30,46,.3);
  display: flex; align-items: center; justify-content: center;
}
.chat-fab:hover { background: var(--primary-dark); }
.chat-fab-badge {
  position: absolute; top: -2px; right: -2px; background: var(--danger); color: #fff;
  border-radius: 999px; font-size: 11px; font-weight: 700; padding: 2px 6px; min-width: 20px;
  text-align: center; box-shadow: 0 0 0 2px var(--card-bg);
}
.chat-panel {
  display: none; flex-direction: column; overflow: hidden;
  position: fixed; bottom: 92px; right: 24px; z-index: 45;
  width: 340px; max-width: calc(100vw - 32px); height: 460px; max-height: calc(100vh - 140px);
  background: var(--card-bg); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 10px 40px rgba(30,30,46,.3);
}
.chat-panel.open { display: flex; }
.chat-panel-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px; border-bottom: 1px solid var(--border); font-weight: 700; flex-shrink: 0;
}
.chat-panel .chat-messages { flex: 1; margin: 10px 10px 8px; height: auto; }
.chat-panel .chat-form { margin: 0 10px 10px; }
@media (max-width: 480px) {
  .chat-fab { bottom: 16px; right: 16px; }
  .chat-panel { bottom: 84px; right: 16px; }
}

/* Hub */
.hub-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 18px; margin-bottom: 24px; }
@media (max-width: 900px) { .hub-grid { grid-template-columns: repeat(2, 1fr); } }
@media (max-width: 520px) { .hub-grid { grid-template-columns: 1fr; } }
.hub-card {
  display: flex; flex-direction: column; gap: 10px; padding: 26px;
  background: var(--card-bg); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); color: var(--text); transition: transform .1s ease;
}
.hub-card:hover { transform: translateY(-2px); color: var(--text); }
.hub-card .hub-icon { font-size: 34px; }
.hub-card h2 { font-size: 19px; margin: 0; }
.hub-card p { color: var(--text-muted); font-size: 13px; margin: 0; }
.hub-card .hub-count { font-size: 13px; font-weight: 700; color: var(--primary); }

/* Simple lists (tasks / shopping) */
.simple-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.simple-list li {
  display: flex; align-items: center; gap: 10px; padding: 10px 4px; border-bottom: 1px solid var(--border);
}
.simple-list li:last-child { border-bottom: none; }
.simple-list li.done .txt { text-decoration: line-through; color: var(--text-muted); }
.simple-list input[type="checkbox"] { width: auto; flex-shrink: 0; accent-color: var(--primary); }
.simple-list .txt { flex: 1; min-width: 0; font-size: 14px; }
.simple-list .meta { font-size: 12px; color: var(--text-muted); white-space: nowrap; display: flex; align-items: center; gap: 6px; }
.simple-list .meta.overdue { color: var(--danger); font-weight: 700; }
.simple-list .del { background: none; border: none; color: var(--text-muted); cursor: pointer; font-size: 18px; flex-shrink: 0; padding: 2px 6px; }
.simple-list .del:hover { color: var(--danger); }
.assignee-dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex-shrink: 0; }
.inline-add-form { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 16px; }
.inline-add-form input, .inline-add-form select { width: auto; flex: 1; min-width: 120px; }

/* Calendar */
.cal-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.cal-header h1 { font-size: 22px; margin: 0; }
.cal-nav { display: flex; gap: 8px; align-items: center; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; }
.cal-weekday { text-align: center; font-size: 12px; font-weight: 700; color: var(--text-muted); text-transform: uppercase; padding-bottom: 4px; }
.cal-day {
  background: var(--card-bg); border: 1px solid var(--border); border-radius: 10px;
  min-height: 96px; padding: 6px; display: flex; flex-direction: column; gap: 3px; cursor: pointer;
  overflow: hidden;
}
.cal-day:hover { border-color: var(--primary); }
.cal-day.outside { opacity: .35; }
.cal-day.today { border-color: var(--primary); box-shadow: 0 0 0 1px var(--primary); }
.cal-day .cal-daynum { font-size: 12px; font-weight: 700; color: var(--text-muted); }
.cal-day.today .cal-daynum { color: var(--primary); }
.cal-event { font-size: 11px; padding: 2px 5px; border-radius: 6px; color: #fff; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cal-event.cal-event-ghost { background: transparent; border: 1px dashed var(--border); color: var(--text-muted); }
.cal-more { font-size: 11px; color: var(--text-muted); }
@media (max-width: 700px) {
  .cal-grid { grid-template-columns: repeat(7, minmax(0,1fr)); gap: 3px; }
  .cal-day { min-height: 64px; padding: 4px; }
  .cal-weekday { font-size: 10px; }
}
.legend-row { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 14px; font-size: 12px; color: var(--text-muted); }
.legend-row span { display: inline-flex; align-items: center; gap: 5px; }
.legend-dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }

/* Today summary (hub) */
.today-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.today-list li { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.today-tag { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: var(--primary-light); color: var(--primary); flex-shrink: 0; }
.today-tag.overdue { background: rgba(231,76,60,.12); color: var(--danger); }

/* View toggle (calendar month/week) */
.view-toggle { display: flex; gap: 2px; background: var(--bg); border-radius: 999px; padding: 3px; }
.view-toggle a { padding: 6px 14px; border-radius: 999px; font-size: 13px; font-weight: 600; color: var(--text-muted); }
.view-toggle a.active { background: var(--primary); color: #fff; }

/* Calendar week (agenda) view */
.week-agenda { display: flex; flex-direction: column; gap: 10px; }
.week-day { border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
.week-day.today { border-color: var(--primary); box-shadow: 0 0 0 1px var(--primary); }
.week-day-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px; background: var(--bg); font-weight: 700; font-size: 13px;
}
.week-day.today .week-day-head { color: var(--primary); }
.week-day-items { list-style: none; margin: 0; padding: 6px; display: flex; flex-direction: column; gap: 4px; }
.week-day-items:empty::after { content: 'Няма нищо планирано.'; display: block; padding: 10px 8px; color: var(--text-muted); font-size: 13px; }
.week-item { display: flex; align-items: center; gap: 8px; padding: 8px 8px; border-radius: 8px; font-size: 14px; }
.week-item:hover { background: var(--primary-light); }
.week-item .week-item-time { font-size: 11px; font-weight: 700; color: #fff; background: var(--primary); padding: 2px 8px; border-radius: 999px; flex-shrink: 0; min-width: 52px; text-align: center; }
.week-item.week-item-ghost .week-item-time { background: transparent; border: 1px dashed var(--border); color: var(--text-muted); }
.week-item-title { flex: 1; min-width: 0; }
.week-day-add { font-size: 12px; color: var(--primary); padding: 8px 8px; display: block; font-weight: 600; }

/* Shopping bought history */
.bought-section { margin-top: 22px; }
.bought-section h2 { font-size: 15px; color: var(--text-muted); }
.simple-list.bought .txt { text-decoration: line-through; color: var(--text-muted); }
.return-btn { background: var(--primary-light); color: var(--primary); border: none; border-radius: 999px; padding: 4px 12px; font-size: 12px; font-weight: 700; cursor: pointer; flex-shrink: 0; }
.return-btn:hover { background: var(--primary); color: #fff; }
