/* ============================================================
   APP STYLES — CV YKW Business Suite
   Refined Minimalism, B2B-grade
   ============================================================ */

* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0; min-height: 100vh;
  font-family: var(--font-sans);
  font-size: var(--text-base);
  color: var(--text);
  background: var(--bg-app);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  font-feature-settings: "cv11", "ss01", "ss03";
}

/* Tabular nums on numeric content */
.num, td.num, .kpi-value, .pill, .badge {
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1;
}

a { color: inherit; text-decoration: none; }
button { font-family: inherit; cursor: pointer; }
input, textarea, select { font-family: inherit; font-size: inherit; color: inherit; }

/* ============================================================
   APP SHELL
   ============================================================ */

.app-root {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 100vh;
  transition: grid-template-columns var(--duration-base) var(--ease-out);
}
.app-root.sidebar-collapsed { grid-template-columns: var(--sidebar-w-collapsed) 1fr; }

@media (max-width: 1023px) {
  .app-root { grid-template-columns: 1fr; }
}

/* ============================================================
   SIDEBAR
   ============================================================ */

.sidebar {
  background: var(--bg-sidebar);
  color: #e2e8f0;
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;      /* dynamic viewport — accounts for iOS Safari URL bar */
  position: sticky;
  top: 0;
  z-index: 30;
  border-right: 1px solid rgba(255,255,255,0.04);
}
@media (max-width: 1023px) {
  .sidebar {
    position: fixed; left: 0; top: 0; bottom: 0; width: var(--sidebar-w);
    height: 100dvh;
    transform: translateX(-100%);
    transition: transform var(--duration-slow) var(--ease-out);
    box-shadow: var(--shadow-modal);
    z-index: 100;       /* above backdrop (99) */
  }
  .sidebar.mobile-open { transform: translateX(0); }
}

.sidebar-brand {
  display: flex; align-items: center; gap: var(--sp-3);
  padding: var(--sp-4) var(--sp-4);
  height: 64px;
  border-bottom: 1px solid rgba(255,255,255,0.06);
}
.brand-logo {
  width: 36px; height: 36px;
  background: linear-gradient(135deg, #059669 0%, #047857 100%);
  border-radius: var(--r-md);
  display: grid; place-items: center;
  color: white;
  flex-shrink: 0;
  box-shadow: 0 0 0 1px rgba(255,255,255,0.04), 0 1px 2px rgba(0,0,0,0.2);
}
.brand-text { flex: 1; min-width: 0; overflow: hidden; }
.brand-name {
  font-size: var(--text-base); font-weight: 700; color: white; letter-spacing: -0.01em;
}
.brand-sub {
  font-size: 10px; color: #94a3b8; letter-spacing: 0.12em;
  text-transform: uppercase; font-weight: 500; margin-top: 1px;
}
.sidebar-collapse {
  background: transparent; border: none; color: #64748b;
  width: 24px; height: 24px; border-radius: var(--r-sm);
  display: grid; place-items: center;
  transition: all var(--duration-fast);
}
.sidebar-collapse:hover { background: rgba(255,255,255,0.06); color: white; }
.sidebar-collapsed .sidebar-collapse svg { transform: rotate(180deg); }

/* Hide text when collapsed */
.sidebar-collapsed .sidebar:not(.mobile-open) .brand-text,
.sidebar-collapsed .sidebar:not(.mobile-open) .nav-section-title,
.sidebar-collapsed .sidebar:not(.mobile-open) .nav-item-label,
.sidebar-collapsed .sidebar:not(.mobile-open) .user-info,
.sidebar-collapsed .sidebar:not(.mobile-open) .user-menu-btn,
.sidebar-collapsed .sidebar:not(.mobile-open) .nav-item-badge {
  display: none;
}

.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding: var(--sp-3) var(--sp-2);
  display: flex;
  flex-direction: column;
  gap: var(--sp-1);
}
.nav-section {
  margin-top: var(--sp-3);
  display: flex; flex-direction: column; gap: 2px;
}
.nav-section:first-child { margin-top: 0; }
.nav-section-title {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.10em;
  text-transform: uppercase;
  color: #475569;
  padding: var(--sp-2) var(--sp-3) var(--sp-1);
}
.nav-item {
  display: flex; align-items: center; gap: var(--sp-3);
  padding: 7px var(--sp-3);
  border-radius: var(--r-md);
  color: #cbd5e1;
  font-size: var(--text-md);
  font-weight: 500;
  position: relative;
  transition: all var(--duration-fast);
  border: 1px solid transparent;
}
.nav-item:hover {
  background: rgba(255,255,255,0.04);
  color: white;
}
.nav-item.active {
  background: rgba(255,255,255,0.06);
  color: white;
}
.nav-item.active::before {
  content: '';
  position: absolute; left: -2px; top: 8px; bottom: 8px;
  width: 2px; background: #cbd5e1;          /* slate-300, neutral */
  border-radius: 2px;
}
.nav-item svg { width: 16px; height: 16px; flex-shrink: 0; opacity: 0.85; }
.nav-item.active svg { opacity: 1; color: white; }  /* white, not brand */
.nav-item-label { flex: 1; }
.nav-item-badge {
  background: var(--danger);
  color: white;
  font-size: 10px;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: var(--r-full);
  line-height: 1.4;
}
.nav-item-badge.muted { background: rgba(255,255,255,0.1); color: #cbd5e1; }

.sidebar-footer {
  padding: var(--sp-3) var(--sp-2);
  border-top: 1px solid rgba(255,255,255,0.06);
}
.user-card {
  display: flex; align-items: center; gap: var(--sp-3);
  padding: var(--sp-2) var(--sp-3);
  border-radius: var(--r-md);
  transition: background var(--duration-fast);
}
.user-card:hover { background: rgba(255,255,255,0.04); }
.user-avatar {
  width: 32px; height: 32px;
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-active) 100%);
  border-radius: var(--r-full);
  display: grid; place-items: center;
  color: white; font-weight: 600; font-size: 11px;
  flex-shrink: 0;
}
.user-info { flex: 1; min-width: 0; }
.user-name {
  font-size: var(--text-md); font-weight: 600; color: white;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.user-role { font-size: 11px; color: #94a3b8; }
.user-menu-btn {
  background: none; border: none; color: #64748b;
  padding: 4px; border-radius: var(--r-sm);
}
.user-menu-btn:hover { background: rgba(255,255,255,0.06); color: white; }

/* ============================================================
   MAIN AREA + TOPBAR
   ============================================================ */

.main-area {
  display: flex;
  flex-direction: column;
  min-width: 0;  /* prevent grid overflow */
  min-height: 100vh;
}

.topbar {
  display: flex; align-items: center; gap: var(--sp-3);
  height: var(--topbar-h);
  padding: 0 var(--sp-5);
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
}
.mobile-menu-btn {
  display: none; background: none; border: none; color: var(--text);
  padding: var(--sp-2); border-radius: var(--r-sm);
}
@media (max-width: 1023px) {
  .mobile-menu-btn { display: grid; place-items: center; }
}
.mobile-menu-btn:hover { background: var(--bg-hover); }

.breadcrumb {
  display: flex; align-items: center; gap: var(--sp-2);
  font-size: var(--text-md); color: var(--muted);
}
.breadcrumb .crumb { color: var(--muted); }
.breadcrumb .crumb.current { color: var(--ink); font-weight: 600; }
.breadcrumb .separator { color: var(--faint); font-size: 11px; }

.topbar-spacer { flex: 1; }

.search-trigger {
  display: flex; align-items: center; gap: var(--sp-2);
  padding: 7px var(--sp-3);
  border-radius: var(--r-md);
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: var(--text-md);
  min-width: 280px;
  transition: all var(--duration-fast);
}
.search-trigger:hover { background: white; border-color: var(--border-strong); }
.search-trigger svg { color: var(--subtle); flex-shrink: 0; }
.search-trigger span { flex: 1; text-align: left; }
.search-trigger kbd {
  font-family: var(--font-mono);
  font-size: 10px;
  background: white;
  border: 1px solid var(--border);
  border-radius: var(--r-xs);
  padding: 1px 5px;
  color: var(--muted);
  box-shadow: 0 1px 0 var(--border);
}
@media (max-width: 768px) {
  .search-trigger { min-width: auto; padding: 7px; }
  .search-trigger span, .search-trigger kbd { display: none; }
}

.quick-action { position: relative; }
.quick-menu {
  position: absolute; top: calc(100% + 6px); right: 0;
  background: white;
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-popover);
  min-width: 200px;
  padding: var(--sp-1);
  z-index: 100;
  animation: dropIn var(--duration-base) var(--ease-out);
}
.quick-menu a {
  display: block;
  padding: 8px var(--sp-3);
  border-radius: var(--r-sm);
  font-size: var(--text-md);
  color: var(--text);
  transition: background var(--duration-fast);
}
.quick-menu a:hover { background: var(--bg-hover); color: var(--ink); }
.quick-menu hr { border: none; border-top: 1px solid var(--divider); margin: var(--sp-1) 0; }
@keyframes dropIn { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: translateY(0); } }

