/* ═══════════════════════════════════════════
   DG 2025 Expense Dashboard — Styles
   ═══════════════════════════════════════════ */

:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --border: #e2e5ea;
  --text: #1a1d23;
  --text-secondary: #6b7280;
  --text-muted: #9ca3af;
  --accent: #2b7bba;
  --accent-light: rgba(43, 123, 186, 0.08);
  --header-bg: #1a1d23;
  --header-text: #f0f1f3;
  --font: 'DM Sans', -apple-system, BlinkMacSystemFont, sans-serif;
  --mono: 'JetBrains Mono', 'SF Mono', monospace;
  --radius: 6px;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.08);
  --color-ops: #2d9d3a;
  --color-inv: #2b7bba;
  --color-fund: #e67e22;
  --color-internal: #95a5a6;
  --color-review: #f1c40f;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
  overflow: hidden;
}

/* ═══ Login Screen ═══ */
.login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #1a1d23 0%, #2d3748 100%);
  padding: 24px;
}

.login-card {
  background: var(--surface);
  border-radius: 12px;
  padding: 48px 40px;
  text-align: center;
  box-shadow: 0 8px 32px rgba(0,0,0,0.2);
  max-width: 380px;
  width: 100%;
}

.login-brand {
  margin-bottom: 32px;
}

.login-logo {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 56px;
  height: 56px;
  background: var(--header-bg);
  color: #fff;
  font-size: 20px;
  font-weight: 700;
  border-radius: 12px;
  letter-spacing: -0.02em;
  margin-bottom: 16px;
}

.login-card h1 {
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.01em;
  margin-bottom: 4px;
}

.login-subtitle {
  font-size: 14px;
  color: var(--text-secondary);
}

.sign-in-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  width: 100%;
  padding: 12px 24px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: var(--font);
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
  cursor: pointer;
  transition: all 0.15s;
}

.sign-in-btn:hover:not(:disabled) {
  border-color: #bbb;
  box-shadow: var(--shadow-sm);
}

.sign-in-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.login-domain {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 16px;
}

.login-error {
  font-size: 13px;
  color: #e74c3c;
  margin-top: 12px;
  padding: 8px 12px;
  background: rgba(231, 76, 60, 0.06);
  border-radius: var(--radius);
}

/* ═══ App Shell ═══ */
.app-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* ═══ Header ═══ */
.app-header {
  background: var(--header-bg);
  color: var(--header-text);
  padding: 0 24px;
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-brand {
  font-size: 15px;
  font-weight: 700;
  background: rgba(255,255,255,0.12);
  padding: 3px 8px;
  border-radius: 4px;
  letter-spacing: -0.02em;
}

.app-header h1 {
  font-size: 15px;
  font-weight: 500;
  letter-spacing: -0.01em;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: var(--radius);
  color: var(--header-text);
  font-family: var(--font);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s;
}

.header-btn:hover { background: rgba(255,255,255,0.14); }

.header-btn.refreshing svg {
  animation: spin 0.8s linear infinite;
}

.header-user {
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-name {
  font-size: 12px;
  color: #8b919a;
}

.header-btn-text {
  background: none;
  border: none;
  color: #8b919a;
  font-family: var(--font);
  font-size: 12px;
  cursor: pointer;
  padding: 4px;
  transition: color 0.15s;
}

.header-btn-text:hover { color: var(--header-text); }

/* ═══ Tab Bar ═══ */
.tab-bar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  display: flex;
  align-items: center;
  height: 42px;
  gap: 0;
  flex-shrink: 0;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.tab {
  position: relative;
  padding: 0 16px;
  height: 42px;
  display: flex;
  align-items: center;
  background: none;
  border: none;
  font-family: var(--font);
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  transition: color 0.15s;
  white-space: nowrap;
}

.tab:hover { color: var(--text-secondary); }

.tab.active {
  color: var(--text);
  font-weight: 600;
}

.tab.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 16px;
  right: 16px;
  height: 2px;
  background: var(--accent);
  border-radius: 1px 1px 0 0;
}

.tab-meta {
  margin-left: auto;
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  padding-left: 16px;
  font-family: var(--mono);
}

/* ═══ Views ═══ */
.view-container {
  flex: 1;
  overflow: hidden;
  position: relative;
  max-width: 100%;
}

.view {
  display: none;
  height: 100%;
  overflow: hidden;
}

.view.active {
  display: flex;
  flex-direction: column;
}

/* ═══ Loading ═══ */
.loading-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: var(--bg);
  color: var(--text-muted);
  font-size: 14px;
  z-index: 10;
}

