/* ============================================================================
   Code CRM — premium interface layer, built strictly on Code Systems tokens.
   Ultra-dark, layered glass, orange/gold + teal accents used as accents only.
   RTL-native, responsive, reduced-motion aware. No new palette, no cheap glow.
   ============================================================================ */

.crm-shell { display: flex; flex-direction: column; gap: 18px; --crm-density: 1; }
.crm-shell.dense { --crm-density: .72; }

/* ---- module header ---- */
.crm-head { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 16px; }
.crm-head .eyebrow { background: linear-gradient(90deg, var(--orange), var(--teal)); -webkit-background-clip: text; background-clip: text; color: transparent; font-weight: 700; letter-spacing: .12em; }
.crm-headline h1 { margin: 2px 0 0; font-size: 26px; letter-spacing: -.02em; }
.crm-headline p { margin: 4px 0 0; color: var(--muted); font-size: 14px; }
.crm-minikpis { display: flex; gap: 10px; flex-wrap: wrap; }
.crm-mini { display: flex; flex-direction: column; padding: 8px 14px; border-radius: 14px; background: linear-gradient(180deg, color-mix(in srgb, var(--raised) 90%, transparent), color-mix(in srgb, var(--surface) 90%, transparent)); border: 1px solid var(--line); box-shadow: inset 0 1px 0 var(--glass-edge); min-width: 96px; }
.crm-mini b { font-size: 18px; font-variant-numeric: tabular-nums; }
.crm-mini span { font-size: 11px; color: var(--muted); }

