:root {
  color-scheme: light;
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #eef3f7;
  --line: #d9e0e8;
  --text: #17212b;
  --muted: #657386;
  --accent: #0f766e;
  --accent-2: #2563eb;
  --danger: #b42318;
  --danger-bg: #fff1f0;
  --warning: #a15c07;
  --warning-bg: #fff7e6;
  --success: #0f7a3d;
  --success-bg: #eaf8ef;
  --info-bg: #edf4ff;
  --shadow: 0 14px 34px rgba(25, 42, 62, 0.10);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 14px;
  line-height: 1.45;
  letter-spacing: 0;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  min-height: 36px;
  padding: 0 14px;
  cursor: pointer;
}

button:hover {
  filter: brightness(0.96);
}

button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  min-height: 36px;
  padding: 8px 10px;
}

textarea {
  resize: vertical;
}

table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 10px;
  text-align: left;
  vertical-align: middle;
  overflow-wrap: anywhere;
}

th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: #f8fafc;
  color: #465568;
  font-size: 12px;
  font-weight: 800;
}

td {
  color: #273442;
}

.hidden {
  display: none !important;
}

.login-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background: #e8edf2;
}

.login-panel {
  display: grid;
  gap: 16px;
  width: min(420px, 100%);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
  padding: 28px;
}

.login-panel h1,
.topbar h2,
.panel h3 {
  margin: 0;
}

.login-panel h1 {
  font-size: 28px;
}

.form-error {
  min-height: 18px;
  margin: 0;
  color: var(--danger);
  font-weight: 700;
}

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr);
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  border-right: 1px solid var(--line);
  background: #182331;
  color: #fff;
  padding: 18px;
}

.brand-lockup {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 22px;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 8px;
  background: #e8f7f4;
  color: #0f766e;
  font-weight: 900;
}

.brand-lockup strong,
.brand-lockup span {
  display: block;
}

.brand-lockup span:not(.brand-mark) {
  color: #b9c6d4;
  font-size: 12px;
}

.nav-list {
  display: grid;
  gap: 6px;
}

.nav-item {
  justify-content: flex-start;
  border-color: transparent;
  background: transparent;
  color: #dbe5ef;
  text-align: left;
}

.nav-item.active,
.nav-item:hover {
  background: #253346;
  color: #fff;
}

.main {
  min-width: 0;
  padding: 18px;
}

.topbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 12px;
}

.topbar-title {
  min-width: 180px;
}

.eyebrow {
  margin: 0 0 3px;
  color: var(--accent);
  font-size: 11px;
  font-weight: 900;
  text-transform: uppercase;
}

.toolbar,
.filter-row {
  display: flex;
  align-items: end;
  justify-content: flex-end;
  gap: 10px;
  flex-wrap: wrap;
}

.toolbar label {
  width: 150px;
}

.toolbar button {
  min-width: 84px;
}

.status-strip {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 38px;
  margin-bottom: 12px;
  color: var(--muted);
}

.tabs {
  display: flex;
  gap: 8px;
  margin: 10px 0;
  flex-wrap: wrap;
}

.tab-button {
  min-height: 34px;
  border-radius: 6px;
  background: #f3f6f9;
  color: #263244;
}

.tab-button.active {
  background: #dff3ef;
  color: #0f766e;
  border-color: #7cc8b9;
}

.bank-tab-panel {
  display: none;
}

.bank-tab-panel.active {
  display: block;
}

.api-status {
  margin-bottom: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 10px 12px;
  font-weight: 700;
}

.api-status.success {
  border-color: #a7e3bd;
  background: var(--success-bg);
  color: var(--success);
}

.api-status.warning {
  border-color: #f4cd8a;
  background: var(--warning-bg);
  color: var(--warning);
}

.api-status.danger {
  border-color: #f2b8b5;
  background: var(--danger-bg);
  color: var(--danger);
}

.status-pill,
.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 24px;
  border-radius: 999px;
  padding: 2px 9px;
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
}

.neutral {
  background: var(--surface-2);
  color: #425269;
}

