/* ============================================
   STL Calculator - Shadcn/Tailwind Inspired CSS
   ============================================ */

/* CSS Variables / Design Tokens */
:root {
  --background: 0 0% 100%;
  --foreground: 222.2 84% 4.9%;
  --card: 0 0% 100%;
  --card-foreground: 222.2 84% 4.9%;
  --primary: 210 54% 48%;
  --primary-foreground: 210 40% 98%;
  --secondary: 210 40% 96.1%;
  --secondary-foreground: 222.2 47.4% 11.2%;
  --muted: 210 40% 96.1%;
  --muted-foreground: 215.4 16.3% 46.9%;
  --accent: 210 40% 96.1%;
  --accent-foreground: 222.2 47.4% 11.2%;
  --destructive: 0 84.2% 60.2%;
  --destructive-foreground: 210 40% 98%;
  --border: 214.3 31.8% 91.4%;
  --input: 214.3 31.8% 91.4%;
  --ring: 222.2 84% 4.9%;
  --radius: 0.5rem;
  
  /* Brand colors */
  --brand-blue: #367abf;
  --brand-dark-blue: #28456e;
  --brand-light-green: #99cc66;
  --brand-light-blue: #99bbdd;
  
  /* Table column colors - refined palette per client request */
  --col-white: #ffffff;
  --col-red-light: #E68B7C;       /* Light red for Total Expenses & Loan Interest */
  --col-red-dark: #CF451F;        /* Darker red for Loan Balance */
  --col-green-dark: #338F0B;      /* Darker green for Account Value, Available Cash, Death Benefit */
  --col-green-light: #85B54E;     /* Lighter green for Interest Earned & Life Insurance */
  --col-blue-light: #99BBDD;      /* Loan blue - brand light blue */
  --col-neutral: #f8fafc;
}

/* Base Styles */
*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: 'Inter', ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ============================================
   Table Styles - Shadcn Inspired
   ============================================ */

.report-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
  background: white;
}

.report-table th,
.report-table td {
  border: 1px solid hsl(var(--border));
  padding: 0.75rem 1rem;
  text-align: left;
  vertical-align: middle;
}

/* Text alignment classes for toggle */
.report-table.align-left td,
.report-table.align-left th { text-align: left; }
.report-table.align-center td,
.report-table.align-center th { text-align: center; }
.report-table.align-right td,
.report-table.align-right th { text-align: right; }

.report-table th {
  background: linear-gradient(to bottom, hsl(210 40% 96.1%), hsl(210 40% 93%));
  color: hsl(var(--foreground));
  font-weight: 600;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  text-align: center;
  white-space: nowrap;
  position: sticky;
  top: 0;
  z-index: 10;
}

.report-table td:first-child {
  text-align: left;
  font-weight: 600;
  background: hsl(var(--secondary));
  color: hsl(var(--secondary-foreground));
  white-space: nowrap;
}

.report-table tbody tr {
  transition: background-color 0.15s ease;
}

.report-table tbody tr:hover {
  background-color: hsl(210 40% 97%);
}

.report-table tbody tr:nth-child(even) {
  background-color: hsl(210 40% 98.5%);
}

.report-table tbody tr:nth-child(even):hover {
  background-color: hsl(210 40% 96%);
}

/* Computed cells styling */
.report-table .computed {
  background-color: hsl(48 96% 95%);
  font-weight: 500;
}

/* Table inputs */
.report-table input {
  width: 100%;
  padding: 0.375rem 0.5rem;
  font-size: 0.875rem;
  border: 1px solid hsl(var(--border));
  border-radius: 0.375rem;
  background: white;
  text-align: left;
  transition: all 0.15s ease;
}

/* Input alignment classes for toggle */
.report-table.align-left input { text-align: left; }
.report-table.align-center input { text-align: center; }
.report-table.align-right input { text-align: right; }

.report-table input:focus {
  outline: none;
  border-color: hsl(var(--primary));
  box-shadow: 0 0 0 3px hsla(var(--primary), 0.1);
}

/* ============================================
   Transposed Table Specific Styles
   ============================================ */

.report-table-transposed {
  table-layout: auto;
}

.report-table-transposed th {
  padding: 0.625rem 0.75rem;
  font-size: 0.6875rem;
  white-space: normal;
  word-break: break-word;
  min-width: 80px;
  max-width: 120px;
}

.report-table-transposed td {
  padding: 0.5rem 0.75rem;
  font-size: 0.8125rem;
}

/* Column background colors - per client color scheme request */

/* Age & Contribution - Clean white */
.report-table-transposed tbody td:nth-child(2),
.report-table-transposed tbody td:nth-child(3) {
  background-color: var(--col-white);
}

/* Total Expenses - Light red (expense/cost) */
.report-table-transposed tbody td:nth-child(4) {
  background-color: var(--col-red-light);
}

