:root {
  color-scheme: dark light;
  --bg: #0b0d12;
  --bg-elevated: #12151c;
  --bg-card: #171b24;
  --border: #262b38;
  --border-strong: #343b4c;
  --text: #e6e9f0;
  --text-dim: #9aa3b5;
  --text-faint: #6b7383;
  --accent: #5b8cff;
  --accent-dim: #2a3a63;
  --danger: #ef4444;
  --danger-dim: #3a1f22;
  --warn: #f5a524;
  --ok: #22c55e;
  --radius: 10px;
  --shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f5f7;
    --bg-elevated: #ffffff;
    --bg-card: #ffffff;
    --border: #e2e4e9;
    --border-strong: #cdd1db;
    --text: #14161b;
    --text-dim: #565d6d;
    --text-faint: #8a90a0;
    --accent: #3468e0;
    --accent-dim: #dde6fb;
    --danger: #dc2626;
    --danger-dim: #fbdcdc;
    --warn: #b8720a;
    --ok: #16a34a;
    --shadow: 0 2px 10px rgba(20, 22, 27, 0.08);
  }
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

#app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

a {
  color: var(--accent);
}

button {
  font-family: inherit;
  font-size: inherit;
  cursor: pointer;
}

input, select, textarea {
  font-family: inherit;
  font-size: inherit;
}

/* ---------- Login screen ---------- */

.login-screen {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px;
  text-align: center;
}

.login-card h1 {
  margin: 0 0 6px;
  font-size: 20px;
}

.login-card p {
  color: var(--text-dim);
  margin: 0 0 20px;
  font-size: 13px;
}

.login-card input {
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  background: var(--bg-elevated);
  color: var(--text);
  margin-bottom: 12px;
}

.login-card button {
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  border: none;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}

.login-card button:disabled {
  opacity: 0.6;
  cursor: default;
}

.login-status {
  margin-top: 12px;
  font-size: 12.5px;
  color: var(--text-dim);
  min-height: 16px;
}

.login-fallback { margin-top: 1rem; font-size: 0.85rem; }
.login-fallback summary { cursor: pointer; opacity: 0.7; }
.login-fallback input { width: 100%; margin: 0.5rem 0; }

/* ---------- Header ---------- */

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.app-header .brand {
  font-weight: 700;
  font-size: 15px;
  margin-right: 8px;
  white-space: nowrap;
}

.nav-tabs {
  display: flex;
  gap: 4px;
  flex: 1;
  min-width: 0;
  overflow-x: auto;
}

.nav-tabs button {
  padding: 7px 12px;
  border-radius: 8px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-dim);
  font-weight: 600;
  white-space: nowrap;
}

.nav-tabs button.active {
  background: var(--accent-dim);
  color: var(--accent);
  border-color: var(--accent-dim);
}

.nav-tabs button.add-product {
  color: var(--text-faint);
  border: 1px dashed var(--border-strong);
}

.header-right {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-left: auto;
}

.icon-btn {
  border: 1px solid var(--border-strong);
  background: var(--bg-card);
  color: var(--text-dim);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 13px;
}

.icon-btn:hover {
  color: var(--text);
}

/* ---------- Main layout ---------- */

.app-main {
  flex: 1;
  padding: 16px;
  max-width: 1400px;
  width: 100%;
  margin: 0 auto;
}

.view-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
  gap: 10px;
  flex-wrap: wrap;
}

.view-toolbar h2 {
  margin: 0;
  font-size: 16px;
}

.toolbar-controls {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 12.5px;
  color: var(--text-dim);
  flex-wrap: wrap;
}

.toolbar-controls label {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

/* ---------- KPI cards ---------- */

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
  margin-bottom: 20px;
}

.kpi-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
}

.kpi-card .kpi-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-faint);
  margin-bottom: 6px;
}

.kpi-card .kpi-value {
  font-size: 20px;
  font-weight: 700;
}

.kpi-card .kpi-sub {
  font-size: 11.5px;
  color: var(--text-dim);
  margin-top: 4px;
}