.success {
  background: var(--success-bg);
  color: var(--success);
}

.warning {
  background: var(--warning-bg);
  color: var(--warning);
}

.danger {
  background: var(--danger-bg);
  color: var(--danger);
}

.info {
  background: var(--info-bg);
  color: var(--accent-2);
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(8, minmax(132px, 1fr));
  gap: 10px;
  margin-bottom: 12px;
}

.compact-metrics {
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 6px;
  margin-bottom: 8px;
}

.compact-metrics + .compact-metrics {
  grid-template-columns: repeat(6, minmax(140px, 1fr));
}

.metric {
  min-height: 96px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 12px;
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.02);
}

.metric span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.metric strong {
  display: block;
  margin-top: 10px;
  font-size: 22px;
  line-height: 1.1;
  overflow-wrap: anywhere;
}

.balance-chip {
  display: grid;
  gap: 2px;
  min-height: 54px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  padding: 7px 9px;
}

.balance-chip span {
  min-width: 0;
  overflow: hidden;
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.balance-chip strong {
  color: var(--text);
  font-size: 15px;
  line-height: 1.1;
}

.balance-chip em {
  overflow: hidden;
  color: var(--muted);
  font-size: 11px;
  font-style: normal;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.content-grid,
.settings-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  overflow: hidden;
}

.panel.wide {
  grid-column: 1 / -1;
}

.panel-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 50px;
  border-bottom: 1px solid var(--line);
  padding: 12px 14px;
}

.panel-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.table-search {
  width: 260px;
  min-height: 34px;
  border-radius: 6px;
}

.muted {
  color: var(--muted);
  font-size: 12px;
}

.bar-chart,
.confidence-chart,
.stack-list,
.record-list,
.detail-list,
.permission-list {
  padding: 14px;
}

.bar-item,
.confidence-row {
  display: grid;
  grid-template-columns: 150px minmax(0, 1fr) 88px;
  align-items: center;
  gap: 10px;
  min-height: 34px;
}

.bar-track,
.confidence-track {
  height: 12px;
  overflow: hidden;
  border-radius: 999px;
  background: #e8edf2;
}

.bar-fill,
.confidence-fill {
  height: 100%;
  width: 0;
  border-radius: inherit;
  background: var(--accent-2);
}

.bar-fill.negative,
.confidence-fill.warn {
  background: var(--warning);
}

.confidence-fill.good {
  background: var(--success);
}

.confidence-fill.bad {
  background: var(--danger);
}

.table-wrap {
  position: relative;
  overflow: auto;
  max-height: 460px;
}

.table-tall {
  max-height: calc(100vh - 255px);
}

.filter-row {
  justify-content: flex-start;
  margin-bottom: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 12px;
}

.filter-row label {
  width: 190px;
}

.filter-row .search-field {
  width: min(480px, 100%);
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  padding: 14px;
}

.form-grid button,
.form-grid .fieldset {
  grid-column: 1 / -1;
}

.fieldset {
  border: 1px solid var(--line);
  border-radius: 8px;
  margin: 0;
  padding: 12px;
}

.fieldset legend {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.checkbox-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
  font-size: 13px;
  font-weight: 700;
}

.checkbox-label input {
  width: 16px;
  min-height: 16px;
}

.excel-table {
  border-collapse: separate;
  border-spacing: 0;
  table-layout: fixed;
  font-size: 12px;
}

.excel-table th,
.excel-table td {
  width: 100px;
  min-width: 40px;
  height: 21px;
  min-height: 21px;
  border-right: 1px solid #cfd8e3;
  border-bottom: 1px solid #cfd8e3;
  padding: 2px 4px;
  position: relative;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: center;
  vertical-align: middle;
}

.excel-table thead tr:first-child th {
  position: sticky;
  top: 0;
  z-index: 20;
  background: #f7fafc;
  vertical-align: bottom;
  line-height: 1.15;
  height: 30px;
  min-height: 30px;
  overflow: visible;
  text-align: center;
}