/* Interest Earned - Lighter green */
.report-table-transposed tbody td:nth-child(5) {
  background-color: var(--col-green-light);
}

/* Account Value - Darker green (key metric) */
.report-table-transposed tbody td:nth-child(6) {
  background-color: var(--col-green-dark);
  font-weight: 600;
}

/* Tax Free Policy Loans - Blue (loans) */
.report-table-transposed tbody td:nth-child(7) {
  background-color: var(--col-blue-light);
}

/* Loan Interest - Light red (same as Total Expenses) */
.report-table-transposed tbody td:nth-child(8) {
  background-color: var(--col-red-light);
}

/* Loan Balance - Darker red */
.report-table-transposed tbody td:nth-child(9) {
  background-color: var(--col-red-dark);
  font-weight: 600;
}

/* Available Cash Value - Darker green (key metric) */
.report-table-transposed tbody td:nth-child(10) {
  background-color: var(--col-green-dark);
  font-weight: 600;
}

/* Life Insurance - Lighter green */
.report-table-transposed tbody td:nth-child(11) {
  background-color: var(--col-green-light);
}

/* Death Benefit - Darker green (key metric) */
.report-table-transposed tbody td:nth-child(12) {
  background-color: var(--col-green-dark);
  font-weight: 600;
}

/* Ensure all table text is dark for readability */
.report-table td,
.report-table th,
.report-table-transposed td,
.report-table-transposed th {
  color: hsl(var(--foreground)) !important;
}

/* Sticky header with shadow */
.report-table-transposed thead th {
  position: sticky;
  top: 0;
  background: linear-gradient(to bottom, #f1f5f9, #e2e8f0);
  z-index: 10;
  box-shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1);
}

/* Row hover effect */
.report-table-transposed tbody tr:hover td {
  filter: brightness(0.97);
}

/* ============================================
   Chart Controls - Modern UI
   ============================================ */

#chart-controls {
  font-size: 0.875rem;
}

#chart-controls label {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  padding: 0.375rem 0.75rem;
  border-radius: 0.5rem;
  cursor: pointer;
  transition: all 0.15s ease;
  background: hsl(var(--secondary));
  border: 1px solid transparent;
}

#chart-controls label:hover {
  background: hsl(210 40% 93%);
}

#chart-controls input[type="checkbox"] {
  width: 1rem;
  height: 1rem;
  border-radius: 0.25rem;
  border: 2px solid hsl(var(--border));
  cursor: pointer;
  accent-color: hsl(var(--primary));
}

#chart-controls input[type="checkbox"]:checked {
  background-color: hsl(var(--primary));
  border-color: hsl(var(--primary));
}

#chart-controls input[type="number"] {
  width: 4.5rem;
  padding: 0.375rem 0.5rem;
  font-size: 0.875rem;
  border: 1px solid hsl(var(--border));
  border-radius: 0.375rem;
  background: white;
  transition: all 0.15s ease;
}

#chart-controls input[type="number"]:focus {
  outline: none;
  border-color: hsl(var(--primary));
  box-shadow: 0 0 0 3px hsla(var(--primary), 0.1);
}

#chart-controls select {
  padding: 0.375rem 2rem 0.375rem 0.75rem;
  font-size: 0.875rem;
  border: 1px solid hsl(var(--border));
  border-radius: 0.375rem;
  background: white;
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3e%3c/svg%3e");
  background-position: right 0.5rem center;
  background-repeat: no-repeat;
  background-size: 1.25rem;
  transition: all 0.15s ease;
}

#chart-controls select:focus {
  outline: none;
  border-color: hsl(var(--primary));
  box-shadow: 0 0 0 3px hsla(var(--primary), 0.1);
}

#chart-controls button {
  padding: 0.5rem 1rem;
  font-size: 0.875rem;
  font-weight: 500;
  border-radius: 0.375rem;
  cursor: pointer;
  transition: all 0.15s ease;
  border: 1px solid hsl(var(--border));
  background: white;
  color: hsl(var(--foreground));
}

#chart-controls button:hover {
  background: hsl(var(--secondary));
}

