:root {
  --bg: #f6f8fb;
  --surface: #ffffff;
  --surface-2: #f0f3f8;
  --border: #dfe4ee;
  --text: #0f172a;
  --text-muted: #526077;
  --primary: #2563eb;
  --primary-soft: rgba(37, 99, 235, 0.1);
  --success: #15803d;
  --success-soft: rgba(21, 128, 61, 0.12);
  --warning: #b45309;
  --warning-soft: rgba(180, 83, 9, 0.12);
  --danger: #b91c1c;
  --danger-soft: rgba(185, 28, 28, 0.12);
  --radius: 10px;
  --shadow: 0 4px 16px rgba(15, 23, 42, 0.06);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

[data-theme="dark"] {
  --bg: #0b1120;
  --surface: #111827;
  --surface-2: #1e293b;
  --border: #2b374b;
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --primary: #3b82f6;
  --primary-soft: rgba(59, 130, 246, 0.16);
  --success: #4ade80;
  --success-soft: rgba(74, 222, 128, 0.15);
  --warning: #fbbf24;
  --warning-soft: rgba(251, 191, 36, 0.15);
  --danger: #f87171;
  --danger-soft: rgba(248, 113, 113, 0.15);
  --shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  max-width: 100%;
  overflow-x: hidden;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.45;
}
.hidden { display: none !important; }

/* Topbar */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 18px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 30; flex-wrap: wrap;
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 32px; height: 32px; border-radius: 8px; background: var(--primary); color: #fff;
  display: grid; place-items: center; font-weight: 700; font-size: 13px;
}
.brand-title { font-weight: 700; font-size: 14px; }
.brand-sub { font-size: 11px; color: var(--text-muted); }

.role-switcher { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 8px; flex-wrap: wrap; }
.role-btn {
  border: none; background: transparent; color: var(--text-muted); padding: 6px 10px;
  border-radius: 6px; cursor: pointer; font-weight: 600; font-size: 12px;
}
.role-btn.active { background: var(--surface); color: var(--primary); box-shadow: var(--shadow); }

/* Buttons & Pills */
.btn-primary {
  background: var(--primary); color: #fff; border: none; padding: 8px 14px;
  border-radius: 7px; font-weight: 600; cursor: pointer; font-size: 13px;
}
.btn-outline, .btn-outline-sm {
  background: transparent; color: var(--text); border: 1px solid var(--border);
  padding: 7px 12px; border-radius: 7px; cursor: pointer; font-weight: 500; font-size: 12px;
}
.btn-outline-sm { padding: 5px 10px; width: 100%; }
.btn-ghost {
  background: var(--surface-2); color: var(--text); border: 1px solid var(--border);
  padding: 5px 10px; border-radius: 7px; cursor: pointer; font-size: 12px;
}
.btn-danger {
  background: var(--danger-soft); color: var(--danger); border: 1px solid var(--danger);
  padding: 6px 12px; border-radius: 7px; cursor: pointer; font-weight: 600; font-size: 12px;
}
kbd {
  background: var(--surface); border: 1px solid var(--border); border-radius: 4px;
  padding: 1px 5px; font-family: var(--mono); font-size: 11px;
}

.pill {
  display: inline-flex; align-items: center; gap: 4px; padding: 3px 8px;
  border-radius: 999px; font-size: 11px; font-weight: 600;
}
.pill-success { background: var(--success-soft); color: var(--success); }
.pill-warning { background: var(--warning-soft); color: var(--warning); }
.pill-danger { background: var(--danger-soft); color: var(--danger); }
.pill-primary { background: var(--primary-soft); color: var(--primary); }
.pill-muted { background: var(--surface-2); color: var(--text-muted); }

/* Desktop Shell */
.app-shell { display: grid; grid-template-columns: 248px 1fr; min-height: calc(100vh - 56px); }
.sidebar {
  background: var(--surface); border-right: 1px solid var(--border);
  padding: 14px; display: flex; flex-direction: column; gap: 14px;
}
.sidebar-context {
  background: var(--surface-2); padding: 10px; border-radius: 8px; border: 1px solid var(--border);
}
.context-label { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .04em; }
.context-value { font-weight: 700; margin-top: 2px; font-size: 13px; }
.context-meta { font-size: 11px; color: var(--text-muted); margin-top: 2px; }

.sidebar-nav { display: flex; flex-direction: column; gap: 3px; flex: 1; overflow-y: auto; }
.nav-item {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 8px 10px; border-radius: 7px; border: none; background: transparent;
  color: var(--text); text-align: left; cursor: pointer; font-size: 13px; font-weight: 500;
}
.nav-item:hover { background: var(--surface-2); }
.nav-item.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }

.main-content { padding: 20px 24px; max-width: 1280px; width: 100%; }
.page-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }
.page-title { margin: 0; font-size: 20px; font-weight: 700; }
.page-desc { margin: 4px 0 0; color: var(--text-muted); font-size: 13px; }

