/* ==================================================================
   Livings Innovators — modern UI
   ================================================================== */

:root {
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-2: #f9fafc;
  --border: #e7eaf0;
  --border-strong: #d8dde6;

  --text: #0f172a;
  --text-2: #475569;
  --text-3: #94a3b8;

  --primary: #4f46e5;
  --primary-2: #6366f1;
  --primary-soft: #eef2ff;
  --primary-grad: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);

  --success: #10b981;
  --success-soft: #ecfdf5;
  --danger: #ef4444;
  --danger-soft: #fef2f2;
  --warning: #f59e0b;
  --warning-soft: #fffbeb;
  --info: #0ea5e9;
  --info-soft: #f0f9ff;

  --radius: 14px;
  --radius-lg: 18px;
  --radius-sm: 10px;

  --shadow-sm: 0 1px 2px rgba(15,23,42,.04), 0 1px 3px rgba(15,23,42,.04);
  --shadow: 0 4px 12px rgba(15,23,42,.06), 0 2px 4px rgba(15,23,42,.04);
  --shadow-lg: 0 12px 28px rgba(15,23,42,.10);

  --side-w: 244px;
  --bottom-h: 64px;
  --topbar-h: 56px;
}

* { box-sizing: border-box; }

html, body { -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; }

body {
  font-family: 'Sarabun', 'Inter', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  margin: 0;
  font-size: 15px;
  line-height: 1.5;
}

a { color: var(--primary); text-decoration: none; }
a:hover { color: var(--primary-2); }

h1,h2,h3,h4,h5,h6 { color: var(--text); font-weight: 700; }
.text-muted { color: var(--text-2) !important; }
small, .small { color: var(--text-2); }

/* ================================ Sidebar (desktop) */
.sidebar {
  position: fixed;
  top: 0; left: 0; bottom: 0;
  width: var(--side-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 18px 14px;
  z-index: 100;
}
.sidebar-brand {
  display: flex; align-items: center; gap: 12px;
  padding: 6px 8px 18px;
  margin-bottom: 8px;
  border-bottom: 1px solid var(--border);
}
.brand-logo {
  width: 40px; height: 40px;
  border-radius: 12px;
  background: var(--primary-grad);
  display: grid; place-items: center;
  color: #fff; font-size: 1.2rem;
  box-shadow: 0 4px 14px rgba(99,102,241,.3);
}
.brand-name { font-weight: 700; font-size: 1rem; line-height: 1.1; }
.brand-sub { color: var(--text-3); font-size: .78rem; }

.sidebar-nav { display: flex; flex-direction: column; gap: 2px; flex: 1; overflow-y: auto; padding-top: 8px;}
.sidebar-nav .nav-item {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  color: var(--text-2);
  font-weight: 500;
  font-size: .94rem;
  transition: all .15s ease;
  text-decoration: none;
}
.sidebar-nav .nav-item i { font-size: 1.15rem; width: 22px; text-align: center; }
.sidebar-nav .nav-item:hover { background: var(--surface-2); color: var(--text); }
.sidebar-nav .nav-item.active {
  background: var(--primary-soft);
  color: var(--primary);
  font-weight: 600;
}

.sidebar-foot { border-top: 1px solid var(--border); padding-top: 12px; margin-top: 8px; }
.user-pill {
  display: flex; align-items: center; gap: 10px;
  width: 100%;
  padding: 8px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  border: 0;
  cursor: pointer;
  transition: background .15s;
}
.user-pill:hover { background: var(--primary-soft); }
.avatar {
  width: 36px; height: 36px;
  border-radius: 50%;
  background: var(--primary-grad);
  color: #fff;
  display: grid; place-items: center;
  font-weight: 700; font-size: .9rem;
  flex-shrink: 0;
}
.user-name { font-size: .88rem; font-weight: 600; line-height: 1.1; }
.user-sub { font-size: .73rem; color: var(--text-3); }

/* ================================ Mobile topbar */
.mobile-topbar {
  position: sticky; top: 0; z-index: 90;
  height: var(--topbar-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 12px;
  padding-top: env(safe-area-inset-top);
}
.brand-mini {
  display: flex; align-items: center; gap: 8px;
  font-weight: 700; font-size: 1rem;
}
.brand-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--primary-grad);
}
.icon-btn {
  width: 40px; height: 40px;
  border-radius: 10px;
  background: var(--surface-2);
  border: 0;
  display: grid; place-items: center;
  color: var(--text);
  font-size: 1.2rem;
  cursor: pointer;
  transition: background .15s;
}
.icon-btn:hover { background: var(--primary-soft); color: var(--primary); }
.icon-btn-spacer { width: 40px; height: 40px; display: inline-block; }