.excel-table thead tr.excel-filter-row th,
.excel-table thead tr.excel-filter-row td {
  position: sticky;
  top: 30px;
  z-index: 18;
  background: #eef3f7;
  height: 34px;
  min-height: 34px;
  padding: 2px;
  overflow: visible;
}

.filter-menu {
  position: relative;
  min-width: 92px;
  z-index: 1;
}

.filter-menu[open] {
  z-index: 80;
}

.filter-menu summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 30px;
  border: 1px solid #b8c4d2;
  border-radius: 4px;
  background: #fff;
  padding: 3px 6px;
  color: #263244;
  font-size: 12px;
  font-weight: 800;
  cursor: pointer;
  list-style: none;
}

.filter-menu summary::-webkit-details-marker {
  display: none;
}

.filter-menu summary::after {
  content: "v";
  margin-left: 8px;
  color: #66758a;
  font-size: 10px;
}

.filter-popover {
  position: fixed;
  top: var(--filter-popover-top, 0);
  left: var(--filter-popover-left, 0);
  z-index: 90;
  width: var(--filter-popover-width, 260px);
  max-height: var(--filter-popover-max-height, 280px);
  border: 1px solid #b8c4d2;
  border-radius: 6px;
  background: #fff;
  box-shadow: 0 16px 34px rgba(17, 24, 39, 0.16);
  padding: 8px;
  overflow: hidden;
}

.filter-search {
  width: 100%;
  min-height: 30px;
  margin-bottom: 6px;
  border: 1px solid #c9d3df;
  border-radius: 4px;
  padding: 5px 8px;
  font: inherit;
}

.filter-action-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-bottom: 6px;
}

.filter-action {
  width: 100%;
  min-height: 30px;
  border-radius: 4px;
  background: #f3f6f9;
  color: #263244;
}

.filter-options {
  display: grid;
  gap: 4px;
  max-height: calc(var(--filter-popover-max-height, 280px) - 86px);
  overflow: auto;
}

.filter-option {
  display: grid;
  grid-template-columns: 16px minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  min-height: 26px;
  padding: 3px 4px;
  border-radius: 4px;
  color: #263244;
  font-size: 12px;
}

.filter-option:hover {
  background: #eef3f7;
}

.filter-option input {
  width: 14px;
  min-height: 14px;
}

.filter-option span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.filter-empty {
  color: var(--muted);
  font-size: 12px;
  padding: 8px 4px;
}

.excel-table th:first-child {
  position: sticky;
  left: 0;
  z-index: 10;
  min-width: 48px;
  max-width: 48px;
  width: 48px;
  text-align: center;
}

.excel-table thead tr:first-child th:first-child {
  z-index: 24;
}

.excel-table thead tr.excel-filter-row th:first-child,
.excel-table thead tr.excel-filter-row td:first-child {
  z-index: 22;
}

.excel-table tbody th {
  position: sticky;
  left: 0;
  z-index: 4;
  background: #f4f6f8;
  color: #465568;
  font-weight: 800;
}

.excel-table td[contenteditable="true"] {
  background: #fff;
  cursor: cell;
}

.col-resize-handle {
  position: absolute;
  top: 0;
  right: 0;
  z-index: 25;
  width: 8px;
  height: 100%;
  cursor: col-resize;
  user-select: none;
}

.col-resize-handle:hover,
.is-resizing-table .col-resize-handle {
  background: rgba(15, 118, 110, 0.22);
}

.row-resize-handle {
  position: absolute;
  right: 2px;
  bottom: 0;
  z-index: 7;
  width: 18px;
  height: 6px;
  border-bottom: 2px solid #7b8a9c;
  cursor: row-resize;
  opacity: 0;
  user-select: none;
}

.excel-table tr:hover .row-resize-handle {
  opacity: 1;
}

.is-resizing-table {
  user-select: none;
}

.is-resizing-column {
  cursor: col-resize;
}

.is-resizing-row {
  cursor: row-resize;
}

.excel-table .readonly-cell {
  background: #f6f8fb;
  color: #5d6b7a;
}