/* Cards & Grids */
.grid-4 { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 190px), 1fr)); gap: 14px; margin-bottom: 18px; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 300px), 1fr)); gap: 16px; margin-bottom: 18px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow); margin-bottom: 14px; min-width: 0; overflow-x: auto;
}
.card-title { margin: 0 0 10px; font-size: 15px; font-weight: 700; display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.kpi-value { font-size: 24px; font-weight: 800; margin: 6px 0 2px; }
.kpi-label { font-size: 12px; color: var(--text-muted); }

/* Tables & Kanban */
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--text-muted); font-weight: 600; font-size: 12px; }
tr.clickable:hover { background: var(--surface-2); cursor: pointer; }

.kanban { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; }
.kanban-col { background: var(--surface-2); border-radius: var(--radius); padding: 10px; border: 1px solid var(--border); }
.kanban-col-title { font-weight: 700; font-size: 12px; text-transform: uppercase; color: var(--text-muted); margin-bottom: 8px; display: flex; justify-content: space-between; }
.task-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 10px; margin-bottom: 8px; cursor: pointer;
}
.task-card:hover { border-color: var(--primary); }
.task-id { font-family: var(--mono); font-size: 11px; color: var(--text-muted); }

.mono { font-family: var(--mono); font-size: 12px; }
.alert-box {
  padding: 12px 14px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface-2); margin-bottom: 12px;
}
.alert-warning { background: var(--warning-soft); border-color: var(--warning); }
.alert-danger { background: var(--danger-soft); border-color: var(--danger); }
.alert-success { background: var(--success-soft); border-color: var(--success); }

/* Mobile PWA Frame */
.mobile-shell { display: flex; justify-content: center; padding: 20px; }
.mobile-device {
  width: 390px; height: 760px; background: var(--surface); border: 8px solid #1e293b;
  border-radius: 34px; display: flex; flex-direction: column; overflow: hidden; box-shadow: 0 20px 50px rgba(0,0,0,.22);
}
.mobile-status-bar {
  display: flex; justify-content: space-between; align-items: center; padding: 10px 18px;
  font-size: 12px; font-weight: 700; border-bottom: 1px solid var(--border);
}
.mobile-pwa-tag { font-size: 11px; color: var(--primary); }
.rec-indicator { color: var(--danger); font-weight: 800; }
.mobile-content { flex: 1; overflow-y: auto; padding: 14px; background: var(--bg); }
.mobile-bottom-nav {
  display: grid; grid-template-columns: repeat(5, 1fr); align-items: end;
  background: var(--surface); border-top: 1px solid var(--border); padding: 6px 4px 10px;
}
.mob-tab {
  border: none; background: transparent; color: var(--text-muted); display: flex;
  flex-direction: column; align-items: center; gap: 2px; font-size: 10px; cursor: pointer;
}
.mob-tab.active { color: var(--primary); font-weight: 700; }
.mob-ico { font-size: 18px; }
.mob-record-btn .rec-circle {
  width: 48px; height: 48px; border-radius: 50%; background: var(--danger); color: #fff;
  display: grid; place-items: center; font-size: 18px; margin-top: -22px;
  box-shadow: 0 6px 16px rgba(185, 28, 28, 0.35);
}

/* Side Peek & Modals */
.side-peek {
  position: fixed; top: 56px; right: 0; width: 440px; max-width: 95vw; height: calc(100vh - 56px);
  background: var(--surface); border-left: 1px solid var(--border); z-index: 40;
  display: flex; flex-direction: column; box-shadow: -8px 0 30px rgba(0,0,0,.15);
}
.side-peek-header { display: flex; justify-content: space-between; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.side-peek-title { font-weight: 700; }
.side-peek-body { padding: 16px; overflow-y: auto; flex: 1; }

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15, 23, 42, 0.55); z-index: 50;
  display: grid; place-items: start center; padding-top: 10vh;
}
.cmdk-box { width: 560px; max-width: 92vw; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; overflow: hidden; box-shadow: var(--shadow); }
.cmdk-box input { width: 100%; padding: 14px 16px; border: none; border-bottom: 1px solid var(--border); background: transparent; color: var(--text); font-size: 14px; outline: none; }
.cmdk-results { max-height: 320px; overflow-y: auto; }
.cmdk-item { padding: 10px 16px; cursor: pointer; display: flex; justify-content: space-between; border-bottom: 1px solid var(--border); }
.cmdk-item:hover { background: var(--surface-2); }

.toast {
  position: fixed; bottom: 20px; right: 20px; background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 8px; font-weight: 600; z-index: 60; box-shadow: var(--shadow);
}

@media (max-width: 820px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar { border-right: none; border-bottom: 1px solid var(--border); }
  .main-content { padding: 14px 12px; }
  .topbar { padding: 8px 10px; }
}

@media (max-width: 640px) {
  .mobile-shell { padding: 0; }
  .mobile-device {
    width: 100%; max-width: 100%; height: calc(100dvh - 120px); min-height: 520px;
    border: none; border-radius: 0; box-shadow: none;
  }
  .kanban { grid-template-columns: 1fr; }
  pre.mono { white-space: pre-wrap; word-break: break-word; }
}
