/* =====================================================================
   Miftah — Components
   Pills for actions and states, rounded white cards for content,
   filled fields for input. Green is the one action colour.
   ===================================================================== */

/* --- Buttons ---------------------------------------------------------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  height: 36px;
  padding: 0 16px;
  border: 1px solid var(--border-strong);
  border-radius: var(--r-pill);
  background: var(--surface);
  color: var(--ink);
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  transition: background var(--t-fast) var(--ease), border-color var(--t-fast) var(--ease), color var(--t-fast) var(--ease);
}
.btn:hover { background: var(--surface-2); border-color: var(--ink-3); }
.btn:active { transform: translateY(.5px); }
.btn svg { flex: none; }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }

.btn-quiet { border-color: transparent; background: transparent; color: var(--ink-2); }
.btn-quiet:hover { background: var(--accent-soft); border-color: transparent; color: var(--accent-text); }

.btn-danger {
  background: var(--crit-mark);
  border-color: var(--crit-mark);
  color: #fff;
  font-weight: 600;
}
.btn-danger:hover { background: var(--crit-ink); border-color: var(--crit-ink); }

.btn-sm { height: 30px; padding: 0 12px; font-size: 12.5px; }
.btn-icon { width: 36px; padding: 0; }
.btn-icon.btn-sm { width: 30px; }
.btn-block { width: 100%; }
.btn[disabled] { opacity: .5; pointer-events: none; }

/* --- Badges / status -------------------------------------------------- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 22px;
  padding: 0 10px;
  border-radius: var(--r-pill);
  background: var(--mute-bg);
  color: var(--mute-ink);
  font-size: 11.5px;
  font-weight: 600;
  white-space: nowrap;
}
.badge::before {
  content: "";
  width: 5px; height: 5px;
  border-radius: 50%;
  background: currentColor;
  flex: none;
}
.badge-plain::before { display: none; }
.badge-good { background: var(--good-bg); color: var(--good-ink); }
.badge-warn { background: var(--warn-bg); color: var(--warn-ink); }
.badge-crit { background: var(--crit-bg); color: var(--crit-ink); }
.badge-info { background: var(--info-bg); color: var(--info-ink); }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 24px;
  padding: 0 10px;
  border: 1px solid transparent;
  border-radius: var(--r-pill);
  background: var(--surface-2);
  color: var(--ink-2);
  font-size: 11.5px;
  font-weight: 500;
  white-space: nowrap;
}

.count-pill {
  min-width: 19px;
  height: 19px;
  padding: 0 6px;
  border-radius: var(--r-pill);
  background: var(--surface-3);
  color: var(--ink-2);
  font-size: 11px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-variant-numeric: tabular-nums;
}
.count-pill-hot { background: var(--accent); color: var(--accent-ink); }

/* --- Avatar ----------------------------------------------------------- */
.avatar {
  width: 30px; height: 30px;
  flex: none;
  border-radius: 50%;
  background: var(--accent-soft);
  border: 1px solid transparent;
  color: var(--accent-text);
  font-size: 11.5px;
  font-weight: 700;
  display: grid;
  place-items: center;
  text-transform: uppercase;
}
.avatar-lg { width: 40px; height: 40px; font-size: 14px; }

/* --- Panel (card) ----------------------------------------------------- */
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-card);
  overflow: hidden;
}
.panel-head {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 10px;
  padding: 16px 20px 12px;
  background: var(--surface);
}
.panel-head h3 { flex: 1; min-width: 0; font-size: 15.5px; }
.panel-head .label { flex: 0 1 auto; min-width: 0; }
.panel-head + .panel-body { padding-top: 6px; }
.panel-body { padding: 18px 20px 20px; }
.panel-body-flush { padding: 0; }
.panel-head + .panel-body-flush { padding-top: 0; border-top: 1px solid var(--border); }
.panel-foot {
  padding: 12px 20px;
  border-top: 1px solid var(--border);
  background: var(--surface-2);
}

/* --- KPI tiles -------------------------------------------------------- */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(168px, 1fr));
  gap: 14px;
}
.kpi {
  display: grid;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-card);
  padding: 16px 18px;
}
.kpi-top { display: flex; align-items: center; gap: 10px; min-width: 0; }
.kpi-ico {
  width: 38px; height: 38px;
  flex: none;
  border-radius: var(--r);
  background: var(--accent-soft);
  color: var(--accent);
  display: grid;
  place-items: center;
}
.kpi-label { font-size: 13px; color: var(--ink-2); font-weight: 500; min-width: 0; }
.kpi-value {
  font-size: 30px;
  font-weight: 600;
  line-height: 1.1;
  letter-spacing: -.02em;
  font-variant-numeric: tabular-nums;
}
.kpi-value small { font-size: 13px; font-weight: 500; color: var(--ink-2); letter-spacing: 0; }
.kpi-foot { display: flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--ink-3); flex-wrap: wrap; }
.delta {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  height: 20px;
  padding: 0 8px;
  border-radius: var(--r-pill);
  font-size: 11.5px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  background: var(--mute-bg);
  color: var(--mute-ink);
}
.delta-up   { background: var(--good-bg); color: var(--good-ink); }
.delta-down { background: var(--crit-bg); color: var(--crit-ink); }
.delta-flat { color: var(--ink-3); }