.lang-switcher {
  display: flex;
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  padding: 2px;
  gap: 1px;
}
.lang-btn {
  background: transparent; border: none;
  padding: 4px 9px;
  font-size: 11px; font-weight: 600;
  color: var(--muted);
  border-radius: var(--r-sm);
  letter-spacing: 0.04em;
  transition: all var(--duration-fast);
}
.lang-btn:hover { color: var(--text); }
.lang-btn.active {
  background: white; color: var(--ink);
  box-shadow: 0 1px 2px rgba(0,0,0,0.06);
}

.cloud-pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px;
  border-radius: var(--r-full);
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  font-size: 11px;
  color: var(--text-2);
  font-weight: 500;
}
.cloud-pill .dot {
  width: 6px; height: 6px;
  border-radius: var(--r-full);
  background: var(--muted);
}
.cloud-pill.connected { background: var(--success-bg); border-color: var(--success-bg); color: var(--success-text); }
.cloud-pill.connected .dot { background: var(--success); box-shadow: 0 0 0 2px rgba(5,150,105,0.18); }
.cloud-pill.syncing .dot { background: var(--warning); animation: pulse 1s infinite; }
.cloud-pill.error { background: var(--danger-bg); border-color: var(--danger-bg); color: var(--danger-text); }
.cloud-pill.error .dot { background: var(--danger); }
@keyframes pulse { 0%,100% {opacity:1;} 50% {opacity:0.4;} }

/* ============================================================
   OUTLET (router target)
   ============================================================ */

.outlet {
  padding: var(--sp-6);
  min-height: calc(100vh - var(--topbar-h));
  max-width: var(--container-max);
  width: 100%;
}
@media (max-width: 768px) { .outlet { padding: var(--sp-4); } }

.page-header {
  display: flex; align-items: flex-start; gap: var(--sp-4);
  margin-bottom: var(--sp-6);
}
.page-title-block { flex: 1; min-width: 0; }
.page-title {
  font-size: var(--text-2xl); font-weight: 700; color: var(--ink);
  letter-spacing: -0.02em; line-height: 1.2;
  margin: 0 0 4px;
}
.page-subtitle {
  font-size: var(--text-md);
  color: var(--muted);
  margin: 0;
}
.page-actions { display: flex; gap: var(--sp-2); align-items: center; flex-wrap: wrap; }

/* ============================================================
   BUTTONS
   ============================================================ */

.btn {
  display: inline-flex; align-items: center; gap: var(--sp-2);
  padding: 7px var(--sp-3);
  border-radius: var(--r-md);
  border: 1px solid var(--border-strong);
  background: white;
  color: var(--text);
  font-size: var(--text-md);
  font-weight: 500;
  line-height: 1.4;
  transition: all var(--duration-fast);
  white-space: nowrap;
}
.btn:hover { background: var(--bg-subtle); border-color: var(--muted); color: var(--ink); }
.btn:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--primary-ring); }
.btn[disabled] { opacity: 0.5; cursor: not-allowed; pointer-events: none; }
.btn-primary {
  background: var(--primary); color: white; border-color: var(--primary);
  box-shadow: 0 1px 2px rgba(79,70,229,0.20), inset 0 1px 0 rgba(255,255,255,0.18);
}
.btn-primary:hover { background: var(--primary-hover); color: white; border-color: var(--primary-hover); }
.btn-brand {
  background: var(--brand); color: white; border-color: var(--brand);
  box-shadow: 0 1px 2px rgba(5,150,105,0.20), inset 0 1px 0 rgba(255,255,255,0.18);
}
.btn-brand:hover { background: var(--brand-hover); border-color: var(--brand-hover); }
.btn-danger { color: var(--danger); }
.btn-danger:hover { background: var(--danger-bg); border-color: var(--danger); color: var(--danger-text); }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-ghost:hover { background: var(--bg-hover); border-color: transparent; }
.btn-sm { padding: 4px var(--sp-2); font-size: var(--text-sm); }
.btn-lg { padding: 9px var(--sp-4); font-size: var(--text-base); }
.btn-icon { padding: 7px; }
.btn-icon.btn-sm { padding: 4px; }

/* ============================================================
   CARDS
   ============================================================ */

.card {
  background: var(--bg-card);
  border-radius: var(--r-lg);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-card);
  overflow: hidden;
  transition: box-shadow var(--duration-base) var(--ease-out),
              transform var(--duration-base) var(--ease-out),
              border-color var(--duration-fast);
}
.card.interactive:hover {
  box-shadow: var(--shadow-hover);
  transform: translateY(-2px);
  border-color: rgba(203, 213, 225, 0.8);
}
.card-header {
  display: flex; align-items: center; gap: var(--sp-3);
  padding: var(--sp-4) var(--sp-5);
  border-bottom: 1px solid var(--divider);
}
.card-title {
  font-size: var(--text-lg);
  font-weight: 600;
  color: var(--ink);
  letter-spacing: -0.01em;
  margin: 0;
  flex: 1;
}
.card-subtitle {
  font-size: var(--text-sm); color: var(--muted); margin: 0;
}
.card-actions { display: flex; gap: var(--sp-2); }
.card-body { padding: var(--sp-5); }
.card-body.tight { padding: var(--sp-3); }
.card-body.dense { padding: 0; }

/* ============================================================
   KPI TILES
   ============================================================ */

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--sp-4);
  margin-bottom: var(--sp-4);
}
@media (max-width: 1280px) { .kpi-grid { grid-template-columns: repeat(2, 1fr); } }
@media (max-width: 640px) { .kpi-grid { grid-template-columns: 1fr; } }

.kpi {
  background: var(--bg-card);
  border-radius: var(--r-lg);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-card);
  padding: var(--sp-5);
  position: relative;
  overflow: hidden;
  transition: box-shadow var(--duration-base) var(--ease-out),
              transform var(--duration-base) var(--ease-out),
              border-color var(--duration-fast);
}
.kpi:hover {
  box-shadow: var(--shadow-hover);
  transform: translateY(-2px);
  border-color: rgba(203, 213, 225, 0.8);
}
.kpi-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  display: flex; align-items: center; gap: var(--sp-2);
  margin-bottom: var(--sp-3);
}
.kpi-label-icon {
  width: 18px; height: 18px;
  border-radius: var(--r-sm);
  display: grid; place-items: center;
  background: var(--primary-light);
  color: var(--primary);
}
.kpi-label-icon.brand { background: var(--brand-light); color: var(--brand); }
.kpi-label-icon.warning { background: var(--warning-bg); color: var(--warning); }
.kpi-label-icon.danger { background: var(--danger-bg); color: var(--danger); }
.kpi-label-icon svg { width: 11px; height: 11px; }

.kpi-value {
  font-size: var(--text-kpi);                /* 36px — refined up */
  font-weight: 700;
  color: var(--ink);
  letter-spacing: -0.03em;
  line-height: 1.05;
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1;
}
.kpi-value.negative { color: var(--danger); }     /* negative cashflow */
.kpi-value.small { font-size: var(--text-2xl); }
.kpi-currency {
  font-size: var(--text-lg);
  font-weight: 500;
  color: var(--muted);
  letter-spacing: 0;
  margin-right: 2px;
}
.kpi-delta {
  display: inline-flex; align-items: center; gap: 3px;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: var(--r-full);
  margin-top: var(--sp-2);
  font-variant-numeric: tabular-nums;
}
.kpi-delta.up { background: var(--success-bg); color: var(--success-text); }
.kpi-delta.down { background: var(--danger-bg); color: var(--danger-text); }
.kpi-delta.flat { background: var(--bg-subtle); color: var(--muted); }
.kpi-meta { font-size: var(--text-sm); color: var(--muted); margin-top: var(--sp-2); }

/* ============================================================
   DASHBOARD CHARTS
   ============================================================ */

.dashboard-grid {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: var(--sp-4);
  margin-bottom: var(--sp-4);
}
@media (max-width: 1024px) { .dashboard-grid { grid-template-columns: 1fr; } }