.chart-controls-row {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.chart-controls-row > div {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

/* ============================================
   Formatted Value Spans in Table
   ============================================ */

.formatted-value {
  font-variant-numeric: tabular-nums;
}

/* ============================================
   Print Styles
   ============================================ */

@media print {
  /* Hide non-essential elements */
  header,
  #upload-section,
  #loan-section,
  #formula-section,
  footer,
  button,
  #chart-controls,
  #table-year-range-controls {
    display: none !important;
  }

  body {
    background: white !important;
    font-size: 10pt;
  }

  main {
    padding: 0 !important;
    max-width: 100% !important;
  }

  section {
    margin: 0 !important;
    padding: 0 !important;
    box-shadow: none !important;
    border: none !important;
  }

  .rounded-xl {
    border-radius: 0 !important;
  }

  /* Hide interactive table, show print snapshot */
  .report-table {
    display: none !important;
  }

  .report-print {
    display: table !important;
    width: 100%;
    table-layout: fixed;
    border-collapse: collapse;
    font-size: 7pt;
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
    color-adjust: exact !important;
  }

  .report-print th,
  .report-print td {
    border: 1px solid #ddd;
    padding: 2px 3px;
    word-wrap: break-word;
    overflow-wrap: break-word;
  }

  .report-print th {
    background: #f3f4f6 !important;
    font-weight: 600;
    font-size: 6pt;
  }

  /* Print table column colors - matching screen table */
  /* Age & Contribution - Clean white */
  .report-print tbody td:nth-child(2),
  .report-print tbody td:nth-child(3) {
    background-color: var(--col-white) !important;
  }

  /* Total Expenses - Light red */
  .report-print tbody td:nth-child(4) {
    background-color: var(--col-red-light) !important;
  }

  /* Interest Earned - Lighter green */
  .report-print tbody td:nth-child(5) {
    background-color: var(--col-green-light) !important;
  }

  /* Account Value - Darker green */
  .report-print tbody td:nth-child(6) {
    background-color: var(--col-green-dark) !important;
    font-weight: 600;
  }

  /* Tax Free Policy Loans - Blue */
  .report-print tbody td:nth-child(7) {
    background-color: var(--col-blue-light) !important;
  }

  /* Loan Interest - Light red */
  .report-print tbody td:nth-child(8) {
    background-color: var(--col-red-light) !important;
  }

  /* Loan Balance - Darker red */
  .report-print tbody td:nth-child(9) {
    background-color: var(--col-red-dark) !important;
    font-weight: 600;
  }

  /* Available Cash Value - Darker green */
  .report-print tbody td:nth-child(10) {
    background-color: var(--col-green-dark) !important;
    font-weight: 600;
  }

  /* Life Insurance - Lighter green */
  .report-print tbody td:nth-child(11) {
    background-color: var(--col-green-light) !important;
  }

  /* Death Benefit - Darker green */
  .report-print tbody td:nth-child(12) {
    background-color: var(--col-green-dark) !important;
    font-weight: 600;
  }

  .report-print tbody tr {
    page-break-inside: avoid;
  }

  /* Table inputs as plain text */
  .report-table input {
    border: none;
    background: transparent;
    padding: 0;
  }

  /* Chart section */
  #chart-section {
    page-break-before: always;
  }

  canvas {
    max-width: 100%;
    height: auto !important;
  }
}

/* Print snapshot hidden on screen */
.report-print {
  display: none;
}

/* ============================================
   Year Cards (Alternative View)
   ============================================ */

.year-cards {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}

.year-card {
  border: 1px solid hsl(var(--border));
  border-radius: 0.75rem;
  padding: 1rem;
  background: white;
  min-width: 240px;
  box-shadow: 0 1px 3px 0 rgb(0 0 0 / 0.05);
  transition: all 0.2s ease;
}

.year-card:hover {
  box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1);
  transform: translateY(-1px);
}

.year-card h4 {
  margin: 0 0 0.75rem 0;
  font-size: 0.875rem;
  font-weight: 600;
  color: hsl(var(--foreground));
  padding-bottom: 0.5rem;
  border-bottom: 1px solid hsl(var(--border));
}

.year-card .kv {
  display: flex;
  justify-content: space-between;
  padding: 0.25rem 0;
  font-size: 0.8125rem;
}

.year-card .kv:nth-child(even) {
  background: hsl(var(--secondary));
  margin: 0 -0.5rem;
  padding: 0.25rem 0.5rem;
  border-radius: 0.25rem;
}

/* ============================================
   Animations & Transitions
   ============================================ */

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

section:not([hidden]) {
  animation: fadeIn 0.3s ease-out;
}

/* ============================================
   Responsive Adjustments
   ============================================ */

@media (max-width: 768px) {
  .report-table-transposed th,
  .report-table-transposed td {
    font-size: 0.75rem;
    padding: 0.375rem 0.5rem;
    min-width: 60px;
  }

  #chart-controls label {
    padding: 0.25rem 0.5rem;
    font-size: 0.75rem;
  }
}

/* ============================================
   Custom Scrollbar
   ============================================ */

#table-container {
  scrollbar-width: thin;
  scrollbar-color: hsl(var(--border)) transparent;
}

#table-container::-webkit-scrollbar {
  height: 8px;
  width: 8px;
}

#table-container::-webkit-scrollbar-track {
  background: transparent;
  border-radius: 4px;
}

