:root {
  --bg: #f6f8fb;
  --surface: #ffffff;
  --surface-muted: #f0f4f9;
  --border: #d5dce6;
  --border-strong: #6b7280;
  --text: #111827;
  --text-muted: #4b5563;
  --primary: #0f4c81;
  --primary-hover: #0a3860;
  --positive: #137333;
  --negative: #c5221f;
  --focus: #0b57d0;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.08), 0 1px 2px rgba(16, 24, 40, 0.04);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
}

.visually-hidden {
  position: absolute;
  clip-path: inset(50%);
  overflow: hidden;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  border: 0;
  white-space: nowrap;
}

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

/* Header & Navigation */
.site-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}

.header-inner {
  width: 100%;
  padding: 0.75rem 1.5rem;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.site-logo {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 700;
  font-size: 1.125rem;
  color: var(--text);
  text-decoration: none;
}

.logo-mark {
  display: inline-grid;
  place-items: center;
  width: 2rem;
  height: 2rem;
  border-radius: 8px;
  background: var(--primary);
  color: #fff;
  font-weight: 700;
}

.site-menu {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  gap: 0.25rem;
}

.site-menu a {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0.5rem 0.9rem;
  border-radius: 8px;
  color: var(--text-muted);
  text-decoration: none;
  font-weight: 500;
}

.site-menu a:hover {
  background: var(--surface-muted);
  color: var(--text);
}

.site-menu a[aria-current="page"] {
  background: #e6eff8;
  color: var(--primary);
  font-weight: 600;
}

.header-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

/* Layout */
.container {
  width: 100%;
  padding: 1.25rem 1.5rem 2.5rem;
}

.page-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-block-end: 1.25rem;
}

.page-header h1 {
  margin: 0;
  font-size: 1.75rem;
  line-height: 1.2;
}

.subtitle {
  margin: 0.25rem 0 0;
  color: var(--text-muted);
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
  margin-block-end: 1.25rem;
}

.card h2 {
  margin-top: 0;
  font-size: 1.125rem;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  min-height: 44px;
  padding: 0.55rem 1rem;
  font: inherit;
  font-weight: 600;
  border-radius: 8px;
  border: 1px solid transparent;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
}

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border-color: var(--border);
}

.btn-secondary:hover:not(:disabled) {
  background: var(--surface-muted);
}

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border-color: var(--border);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--surface-muted);
  color: var(--text);
}

.btn-danger-sm {
  min-height: 36px;
  padding: 0.25rem 0.65rem;
  font-size: 0.875rem;
  background: #fef2f2;
  color: var(--negative);
  border: 1px solid #fecaca;
  border-radius: 6px;
  cursor: pointer;
}

.btn-danger-sm:hover {
  background: #fee2e2;
}

/* KPI Grid */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(185px, 1fr));
  gap: 1rem;
  margin-block-end: 1.25rem;
}

.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.kpi-label {
  font-size: 0.875rem;
  color: var(--text-muted);
}

.kpi-value {
  font-size: 1.5rem;
  font-variant-numeric: tabular-nums;
}

.positive {
  color: var(--positive);
}

.negative {
  color: var(--negative);
}

/* Filters */
.filter-panel {
  display: block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  margin-block-end: 1.25rem;
  box-shadow: var(--shadow);
}

.filter-form {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 0.9rem;
  align-items: end;
}

.field-checkbox {
  display: flex;
  align-items: center;
  min-height: 38px;
}

.checkbox-label {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--text);
  cursor: pointer;
  user-select: none;
}

.checkbox-label input[type='checkbox'] {
  width: 16px;
  height: 16px;
  accent-color: var(--primary);
  cursor: pointer;
}

@media (min-width: 1024px) {
  .filter-form {
    grid-template-columns: minmax(150px, 1fr) minmax(150px, 1fr) minmax(180px, 1.2fr) minmax(260px, 2fr) auto;
  }
}

.filter-actions {
  display: flex;
  gap: 0.5rem;
}

/* Form Controls */
.field {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.field label {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text);
}

.field input,
.field select {
  font: inherit;
  font-size: max(1rem, 1em);
  min-height: 48px;
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  width: 100%;
}

.field input:user-invalid {
  border-color: var(--negative);
}

.field-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
}