/* ---- section nav ---- */
.crm-subnav { display: flex; gap: 4px; overflow-x: auto; padding: 4px; border: 1px solid var(--line); border-radius: 16px; background: color-mix(in srgb, var(--surface) 60%, transparent); backdrop-filter: blur(8px); scrollbar-width: thin; }
.crm-tab { display: inline-flex; align-items: center; gap: 8px; padding: 9px 14px; border-radius: 12px; color: var(--muted); white-space: nowrap; border: 1px solid transparent; transition: color .15s var(--ease), background .15s var(--ease), border-color .15s var(--ease); }
.crm-tab:hover { color: var(--text); background: color-mix(in srgb, var(--raised) 60%, transparent); }
.crm-tab.active { color: var(--text); background: var(--raised); border-color: color-mix(in srgb, var(--orange) 30%, var(--line)); box-shadow: 0 1px 0 var(--glass-edge) inset, 0 6px 16px #0003; }
.crm-tab.active svg { color: var(--orange); }
.crm-tab svg { color: var(--teal); }
.crm-section { display: flex; flex-direction: column; gap: 16px; min-height: 220px; }

/* ---- toolbar & controls ---- */
.crm-toolbar { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; justify-content: space-between; }
.crm-toolbar-l, .crm-toolbar-r { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.crm-toolbar .field { margin: 0; min-width: 140px; }
.crm-toolbar .field label { font-size: 11px; color: var(--muted); }
.crm-toolbar .field select, .crm-toolbar .field input { padding: 8px 10px; }
.crm-toolbar .search-input { min-width: 230px; }
.crm-h2 { font-size: 18px; margin: 0; }
.seg { display: inline-flex; gap: 3px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 3px; }
.seg .btn { border-radius: 9px; }
.crm-range .btn { min-width: 44px; }

/* ---- saved views bar ---- */
.crm-views { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.crm-view-pill { display: inline-flex; align-items: center; gap: 6px; padding: 5px 12px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); color: var(--muted); font-size: 13px; cursor: pointer; transition: all .12s var(--ease); }
.crm-view-pill:hover { color: var(--text); border-color: color-mix(in srgb, var(--teal) 40%, var(--line)); }
.crm-view-pill.active { color: var(--text); border-color: var(--orange); background: color-mix(in srgb, var(--orange-dark) 45%, var(--surface)); }
.crm-view-pill .x { opacity: .55; }
.crm-view-pill .x:hover { opacity: 1; }

/* ---- KPI grid + sparklines ---- */
.crm-kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px; }
.crm-kpi { position: relative; overflow: hidden; padding: 16px; border-radius: 18px; background: linear-gradient(180deg, var(--raised), color-mix(in srgb, var(--surface) 92%, transparent)); border: 1px solid var(--line); box-shadow: inset 0 1px 0 var(--glass-edge), 0 8px 24px #0002; transition: transform .15s var(--ease), border-color .15s var(--ease); }
.crm-kpi:hover { transform: translateY(-2px); border-color: color-mix(in srgb, var(--teal) 26%, var(--line)); }
.crm-kpi::after { content: ""; position: absolute; inset: 0 0 auto 0; height: 2px; background: linear-gradient(90deg, transparent, color-mix(in srgb, var(--orange) 60%, transparent), transparent); opacity: .5; }
.crm-kpi-top { display: flex; align-items: center; justify-content: space-between; color: var(--muted); font-size: 12.5px; }
.crm-kpi-top svg { color: var(--teal); }
.crm-kpi b { display: block; font-size: 26px; font-variant-numeric: tabular-nums; margin-top: 6px; letter-spacing: -.02em; }
.crm-kpi small { color: var(--muted); font-size: 12px; }
.crm-kpi .delta { font-size: 12px; font-weight: 600; }
.crm-kpi .delta.up { color: var(--success); } .crm-kpi .delta.down { color: var(--red); }
.spark { display: flex; align-items: flex-end; gap: 2px; height: 30px; margin-top: 10px; }
.spark i { flex: 1; min-width: 2px; border-radius: 2px 2px 0 0; background: linear-gradient(180deg, var(--teal), color-mix(in srgb, var(--teal) 30%, transparent)); opacity: .85; }

/* ---- charts ---- */
.crm-charts { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 14px; }
.crm-bars { display: flex; flex-direction: column; gap: 10px; }
.crm-bar-row { display: grid; grid-template-columns: 128px 1fr auto; gap: 10px; align-items: center; font-size: 13px; }
.crm-bar-label { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.crm-bar-track { height: 12px; border-radius: 999px; background: color-mix(in srgb, var(--line) 55%, transparent); overflow: hidden; }
.crm-bar-fill { display: block; height: 100%; border-radius: 999px; background: linear-gradient(90deg, var(--teal), var(--orange)); min-width: 4px; transition: width .5s var(--ease); }
.crm-bar-value { font-variant-numeric: tabular-nums; display: flex; flex-direction: column; align-items: flex-start; min-width: 54px; }
.crm-bar-value small { color: var(--muted); }
.crm-funnel .crm-bar-fill { background: linear-gradient(90deg, var(--orange), color-mix(in srgb, var(--teal) 80%, var(--orange))); }
.crm-donut { display: flex; align-items: center; gap: 18px; }
.crm-donut-ring { width: 120px; height: 120px; border-radius: 50%; display: grid; place-items: center; }
.crm-donut-ring b { font-size: 22px; }
.crm-legend { display: flex; flex-direction: column; gap: 6px; font-size: 13px; }
.crm-legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-inline-end: 8px; }

/* ---- tables ---- */
.crm-table { border: 1px solid var(--line); border-radius: 16px; overflow: hidden; }
.crm-table table { width: 100%; border-collapse: collapse; }
.crm-table thead th { background: color-mix(in srgb, var(--surface) 80%, transparent); position: sticky; top: 0; z-index: 1; font-size: 12px; color: var(--muted); text-align: start; padding: calc(11px * var(--crm-density)) 12px; border-bottom: 1px solid var(--line); }
.crm-table td { padding: calc(12px * var(--crm-density)) 12px; border-bottom: 1px solid color-mix(in srgb, var(--line) 45%, transparent); font-size: 14px; vertical-align: middle; }
.crm-table tbody tr:last-child td { border-bottom: none; }
.crm-tr-click { cursor: pointer; transition: background .12s var(--ease); }
.crm-tr-click:hover { background: color-mix(in srgb, var(--teal) 7%, transparent); }
.crm-tr-click:focus-visible { outline: 2px solid var(--teal); outline-offset: -2px; }
.crm-cell-main { display: flex; flex-direction: column; }
.crm-cell-main small { color: var(--muted); }
.crm-flags { display: inline-flex; gap: 4px; flex-wrap: wrap; }
.crm-inline-status { background: transparent; border: 1px solid var(--line); border-radius: 8px; color: var(--text); padding: 4px 8px; font-size: 12px; }

/* ---- score ring ---- */
.crm-score { --v: 0; --tone: var(--muted); display: inline-grid; place-items: center; width: 36px; height: 36px; border-radius: 50%; background: conic-gradient(var(--tone) calc(var(--v) * 1%), color-mix(in srgb, var(--line) 65%, transparent) 0); }
.crm-score-num { display: grid; place-items: center; width: 27px; height: 27px; border-radius: 50%; background: var(--surface); font-size: 12px; font-variant-numeric: tabular-nums; }

/* ---- bulk bar ---- */
.crm-bulkbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 10px 14px; border-radius: 14px; background: color-mix(in srgb, var(--orange-dark) 40%, var(--surface)); border: 1px solid color-mix(in srgb, var(--orange) 34%, var(--line)); box-shadow: 0 6px 18px #0003; }
.crm-bulkbar > span:first-child { color: var(--orange); font-weight: 700; }

/* ---- kanban / pipeline board ---- */
.crm-board { display: flex; gap: 12px; overflow-x: auto; padding-bottom: 12px; align-items: flex-start; scrollbar-width: thin; }
.crm-col { flex: 0 0 292px; min-width: 268px; display: flex; flex-direction: column; gap: 8px; background: linear-gradient(180deg, color-mix(in srgb, var(--surface) 82%, transparent), color-mix(in srgb, var(--bg) 60%, transparent)); border: 1px solid var(--line); border-radius: 18px; padding: 12px; max-height: 76vh; box-shadow: inset 0 1px 0 var(--glass-edge); }
.crm-col.won { border-color: color-mix(in srgb, var(--success) 42%, var(--line)); }
.crm-col.lost { border-color: color-mix(in srgb, var(--red) 38%, var(--line)); }
.crm-col.drop { border-color: var(--teal); background: color-mix(in srgb, var(--teal) 10%, var(--surface)); }
.crm-col-head { display: flex; align-items: center; justify-content: space-between; font-weight: 600; padding: 2px 4px; }
.crm-col-count { min-width: 24px; text-align: center; font-size: 12px; color: var(--muted); background: var(--raised); border-radius: 999px; padding: 2px 9px; }
.crm-col-prob { height: 4px; border-radius: 999px; background: color-mix(in srgb, var(--line) 55%, transparent); overflow: hidden; margin: 0 4px; }
.crm-col-prob i { display: block; height: 100%; background: linear-gradient(90deg, var(--teal), var(--orange)); }
.crm-col-sum { font-size: 12px; color: var(--teal); padding: 0 4px; display: flex; justify-content: space-between; }
.crm-col-sum small { color: var(--muted); }
.crm-col-body { display: flex; flex-direction: column; gap: 8px; overflow-y: auto; min-height: 36px; scrollbar-width: thin; }
.crm-card { background: linear-gradient(180deg, var(--raised), color-mix(in srgb, var(--surface) 90%, transparent)); border: 1px solid var(--line); border-radius: 13px; padding: 11px 12px; display: flex; flex-direction: column; gap: 5px; cursor: pointer; box-shadow: 0 4px 12px #0002; transition: transform .12s var(--ease), border-color .12s var(--ease), box-shadow .12s var(--ease); }
.crm-card:hover { transform: translateY(-2px); border-color: color-mix(in srgb, var(--teal) 40%, var(--line)); box-shadow: 0 10px 22px #0004; }
.crm-card.dragging { opacity: .5; }
.crm-card strong { font-size: 13.5px; }
.crm-card small { color: var(--muted); font-size: 12px; }
.crm-card-foot { display: flex; align-items: center; justify-content: space-between; margin-top: 2px; font-size: 12px; }
.crm-card-tags { display: flex; gap: 4px; flex-wrap: wrap; }
.crm-prio { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.crm-prio.high { background: var(--warning); } .crm-prio.urgent { background: var(--red); } .crm-prio.normal { background: var(--teal); } .crm-prio.low { background: var(--muted); }

/* ---- timeline ---- */
.crm-timeline { display: flex; flex-direction: column; gap: 2px; }
.crm-tl-item { display: grid; grid-template-columns: 32px 1fr; gap: 10px; padding: 9px 0; border-bottom: 1px solid color-mix(in srgb, var(--line) 50%, transparent); }
.crm-tl-item:last-child { border-bottom: none; }
.crm-tl-dot { display: grid; place-items: center; width: 32px; height: 32px; border-radius: 50%; background: var(--surface); border: 1px solid var(--line); color: var(--teal); }
.crm-tl-body strong { font-size: 13px; }
.crm-tl-body p { margin: 2px 0; font-size: 13px; white-space: pre-wrap; }

/* ---- forms ---- */
.crm-form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.crm-check { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 14px; margin: 4px 0; }

/* ---- entity drawer ---- */
.crm-drawer { display: flex; flex-direction: column; gap: 14px; }
.crm-drawer-head { display: flex; gap: 12px; align-items: center; }
.crm-avatar { display: grid; place-items: center; width: 52px; height: 52px; border-radius: 15px; background: linear-gradient(135deg, color-mix(in srgb, var(--orange) 34%, var(--surface)), color-mix(in srgb, var(--teal) 34%, var(--surface))); font-size: 22px; font-weight: 800; box-shadow: inset 0 1px 0 var(--glass-edge); }
.crm-drawer-head h3 { margin: 0; font-size: 18px; }
.crm-drawer-meta { display: flex; align-items: center; gap: 8px; margin-top: 6px; }
.crm-quick { display: flex; flex-wrap: wrap; gap: 8px; }
.crm-drawer-tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); overflow-x: auto; scrollbar-width: none; }
.crm-dtab { padding: 8px 13px; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; transition: color .12s var(--ease); }
.crm-dtab:hover { color: var(--text); }
.crm-dtab.active { color: var(--text); border-bottom-color: var(--orange); }
.crm-facts { display: grid; grid-template-columns: max-content 1fr; gap: 9px 16px; margin: 0; }
.crm-facts dt { color: var(--muted); font-size: 13px; }
.crm-facts dd { margin: 0; font-size: 14px; }
.crm-facts dd input, .crm-facts dd select { width: 100%; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; color: var(--text); padding: 5px 8px; font-size: 13px; }
.crm-tasklist { display: flex; flex-direction: column; gap: 6px; }
.crm-task { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); font-size: 13px; }
.crm-task.done { opacity: .55; text-decoration: line-through; }
.crm-task small { color: var(--muted); margin-inline-start: auto; }

/* ---- tags & stages ---- */
.crm-tags { display: inline-flex; flex-wrap: wrap; gap: 6px; }
.crm-tag { font-size: 12px; padding: 2px 10px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); }
.crm-tag.tag-teal { border-color: color-mix(in srgb, var(--teal) 50%, var(--line)); color: var(--teal); }
.crm-tag.tag-orange { border-color: color-mix(in srgb, var(--orange) 50%, var(--line)); color: var(--orange); }
.crm-tag.tag-success { border-color: color-mix(in srgb, var(--success) 50%, var(--line)); color: var(--success); }
.crm-tag.tag-warning { border-color: color-mix(in srgb, var(--warning) 50%, var(--line)); color: var(--warning); }
.crm-tag.tag-red { border-color: color-mix(in srgb, var(--red) 50%, var(--line)); color: var(--red); }
.crm-tag button { margin-inline-start: 6px; opacity: .6; }
.crm-tag button:hover { opacity: 1; }
.crm-stage-list { display: flex; flex-wrap: wrap; gap: 8px; }
.crm-stage-chip { display: inline-flex; align-items: center; gap: 8px; padding: 7px 12px; border-radius: 10px; background: var(--surface); border: 1px solid var(--line); font-size: 13px; }
.crm-stage-chip small { color: var(--muted); font-variant-numeric: tabular-nums; }
.crm-stage-chip.won { border-color: color-mix(in srgb, var(--success) 45%, var(--line)); }
.crm-stage-chip.lost { border-color: color-mix(in srgb, var(--red) 40%, var(--line)); }
.crm-inline-add { display: flex; gap: 10px; align-items: flex-end; margin-bottom: 12px; flex-wrap: wrap; }
.crm-inline-add .field { margin: 0; flex: 1; min-width: 140px; }

/* ---- settings cards ---- */
.crm-cf-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 9px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); margin-bottom: 6px; }
.crm-cf-row small { color: var(--muted); }