/* ================================ Bottom nav (mobile) */
.bottom-nav {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  z-index: 90;
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  padding: 6px 0 calc(6px + env(safe-area-inset-bottom));
  height: calc(var(--bottom-h) + env(safe-area-inset-bottom));
  box-shadow: 0 -2px 16px rgba(15,23,42,.05);
}
.bn-item {
  flex: 1;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px;
  background: transparent; border: 0;
  color: var(--text-3);
  font-size: .68rem; font-weight: 500;
  padding: 4px 2px;
  text-decoration: none;
  cursor: pointer;
  transition: color .15s;
}
.bn-item i { font-size: 1.3rem; }
.bn-item.active { color: var(--primary); }
.bn-item:active { transform: scale(.95); }

/* ================================ More menu overlay */
.more-overlay {
  position: fixed; inset: 0;
  z-index: 1050;
  background: rgba(15,23,42,.45);
  backdrop-filter: blur(4px);
  display: flex; align-items: flex-end;
  animation: fadeIn .2s ease;
}
.more-overlay[hidden] { display: none; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.more-sheet {
  width: 100%;
  background: var(--surface);
  border-radius: 24px 24px 0 0;
  padding: 12px 18px calc(24px + env(safe-area-inset-bottom));
  max-height: 85vh; overflow-y: auto;
  box-shadow: 0 -10px 40px rgba(0,0,0,.2);
  animation: sheetUp .3s cubic-bezier(.34,1.56,.64,1);
}
@keyframes sheetUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
.more-handle {
  width: 40px; height: 4px;
  background: var(--border-strong);
  border-radius: 2px;
  margin: 4px auto 14px;
}
.more-title {
  font-size: 1rem; font-weight: 700;
  margin: 0 0 14px;
}
.more-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}
.more-item {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 8px;
  padding: 16px 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  text-decoration: none;
  font-size: .82rem; font-weight: 500;
  text-align: center;
  transition: transform .15s, background .15s;
}
.more-item:active { transform: scale(.95); background: var(--primary-soft); }
.more-item.active { background: var(--primary-soft); color: var(--primary); border-color: var(--primary); }
.more-item.danger { color: var(--danger); }
.more-icon {
  width: 44px; height: 44px;
  border-radius: 12px;
  background: var(--surface);
  display: grid; place-items: center;
  font-size: 1.3rem;
  color: var(--primary);
}
.more-item.active .more-icon { background: var(--primary); color: #fff; }
.more-item.danger .more-icon { color: var(--danger); }
.more-label { line-height: 1.2; word-break: break-word; }

/* ================================ Main area */
.main-area {
  margin-left: 0;
  padding-bottom: calc(var(--bottom-h) + 24px + env(safe-area-inset-bottom));
  min-height: 100vh;
}
@media (min-width: 992px) {
  .main-area { margin-left: var(--side-w); padding-bottom: 24px; }
}
.content { padding: 20px 16px; max-width: 1280px; margin: 0 auto; }
@media (min-width: 992px) { .content { padding: 28px 32px; } }

/* ================================ Flash messages */
.flash-stack {
  position: sticky; top: 12px; z-index: 50;
  margin: 12px 16px 0; display: flex; flex-direction: column; gap: 8px;
}
@media (min-width: 992px) { .flash-stack { margin: 12px 32px 0; } }
.flash {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 14px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  font-size: .92rem; font-weight: 500;
  animation: slideDown .25s ease;
}
@keyframes slideDown { from {opacity:0; transform: translateY(-8px);} to {opacity:1; transform:none;} }
.flash i { font-size: 1.1rem; flex-shrink: 0; }
.flash-close { margin-left: auto; background: transparent; border: 0; opacity: .6; cursor: pointer; padding: 4px;}
.flash-close:hover { opacity: 1; }
.flash-success { background: var(--success-soft); color: #065f46; border: 1px solid #a7f3d0; }
.flash-danger  { background: var(--danger-soft);  color: #991b1b; border: 1px solid #fecaca; }
.flash-warning { background: var(--warning-soft); color: #92400e; border: 1px solid #fcd34d; }
.flash-info    { background: var(--info-soft);    color: #075985; border: 1px solid #bae6fd; }

/* ================================ Auth pages */
body.auth-page {
  background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 50%, #ec4899 100%);
  min-height: 100vh;
}
.auth-main { position: relative; display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.auth-main > .flash-stack {
  position: absolute; top: 16px; left: 16px; right: 16px;
  margin: 0; z-index: 50;
}
.auth-card {
  background: #fff; border-radius: 24px;
  padding: 32px;
  width: 100%; max-width: 400px;
  box-shadow: 0 20px 60px rgba(0,0,0,.18);
}
.auth-logo {
  width: 64px; height: 64px;
  border-radius: 18px;
  background: var(--primary-grad);
  color: #fff;
  display: grid; place-items: center;
  font-size: 1.8rem;
  margin: 0 auto 16px;
  box-shadow: 0 8px 24px rgba(99,102,241,.4);
}

/* ================================ Cards / surfaces */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}
.card-body { padding: 18px; }
@media (min-width: 768px) { .card-body { padding: 20px; } }

/* ================================ Stat cards */
.stat-grid {
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(2, 1fr);
}
@media (min-width: 768px) { .stat-grid { gap: 16px; } }
@media (min-width: 1100px) { .stat-grid { grid-template-columns: repeat(4, 1fr); } }
.stat-grid.cols-3 { grid-template-columns: repeat(2, 1fr); }
@media (min-width: 768px) { .stat-grid.cols-3 { grid-template-columns: repeat(3, 1fr); } }

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px;
  position: relative;
  overflow: hidden;
}
.stat::before {
  content: ""; position: absolute; top: 0; left: 0; right: 0; height: 3px;
  background: var(--primary-grad);
}
.stat.success::before { background: linear-gradient(90deg, #10b981, #34d399); }
.stat.danger::before  { background: linear-gradient(90deg, #ef4444, #f87171); }
.stat.warning::before { background: linear-gradient(90deg, #f59e0b, #fbbf24); }
.stat .stat-icon {
  width: 36px; height: 36px;
  border-radius: 10px;
  background: var(--primary-soft);
  color: var(--primary);
  display: grid; place-items: center;
  font-size: 1rem;
  margin-bottom: 10px;
}
.stat.success .stat-icon { background: var(--success-soft); color: var(--success); }
.stat.danger  .stat-icon { background: var(--danger-soft);  color: var(--danger); }
.stat.warning .stat-icon { background: var(--warning-soft); color: var(--warning); }
.stat .stat-label { font-size: .78rem; color: var(--text-2); font-weight: 500; margin-bottom: 4px; }
.stat .stat-value {
  font-size: 1.25rem; font-weight: 700; line-height: 1.2; letter-spacing: -.01em;
  word-break: break-word;
}
@media (min-width: 768px) { .stat .stat-value { font-size: 1.5rem; } }
@media (min-width: 1100px) { .stat .stat-value { font-size: 1.65rem; } }
.stat .stat-foot { color: var(--text-3); font-size: .72rem; margin-top: 4px; }

/* ================================ Page header */
.page-header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; margin-bottom: 18px; flex-wrap: wrap;
}
.page-header > div:first-child { min-width: 0; flex: 1 1 200px; }
.page-title {
  font-size: 1.3rem; font-weight: 700; margin: 0;
  word-break: break-word;
}
@media (min-width: 768px) { .page-title { font-size: 1.55rem; } }
.page-subtitle { color: var(--text-2); font-size: .88rem; margin: 4px 0 0; word-break: break-word; }

/* ================================ Buttons */
.btn {
  font-weight: 500;
  border-radius: 10px;
  padding: .55rem 1rem;
  transition: all .15s;
  border: 1px solid transparent;
}
.btn-lg { padding: .75rem 1.25rem; font-size: 1rem; border-radius: 12px; }
.btn-sm { padding: .35rem .7rem; font-size: .85rem; border-radius: 8px; }
.btn-primary {
  background: var(--primary-grad);
  border-color: transparent;
  color: #fff;
  box-shadow: 0 2px 8px rgba(99,102,241,.25);
}
.btn-primary:hover { background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%); transform: translateY(-1px); box-shadow: 0 4px 14px rgba(99,102,241,.35); }
.btn-primary:active { transform: translateY(0); }
.btn-success {
  background: linear-gradient(135deg, #10b981 0%, #059669 100%);
  border-color: transparent; color: #fff;
}
.btn-danger {
  background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
  border-color: transparent; color: #fff;
}
.btn-outline-secondary {
  background: var(--surface);
  border-color: var(--border-strong);
  color: var(--text-2);
}
.btn-outline-secondary:hover { background: var(--surface-2); color: var(--text); border-color: var(--border-strong); }
.btn-outline-danger { color: var(--danger); border-color: #fecaca; background: #fff; }
.btn-outline-danger:hover { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-outline-warning { color: var(--warning); border-color: #fcd34d; background: #fff; }
.btn-outline-primary { color: var(--primary); border-color: #c7d2fe; background: #fff; }

/* ================================ Forms */
.form-control, .form-select {
  border-radius: 10px;
  border-color: var(--border-strong);
  padding: .55rem .8rem;
  font-size: .94rem;
  transition: all .15s;
  min-height: 44px;
}
.form-control:focus, .form-select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(99,102,241,.15);
}
.form-control-lg { font-size: 1rem; padding: .8rem 1rem; }
.form-label { font-weight: 500; color: var(--text); font-size: .87rem; margin-bottom: .35rem; }
.form-check-input:checked { background-color: var(--primary); border-color: var(--primary); }
.form-switch .form-check-input { width: 2.4em; }

/* ================================ Tables → cards on mobile */
.table {
  --bs-table-bg: transparent;
  margin-bottom: 0;
}
.table thead th {
  background: var(--surface-2);
  color: var(--text-2);
  font-weight: 600;
  font-size: .78rem;
  text-transform: uppercase;
  letter-spacing: .03em;
  border: 0;
  padding: 12px 14px;
}
.table tbody td {
  padding: 12px 14px;
  border-color: var(--border);
  vertical-align: middle;
}
.table-clickable tbody tr { cursor: pointer; transition: background .12s; }
.table-clickable tbody tr:hover { background: var(--surface-2); }

.right { text-align: right; }
.num { font-variant-numeric: tabular-nums; }

/* mobile card style */
@media (max-width: 767.98px) {
  .table-responsive-cards thead { display: none; }
  .table-responsive-cards tbody, .table-responsive-cards tr,
  .table-responsive-cards td { display: block; width: 100%; }
  .table-responsive-cards tr {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 10px;
    padding: 12px 14px;
    box-shadow: var(--shadow-sm);
  }
  .table-responsive-cards td {
    padding: 4px 0;
    border: 0 !important;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
  }
  .table-responsive-cards td::before {
    content: attr(data-label);
    color: var(--text-3);
    font-size: .78rem;
    font-weight: 500;
    flex-shrink: 0;
  }
  .table-responsive-cards td[data-label=""]::before { display: none; }
  .table-responsive-cards td.cell-main {
    font-size: 1rem; font-weight: 600;
    border-bottom: 1px solid var(--border) !important;
    padding-bottom: 10px !important;
    margin-bottom: 4px !important;
    flex-direction: column; align-items: flex-start;
  }
  .table-responsive-cards td.cell-main::before { display: none; }
}

/* ================================ Badges */
.badge {
  font-weight: 500;
  font-size: .72rem;
  padding: .35em .65em;
  border-radius: 8px;
  letter-spacing: .01em;
}
.badge-soft-success { background: var(--success-soft); color: #065f46; }
.badge-soft-danger  { background: var(--danger-soft); color: #991b1b; }
.badge-soft-warning { background: var(--warning-soft); color: #92400e; }
.badge-soft-info    { background: var(--info-soft); color: #075985; }
.badge-soft-primary { background: var(--primary-soft); color: var(--primary); }
.badge-soft-secondary { background: #f1f5f9; color: #475569; }

.badge-status-active   { background: var(--success-soft); color: #065f46; }
.badge-status-closed   { background: #e2e8f0; color: #475569; }
.badge-status-archived { background: #f1f5f9; color: #94a3b8; }

/* ================================ Tabs / pills */
.nav-pills .nav-link {
  color: var(--text-2);
  font-weight: 500;
  border-radius: 10px;
  padding: .5rem 1rem;
  font-size: .9rem;
}
.nav-pills .nav-link.active {
  background: var(--primary-grad);
  color: #fff;
  box-shadow: 0 2px 8px rgba(99,102,241,.3);
}
.nav-pills .nav-link:not(.active):hover { background: var(--primary-soft); color: var(--primary); }

/* ================================ Progress */
.progress { background: var(--surface-2); border-radius: 999px; height: 8px; }
.progress-bar { border-radius: 999px; }
.progress.lg { height: 12px; }

/* ================================ Money tones */
.money-pos { color: var(--success); font-weight: 600; }
.money-neg { color: var(--danger); font-weight: 600; }
.money-pri { color: var(--primary); font-weight: 700; }

/* ================================ Filter bar */
.filter-bar {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 16px;
}

/* ================================ Empty state */
.empty {
  text-align: center; padding: 48px 20px;
  color: var(--text-3);
}
.empty i { font-size: 3rem; opacity: .3; display: block; margin-bottom: 12px; }

/* ================================ Floating action (mobile) */
.fab {
  position: fixed;
  right: 18px;
  bottom: calc(var(--bottom-h) + 14px + env(safe-area-inset-bottom));
  z-index: 80;
  width: 56px; height: 56px;
  border-radius: 50%;
  background: var(--primary-grad);
  color: #fff;
  border: 0;
  box-shadow: 0 8px 20px rgba(99,102,241,.4);
  display: grid; place-items: center;
  font-size: 1.4rem;
  text-decoration: none;
}
.fab:active { transform: scale(.95); }
@media (min-width: 992px) { .fab { display: none; } }

/* ================================ AI chat — floating draggable bubble */
#chat-toggle {
  position: fixed;
  right: 18px;
  bottom: calc(var(--bottom-h) + 18px + env(safe-area-inset-bottom));
  z-index: 1040;
  width: 58px; height: 58px;
  border-radius: 50%;
  background: var(--primary-grad);
  color: #fff;
  border: 0;
  box-shadow: 0 10px 28px rgba(99,102,241,.5), 0 0 0 4px rgba(255,255,255,.6);
  font-size: 1.55rem;
  cursor: grab;
  display: grid; place-items: center;
  touch-action: none;
  user-select: none; -webkit-user-select: none;
  animation: aiFloat 3.2s ease-in-out infinite;
  transition: box-shadow .25s ease;
}
#chat-toggle::after {
  content: ""; position: absolute; inset: -3px;
  border-radius: 50%;
  background: var(--primary-grad);
  z-index: -1; opacity: .35;
  filter: blur(8px);
  animation: aiPulse 2.4s ease-in-out infinite;
}
#chat-toggle.dragging {
  animation: none;
  cursor: grabbing;
  transform: scale(1.18) rotate(-8deg);
  box-shadow: 0 18px 40px rgba(99,102,241,.6), 0 0 0 6px rgba(255,255,255,.8);
}
#chat-toggle.tap { animation: aiTap .45s ease; }
#chat-toggle.snap { transition: left .35s cubic-bezier(.34,1.56,.64,1), right .35s cubic-bezier(.34,1.56,.64,1), top .35s cubic-bezier(.34,1.56,.64,1), bottom .35s cubic-bezier(.34,1.56,.64,1); }
#chat-toggle .ai-face {
  display: grid; place-items: center; line-height: 1;
  transition: transform .2s ease;
}
#chat-toggle.dragging .ai-face { transform: scale(1.2) rotate(15deg); }
#chat-toggle .ai-tip {
  position: absolute;
  right: calc(100% + 10px);
  top: 50%; transform: translateY(-50%) scale(.85);
  background: #0f172a; color: #fff;
  font-size: .72rem; font-weight: 600;
  padding: 6px 10px; border-radius: 12px;
  white-space: nowrap;
  opacity: 0; pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
}
#chat-toggle .ai-tip::after {
  content: ""; position: absolute; left: 100%; top: 50%;
  transform: translateY(-50%);
  border: 5px solid transparent; border-left-color: #0f172a;
}
#chat-toggle.dragging .ai-tip,
#chat-toggle.show-tip .ai-tip { opacity: 1; transform: translateY(-50%) scale(1); }
#chat-toggle[data-side="left"] .ai-tip { right: auto; left: calc(100% + 10px); }
#chat-toggle[data-side="left"] .ai-tip::after { left: auto; right: 100%; border-left-color: transparent; border-right-color: #0f172a; }

@keyframes aiFloat {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}
@keyframes aiPulse {
  0%, 100% { opacity: .25; transform: scale(1); }
  50% { opacity: .5; transform: scale(1.08); }
}
@keyframes aiTap {
  0% { transform: scale(1); }
  30% { transform: scale(.82); }
  60% { transform: scale(1.12) rotate(6deg); }
  100% { transform: scale(1); }
}

@media (min-width: 992px) {
  #chat-toggle { width: 60px; height: 60px; }
}
#chat-window {
  position: fixed;
  right: 16px; left: 16px;
  bottom: calc(var(--bottom-h) + 88px + env(safe-area-inset-bottom));
  z-index: 1041;
  background: #fff;
  border-radius: 18px;
  border: 1px solid var(--border);
  box-shadow: 0 20px 60px rgba(0,0,0,.18);
  display: none;
  flex-direction: column;
  overflow: hidden;
  height: 70vh;
  max-height: 560px;
}
#chat-window.open { display: flex; animation: chatPop .25s cubic-bezier(.34,1.56,.64,1); }
@keyframes chatPop {
  from { opacity: 0; transform: translateY(20px) scale(.95); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}
@media (min-width: 768px) {
  #chat-window { left: auto; right: 24px; bottom: 96px; width: 440px; height: 620px; max-height: 80vh; }
}
#chat-header {
  background: var(--primary-grad);
  color: #fff;
  padding: 14px 16px;
  display: flex; align-items: center; justify-content: space-between;
}
#chat-header .h-title { display: flex; align-items: center; gap: 8px; font-weight: 600; flex-wrap: wrap; }
#chat-header .h-sub { font-size: .72rem; font-weight: 400; opacity: .85; margin-left: 2px; }
#chat-close { background: rgba(255,255,255,.2); color: #fff; border: 0; width: 28px; height: 28px; border-radius: 8px; display: grid; place-items: center; cursor: pointer; }
#chat-messages { flex: 1; overflow-y: auto; padding: 14px; background: var(--surface-2); }
.chat-msg { margin-bottom: 12px; }
.chat-msg .bubble {
  display: inline-block; padding: 10px 14px;
  border-radius: 16px; max-width: 92%;
  line-height: 1.55; font-size: .92rem;
  text-align: left;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.chat-msg.user { text-align: right; }
.chat-msg.user .bubble { background: var(--primary-grad); color: #fff; border-bottom-right-radius: 4px; }
.chat-msg.assistant .bubble { background: #fff; border: 1px solid var(--border); border-bottom-left-radius: 4px; box-shadow: var(--shadow-sm); }
.chat-msg.system .bubble { background: var(--warning-soft); border: 1px solid #fcd34d; color: #92400e; font-size: .85rem; }
.chat-msg .bubble p { margin: 0 0 .5rem; }
.chat-msg .bubble p:last-child { margin-bottom: 0; }
.chat-msg .bubble ul, .chat-msg .bubble ol { margin: .25rem 0 .5rem; padding-left: 1.25rem; }
.chat-msg .bubble ul:last-child, .chat-msg .bubble ol:last-child { margin-bottom: 0; }
.chat-msg .bubble li { margin-bottom: .15rem; }
.chat-msg .bubble h3, .chat-msg .bubble h4, .chat-msg .bubble h5 {
  margin: .5rem 0 .35rem; font-size: 1rem; font-weight: 700;
}
.chat-msg .bubble h3:first-child, .chat-msg .bubble h4:first-child, .chat-msg .bubble h5:first-child { margin-top: 0; }
.chat-msg .bubble strong { font-weight: 700; }
.chat-msg .bubble code {
  background: rgba(99,102,241,.1); color: #4338ca;
  padding: 1px 6px; border-radius: 6px;
  font-size: .88em; font-family: ui-monospace, "SF Mono", Menlo, monospace;
}
.chat-msg.user .bubble code { background: rgba(255,255,255,.25); color: #fff; }
#chat-form { display: flex; border-top: 1px solid var(--border); background: #fff; }
#chat-input { flex: 1; border: 0; padding: 14px; font-size: .94rem; outline: 0; background: transparent; }
#chat-send { border: 0; background: var(--primary-grad); color: #fff; padding: 0 18px; cursor: pointer; }

/* ================================ Misc */
.dropdown-menu {
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-lg);
  padding: 6px;
}
.dropdown-item {
  border-radius: 8px;
  padding: 8px 12px;
  font-size: .92rem;
}
.dropdown-item:hover { background: var(--primary-soft); color: var(--primary); }

code {
  background: var(--surface-2);
  color: var(--primary);
  padding: 1px 6px;
  border-radius: 4px;
  font-size: .85em;
}

hr { border-color: var(--border); opacity: 1; }

body.no-scroll { overflow: hidden; }

/* hide scrollbar but keep functionality on sidebar */
.sidebar-nav::-webkit-scrollbar { width: 4px; }
.sidebar-nav::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 2px; }

/* tabular numerics in money columns */
td.right, .right.num { font-variant-numeric: tabular-nums; }

/* ================================ Long text safety in mobile cards */
.card .flex-grow-1 { min-width: 0; }
.card small, .card .small { word-break: break-word; overflow-wrap: anywhere; }
.card code { word-break: break-all; }

/* ================================ Touch targets on mobile */
@media (max-width: 767.98px) {
  .btn-sm { min-height: 38px; padding: .4rem .7rem; }
  .icon-btn { width: 44px; height: 44px; }
  .empty .btn-sm { min-height: 44px; padding: .55rem 1rem; font-size: .92rem; }
  .page-header .btn { white-space: nowrap; }
}

/* ================================ Mobile keyboard handling */
@media (max-width: 991.98px) {
  input, textarea, select, .form-control, .form-select {
    scroll-margin-bottom: 280px;
    scroll-margin-top: 80px;
  }
  body.kb-open .bottom-nav { display: none !important; }
  body.kb-open .main-area { padding-bottom: 24px !important; }
  body.kb-open #chat-toggle, body.kb-open .fab { display: none !important; }
}

/* ================================ Print styles */
@media print {
  .sidebar, .mobile-topbar, .bottom-nav, .more-overlay,
  .fab, #chat-toggle, #chat-window,
  .page-header .btn, .page-header form,
  .filter-bar,
  .btn, .dropdown, .dropdown-menu,
  form[data-confirm], tr td:last-child form,
  .flash-stack { display: none !important; }
  .main-area { margin-left: 0 !important; padding: 0 !important; }
  .content { padding: 0 !important; max-width: 100% !important; }
  body { background: #fff !important; font-size: 12px; }
  .card { break-inside: avoid; box-shadow: none !important; border: 1px solid #ddd !important; }
  .stat-grid { grid-template-columns: repeat(4, 1fr) !important; }
  .table thead th { background: #f3f4f6 !important; color: #000 !important; }
  a { color: inherit !important; text-decoration: none !important; }
}