/* ---------- Section blocks ---------- */

.section-block {
  margin-bottom: 24px;
}

.section-block h3 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-faint);
  margin: 0 0 10px;
}

/* ---------- Product summary table / factory panel ---------- */

.product-summary-row {
  cursor: pointer;
}

.product-summary-row:hover {
  background: var(--bg-card);
}

.stage-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  border: 1px solid var(--border-strong);
  color: var(--text-dim);
}

.stage-badge .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}

.factory-panel {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.factory-panel .chip {
  font-size: 12px;
}

.activity-feed {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.activity-feed-row {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 12.5px;
}

/* ---------- Product header ---------- */

.product-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.product-header-main {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.product-header h2 {
  margin: 0;
  font-size: 18px;
}

.product-links {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.product-links a {
  font-size: 12px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 10px;
  text-decoration: none;
}

.product-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 14px;
  overflow-x: auto;
}

.product-tabs button {
  padding: 6px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text-dim);
  font-weight: 600;
  font-size: 12.5px;
  white-space: nowrap;
}

.product-tabs button.active {
  background: var(--accent-dim);
  color: var(--accent);
  border-color: var(--accent-dim);
}

/* ---------- Buttons ---------- */

.btn {
  border: 1px solid var(--border-strong);
  background: var(--bg-card);
  color: var(--text);
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 12.5px;
  font-weight: 600;
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.btn.approve {
  border-color: var(--ok);
  color: var(--ok);
}

.btn.reject, .btn.danger {
  border-color: var(--danger);
  color: var(--danger);
}

.btn:disabled {
  opacity: 0.55;
  cursor: default;
}

/* ---------- Tables ---------- */

.data-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.data-table th, .data-table td {
  text-align: left;
  padding: 10px 12px;
  font-size: 12.5px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

.data-table th {
  color: var(--text-faint);
  text-transform: uppercase;
  font-size: 11px;
  letter-spacing: 0.03em;
  background: var(--bg-card);
}

.data-table tr:last-child td {
  border-bottom: none;
}

.data-table tbody tr {
  cursor: pointer;
}

.data-table tbody tr:hover {
  background: var(--bg-card);
}

.table-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.filter-select, .search-input {
  padding: 6px 10px;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  background: var(--bg-card);
  color: var(--text);
}

.search-input {
  min-width: 180px;
}

.status-tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
}

.status-tag.lead { background: var(--bg-card); color: var(--text-dim); border: 1px solid var(--border-strong); }
.status-tag.prospect { background: var(--accent-dim); color: var(--accent); }
.status-tag.trial { background: var(--warn); color: #1a1200; }
.status-tag.customer { background: var(--ok); color: #06210e; }
.status-tag.churned { background: var(--danger-dim); color: var(--danger); }

.status-tag.new { background: var(--bg-card); color: var(--text-dim); border: 1px solid var(--border-strong); }
.status-tag.contacted { background: var(--accent-dim); color: var(--accent); }
.status-tag.demo { background: var(--accent-dim); color: var(--accent); }
.status-tag.negotiating { background: var(--warn); color: #1a1200; }
.status-tag.won { background: var(--ok); color: #06210e; }
.status-tag.lost { background: var(--danger-dim); color: var(--danger); }

.test-flag {
  font-size: 10px;
  color: var(--warn);
  border: 1px solid var(--warn);
  border-radius: 999px;
  padding: 0 6px;
  margin-left: 6px;
}

/* ---------- Deals kanban ---------- */

.kanban {
  display: flex;
  gap: 12px;
  overflow-x: auto;
  padding-bottom: 12px;
}

.kanban-col {
  flex: 0 0 240px;
  min-width: 240px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 260px);
}

.kanban-col.drag-over {
  border-color: var(--accent);
}

.kanban-col-header {
  padding: 10px 12px;
  border-bottom: 3px solid var(--stage-color, var(--accent));
  font-weight: 700;
  font-size: 12.5px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-dim);
}

.kanban-col-count {
  background: var(--bg-card);
  border-radius: 999px;
  padding: 1px 8px;
  font-size: 11.5px;
  color: var(--text);
}

.kanban-col-body {
  overflow-y: auto;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.deal-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  cursor: pointer;
}

.deal-card:hover {
  border-color: var(--border-strong);
}

.deal-card .deal-title {
  font-weight: 700;
  font-size: 13px;
  margin-bottom: 3px;
}

.deal-card .deal-meta {
  color: var(--text-dim);
  font-size: 11.5px;
}

.empty-msg {
  color: var(--text-faint);
  font-size: 12.5px;
  padding: 16px 8px;
  text-align: center;
}

/* ---------- Activities list ---------- */

.activity-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.activity-row {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  display: flex;
  gap: 10px;
  align-items: flex-start;
  font-size: 12.5px;
}

.activity-row.done {
  opacity: 0.55;
}

.activity-row .activity-kind {
  font-weight: 700;
  text-transform: uppercase;
  font-size: 10.5px;
  color: var(--text-faint);
  min-width: 44px;
}

.activity-row .activity-body {
  flex: 1;
  min-width: 0;
  word-break: break-word;
}

.activity-row .activity-meta {
  color: var(--text-faint);
  font-size: 11px;
  margin-top: 3px;
}

.activity-row input[type="checkbox"] {
  margin-top: 2px;
}

/* ---------- Slide-over forms ---------- */

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 40;
  display: flex;
  justify-content: flex-end;
}

.slide-over {
  width: 100%;
  max-width: 480px;
  background: var(--bg-elevated);
  border-left: 1px solid var(--border);
  height: 100%;
  overflow-y: auto;
  padding: 20px;
  box-shadow: var(--shadow);
}

.slide-over-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 4px;
  gap: 10px;
}

.slide-over-header h2 {
  margin: 0;
  font-size: 18px;
}

.close-btn {
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-size: 20px;
  line-height: 1;
  padding: 4px;
}

.form-grid {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 16px;
}

.form-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.form-row.two-col {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.form-row label {
  font-size: 11.5px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.form-row input, .form-row select, .form-row textarea {
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  background: var(--bg-card);
  color: var(--text);
  width: 100%;
}

.form-row textarea {
  resize: vertical;
  min-height: 60px;
}

.form-row.checkbox-row {
  flex-direction: row;
  align-items: center;
  gap: 8px;
}

.form-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
  gap: 8px;
}

.form-actions .form-actions-right {
  display: flex;
  gap: 8px;
}

.detail-section {
  margin-top: 20px;
}

.detail-section h3 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-faint);
  margin: 0 0 8px;
}

.kv-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 6px 12px;
  font-size: 13px;
}

.kv-grid dt {
  color: var(--text-dim);
}

.kv-grid dd {
  margin: 0;
  word-break: break-word;
}

.form-error {
  color: var(--danger);
  font-size: 12px;
  min-height: 16px;
}

/* ---------- Confirm overlay ---------- */

.confirm-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.confirm-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 20px;
  max-width: 360px;
  width: 100%;
  box-shadow: var(--shadow);
}

.confirm-card h3 {
  margin: 0 0 8px;
  font-size: 15px;
}

.confirm-card p {
  margin: 0 0 16px;
  font-size: 13px;
  color: var(--text-dim);
}

.confirm-card .form-row {
  margin-bottom: 12px;
}

.confirm-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.loading, .error-msg {
  padding: 40px 12px;
  text-align: center;
  color: var(--text-faint);
  font-size: 13px;
}

.error-msg {
  color: var(--danger);
}

@media (max-width: 640px) {
  .app-header {
    padding: 8px 10px;
  }
  .app-main {
    padding: 10px;
  }
  .slide-over {
    max-width: 100%;
  }
  .form-row.two-col {
    grid-template-columns: 1fr;
  }
  .kanban-col {
    flex: 0 0 82vw;
    min-width: 82vw;
  }
}