.chart-container { position: relative; height: 280px; padding: var(--sp-4) var(--sp-5); }
.chart-container.tall { height: 320px; }
.chart-container canvas { max-height: 100%; }

/* Top customers leaderboard */
.leaderboard { padding: var(--sp-2) var(--sp-3) var(--sp-4); }
.leader-row {
  display: grid;
  grid-template-columns: 18px 1fr auto;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-2) var(--sp-2);
  border-radius: var(--r-sm);
  cursor: pointer;
  transition: background var(--duration-fast);
}
.leader-row:hover { background: var(--bg-hover); }
.leader-rank {
  font-size: 10px; font-weight: 700;
  color: var(--muted);
  text-align: center;
}
.leader-rank.first { color: var(--warning); }
.leader-info { min-width: 0; }
.leader-name {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.leader-bar {
  margin-top: 4px;
  height: 4px;
  background: var(--divider);
  border-radius: var(--r-full);
  overflow: hidden;
}
.leader-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary) 0%, var(--primary-active) 100%);
  border-radius: var(--r-full);
  transition: width var(--duration-slow) var(--ease-out);
}
.leader-amount {
  text-align: right;
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1;
}
.leader-amount-meta { font-size: 10px; color: var(--muted); font-variant-numeric: tabular-nums; }

/* AR Aging stacked bars */
.aging-list { padding: var(--sp-3) var(--sp-3) var(--sp-4); display: flex; flex-direction: column; gap: var(--sp-3); }
.aging-row {
  display: grid;
  grid-template-columns: 80px 1fr auto;
  gap: var(--sp-3);
  align-items: center;
  cursor: pointer;
  padding: 4px var(--sp-2);
  border-radius: var(--r-sm);
  transition: background var(--duration-fast);
}
.aging-row:hover { background: var(--bg-hover); }
.aging-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-2);
  letter-spacing: 0.02em;
}
.aging-bar {
  height: 8px;
  background: var(--divider);
  border-radius: var(--r-full);
  overflow: hidden;
  position: relative;
}
.aging-fill {
  position: absolute; inset: 0 auto 0 0;
  border-radius: var(--r-full);
  transition: width var(--duration-slow) var(--ease-out);
}
.aging-fill.tier-0 { background: var(--success); }
.aging-fill.tier-1 { background: #84cc16; }   /* lime-500 */
.aging-fill.tier-2 { background: var(--warning); }
.aging-fill.tier-3 { background: #ea580c; }   /* orange-600 */
.aging-fill.tier-4 { background: var(--danger); }
.aging-amount {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--ink);
  white-space: nowrap;
}

/* Activity feed */
.activity-list { padding: var(--sp-2) 0; }
.activity-row {
  display: grid;
  grid-template-columns: 28px 1fr auto;
  gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-5);
  align-items: center;
  border-bottom: 1px solid var(--divider);
  transition: background var(--duration-fast);
  cursor: pointer;
}
.activity-row:last-child { border-bottom: none; }
.activity-row:hover { background: var(--bg-subtle); }
.activity-icon {
  width: 28px; height: 28px;
  border-radius: var(--r-md);
  display: grid; place-items: center;
}
.activity-icon.created { background: var(--primary-light); color: var(--primary-text); }
.activity-icon.paid { background: var(--success-bg); color: var(--success-text); }
.activity-icon.signed { background: var(--primary-light); color: var(--primary); }
.activity-icon.warning { background: var(--warning-bg); color: var(--warning-text); }
.activity-icon svg { width: 14px; height: 14px; }
.activity-text {
  font-size: var(--text-md);
  color: var(--text);
  line-height: 1.4;
}
.activity-text strong { color: var(--ink); font-weight: 600; }
.activity-meta { font-size: 11px; color: var(--muted); margin-top: 2px; }
.activity-time { font-size: var(--text-sm); color: var(--muted); white-space: nowrap; }

/* ============================================================
   DATA TABLE (Linear-style)
   ============================================================ */

.dt-wrap { overflow: auto; }
.dt {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: var(--text-md);
}
.dt thead th {
  background: var(--bg-subtle);
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  text-align: left;
  padding: var(--sp-2) var(--sp-4);   /* Stripe-style horizontal padding */
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0;
}
.dt thead th.right, .dt thead th.num { text-align: right; }
.dt thead th.center { text-align: center; }
.dt tbody td {
  padding: 10px var(--sp-4);          /* py-2.5 px-4 — Stripe Dashboard density */
  border-bottom: 1px solid var(--divider);
  color: var(--text);
  vertical-align: middle;
}
.dt tbody tr { transition: background var(--duration-fast); cursor: pointer; position: relative; }
.dt tbody tr:hover { background: var(--bg-subtle); }
.dt tbody tr:last-child td { border-bottom: none; }
.dt tbody td.right, .dt tbody td.num { text-align: right; font-variant-numeric: tabular-nums; font-feature-settings: "tnum" 1; }
.dt tbody td.center { text-align: center; }
.dt tbody td.mono { font-family: var(--font-mono); font-size: var(--text-sm); color: var(--text-2); }
.dt tbody td strong { color: var(--ink); font-weight: 600; }
.dt-empty {
  text-align: center;
  padding: var(--sp-12) var(--sp-4);
  color: var(--muted);
  font-size: var(--text-md);
}

/* Row hover quick-actions */
.dt-row-actions {
  display: inline-flex;
  gap: 2px;
  align-items: center;
  opacity: 0;
  transition: opacity var(--duration-fast);
}
.dt tbody tr:hover .dt-row-actions { opacity: 1; }
.dt-row-actions button {
  width: 26px; height: 26px;
  border: none;
  background: transparent;
  border-radius: var(--r-sm);
  color: var(--muted);
  cursor: pointer;
  display: inline-grid; place-items: center;
  transition: all var(--duration-fast);
}
.dt-row-actions button:hover { background: white; color: var(--ink); box-shadow: var(--shadow-xs); }
.dt-row-actions button svg { width: 14px; height: 14px; }

/* Filter chips */
.filter-chips {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-2);
  margin-bottom: var(--sp-4);
}
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 5px var(--sp-3);
  border-radius: var(--r-full);
  font-size: var(--text-sm); font-weight: 500;
  background: white;
  border: 1px solid var(--border);
  color: var(--text);
  transition: all var(--duration-fast);
}
.chip:hover { border-color: var(--primary-soft); color: var(--ink); }
.chip.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}
.chip-count {
  font-size: 10px;
  background: rgba(0,0,0,0.06);
  padding: 1px 6px;
  border-radius: var(--r-full);
}
.chip.active .chip-count { background: rgba(255,255,255,0.20); color: white; }

/* ============================================================
   PILLS / STATUS BADGES
   ============================================================ */

.pill {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 2px 8px;
  border-radius: var(--r-full);
  font-size: 11px;
  font-weight: 600;
  line-height: 1.4;
  white-space: nowrap;
}
.pill::before {
  content: '';
  width: 6px; height: 6px;
  border-radius: var(--r-full);
  background: currentColor;
  flex-shrink: 0;
}
.pill-draft   { background: var(--bg-subtle); color: var(--muted); border: 1px solid var(--border); }
.pill-sent    { background: var(--primary-light); color: var(--primary-text); }
.pill-accepted{ background: var(--success-bg); color: var(--success-text); }
.pill-rejected{ background: var(--danger-bg); color: var(--danger-text); }
.pill-expired { background: var(--warning-bg); color: var(--warning-text); }
.pill-converted{ background: #f3e8ff; color: #6b21a8; }
.pill-paid    { background: var(--success-bg); color: var(--success-text); }
.pill-partial { background: var(--warning-bg); color: var(--warning-text); }
.pill-overdue { background: var(--danger-bg); color: var(--danger-text); }
.pill-signed  { background: var(--success-bg); color: var(--success-text); }
.pill-issued  { background: var(--primary-light); color: var(--primary-text); }
.pill-pending { background: var(--bg-subtle); color: var(--muted); border: 1px solid var(--border); }
.pill-completed{ background: var(--success-bg); color: var(--success-text); }
.pill-cancelled{ background: var(--bg-subtle); color: var(--muted); text-decoration: line-through; }
.pill-in_progress{ background: var(--primary-light); color: var(--primary-text); }

/* ============================================================
   FORM INPUTS
   ============================================================ */

.field { margin-bottom: var(--sp-4); }
.field-label {
  display: block;
  font-size: var(--text-sm); font-weight: 500;
  color: var(--text); margin-bottom: 6px;
}
.field-required::after { content: ' *'; color: var(--danger); }
.input, .select, .textarea {
  width: 100%;
  padding: 7px var(--sp-3);
  font-size: var(--text-md);
  background: white;
  border: 1px solid var(--border-strong);
  border-radius: var(--r-md);
  color: var(--ink);
  transition: all var(--duration-fast);
}
.input:focus, .select:focus, .textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-ring);
}
.input::placeholder { color: var(--subtle); }
.textarea { resize: vertical; min-height: 72px; font-family: inherit; }
.field-help { font-size: 11px; color: var(--muted); margin-top: 4px; }