.hint {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.form-card .field {
  margin-block-end: 1.15rem;
}

.form-actions {
  display: flex;
  gap: 0.75rem;
  margin-block-start: 1.25rem;
}

.form-error {
  padding: 0.75rem 1rem;
  border-radius: 8px;
  background: #fef2f2;
  color: var(--negative);
  border: 1px solid #fecaca;
  margin-block-end: 1rem;
}

/* Autocomplete */
.autocomplete-field {
  position: relative;
}

.autocomplete-list {
  position: absolute;
  top: calc(100% - 1.2rem);
  left: 0;
  right: 0;
  z-index: 30;
  margin: 0.25rem 0 0;
  padding: 0.25rem;
  list-style: none;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.12);
  max-height: 280px;
  overflow-y: auto;
}

.autocomplete-list [role="option"] {
  padding: 0.6rem 0.75rem;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  font-size: 0.95rem;
}

.autocomplete-list [role="option"]:hover,
.autocomplete-list [role="option"][aria-selected="true"] {
  background: #e6eff8;
  color: var(--primary);
}

.scheme-code-badge {
  font-size: 0.8rem;
  color: var(--text-muted);
  white-space: nowrap;
}

/* Tables */
.table-wrapper {
  overflow-x: auto;
  max-inline-size: 100%;
}

table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  min-inline-size: 820px;
  font-size: 0.925rem;
}

caption {
  text-align: left;
  font-weight: 600;
  margin-block-end: 0.75rem;
  color: var(--text-muted);
}

th,
td {
  padding: 0.7rem 0.75rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: middle;
}

thead th {
  background: var(--surface-muted);
  font-weight: 600;
  white-space: nowrap;
  position: sticky;
  top: 0;
  z-index: 1;
}

thead th:has(> button) {
  padding: 0;
}

.sort-btn {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.35rem;
  width: 100%;
  min-height: 42px;
  padding: 0.7rem 0.75rem;
  border: 0;
  background: transparent;
  font: inherit;
  font-weight: 600;
  color: inherit;
  cursor: pointer;
  text-align: left;
}

th.num .sort-btn {
  justify-content: flex-end;
}

th[aria-sort="ascending"] .sort-icon::after {
  content: "▲";
  font-size: 0.7em;
}

th[aria-sort="descending"] .sort-icon::after {
  content: "▼";
  font-size: 0.7em;
}

.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.fund-name-cell {
  min-width: 190px;
  max-width: 380px;
  font-weight: 500;
}

.empty-state {
  text-align: center;
  padding: 2rem 1rem;
  color: var(--text-muted);
}

/* Chart */
.chart-container {
  position: relative;
  height: clamp(360px, 46vh, 520px);
  width: 100%;
}

/* Add Page Layout */
.add-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr);
  gap: 1.25rem;
  align-items: start;
}

@media (max-width: 860px) {
  .add-layout {
    grid-template-columns: 1fr;
  }
}

.stats-list {
  margin: 0 0 1rem;
  display: grid;
  gap: 0.6rem;
}

.stats-list > div {
  display: flex;
  justify-content: space-between;
  padding-bottom: 0.4rem;
  border-bottom: 1px solid var(--border);
}

.stats-list dt {
  color: var(--text-muted);
}

.stats-list dd {
  margin: 0;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.sync-logs {
  list-style: none;
  margin: 0.5rem 0 0;
  padding: 0;
  display: grid;
  gap: 0.5rem;
  max-height: 260px;
  overflow-y: auto;
  font-size: 0.85rem;
}

.sync-logs li {
  padding: 0.5rem 0.65rem;
  border-radius: 6px;
  background: var(--surface-muted);
  border-left: 3px solid var(--primary);
}

.sync-logs li.error {
  border-left-color: var(--negative);
}

/* Toast */
.toast-banner {
  padding: 0.85rem 1rem;
  border-radius: 8px;
  margin-block-end: 1rem;
  background: #e6f4ea;
  color: var(--positive);
  border: 1px solid #b7dfc2;
  font-weight: 500;
}

.toast-banner.error {
  background: #fef2f2;
  color: var(--negative);
  border-color: #fecaca;
}

/* Page header actions & row actions */
.page-header-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.row-actions {
  display: flex;
  gap: 0.4rem;
  align-items: center;
  flex-wrap: wrap;
}

.compact-select {
  min-height: 36px;
  padding: 0.25rem 0.6rem;
  font-size: 0.875rem;
  font-family: inherit;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
}

.btn-secondary-sm {
  min-height: 36px;
  padding: 0.25rem 0.65rem;
  font-size: 0.875rem;
  background: var(--surface-muted);
  color: var(--primary);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  font-weight: 600;
}

.btn-secondary-sm:hover:not(:disabled) {
  background: #e6eff8;
}

.btn-secondary-sm:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Native Modal Dialog */
.modal-dialog {
  width: min(520px, calc(100vw - 2rem));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 45px rgba(16, 24, 40, 0.2);
}

.modal-dialog-wide {
  width: min(820px, calc(100vw - 2rem));
}

.fund-name-link {
  background: none;
  border: none;
  padding: 0;
  margin: 0;
  font: inherit;
  font-weight: 600;
  color: var(--primary);
  text-align: left;
  cursor: pointer;
}

.fund-name-link:hover {
  text-decoration: underline;
}

.nav-history-controls {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  margin-block: 0.5rem 0.75rem;
}

.nav-range-btn.is-active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

.nav-history-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  font-size: 0.875rem;
  color: var(--text-muted);
  margin-block-end: 0.75rem;
}