/* --- Tables ----------------------------------------------------------- */
.table-wrap { overflow-x: auto; }
/* The actions cell stays in view when a wide table scrolls sideways */
table.data td:last-child:has(.row-actions) { position: sticky; inset-inline-end: 0; background: var(--surface); }
table.data tbody tr.clickable:hover td:last-child:has(.row-actions) { background: var(--surface-2); }
/* A Latin title in an Arabic page keeps its ellipsis at its own end */
:root[dir="rtl"] .cell-main.truncate { unicode-bidi: plaintext; text-align: end; }
table.data {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
table.data th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 10px 16px;
  text-align: start;
  font-size: 12px;
  font-weight: 500;
  color: var(--ink-3);
  white-space: nowrap;
}
table.data td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
table.data tbody tr:last-child td { border-bottom: none; }
table.data tbody tr.clickable { cursor: pointer; }
table.data tbody tr.clickable:hover { background: var(--surface-2); }
table.data td.end, table.data th.end { text-align: end; }
table.data .cell-main { font-weight: 600; }
table.data .cell-sub { font-size: 12px; color: var(--ink-3); }

/* --- Forms ------------------------------------------------------------ */
.field { display: grid; gap: 6px; }
.field > label { font-size: 12.5px; font-weight: 500; color: var(--ink-2); }
.field .hint { font-size: 11.5px; color: var(--ink-3); }
.input, .select, textarea.input {
  width: 100%;
  height: 38px;
  padding: 0 13px;
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  background: var(--surface-2);
  color: var(--ink);
  font-size: 13.5px;
  transition: border-color var(--t-fast) var(--ease), background var(--t-fast) var(--ease);
}
textarea.input { height: auto; padding: 9px 13px; resize: vertical; }
.input:focus, .select:focus {
  outline: none;
  background: var(--surface);
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 16%, transparent);
}
.input::placeholder { color: var(--ink-3); }
.select {
  appearance: none;
  padding-inline-end: 32px;
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%), linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 17px) 16px, calc(100% - 12px) 16px;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}
:root[dir="rtl"] .select { background-position: 17px 16px, 12px 16px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-grid .span-2 { grid-column: 1 / -1; }

.toggle-group { display: flex; flex-wrap: wrap; gap: 6px; }
.toggle-group label {
  display: inline-flex;
  align-items: center;
  height: 30px;
  padding: 0 13px;
  border: 1px solid var(--border-strong);
  border-radius: var(--r-pill);
  background: var(--surface);
  font-size: 12.5px;
  font-weight: 500;
  color: var(--ink-2);
  cursor: pointer;
  user-select: none;
  transition: background var(--t-fast) var(--ease), border-color var(--t-fast) var(--ease);
}
.toggle-group label:hover { border-color: var(--accent); color: var(--accent-text); }
.toggle-group input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.toggle-group label:has(input:checked) {
  background: var(--accent-soft); border-color: var(--accent); color: var(--accent-text); font-weight: 600;
}
.toggle-group label:has(input:focus-visible) { outline: 2px solid var(--focus); outline-offset: 2px; }

/* Switch */
.switch { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch .track {
  width: 38px; height: 22px;
  border-radius: 11px;
  background: var(--border-strong);
  position: relative;
  transition: background var(--t-fast) var(--ease);
  flex: none;
}
.switch .track::after {
  content: "";
  position: absolute;
  top: 2px; inset-inline-start: 2px;
  width: 18px; height: 18px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, .2);
  transition: transform var(--t-fast) var(--ease);
}
.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track::after { transform: translateX(calc(var(--dir) * 16px)); }
.switch input:focus-visible + .track { outline: 2px solid var(--focus); outline-offset: 2px; }

/* --- Segmented control ------------------------------------------------ */
.segmented {
  display: inline-flex;
  padding: 3px;
  border: 1px solid transparent;
  border-radius: var(--r-pill);
  background: var(--surface-3);
}
.segmented button {
  height: 28px;
  padding: 0 13px;
  border-radius: var(--r-pill);
  font-size: 12.5px;
  font-weight: 500;
  color: var(--ink-2);
  transition: background var(--t-fast) var(--ease), color var(--t-fast) var(--ease);
}
.segmented button:hover { color: var(--ink); }
.segmented button[aria-pressed="true"] {
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}

/* --- Tabs (pills) ----------------------------------------------------- */
.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.tabs button {
  height: 34px;
  padding: 0 15px;
  border-radius: var(--r-pill);
  font-size: 13px;
  font-weight: 500;
  color: var(--ink-2);
  background: transparent;
  transition: background var(--t-fast) var(--ease), color var(--t-fast) var(--ease);
}
.tabs button:hover { background: var(--surface-3); color: var(--ink); }
.tabs button[aria-selected="true"] { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.tabs button .count-pill { margin-inline-start: 4px; }
.tabs button[aria-selected="true"] .count-pill { background: rgba(255, 255, 255, .22); color: var(--accent-ink); }

/* --- Progress / meters ------------------------------------------------ */
.meter { height: 8px; border-radius: 4px; background: var(--surface-3); overflow: hidden; }
.meter > i { display: block; height: 100%; border-radius: 4px; background: var(--accent); }

.score { display: inline-flex; align-items: baseline; gap: 2px; font-weight: 600; font-variant-numeric: tabular-nums; }
.score-hi { color: var(--good-ink); }
.score-mid { color: var(--ink); }

/* --- Empty state ------------------------------------------------------ */
.empty {
  display: grid;
  justify-items: center;
  gap: 10px;
  padding: 44px 24px;
  text-align: center;
  color: var(--ink-3);
}
.empty svg { color: var(--accent-soft-2); }
.empty .empty-title { font-weight: 600; color: var(--ink-2); font-size: 14px; }

/* --- Modal ------------------------------------------------------------ */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: color-mix(in srgb, #0B0F0D 55%, transparent);
  display: grid;
  place-items: center;
  padding: 20px;
}
.modal {
  width: 100%;
  max-width: 580px;
  max-height: min(88vh, 820px);
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-modal);
  overflow: hidden;
}
.modal-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 18px 22px 12px;
}
.modal-head h3 { flex: 1; font-size: 17px; }
.modal-body { padding: 8px 22px 22px; overflow-y: auto; }
.modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 14px 22px;
  border-top: 1px solid var(--border);
  background: var(--surface-2);
}