/* Validation: red border + small inline error message under the field */
.input-error,
.input.input-error,
.select.input-error,
.textarea.input-error {
  border-color: var(--danger) !important;
  background: var(--danger-bg, #fef2f2);
}
.input-error:focus { box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.18) !important; }
.field-error {
  font-size: 11px;
  color: var(--danger);
  margin-top: 4px;
  font-weight: 500;
}

.input-group {
  display: flex;
  align-items: stretch;     /* prefix and input share height */
  gap: 0;                   /* attached look — visual joined element */
}
.input-group-prefix {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 var(--sp-3);
  background: var(--bg-subtle);
  border: 1px solid var(--border-strong);
  border-right: none;
  border-radius: var(--r-md) 0 0 var(--r-md);
  color: var(--muted);
  font-size: var(--text-sm);
  font-weight: 500;
  white-space: nowrap;       /* never wrap "1 USD =" */
  line-height: 1;
}
.input-group .input {
  flex: 1 1 auto;
  width: auto;
  min-width: 0;
  border-radius: 0 var(--r-md) var(--r-md) 0;
}

/* Exchange rate row: input-group + a refresh button to its right */
.input-group.rate-group {
  align-items: stretch;
  flex-wrap: wrap;
  gap: var(--sp-2);
}
.input-group.rate-group .input-group-prefix,
.input-group.rate-group .input {
  /* keep prefix + input attached even with gap on the parent */
  margin: 0;
}
.input-group.rate-group .input {
  flex: 1 1 200px;
}
.rate-fetch {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  padding: 0 var(--sp-3);
  background: white;
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  color: var(--text);
  font-size: var(--text-sm);
  cursor: pointer;
  transition: all var(--duration-fast);
  white-space: nowrap;
  line-height: 1;
}
.rate-fetch:hover { border-color: var(--primary); color: var(--primary); background: var(--primary-light); }
.rate-fetch:disabled { opacity: 0.7; cursor: progress; }
.rate-fetch:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
/* When disabled (in-flight): swap the static refresh icon for a spinner */
.rate-fetch:disabled svg { animation: rate-spin 0.7s linear infinite; }
@keyframes rate-spin {
  from { transform: rotate(0deg); }
  to   { transform: rotate(360deg); }
}
.rate-note {
  font-size: 11px;
  margin-top: 4px;
  padding-left: var(--sp-1);
}

.row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: var(--sp-3); }
.row-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: var(--sp-3); }
.row-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: var(--sp-3); }
@media (max-width: 640px) { .row-2, .row-3, .row-4 { grid-template-columns: 1fr; } }

/* ============================================================
   DRAWER
   ============================================================ */

.drawer {
  position: fixed; inset: 0;
  z-index: 200;
  pointer-events: none;
}
.drawer:not([hidden]) { pointer-events: auto; }
.drawer-backdrop {
  position: absolute; inset: 0;
  background: var(--overlay-soft);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  opacity: 0;
  transition: opacity var(--duration-slow) var(--ease-out);
}
.drawer:not([hidden]) .drawer-backdrop { opacity: 1; }
.drawer-panel {
  position: absolute; right: 0; top: 0; bottom: 0;
  width: var(--drawer-w);
  background: white;
  box-shadow: var(--shadow-drawer);
  display: flex; flex-direction: column;
  transform: translateX(100%);
  transition: transform var(--duration-slow) var(--ease-out);
}
.drawer:not([hidden]) .drawer-panel { transform: translateX(0); }
@media (max-width: 768px) {
  /* Mobile: drawer becomes full-screen overlay with stronger dim */
  .drawer-backdrop { background: var(--overlay-strong); backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px); }
  .drawer-panel { width: 100%; }
}

.drawer-header {
  display: flex; align-items: center; gap: var(--sp-3);
  padding: var(--sp-4) var(--sp-5);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0;
  background: white;
  z-index: 1;
}
.drawer-close, .drawer-more {
  background: transparent; border: none; color: var(--muted);
  width: 32px; height: 32px; border-radius: var(--r-sm);
  display: grid; place-items: center;
  cursor: pointer;
  transition: all var(--duration-fast);
}
.drawer-close:hover, .drawer-more:hover { background: var(--bg-hover); color: var(--ink); }
.drawer-title {
  flex: 1;
  font-size: var(--text-base); font-weight: 600; color: var(--ink);
  min-width: 0;
}
.drawer-title .subtitle { font-size: var(--text-sm); color: var(--muted); font-weight: 400; margin-top: 1px; }
.drawer-body {
  flex: 1; overflow-y: auto;
  padding: var(--sp-5);
  /* Isolate scrolling: prevent scroll chaining into the body underneath
     when user reaches the top/bottom of the drawer content. Modern
     browsers + iOS 16+; older iOS falls back to JS body-lock in drawer.js. */
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.drawer-section { margin-bottom: var(--sp-5); }
.drawer-section-title {
  font-size: 10px; font-weight: 600; text-transform: uppercase;
  letter-spacing: 0.10em; color: var(--muted);
  margin-bottom: var(--sp-3);
}
.drawer-stat-list { display: flex; flex-direction: column; gap: var(--sp-3); }
.drawer-stat {
  display: flex; justify-content: space-between; align-items: baseline;
  font-size: var(--text-md);
}
.drawer-stat .label { color: var(--muted); }
.drawer-stat .value { color: var(--ink); font-weight: 600; }
.drawer-stat .value.big { font-size: var(--text-lg); }

.drawer-totals {
  background: var(--bg-subtle);
  border-radius: var(--r-md);
  padding: var(--sp-3) var(--sp-4);
  display: flex; flex-direction: column; gap: 6px;
}
.drawer-total-row {
  display: flex; justify-content: space-between;
  font-size: var(--text-md);
}
.drawer-total-row.divide-top { border-top: 1px solid var(--border); padding-top: var(--sp-2); margin-top: 2px; }
.drawer-total-row.grand { font-weight: 700; color: var(--ink); font-size: var(--text-base); }

/* Drawer actions: hierarchical priority — Primary (full width) | Secondary (grid) | Danger (separated) */
.drawer-actions { display: flex; flex-direction: column; gap: var(--sp-2); }
.drawer-actions-primary .btn {
  width: 100%;
  justify-content: center;
  padding: 10px var(--sp-4);   /* slightly taller for emphasis */
}
.drawer-actions-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: var(--sp-2);
}
.drawer-actions-grid .btn { justify-content: center; }
.drawer-actions-danger {
  margin-top: var(--sp-3);
  padding-top: var(--sp-3);
  border-top: 1px solid var(--divider);
}
.drawer-actions-danger .btn { width: 100%; justify-content: center; }

/* Phase 2 placeholder buttons: visually muted + small P2 tag */
.btn.phase2 {
  position: relative;
  opacity: 0.85;
}
.btn.phase2 .phase2-tag {
  display: inline-block;
  background: var(--bg-subtle);
  color: var(--muted);
  font-size: 9px;
  font-weight: 700;
  padding: 1px 5px;
  border-radius: 3px;
  margin-left: 6px;
  letter-spacing: 0.04em;
  vertical-align: middle;
  border: 1px solid var(--border);
}
.btn-danger.phase2 .phase2-tag {
  background: rgba(255,255,255,0.18);
  color: white;
  border-color: rgba(255,255,255,0.25);
}

/* Timeline */
.timeline { list-style: none; padding: 0; margin: 0; position: relative; }
.timeline::before {
  content: ''; position: absolute; left: 5px; top: 8px; bottom: 8px; width: 1px;
  background: var(--border);
}
.timeline-item {
  position: relative;
  padding-left: var(--sp-5);
  padding-bottom: var(--sp-3);
}
.timeline-item::before {
  content: ''; position: absolute; left: 2px; top: 4px;
  width: 7px; height: 7px;
  border-radius: var(--r-full);
  background: var(--primary);
  border: 2px solid white;
  box-shadow: 0 0 0 1px var(--primary);
}
.timeline-item.success::before { background: var(--success); box-shadow: 0 0 0 1px var(--success); }
.timeline-item.warning::before { background: var(--warning); box-shadow: 0 0 0 1px var(--warning); }
.timeline-text { font-size: var(--text-md); color: var(--text); }
.timeline-text strong { color: var(--ink); }
.timeline-meta { font-size: 11px; color: var(--muted); margin-top: 2px; }