.excel-table td[contenteditable="true"]:focus {
  position: relative;
  outline: 2px solid var(--accent-2);
  outline-offset: -2px;
  background: #f8fbff;
}

.excel-table td.sheet-selected {
  background: #dff3ef;
  outline: 1px solid #0f766e;
  outline-offset: -1px;
}

.excel-table .blank-row td,
.excel-table .blank-cell {
  background: #fcfdff;
  color: #273442;
}

.excel-table .blank-row th {
  color: #8a98a8;
}

.excel-table .virtual-spacer-row td {
  border: 0;
  padding: 0;
  background: transparent;
  overflow: hidden;
}

.excel-table .virtual-loading-row th,
.excel-table .virtual-loading-row td {
  height: 26px;
  min-height: 26px;
  color: #8a98a8;
  background: #f8fafc;
}

.excel-table .virtual-loading-cell::after {
  content: "";
  display: block;
  width: 70%;
  height: 8px;
  margin: 0 auto;
  border-radius: 4px;
  background: linear-gradient(90deg, #eef3f7, #dfe7ef, #eef3f7);
}

.row-actions {
  display: flex;
  gap: 6px;
  justify-content: flex-end;
}

.row-actions button,
.ghost-button {
  min-height: 30px;
  border-color: var(--line);
  background: #fff;
  color: var(--text);
}

.stack-item,
.record-card {
  display: grid;
  gap: 6px;
  border-bottom: 1px solid var(--line);
  padding: 10px 0;
}

.stack-item:first-child,
.record-card:first-child {
  padding-top: 0;
}

.stack-item:last-child,
.record-card:last-child {
  border-bottom: 0;
  padding-bottom: 0;
}

.stack-item strong,
.record-card strong {
  overflow-wrap: anywhere;
}

.detail-list {
  display: grid;
  grid-template-columns: minmax(110px, 160px) minmax(0, 1fr);
  gap: 8px 12px;
  margin: 0;
}

.detail-list dt {
  color: var(--muted);
  font-weight: 800;
}

.detail-list dd {
  margin: 0;
  overflow-wrap: anywhere;
}

.permission-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.review-panel {
  position: fixed;
  top: 0;
  right: 0;
  z-index: 20;
  display: none;
  width: min(420px, 100vw);
  height: 100vh;
  overflow: auto;
  border-left: 1px solid var(--line);
  background: var(--surface);
  box-shadow: var(--shadow);
  padding-bottom: 18px;
}

.review-panel.open {
  display: block;
}

.review-panel label,
.review-panel button {
  margin: 12px 14px 0;
}

.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 40;
  display: none;
  max-width: 360px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #17212b;
  color: #fff;
  box-shadow: var(--shadow);
  padding: 12px 14px;
  font-weight: 700;
}

.toast.show {
  display: block;
}

.selection-summary {
  min-width: 150px;
  border: 1px solid #8bc8bc;
  border-radius: 6px;
  background: #fff;
  padding: 7px 10px;
  color: #173b36;
  font-size: 12px;
  font-weight: 800;
  text-align: right;
}

@media (max-width: 1260px) {
  .metric-grid {
    grid-template-columns: repeat(4, minmax(132px, 1fr));
  }

  .topbar {
    align-items: stretch;
    flex-direction: column;
  }

  .toolbar {
    justify-content: flex-start;
  }
}

@media (max-width: 900px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
    height: auto;
  }

  .nav-list {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .content-grid,
  .settings-grid {
    grid-template-columns: 1fr;
  }

  .form-grid,
  .checkbox-grid {
    grid-template-columns: 1fr;
  }

  .metric-grid {
    grid-template-columns: repeat(2, minmax(132px, 1fr));
  }
}

@media (max-width: 560px) {
  .main {
    padding: 12px;
  }

  .toolbar label,
  .toolbar button,
  .filter-row label,
  .filter-row button {
    width: 100%;
  }

  .nav-list {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .metric-grid {
    grid-template-columns: 1fr;
  }

  .bar-item,
  .confidence-row {
    grid-template-columns: 1fr;
  }
}