dialog:modal {
  overscroll-behavior: contain;
}

dialog::backdrop {
  background: rgba(17, 24, 39, 0.45);
  overflow: hidden;
  overscroll-behavior: contain;
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-block-end: 1.25rem;
}

.dialog-header h1 {
  margin: 0;
  font-size: 1.25rem;
}

.btn-sm {
  min-height: 36px;
  padding: 0.25rem 0.6rem;
}

.modal-dialog .field {
  margin-block-end: 1rem;
}

.input-with-btn {
  display: flex;
  gap: 0.5rem;
  align-items: stretch;
}

.input-with-btn input {
  flex: 1;
}

.dialog-actions {
  justify-content: flex-end;
}

/* Chart Header & Segmented Toggle */
.chart-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-block-end: 1rem;
}

.chart-header h2 {
  margin: 0;
}

.chart-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

.segmented-toggle {
  display: inline-flex;
  margin: 0;
  padding: 0.2rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-muted);
  gap: 0.2rem;
}

.segmented-toggle input[type="radio"] {
  position: absolute;
  clip-path: inset(50%);
  width: 1px;
  height: 1px;
  overflow: hidden;
}

.segmented-toggle label {
  display: inline-flex;
  align-items: center;
  min-height: 36px;
  padding: 0.3rem 0.85rem;
  border-radius: 6px;
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
}

.segmented-toggle input[type="radio"]:checked + label {
  background: var(--surface);
  color: var(--primary);
  box-shadow: 0 1px 2px rgba(16, 24, 40, 0.08);
}