/* ============================================================
   COMMAND PALETTE
   ============================================================ */

.cmd-palette {
  position: fixed; inset: 0;
  z-index: 300;
  pointer-events: none;
}
.cmd-palette:not([hidden]) { pointer-events: auto; }
.cmd-backdrop {
  position: absolute; inset: 0;
  background: rgba(15,23,42,0.34);
  backdrop-filter: blur(2px);
  opacity: 0;
  transition: opacity var(--duration-base);
}
.cmd-palette:not([hidden]) .cmd-backdrop { opacity: 1; }
.cmd-panel {
  position: absolute; left: 50%; top: 15%;
  transform: translateX(-50%) translateY(-12px);
  width: 600px; max-width: 92vw;
  background: white;
  border-radius: var(--r-xl);
  box-shadow: var(--shadow-modal);
  border: 1px solid var(--border);
  overflow: hidden;
  opacity: 0;
  transition: all var(--duration-base) var(--ease-out);
}
.cmd-palette:not([hidden]) .cmd-panel {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.cmd-input-row {
  display: flex; align-items: center; gap: var(--sp-3);
  padding: var(--sp-4) var(--sp-5);
  border-bottom: 1px solid var(--divider);
}
.cmd-input-row svg { color: var(--muted); flex-shrink: 0; }
.cmd-input-row input {
  flex: 1; border: none; outline: none;
  font-size: var(--text-lg);
  background: transparent;
  color: var(--ink);
}
.cmd-input-row input::placeholder { color: var(--subtle); }
.cmd-input-row kbd {
  font-family: var(--font-mono); font-size: 10px;
  background: var(--bg-subtle); border: 1px solid var(--border);
  border-radius: var(--r-xs); padding: 1px 5px;
  color: var(--muted);
}

.cmd-results { max-height: 420px; overflow-y: auto; padding: var(--sp-2); }
.cmd-group { margin-bottom: var(--sp-3); }
.cmd-group:last-child { margin-bottom: 0; }
.cmd-group-title {
  font-size: 10px; font-weight: 600; text-transform: uppercase;
  letter-spacing: 0.10em; color: var(--muted);
  padding: var(--sp-2) var(--sp-3) var(--sp-1);
}
.cmd-item {
  display: flex; align-items: center; gap: var(--sp-3);
  padding: var(--sp-2) var(--sp-3);
  border-radius: var(--r-md);
  cursor: pointer;
  font-size: var(--text-md);
  color: var(--text);
  transition: background var(--duration-fast);
}
.cmd-item:hover, .cmd-item.active {
  background: var(--primary-light);
  color: var(--primary-active);
}
.cmd-item-icon {
  width: 22px; height: 22px;
  border-radius: var(--r-sm);
  background: var(--bg-subtle);
  display: grid; place-items: center;
  color: var(--muted);
  flex-shrink: 0;
}
.cmd-item.active .cmd-item-icon { background: var(--primary); color: white; }
.cmd-item-icon svg { width: 12px; height: 12px; }
.cmd-item-text { flex: 1; min-width: 0; }
.cmd-item-title { color: var(--ink); font-weight: 500; }
.cmd-item.active .cmd-item-title { color: var(--primary-active); }
.cmd-item-sub { font-size: 11px; color: var(--muted); }
.cmd-item-amount { font-variant-numeric: tabular-nums; font-size: var(--text-sm); font-weight: 600; color: var(--text-2); }
.cmd-empty { padding: var(--sp-8) var(--sp-4); text-align: center; color: var(--muted); font-size: var(--text-md); }

/* ============================================================
   TOAST
   ============================================================ */
.toast {
  position: fixed; bottom: var(--sp-6); right: var(--sp-6);
  z-index: 400;
  padding: var(--sp-3) var(--sp-4);
  background: var(--ink);
  color: white;
  border-radius: var(--r-md);
  box-shadow: var(--shadow-popover);
  font-size: var(--text-md);
  font-weight: 500;
  transform: translateY(8px);
  opacity: 0;
  transition: all var(--duration-base) var(--ease-out);
  pointer-events: none;
}
.toast.show { transform: translateY(0); opacity: 1; }
.toast.success { background: var(--brand); }
.toast.error { background: var(--danger); }

/* ============================================================
   QUOTATION EDITOR (Focus Mode)
   ============================================================ */
.editor-shell {
  display: grid;
  grid-template-columns: 1fr 320px;
  gap: var(--sp-5);
  align-items: start;
}
@media (max-width: 1024px) { .editor-shell { grid-template-columns: 1fr; } }

.editor-header {
  display: flex; align-items: center; gap: var(--sp-3);
  margin-bottom: var(--sp-5);
  /* Sticky so 取消 / 儲存 are always accessible while scrolling through items.
     Top offset matches the topbar height so it lands right under the app bar. */
  position: sticky;
  top: var(--topbar-h);
  z-index: 5;
  background: var(--bg-app);
  padding: var(--sp-3) 0;
  margin-top: calc(-1 * var(--sp-3));
  border-bottom: 1px solid transparent;
  transition: border-color var(--duration-fast), box-shadow var(--duration-fast);
}
.editor-header.is-stuck {
  border-bottom-color: var(--divider);
  box-shadow: 0 2px 4px -2px rgba(0,0,0,0.04);
}
.editor-back {
  background: transparent; border: none; color: var(--muted);
  padding: var(--sp-2); border-radius: var(--r-sm);
  display: grid; place-items: center;
}
.editor-back:hover { background: var(--bg-hover); color: var(--ink); }
.editor-title-block { flex: 1; min-width: 0; }
.editor-title { font-size: var(--text-xl); font-weight: 700; color: var(--ink); }
.editor-meta { font-size: var(--text-sm); color: var(--muted); margin-top: 2px; }
.editor-actions { display: flex; gap: var(--sp-2); align-items: center; }

.editor-section-block {
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: var(--sp-3);
  margin-bottom: var(--sp-3);
  background: white;
  transition: border-color var(--duration-fast);
}
.editor-section-block.drop-target { border-color: var(--primary); border-style: dashed; background: var(--primary-light); }
.editor-section-header {
  display: flex; align-items: center; gap: var(--sp-2);
  padding: var(--sp-1) var(--sp-2);
  margin-bottom: var(--sp-2);
}
.editor-section-name {
  flex: 1;
  font-size: var(--text-sm); font-weight: 600; color: var(--ink);
  background: transparent; border: 1px solid transparent;
  padding: 4px var(--sp-2); border-radius: var(--r-sm);
  text-transform: uppercase; letter-spacing: 0.04em;
}
.editor-section-name:hover { border-color: var(--border); background: var(--bg-subtle); }
.editor-section-name:focus { outline: none; border-color: var(--primary); background: white; }
.editor-item-list { display: flex; flex-direction: column; gap: 2px; }

/* Column header row above items — same grid as .editor-item so columns align */
.editor-item-colhead {
  display: grid;
  grid-template-columns: 18px minmax(0, 1fr) 110px 64px 70px 110px 28px;
  gap: var(--sp-2);
  padding: 6px var(--sp-2) 4px;
  font-size: 10px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  border-bottom: 1px solid var(--divider);
  margin-bottom: 4px;
}
.editor-item-colhead.has-cmp {
  grid-template-columns: 18px minmax(0, 1fr) 96px 56px 60px 96px 96px 28px;
}
.editor-item-colhead .ch-price,
.editor-item-colhead .ch-qty,
.editor-item-colhead .ch-total,
.editor-item-colhead .ch-total-cmp { text-align: right; }
.editor-item-colhead .ch-unit { text-align: center; }
/* Currency symbols must keep their own casing — Rp must NOT become RP */
.no-uppercase { text-transform: none !important; letter-spacing: 0 !important; }

/* Inline checkbox style (used by "顯示美金對照" toggle in items card header) */
.checkbox-inline {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: var(--text-sm);
  color: var(--text);
  cursor: pointer;
  user-select: none;
  margin-right: var(--sp-2);
}
.checkbox-inline input[type="checkbox"] {
  width: 14px;
  height: 14px;
  cursor: pointer;
  accent-color: var(--primary);
}

@media (max-width: 1180px) {
  .editor-item-colhead {
    grid-template-columns: 18px minmax(0, 1fr) 96px 56px 60px 100px 28px;
  }
}
@media (max-width: 768px) {
  .editor-item-colhead { display: none; }   /* mobile uses stacked layout with placeholders */
}

.editor-item {
  display: grid;
  /* handle | description | price | unit | qty | total | remove */
  grid-template-columns: 18px minmax(0, 1fr) 110px 64px 70px 110px 28px;
  gap: var(--sp-2);
  padding: 6px var(--sp-2);
  border-radius: var(--r-sm);
  /* Start (top) alignment so multi-line description reads naturally and other
     short inputs sit at the same baseline as the first line of description. */
  align-items: start;
  font-size: var(--text-md);
  transition: background var(--duration-fast);
  cursor: grab;
}
/* When USD comparison column is enabled, expand grid to 8 cols */
.editor-item.has-cmp {
  grid-template-columns: 18px minmax(0, 1fr) 96px 56px 60px 96px 96px 28px;
}
.editor-item:hover { background: var(--bg-subtle); }
.editor-item.dragging { opacity: 0.5; cursor: grabbing; }
.editor-item-handle {
  color: var(--faint);
  font-size: 16px;
  text-align: center;
  user-select: none;
}
.editor-item-handle:hover { color: var(--muted); }
.editor-item input {
  background: transparent;
  border: 1px solid transparent;
  padding: 3px var(--sp-2);
  border-radius: var(--r-xs);
  font-size: inherit;
  width: 100%;
  min-width: 0;     /* allow shrink inside grid */
}
.editor-item input:hover { background: white; border-color: var(--border); }
.editor-item input:focus { background: white; border-color: var(--primary); outline: none; box-shadow: 0 0 0 2px var(--primary-ring); }
.editor-item input.text-right { text-align: right; }
.editor-item input.text-center { text-align: center; }

/* Description textarea: same look as inputs, but multi-line + auto-grow */
.editor-item textarea.ei-desc {
  background: transparent;
  border: 1px solid transparent;
  padding: 3px var(--sp-2);
  border-radius: var(--r-xs);
  font-size: inherit;
  font-family: inherit;
  width: 100%;
  min-width: 0;
  /* Multi-line behavior: wrap long content, grow vertically via JS */
  resize: none;
  overflow: hidden;
  min-height: 26px;
  line-height: 1.4;
  /* Wrap long words and URLs that lack natural break opportunities */
  word-break: break-word;
  overflow-wrap: break-word;
}
.editor-item textarea.ei-desc:hover { background: white; border-color: var(--border); }
.editor-item textarea.ei-desc:focus { background: white; border-color: var(--primary); outline: none; box-shadow: 0 0 0 2px var(--primary-ring); }

/* When a row has a multi-line description, align other inputs to top
   so the row reads top-to-bottom rather than vertically centered.
   (Set on .editor-item parent above; here just minor padding tweaks.) */
.editor-item .editor-item-handle { padding-top: 6px; }
.editor-item .ei-remove { margin-top: 2px; }
.editor-item .item-total {
  text-align: right; font-weight: 600; color: var(--ink);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.editor-item .item-total-cmp {
  text-align: right; font-weight: 500; color: var(--muted);
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.editor-item .ei-remove {
  background: transparent; border: none; color: var(--muted);
  width: 28px; height: 28px; padding: 0;
  border-radius: var(--r-xs);
  display: grid; place-items: center;
  opacity: 0;
  transition: all var(--duration-fast);
}
.editor-item:hover .ei-remove,
.editor-item:focus-within .ei-remove { opacity: 1; }
.editor-item .ei-remove:hover { color: var(--danger); background: var(--danger-bg); }

/* Tablet: shrink price + qty inputs */
@media (max-width: 1180px) {
  .editor-item {
    grid-template-columns: 18px minmax(0, 1fr) 96px 56px 60px 100px 28px;
  }
  .editor-item.has-cmp {
    grid-template-columns: 18px minmax(0, 1fr) 84px 50px 56px 88px 88px 28px;
  }
  .editor-item-colhead.has-cmp {
    grid-template-columns: 18px minmax(0, 1fr) 84px 50px 56px 88px 88px 28px;
  }
}

.editor-add-item {
  padding: 5px var(--sp-2);
  background: transparent; border: 1px dashed var(--border);
  border-radius: var(--r-sm);
  color: var(--muted);
  font-size: var(--text-sm);
  width: 100%;
  text-align: left;
  margin-top: var(--sp-1);
  transition: all var(--duration-fast);
}
.editor-add-item:hover { border-color: var(--primary); color: var(--primary); background: var(--primary-light); }

.editor-totals {
  margin-top: var(--sp-4);
  padding: var(--sp-4) var(--sp-5);
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  display: flex; flex-direction: column; gap: 6px;
  font-variant-numeric: tabular-nums;
}
.editor-total-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: var(--sp-4);
  font-size: var(--text-md);
  align-items: baseline;
}
.editor-total-row .label { color: var(--muted); }
.editor-total-row .label.bold { color: var(--ink); font-weight: 700; }
.editor-total-row .value-idr { text-align: right; font-weight: 600; color: var(--ink); }
.editor-total-row .value-usd { font-size: 11px; color: var(--muted); text-align: right; min-width: 90px; }
.editor-total-row.grand .value-idr { color: var(--primary); font-size: var(--text-lg); font-weight: 700; }
.editor-total-row.discount input {
  background: white; border: 1px solid var(--border-strong);
  padding: 3px var(--sp-2); border-radius: var(--r-sm);
  text-align: right; font-variant-numeric: tabular-nums;
  font-size: var(--text-md); width: 140px;
}
.editor-total-row.discount input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 2px var(--primary-ring); }
.editor-divider { border-top: 1px solid var(--border); margin: 4px 0; }

/* Catalog side panel */
.catalog-panel {
  position: sticky; top: calc(var(--topbar-h) + var(--sp-5));
  background: white;
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-card);
  display: flex; flex-direction: column;
  max-height: calc(100vh - var(--topbar-h) - var(--sp-10));
}
.catalog-search {
  padding: var(--sp-3);
  border-bottom: 1px solid var(--divider);
}
.catalog-list {
  flex: 1;
  overflow-y: auto;
  padding: var(--sp-2);
}
.catalog-item {
  padding: var(--sp-2) var(--sp-3);
  border-radius: var(--r-md);
  margin-bottom: 2px;
  cursor: grab;
  transition: all var(--duration-fast);
  display: grid;
  grid-template-columns: 1fr auto;
  gap: var(--sp-2);
  border: 1px solid transparent;
  touch-action: manipulation;   /* mobile: prevent drag from stealing tap */
}
.catalog-item:hover { background: var(--primary-light); border-color: var(--primary-soft); }
.catalog-item:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.catalog-item.dragging { opacity: 0.4; }
.catalog-item-desc {
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--ink);
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.catalog-item-meta { font-size: 10px; color: var(--muted); margin-top: 2px; }
.catalog-item-price {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--primary);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  align-self: start;
}