.loading-spinner {
  width: 28px;
  height: 28px;
  border: 2.5px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ═══════════════════════════════════════════
   PIE VIEW
   ═══════════════════════════════════════════ */

/* Breadcrumb */
.pie-breadcrumb {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  display: flex;
  align-items: center;
  min-height: 40px;
  gap: 2px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  flex-shrink: 0;
}

.bc-segment {
  display: flex;
  align-items: center;
  gap: 2px;
  flex-shrink: 0;
}

.bc-link {
  font-size: 13px;
  font-weight: 500;
  color: var(--accent);
  cursor: pointer;
  padding: 6px 8px;
  border-radius: 4px;
  transition: background 0.15s;
  white-space: nowrap;
  text-decoration: none;
}

.bc-link:hover { background: var(--accent-light); }

.bc-sep {
  color: var(--text-muted);
  font-size: 11px;
  margin: 0 2px;
  user-select: none;
}

.bc-current {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  padding: 6px 8px;
  white-space: nowrap;
}

.bc-hint {
  margin-left: auto;
  font-size: 11px;
  color: var(--text-muted);
  flex-shrink: 0;
  padding-left: 16px;
}

.bc-hint kbd {
  font-family: var(--mono);
  font-size: 10px;
  background: #f0f1f3;
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 1px 5px;
  margin: 0 1px;
}

/* Pie Content */
.pie-content {
  flex: 1;
  display: flex;
  overflow: hidden;
}

.pie-chart-area {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Hide Highcharts built-in drilldown breadcrumbs (we use our own) */
.highcharts-breadcrumbs-group { display: none !important; }

/* Scrollable Legend */
.pie-legend-wrap {
  width: 240px;
  flex-shrink: 0;
  border-left: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.pie-legend {
  flex: 1;
  overflow-y: auto;
  padding: 10px 0;
  -webkit-overflow-scrolling: touch;
}

.pie-legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 14px;
  cursor: pointer;
  transition: background 0.12s;
  font-size: 12px;
}

.pie-legend-item:hover { background: #f8f9fb; }

.pie-legend-dot {
  width: 10px;
  height: 10px;
  border-radius: 2px;
  flex-shrink: 0;
}

.pie-legend-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 500;
  color: var(--text);
}

.pie-legend-val {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-secondary);
  flex-shrink: 0;
}

.pie-legend-pct {
  font-size: 10px;
  color: var(--text-muted);
  width: 36px;
  text-align: right;
  flex-shrink: 0;
}

.pie-chart-area .plotly-chart {
  width: 100%;
  height: 100%;
}

/* Pie Status */
.pie-status {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 24px;
  height: 36px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}

.pie-status-total {
  font-family: var(--mono);
  font-size: 13px;
  font-weight: 500;
  color: var(--accent);
}

.pie-status-hint {
  font-size: 12px;
  color: var(--text-muted);
}

/* Pie Transaction Table */
.pie-table-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.pie-table-header {
  padding: 12px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  align-items: baseline;
  gap: 12px;
  flex-shrink: 0;
}

.pie-table-header .count {
  font-size: 14px;
  font-weight: 600;
}

.pie-table-header .amount {
  font-family: var(--mono);
  font-size: 13px;
  font-weight: 500;
  color: var(--accent);
}

.pie-table-scroll {
  flex: 1;
  overflow: auto;
}

/* ═══════════════════════════════════════════
   SANKEY VIEW
   ═══════════════════════════════════════════ */

.sankey-controls-bar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 6px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.sankey-toggle-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 10px;
  font-family: var(--font);
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 4px;
  transition: all 0.15s;
}