/* --- Toast ------------------------------------------------------------ */
#toast {
  position: fixed;
  z-index: 300;
  bottom: calc(22px + env(safe-area-inset-bottom, 0px));
  inset-inline: 0;
  display: flex;
  justify-content: center;
  pointer-events: none;
  padding-inline: 16px;
}
#toast .toast-inner {
  display: flex;
  align-items: center;
  gap: 9px;
  max-width: 460px;
  padding: 11px 18px;
  border-radius: var(--r-pill);
  background: var(--ink);
  color: var(--canvas);
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow-modal);
  opacity: 0;
  transform: translateY(8px);
  transition: opacity var(--t) var(--ease), transform var(--t) var(--ease);
}
#toast.show .toast-inner { opacity: 1; transform: none; }

/* --- Charts ----------------------------------------------------------- */
.chart { width: 100%; }
.chart text { font-size: 10.5px; fill: var(--ink-3); font-variant-numeric: tabular-nums; }
.chart .chart-value { fill: var(--ink-2); font-weight: 600; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .axisline { stroke: var(--axis); stroke-width: 1; }
.chart .bar { fill: var(--accent); }
.chart .bar-muted { fill: var(--accent-soft-2); }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 12px; color: var(--ink-2); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 9px; height: 9px; border-radius: 50%; flex: none; }

/* Composition bar (stacked, one row) */
.compbar { display: flex; height: 10px; border-radius: 5px; overflow: hidden; gap: 2px; background: transparent; }
.compbar > i { display: block; height: 100%; border-radius: 2px; }