/* ============================================================
   UTILITIES
   ============================================================ */
.flex { display: flex; }
.flex-1 { flex: 1; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.gap-1 { gap: var(--sp-1); }
.gap-2 { gap: var(--sp-2); }
.gap-3 { gap: var(--sp-3); }
.gap-4 { gap: var(--sp-4); }
.text-right { text-align: right; }
.text-center { text-align: center; }
.text-muted { color: var(--muted); }
.text-success { color: var(--success); }
.text-danger { color: var(--danger); }
.text-warning { color: var(--warning); }
.font-mono { font-family: var(--font-mono); }
.font-medium { font-weight: 500; }
.font-semibold { font-weight: 600; }
.font-bold { font-weight: 700; }
.mb-2 { margin-bottom: var(--sp-2); }
.mb-3 { margin-bottom: var(--sp-3); }
.mb-4 { margin-bottom: var(--sp-4); }
.mb-5 { margin-bottom: var(--sp-5); }
.mt-2 { margin-top: var(--sp-2); }
.mt-3 { margin-top: var(--sp-3); }
.mt-4 { margin-top: var(--sp-4); }
.mt-5 { margin-top: var(--sp-5); }

/* Skeleton */
.skeleton {
  background: linear-gradient(90deg, var(--divider) 0%, var(--bg-hover) 50%, var(--divider) 100%);
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite;
  border-radius: var(--r-sm);
}
@keyframes shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

/* Empty state */
.empty-state {
  padding: var(--sp-12) var(--sp-4);
  text-align: center;
  color: var(--muted);
}
.empty-state-icon {
  width: 56px; height: 56px;
  border-radius: var(--r-full);
  background: var(--bg-subtle);
  display: grid; place-items: center;
  margin: 0 auto var(--sp-3);
  color: var(--subtle);
}
.empty-state-title {
  font-size: var(--text-base); font-weight: 600; color: var(--ink);
  margin-bottom: 4px;
}
.empty-state-msg { font-size: var(--text-md); }

/* ============================================================
   MOBILE RWD — Complete responsive transformation
   ============================================================
   - Tables collapse into card list at <768px
   - Drawer becomes full-screen with stronger dim
   - Sidebar swipes in as overlay (not push)
   - Topbar compacts
   - Charts resize
   ============================================================ */

/* Mobile card list (replaces table) */
.mobile-card-list { display: none; }

@media (max-width: 768px) {
  /* Hide table, show card list */
  .dt-wrap { display: none; }
  .mobile-card-list { display: flex; flex-direction: column; gap: var(--sp-2); padding: var(--sp-2) 0; }
  .mobile-row {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--r-md);
    padding: var(--sp-3) var(--sp-4);
    cursor: pointer;
    transition: all var(--duration-fast);
  }
  .mobile-row:active { transform: scale(0.99); background: var(--bg-subtle); }
  .mobile-row-top {
    display: flex; justify-content: space-between; align-items: flex-start; gap: var(--sp-2);
    margin-bottom: var(--sp-1);
  }
  .mobile-row-id {
    font-family: var(--font-mono); font-size: var(--text-sm); color: var(--muted);
  }
  .mobile-row-name {
    font-size: var(--text-base); font-weight: 600; color: var(--ink);
    line-height: 1.3;
  }
  .mobile-row-subtitle {
    font-size: var(--text-sm); color: var(--muted);
    margin-top: 2px; line-height: 1.4;
  }
  .mobile-row-bottom {
    display: flex; justify-content: space-between; align-items: center;
    margin-top: var(--sp-2); gap: var(--sp-2);
  }
  .mobile-row-amount {
    font-size: var(--text-base); font-weight: 600; color: var(--ink);
    font-variant-numeric: tabular-nums;
  }

  /* Filter chips: scroll horizontally on mobile */
  .filter-chips { overflow-x: auto; flex-wrap: nowrap; padding-bottom: var(--sp-2); -webkit-overflow-scrolling: touch; }
  .filter-chips::-webkit-scrollbar { height: 0; }
  .filter-chip,
  .chip {
    flex-shrink: 0;
    /* Apple HIG / WCAG: min 44x44 tap target on touch */
    min-height: 36px;
    padding: 10px var(--sp-3);
  }

  /* Topbar mobile compaction — use actual HTML selectors */
  .topbar {
    gap: 6px;
    padding: 0 10px;
  }
  /* Mobile breadcrumb: show only the current (deepest) page so user knows where they are */
  .breadcrumb { display: flex; }
  .breadcrumb .crumb:not(.current),
  .breadcrumb .separator { display: none; }
  .breadcrumb .crumb.current {
    font-size: var(--text-base);
    font-weight: 600;
    color: var(--ink);
    max-width: 50vw;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  /* Search trigger becomes 36px icon */
  .search-trigger {
    min-width: 36px;
    width: 36px;
    height: 36px;
    padding: 0;
    justify-content: center;
    flex-shrink: 0;
  }
  .search-trigger span, .search-trigger kbd { display: none; }
  /* Quick Action: icon only on mobile */
  .quick-action .btn-primary {
    padding: 7px 10px;
    flex-shrink: 0;
  }
  .quick-action .btn-primary > span { display: none; }
  .quick-action .btn-primary > svg:last-child { display: none; }
  /* Language switcher: compact */
  .lang-switcher { padding: 1px; flex-shrink: 0; }
  .lang-btn { font-size: 10px; padding: 3px 6px; }
  /* Demo cloud-pill: hide on narrow screens (saves space) */
  .cloud-pill { display: none; }

  /* Page padding tighter */
  .page-header { padding: var(--sp-4) var(--sp-4) 0; }
  .page-title { font-size: var(--text-xl); }
  .outlet { padding: var(--sp-3); }

  /* Drawer footer becomes sticky */
  .drawer-body { padding-bottom: var(--sp-12); }
  .drawer-actions { position: sticky; bottom: 0; background: white;
    margin: 0 calc(-1 * var(--sp-5)); padding: var(--sp-4) var(--sp-5);
    border-top: 1px solid var(--divider);
    z-index: 2; }

  /* Toast: position higher up on mobile so it isn't covered by sticky drawer actions */
  .toast {
    bottom: auto;
    top: calc(var(--topbar-h) + var(--sp-3));
    left: 50%;
    right: auto;
    transform: translate(-50%, -8px);
    max-width: calc(100vw - var(--sp-6));
    z-index: 500;        /* above drawer (200) and sticky drawer-actions */
  }
  .toast.show { transform: translate(-50%, 0); }

  /* Editor: stack catalog below form on mobile */
  .editor-shell { grid-template-columns: 1fr !important; }
  .catalog-panel { position: static !important; max-height: 360px; overflow-y: auto; }
  /* Editor header sticks above keyboard for save access */
  .editor-header {
    position: sticky; top: var(--topbar-h);
    background: var(--bg-app);
    z-index: 10;
    padding: var(--sp-2) 0;
    margin: 0 calc(-1 * var(--sp-3)) var(--sp-3);
    padding-left: var(--sp-3); padding-right: var(--sp-3);
    border-bottom: 1px solid var(--divider);
  }
  .editor-title { font-size: var(--text-base); }
  .editor-meta { font-size: 11px; }

  /* Editor items: stack into 2 rows on mobile (description on top, controls below) */
  .editor-item {
    grid-template-columns: 18px minmax(0, 1fr) 28px;
    grid-template-areas:
      "handle desc    remove"
      "handle controls controls";
    row-gap: 4px;
    column-gap: var(--sp-2);
    padding: var(--sp-2);
    border: 1px solid var(--divider);
    border-radius: var(--r-md);
    background: white;
  }
  .editor-item-handle { grid-area: handle; align-self: center; }
  .editor-item .ei-desc { grid-area: desc; font-weight: 500; }
  .editor-item .ei-remove { grid-area: remove; opacity: 1; align-self: start; }
  .editor-item .ei-price,
  .editor-item .ei-unit,
  .editor-item .ei-qty,
  .editor-item .ei-total {
    grid-area: controls;
  }
  /* Controls row becomes a sub-grid */
  .editor-item::after {
    /* no decoration; we use real inline-grid for controls instead */
    content: none;
  }
  /* Group the 4 control children inside the controls cell */
  .editor-item .ei-price { grid-column: 1; }
  .editor-item .ei-unit  { grid-column: 1; }
  .editor-item .ei-qty   { grid-column: 1; }
  .editor-item .ei-total { grid-column: 1; }
  /* Override grid-area for controls: use a flex row */
  .editor-item {
    /* Use flex layout instead of complex grid areas for mobile reliability */
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--sp-2);
  }
  .editor-item .editor-item-handle { flex: 0 0 18px; }
  .editor-item .ei-desc {
    flex: 1 1 100%;
    order: 1;
    background: white;
    border: 1px solid var(--border);
    padding: 8px var(--sp-2);
    font-size: var(--text-md);
  }
  .editor-item .ei-remove {
    flex: 0 0 44px;
    order: 2;
    /* Apple HIG / WCAG: tap targets ≥ 44x44 on touch devices */
    width: 44px; height: 44px;
    min-width: 44px;
    opacity: 1;            /* always visible on touch — no hover state */
  }
  /* Section delete button: also bump to comfortable touch size */
  .editor-section-header .btn-sm {
    min-width: 44px; min-height: 44px;
    padding: 10px;
  }
  .editor-item .ei-price  {
    flex: 1 1 0; order: 3; min-width: 0;
    border: 1px solid var(--border); background: white;
    padding: 6px var(--sp-2);
  }
  .editor-item .ei-unit   {
    flex: 0 0 60px; order: 4;
    border: 1px solid var(--border); background: white;
    padding: 6px var(--sp-2);
  }
  .editor-item .ei-qty    {
    flex: 0 0 70px; order: 5;
    border: 1px solid var(--border); background: white;
    padding: 6px var(--sp-2);
  }
  .editor-item .ei-total  {
    flex: 1 1 100%; order: 6;
    text-align: right;
    padding-right: var(--sp-2);
    font-size: var(--text-md);
  }
  .editor-item .ei-total-cmp {
    flex: 1 1 100%; order: 7;
    text-align: right;
    padding-right: var(--sp-2);
    font-size: 12px;
    color: var(--muted);
  }

  /* Exchange rate input group: prefix + input stay on one row, refresh button wraps below */
  .input-group.rate-group {
    flex-wrap: wrap;
  }
  .input-group.rate-group .input-group-prefix {
    flex: 0 0 auto;
  }
  .input-group.rate-group .input {
    flex: 1 1 0;
    min-width: 100px;
  }
  .rate-fetch {
    flex: 1 1 100%;
    justify-content: center;
    padding: 10px var(--sp-3);
    margin-top: var(--sp-2);
  }
  .rate-note { font-size: 10px; }

  /* Totals row: ensure value-usd doesn't get pushed off screen */
  .editor-total-row {
    grid-template-columns: 1fr auto;
    column-gap: var(--sp-3);
  }
  .editor-total-row .value-usd {
    grid-column: 2;
    min-width: 0;
    font-size: 10px;
  }
  .editor-total-row.discount {
    grid-template-columns: 1fr auto auto;
  }
  .editor-total-row.discount input { width: 100px; }

  /* Catalog items: full tap target on mobile, hide drag handle, prefix + icon */
  .catalog-item {
    padding: 12px var(--sp-3);
    cursor: pointer;
  }
  .catalog-item:active { background: var(--primary-light); }
  .catalog-item::before {
    content: '+';
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 22px; height: 22px;
    border-radius: 50%;
    background: var(--primary-light);
    color: var(--primary);
    font-weight: 700;
    margin-right: var(--sp-2);
    flex-shrink: 0;
  }
  .catalog-item {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
  }
  .catalog-item > div:first-child { flex: 1; min-width: 0; }

  /* Cmd+K modal */
  .cmd-palette-modal { width: 92vw; max-width: 92vw; top: 8% !important; }

  /* Cards take less padding */
  .card-body { padding: var(--sp-4); }
  .kpi { padding: var(--sp-4); }
  .kpi-value { font-size: 28px; }

  /* "+ 增加品項" button: bigger tap target */
  .editor-add-item { padding: 10px var(--sp-2); }
}