/* ---- empty ---- */
.crm-empty { display: grid; place-items: center; text-align: center; gap: 8px; padding: 40px 16px; }
.crm-empty .glyph { display: grid; place-items: center; width: 66px; height: 66px; border-radius: 20px; background: linear-gradient(135deg, color-mix(in srgb, var(--orange) 22%, var(--surface)), color-mix(in srgb, var(--teal) 22%, var(--surface))); color: var(--text); border: 1px solid var(--line); }

/* ---- kbd hint ---- */
.crm-kbd { display: inline-flex; gap: 4px; align-items: center; color: var(--muted); font-size: 11px; }
.crm-kbd kbd { border: 1px solid var(--line); border-radius: 5px; padding: 1px 6px; background: var(--surface); }

/* ---- responsive ---- */
@media (max-width: 760px) {
  .crm-form-grid { grid-template-columns: 1fr; }
  .crm-bar-row { grid-template-columns: 92px 1fr auto; }
  .crm-toolbar-l, .crm-toolbar-r { width: 100%; }
  .crm-toolbar .search-input, .crm-toolbar .field { min-width: 0; flex: 1; }
  .crm-facts { grid-template-columns: 1fr; gap: 2px 0; }
  .crm-facts dd { margin-bottom: 8px; }
  .crm-headline h1 { font-size: 22px; }
  .crm-minikpis { width: 100%; }
}
@media (prefers-reduced-motion: reduce) {
  .crm-card, .crm-bar-fill, .crm-tab, .crm-kpi, .crm-view-pill { transition: none; }
}