.sankey-toggle-btn:hover { border-color: #adb5bd; color: var(--text); }

.sankey-toggle-icon { font-size: 8px; }

.sankey-controls {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 10px 24px;
  display: flex;
  align-items: flex-start;
  gap: 28px;
  flex-wrap: wrap;
  flex-shrink: 0;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.control-group .group-label {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
}

.checkboxes {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.checkboxes label {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  cursor: pointer;
  padding: 3px 8px;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: var(--surface);
  transition: all 0.15s;
}

.checkboxes label:hover { border-color: #adb5bd; }
.checkboxes label.checked { border-color: currentColor; background: color-mix(in srgb, currentColor 8%, white); }
.checkboxes input[type="checkbox"] { display: none; }

.ctl-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
}

.btn-group {
  display: flex;
  gap: 4px;
}

.btn-sm {
  font-family: var(--font);
  font-size: 12px;
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}

.btn-sm:hover { border-color: #adb5bd; background: #f1f3f5; }
.btn-sm.active { background: var(--header-bg); color: #fff; border-color: var(--header-bg); }

.threshold-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.threshold-row input[type="range"] { width: 140px; }

.threshold-val {
  font-family: var(--mono);
  font-size: 12px;
  min-width: 55px;
  color: var(--text-secondary);
}

.sankey-info {
  font-size: 11px;
  color: var(--text-muted);
  font-family: var(--mono);
}

.sankey-chart-area {
  flex: 1;
  min-height: 0;
}

.sankey-chart-area .plotly-chart {
  width: 100%;
  height: 100%;
}

/* ═══════════════════════════════════════════
   TABLE VIEW
   ═══════════════════════════════════════════ */

.table-toolbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 10px 24px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  flex-shrink: 0;
}

.table-toolbar select {
  font-family: var(--font);
  font-size: 12px;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  color: var(--text);
  max-width: 160px;
  cursor: pointer;
}

.table-toolbar select:focus {
  outline: none;
  border-color: var(--accent);
}

.table-search {
  font-family: var(--font);
  font-size: 12px;
  padding: 5px 10px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  color: var(--text);
  min-width: 180px;
  flex: 1;
  max-width: 300px;
}

.table-search:focus {
  outline: none;
  border-color: var(--accent);
}

.table-search::placeholder { color: var(--text-muted); }

.table-stats {
  font-size: 12px;
  color: var(--text-secondary);
  margin-left: auto;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 8px;
}

.table-stats .stat-count {
  font-weight: 600;
  color: var(--text);
}

.table-stats .stat-amount {
  font-family: var(--mono);
  font-weight: 500;
  color: var(--accent);
}

.btn-export {
  font-family: var(--font);
  font-size: 12px;
  padding: 5px 12px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}

.btn-export:hover { border-color: #adb5bd; background: #f1f3f5; }

.table-scroll-area {
  flex: 1;
  overflow: auto;
}

/* Shared table styles (pie txn table + table view) */
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.data-table thead { position: sticky; top: 0; z-index: 2; }

.data-table th {
  background: #f0f1f3;
  color: var(--text-secondary);
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 9px 14px;
  text-align: left;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
  border-bottom: 2px solid var(--border);
  transition: color 0.15s;
}

.data-table th:hover { color: var(--text); }
.data-table th.active { color: var(--accent); }
.data-table th .sort-arrow { font-size: 10px; margin-left: 3px; }
.data-table th.num { text-align: right; }

.data-table td {
  padding: 8px 14px;
  border-bottom: 1px solid #f0f1f3;
  color: var(--text);
  vertical-align: top;
}

.data-table td.num {
  text-align: right;
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
}

.data-table td.date-cell {
  white-space: nowrap;
  color: var(--text-secondary);
  font-family: var(--mono);
  font-size: 12px;
}

.data-table td.desc-cell {
  max-width: 280px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text-secondary);
  font-size: 12px;
}

.data-table td.entity-cell {
  font-size: 12px;
  color: var(--text-secondary);
  white-space: nowrap;
}

.data-table td.source-cell {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
}

.data-table tbody tr { transition: background 0.1s; }
.data-table tbody tr:hover { background: #f8f9fb; }

.data-table a.invoice-link {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  color: var(--accent);
  font-size: 12px;
  font-weight: 500;
  text-decoration: none;
  padding: 2px 8px;
  border-radius: 3px;
  background: var(--accent-light);
  transition: background 0.15s;
}

.data-table a.invoice-link:hover { background: rgba(43, 123, 186, 0.14); }

/* ═══ Empty / Error States ═══ */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  gap: 8px;
  color: var(--text-muted);
  font-size: 14px;
  padding: 40px;
  text-align: center;
}

/* ═══════════════════════════════════════════
   RESPONSIVE — TABLET
   ═══════════════════════════════════════════ */
@media (max-width: 900px) {
  .pie-legend-wrap { width: 200px; }
  .sankey-controls { gap: 16px; }
}

/* ═══════════════════════════════════════════
   RESPONSIVE — MOBILE
   ═══════════════════════════════════════════ */
@media (max-width: 768px) {
  /* Header */
  .app-header { padding: 0 16px; height: 48px; }
  .app-header h1 { font-size: 14px; }
  .header-brand { font-size: 13px; padding: 2px 6px; }
  .user-name { display: none; }

  /* Tab bar */
  .tab-bar { padding: 0 12px; height: 38px; }
  .tab { padding: 0 12px; font-size: 12px; }
  .tab-meta { display: none; }

  /* Pie: stack chart and legend vertically, scroll as one unit */
  .pie-content {
    flex-direction: column;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
  }
  .pie-chart-area {
    flex: none;
    height: 55vh;
    display: block;
  }
  .pie-legend-wrap {
    flex: none;
    width: 100%;
    border-left: none;
    border-top: 2px solid var(--border);
  }
  .pie-legend {
    max-height: none;
    overflow-y: visible;
  }
  .pie-breadcrumb { padding: 0 12px; min-height: 36px; }
  .pie-status { padding: 0 12px; height: 30px; }
  .pie-status-total { font-size: 12px; }
  .pie-status-hint { font-size: 11px; }
  .pie-table-header { padding: 10px 16px; }
  .pie-table-container { overflow: auto; }
  .pie-table-scroll { overflow: auto; -webkit-overflow-scrolling: touch; }

  /* Sankey */
  .sankey-controls-bar { padding: 5px 12px; }
  .sankey-controls {
    padding: 8px 12px;
    gap: 8px;
    max-height: 300px;
    overflow: hidden;
    transition: max-height 0.3s ease, padding 0.3s ease, border-width 0.3s ease;
  }
  .sankey-controls.collapsed {
    max-height: 0;
    padding-top: 0;
    padding-bottom: 0;
    border-bottom-width: 0;
  }
  .sankey-controls .control-group .group-label { font-size: 9px; }
  .sankey-controls .checkboxes { gap: 4px; }
  .sankey-controls .checkboxes label { font-size: 11px; padding: 2px 6px; }
  .sankey-controls .btn-sm { font-size: 11px; padding: 3px 7px; }
  .threshold-row input[type="range"] { width: 80px; }
  .sankey-info { font-size: 10px; }
  .sankey-chart-area {
    min-height: 0;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
  }
  .sankey-chart-area .plotly-chart {
    min-width: 600px;
    height: 100%;
  }

  /* Table */
  .table-toolbar { padding: 8px 16px; gap: 8px; }
  .table-search { min-width: 140px; }
  .table-toolbar select { max-width: 130px; }

  .data-table th { padding: 7px 10px; font-size: 10px; }
  .data-table td { padding: 6px 10px; font-size: 12px; }
  .data-table td.desc-cell { max-width: 180px; }
}

@media (max-width: 480px) {
  .login-card { padding: 32px 24px; }
  .login-card h1 { font-size: 18px; }

  .header-btn span { display: none; }
  .header-btn { padding: 6px 8px; }

  .pie-chart-area { min-height: 40vh; }

  .table-toolbar select { max-width: 110px; font-size: 11px; }
  .table-search { min-width: 120px; font-size: 11px; }
  .btn-export { display: none; }
}