#table-container::-webkit-scrollbar-thumb {
  background: hsl(var(--border));
  border-radius: 4px;
}

#table-container::-webkit-scrollbar-thumb:hover {
  background: hsl(215.4 16.3% 70%);
}

/* ============================================
   Status Messages
   ============================================ */

#upload-status {
  font-size: 0.875rem;
}

#upload-status:empty {
  display: none;
}

/* Success state */
#upload-status[style*="color: green"],
#upload-status:not([style*="color: red"]):not(:empty) {
  color: hsl(142 76% 36%);
  background: hsl(142 76% 94%);
  padding: 0.5rem 0.75rem;
  border-radius: 0.375rem;
  border: 1px solid hsl(142 76% 85%);
}

/* Error state */
#upload-status[style*="color: red"] {
  color: hsl(0 84.2% 50%);
  background: hsl(0 84.2% 95%);
  padding: 0.5rem 0.75rem;
  border-radius: 0.375rem;
  border: 1px solid hsl(0 84.2% 85%);
}

/* ============================================
   Focus States (Accessibility)
   ============================================ */

button:focus-visible,
input:focus-visible,
select:focus-visible {
  outline: none;
  ring: 2px;
  ring-color: hsl(var(--primary));
  ring-offset: 2px;
}

/* ============================================
   Legacy Support - Ensure Compatibility
   ============================================ */

/* File upload button spacing */
.file-upload-btn {
  margin-left: 0;
}

/* Container fallback */
.container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 1.5rem;
}

/* Row flex utility */
.row {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  flex-wrap: wrap;
}

/* Muted text */
.muted {
  color: hsl(var(--muted-foreground));
}

/* ============================================
   Tooltip Styles
   ============================================ */

/* Base tooltip container */
[data-tooltip] {
  position: relative;
  cursor: help;
}

/* Tooltip pseudo-element */
[data-tooltip]::before {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  padding: 0.5rem 0.75rem;
  background-color: hsl(222.2 84% 4.9%);
  color: white;
  font-size: 0.75rem;
  font-weight: 500;
  line-height: 1.4;
  white-space: nowrap;
  border-radius: 0.375rem;
  box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s ease, visibility 0.2s ease;
  z-index: 1000;
  pointer-events: none;
}

/* Tooltip arrow */
[data-tooltip]::after {
  content: '';
  position: absolute;
  bottom: calc(100% + 2px);
  left: 50%;
  transform: translateX(-50%);
  border: 6px solid transparent;
  border-top-color: hsl(222.2 84% 4.9%);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s ease, visibility 0.2s ease;
  z-index: 1000;
  pointer-events: none;
}

/* Show tooltip on hover */
[data-tooltip]:hover::before,
[data-tooltip]:hover::after {
  opacity: 1;
  visibility: visible;
}

/* Tooltip position variants */

/* Bottom position */
[data-tooltip-position="bottom"]::before {
  bottom: auto;
  top: calc(100% + 8px);
}

[data-tooltip-position="bottom"]::after {
  bottom: auto;
  top: calc(100% + 2px);
  border-top-color: transparent;
  border-bottom-color: hsl(222.2 84% 4.9%);
}

/* Left position */
[data-tooltip-position="left"]::before {
  bottom: auto;
  top: 50%;
  left: auto;
  right: calc(100% + 8px);
  transform: translateY(-50%);
}

[data-tooltip-position="left"]::after {
  bottom: auto;
  top: 50%;
  left: auto;
  right: calc(100% + 2px);
  transform: translateY(-50%);
  border-top-color: transparent;
  border-left-color: hsl(222.2 84% 4.9%);
}

/* Right position */
[data-tooltip-position="right"]::before {
  bottom: auto;
  top: 50%;
  left: calc(100% + 8px);
  transform: translateY(-50%);
}

[data-tooltip-position="right"]::after {
  bottom: auto;
  top: 50%;
  left: calc(100% + 2px);
  transform: translateY(-50%);
  border-top-color: transparent;
  border-right-color: hsl(222.2 84% 4.9%);
}

/* Multiline tooltip (wrap text) */
[data-tooltip-multiline]::before {
  white-space: normal;
  width: max-content;
  max-width: 250px;
  text-align: center;
}

/* Tooltip on buttons - ensure proper cursor */
button[data-tooltip],
input[data-tooltip],
select[data-tooltip] {
  cursor: pointer;
}

/* Table header tooltips - prevent overflow issues */
th[data-tooltip] {
  overflow: visible;
}

/* Ensure tooltips are visible even in overflow containers */
.tooltip-container {
  position: relative;
  overflow: visible !important;
}

/* Hide tooltip in print */
@media print {
  [data-tooltip]::before,
  [data-tooltip]::after {
    display: none !important;
  }
}