/* Sidebar overlay close button on mobile */
.sidebar-overlay-close {
  display: none;
  position: absolute; top: 12px; right: 12px;
  width: 32px; height: 32px;
  background: rgba(255,255,255,0.08);
  border: none; border-radius: var(--r-sm);
  color: white; cursor: pointer;
}
@media (max-width: 1023px) {
  .sidebar.mobile-open .sidebar-overlay-close { display: grid; place-items: center; }
}

/* Sidebar backdrop on mobile */
.sidebar-backdrop {
  display: none;
  position: fixed; inset: 0; z-index: 99;
  background: var(--overlay-strong);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
}
.sidebar-backdrop.show { display: block; }

/* KPI delta visibility for negative values */
.kpi-value.negative { color: var(--danger); }

/* Editor autosave indicator */
.editor-save-indicator {
  display: inline-flex;
  align-items: center;
  color: var(--muted);
  transition: color var(--duration-fast);
}
.editor-save-indicator::before {
  content: '';
  display: inline-block;
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--success);
  margin-right: 6px;
  transition: background var(--duration-fast);
}
/* Autosave failed: red dot + pulse + visible text colour so user notices */
.editor-save-indicator.is-error { color: var(--danger); font-weight: 500; }
.editor-save-indicator.is-error::before { background: var(--danger); animation: save-pulse 1.2s ease-in-out infinite; }
@keyframes save-pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.5; transform: scale(0.6); }
}