.segmented-toggle input[type="radio"]:focus-visible + label {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

.type-badge {
  display: inline-block;
  padding: 0.1rem 0.45rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
  margin-inline-start: 0.35rem;
  vertical-align: middle;
}

.type-badge.other {
  background: #fef3c7;
  color: #92400e;
}

.type-badge.mf {
  background: #e6eff8;
  color: var(--primary);
}

.add-side-col {
  display: grid;
  gap: 0;
}

/* Login & Header Auth */
.user-badge {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0.35rem 0.75rem;
  border-radius: 8px;
  background: var(--surface-muted);
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 600;
}

.login-section {
  min-height: calc(100vh - 5rem);
  display: grid;
  place-items: center;
  padding-block: 2rem;
}

.login-card {
  width: min(440px, 100%);
  padding: 2rem;
}

.login-brand {
  display: flex;
  align-items: center;
  gap: 0.85rem;
  margin-block-end: 1.5rem;
}

.login-brand .logo-mark {
  width: 2.5rem;
  height: 2.5rem;
  font-size: 1.25rem;
  flex-shrink: 0;
}

.login-brand h1 {
  margin: 0;
  font-size: 1.35rem;
  line-height: 1.25;
}

.login-card .field {
  margin-block-end: 1.15rem;
}

.btn-full {
  width: 100%;
}

.field input[type="file"] {
  padding: 0.5rem 0.65rem;
  min-height: auto;
  cursor: pointer;
}

.import-sample-row {
  margin-block-end: 1rem;
}

.import-error-list {
  margin: 0.35rem 0 0;
  padding-inline-start: 1.2rem;
  font-size: 0.9rem;
}

.auth-mode-toggle {
  display: grid;
  grid-template-columns: 1fr 1fr;
  width: 100%;
  margin-block-end: 1.25rem;
}

.auth-mode-toggle label {
  justify-content: center;
}

.nav-badge {
  display: inline-grid;
  place-items: center;
  min-width: 1.35rem;
  height: 1.35rem;
  padding: 0 0.35rem;
  margin-inline-start: 0.35rem;
  border-radius: 999px;
  background: var(--negative);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 700;
  line-height: 1;
}

.alerts-banner {
  display: grid;
  gap: 0.6rem;
  margin-block-end: 1.15rem;
}

.alert-banner-item {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.85rem 1rem;
  border-radius: var(--radius);
  background: #fffbeb;
  border: 1px solid #fcd34d;
  color: #78350f;
}

.alert-banner-note {
  margin-top: 0.15rem;
  font-size: 0.9rem;
  color: #92400e;
}

.label-with-link {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.inline-link {
  font-size: 0.85rem;
  color: var(--primary);
  text-decoration: none;
  font-weight: 600;
}

.inline-link:hover {
  text-decoration: underline;
}

.status-pill {
  display: inline-block;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
}

.status-pill.active {
  background: #e6f4ea;
  color: var(--positive);
}

.status-pill.triggered {
  background: #fef3c7;
  color: #92400e;
}

.status-pill.disabled,
.status-pill.dismissed {
  background: #fef2f2;
  color: var(--negative);
}

.checkbox-field {
  justify-content: flex-end;
}

.checkbox-label {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  min-height: 48px;
  cursor: pointer;
}

.checkbox-label input[type="checkbox"] {
  width: 1.15rem;
  height: 1.15rem;
  min-height: auto;
}

.import-target-note {
  margin: 0 0 0.85rem;
  padding: 0.55rem 0.75rem;
  border-radius: 6px;
  background: #e6eff8;
  color: var(--primary);
  font-size: 0.9rem;
  font-weight: 600;
}

/* Grouped Holdings */
.grouped-holdings {
  display: grid;
  gap: 1rem;
}

.holder-group {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface);
  transition: opacity 0.15s ease, box-shadow 0.12s ease;
}

.holder-group.is-dragging {
  opacity: 0.5;
}

.holder-group.drag-over-top {
  box-shadow: 0 -3px 0 0 var(--primary);
}

.holder-group.drag-over-bottom {
  box-shadow: 0 3px 0 0 var(--primary);
}

.holder-group-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.85rem;
  padding: 0.85rem 1rem;
  background: var(--surface-muted);
  border-bottom: 1px solid var(--border);
}

.holder-drag-handle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.2rem 0.35rem;
  border-radius: 5px;
  color: var(--text-muted);
  font-size: 1rem;
  line-height: 1;
  letter-spacing: -1px;
  cursor: grab;
  user-select: none;
}

.holder-drag-handle:hover,
.holder-drag-handle:focus-visible {
  background: #e6eff8;
  color: var(--primary);
}

.holder-group.is-dragging .holder-drag-handle {
  cursor: grabbing;
}

.holder-group-title {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

.holder-group-title h3 {
  margin: 0;
  font-size: 1.05rem;
}

.holder-toggle-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.25rem 0.5rem;
  margin-inline-start: -0.35rem;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 1.05rem;
  font-weight: 700;
  cursor: pointer;
}

.holder-toggle-btn:hover {
  background: #e6eff8;
  color: var(--primary);
}

.holder-group.is-collapsed .holder-group-header {
  border-bottom: 0;
}

.holder-group-metrics {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1rem;
  font-size: 0.9rem;
}

.holder-metric {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  line-height: 1.25;
}

.holder-metric span {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.holder-metric strong {
  font-variant-numeric: tabular-nums;
}

.grouped-fund-table {
  min-inline-size: 840px;
}

.lot-subrow td {
  background: #fafbfd;
  font-size: 0.87rem;
  padding-block: 0.5rem;
}

.lot-subrow td:first-child {
  padding-inline-start: 2rem;
}

.checkbox-field {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-block: 0.5rem 1rem;
}

.checkbox-field input[type="checkbox"] {
  width: 1.2rem;
  height: 1.2rem;
  accent-color: var(--primary);
}

.checkbox-field label {
  font-weight: 600;
  cursor: pointer;
}

.email-preview-frame {
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: auto;
  background: #f6f8fb;
  padding: 0.5rem;
}

body.is-subaccount .row-actions,
body.is-subaccount .delete-withdraw-btn,
body.is-subaccount .holder-alert-btn,
body.is-subaccount .fund-alert-btn,
body.is-subaccount .perf-fund-value-alert-btn,
body.is-subaccount .drag-handle {
  display: none !important;
}