/* --- Media (property gallery) ----------------------------------------- */
.media-main {
  position: relative;
  aspect-ratio: 16 / 10;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r);
  overflow: hidden;
}
.media-main > * { width: 100%; height: 100%; object-fit: cover; }
.media-tag {
  position: absolute;
  bottom: 10px; inset-inline-start: 10px;
  padding: 3px 10px;
  border-radius: var(--r-pill);
  background: color-mix(in srgb, #0B0F0D 70%, transparent);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
}
.media-strip { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.media-thumb {
  width: 64px; height: 48px;
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  overflow: hidden;
  background: var(--surface-2);
  padding: 0;
  cursor: pointer;
}
.media-thumb > * { width: 100%; height: 100%; object-fit: cover; }
.media-thumb[aria-current="true"] { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.media-add {
  width: 64px; height: 48px;
  border: 1px dashed var(--border-strong);
  border-radius: var(--r-sm);
  color: var(--ink-3);
  display: grid;
  place-items: center;
  font-size: 17px;
  background: transparent;
}
.media-add:hover { color: var(--accent); border-color: var(--accent); }

.thumb-xs {
  width: 40px; height: 32px;
  flex: none;
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  overflow: hidden;
  background: var(--surface-2);
}
.thumb-xs > * { width: 100%; height: 100%; object-fit: cover; }

/* --- Form validation --------------------------------------------------- */
.field-error { font-size: 11.5px; font-weight: 500; color: var(--crit-ink); }
.input.invalid, .select.invalid { border-color: var(--crit-mark); }
.input.invalid:focus, .select.invalid:focus {
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--crit-mark) 20%, transparent);
}

/* --- Sortable table headers -------------------------------------------- */
table.data th.sortable { cursor: pointer; user-select: none; white-space: nowrap; }
table.data th.sortable:hover { color: var(--ink); }
table.data th.sortable::after {
  content: "";
  display: inline-block;
  width: 0; height: 0;
  margin-inline-start: 5px;
  vertical-align: middle;
  border-inline-start: 3.5px solid transparent;
  border-inline-end: 3.5px solid transparent;
  border-top: 4px solid currentColor;
  opacity: 0;
  transition: opacity var(--t-fast) var(--ease);
}
table.data th.sortable:hover::after { opacity: .35; }
table.data th[data-sort="asc"]::after { opacity: 1; transform: rotate(180deg); }
table.data th[data-sort="desc"]::after { opacity: 1; }

/* --- Row actions ------------------------------------------------------- */
.row-actions { display: flex; gap: 4px; justify-content: flex-end; }
.row-actions .icon-btn {
  width: 28px; height: 28px;
  border-color: transparent;
  background: transparent;
  color: var(--ink-3);
}
tr:hover .row-actions .icon-btn { background: var(--surface-2); }
.row-actions .icon-btn:hover { color: var(--accent-text); background: var(--accent-soft); }
.row-actions .icon-btn.danger:hover { color: var(--crit-ink); background: var(--crit-bg); }

/* --- Filter bar --------------------------------------------------------- */
.filter-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.filter-bar .select, .filter-bar .input { height: 32px; width: auto; min-width: 140px; font-size: 12.5px; border-radius: var(--r-pill); padding-inline-start: 14px; }
.filter-bar .filter-count { margin-inline-start: auto; font-size: 12px; color: var(--ink-3); }

/* --- Dropdown panels (search, notifications, more menu) ------------------ */
.pop-panel {
  position: absolute;
  top: calc(100% + 8px);
  z-index: 90;
  width: 320px;
  max-height: 60vh;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r);
  box-shadow: var(--shadow-pop);
  padding: 6px;
}
.search-results { inset-inline-start: 0; width: 100%; min-width: 300px; }
.notif-panel { inset-inline-end: 0; }
.search-group {
  padding: 8px 10px 4px;
  font-size: 11.5px;
  font-weight: 600;
  color: var(--ink-3);
}
.search-hit, .notif-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 9px 10px;
  border-radius: var(--r-sm);
  text-align: start;
  color: var(--ink-2);
}
.search-hit:hover, .notif-item:hover { background: var(--accent-soft); color: var(--accent-text); }
.search-hit svg, .notif-item svg { flex: none; color: var(--ink-3); }
.search-hit:hover svg, .notif-item:hover svg { color: var(--accent); }
.search-empty { padding: 20px 12px; text-align: center; font-size: 13px; color: var(--ink-3); }
.notif-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px 10px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 4px;
}
.notif-dot { width: 7px; height: 7px; border-radius: 50%; flex: none; }
.notif-crit { background: var(--crit-mark); }
.notif-warn { background: var(--warn-mark); }
.notif-info { background: var(--accent); }

/* --- Timeline (notary pipeline) --------------------------------------- */
.timeline { display: grid; gap: 0; }
.tl-step { display: grid; grid-template-columns: 22px 1fr; gap: 12px; }
.tl-mark { display: grid; justify-items: center; }
.tl-dot {
  width: 12px; height: 12px;
  margin-top: 5px;
  border-radius: 50%;
  border: 2px solid var(--border-strong);
  background: var(--surface);
  flex: none;
}
.tl-step.done .tl-dot { background: var(--accent); border-color: var(--accent); }
.tl-step.current .tl-dot { border-color: var(--accent-deep); background: var(--accent-deep); box-shadow: 0 0 0 3px var(--accent-soft); }
.tl-line { width: 1px; flex: 1; background: var(--border); min-height: 18px; }
.tl-step:last-child .tl-line { display: none; }
.tl-body { padding-bottom: 14px; }
.tl-title { font-size: 13px; font-weight: 600; }
.tl-meta { font-size: 12px; color: var(--ink-3); }