/* ============================================================
   SIDEBAR SHORTCUTS (bottom of sidebar, above user card)
   ============================================================ */
.sidebar-shortcuts {
  padding: var(--sp-3) var(--sp-2);
  border-top: 1px solid rgba(255,255,255,0.06);
  display: flex; flex-direction: column; gap: 2px;
}
.shortcut-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px;
  background: transparent;
  border: none;
  border-radius: var(--r-sm);
  color: #94a3b8;
  font-size: var(--text-sm);
  cursor: pointer;
  text-align: left;
  width: 100%;
  transition: all var(--duration-fast);
}
.shortcut-row:hover { background: rgba(255,255,255,0.04); color: #e2e8f0; }
.shortcut-row .shortcut-keys { display: inline-flex; gap: 3px; }
.shortcut-row kbd {
  background: rgba(255,255,255,0.06);
  color: #94a3b8;
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 10px;
  font-family: var(--font-sans);
  font-weight: 600;
  letter-spacing: 0;
  border: none;
  line-height: 1.4;
}
.sidebar-collapsed .sidebar:not(.mobile-open) .sidebar-shortcuts { display: none; }

/* ============================================================
   SHORTCUTS HELP MODAL
   ============================================================ */
.shortcuts-modal {
  position: fixed; inset: 0;
  z-index: 400;
  display: grid; place-items: center;
}
.shortcuts-backdrop {
  position: absolute; inset: 0;
  background: var(--overlay-strong);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}
.shortcuts-panel {
  position: relative;
  background: white;
  width: 480px; max-width: 92vw;
  border-radius: var(--r-xl);
  box-shadow: var(--shadow-modal);
  overflow: hidden;
}
.shortcuts-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: var(--sp-4) var(--sp-5);
  border-bottom: 1px solid var(--divider);
}
.shortcuts-header h3 {
  font-size: var(--text-lg);
  font-weight: 600;
  margin: 0;
  color: var(--ink);
}
.shortcuts-close {
  background: transparent;
  border: none;
  color: var(--muted);
  width: 28px; height: 28px;
  border-radius: var(--r-sm);
  display: grid; place-items: center;
  cursor: pointer;
}
.shortcuts-close:hover { background: var(--bg-hover); color: var(--ink); }
.shortcuts-body {
  padding: var(--sp-3) var(--sp-2);
  max-height: 480px;
  overflow-y: auto;
}
.shortcut-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px var(--sp-3);
  border-radius: var(--r-sm);
}
.shortcut-item:hover { background: var(--bg-subtle); }
.shortcut-text-label {
  font-size: var(--text-base); color: var(--ink); font-weight: 500;
}
.shortcut-text-desc {
  font-size: var(--text-sm); color: var(--muted); margin-top: 1px;
}
.shortcut-item .shortcut-keys { display: inline-flex; gap: 4px; }
.shortcut-item kbd {
  background: var(--bg-subtle);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 3px 8px;
  border-radius: 5px;
  font-size: 11px;
  font-family: var(--font-sans);
  font-weight: 600;
  letter-spacing: 0;
  min-width: 24px;
  text-align: center;
  box-shadow: 0 1px 0 var(--border);
}

/* ============================================================
   MOBILE SIDEBAR — proper close button visibility
   ============================================================ */
@media (max-width: 1023px) {
  /* Show collapse button as a close button in mobile-open state */
  .sidebar.mobile-open .sidebar-collapse {
    transform: rotate(180deg);
  }
}

/* Sidebar narrower on small phones so content peek-through */
@media (max-width: 480px) {
  .sidebar { width: 80vw; max-width: 280px; }
}

/* Hidden modals/drawers must not capture pointer events */
.shortcuts-modal[hidden], .cmd-palette[hidden], .drawer[hidden] {
  display: none !important;
}

/* ============================================================
   SIDEBAR HEIGHT FIX — ensure shortcuts + user visible on mobile
   ============================================================ */

/* Default: nav scrolls if it overflows; shortcuts + footer always visible */
.sidebar-nav {
  flex: 1 1 auto;
  min-height: 0;       /* allow flex shrink */
}
.sidebar-shortcuts {
  flex-shrink: 0;      /* never compress */
}
.sidebar-footer {
  flex-shrink: 0;
}

/* On short viewports (most phones), tighten nav padding */
@media (max-height: 740px) {
  .sidebar-nav { padding: var(--sp-2) var(--sp-2); gap: 0; }
  .nav-section { margin-top: var(--sp-2); }
  .nav-item { padding: 6px var(--sp-3); }
  .nav-section-title { padding: 4px var(--sp-3); }
  .sidebar-brand { height: 56px; padding: var(--sp-3) var(--sp-4); }
  .sidebar-shortcuts { padding: var(--sp-2); }
  .shortcut-row { padding: 4px 10px; }
}
