:root {
  --primary-color: #3b82f6;
  --primary-hover: #2563eb;
  --bg-color: #f8fafc;
  --surface-color: #ffffff;
  --border-color: #e2e8f0;
  --text-primary: #0f172a;
  --text-secondary: #64748b;
  --text-muted: #94a3b8;
  --success-color: #10b981;
  --warning-color: #f59e0b;
  --error-color: #ef4444;
  --shadow-sm: none;
  --shadow-md: none;
  --radius-sm: 2px;
  --radius-md: 3px;
  --radius-lg: 4px;
  --transition: 200ms ease;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html,
body {
  background-color: var(--bg-color);
}

body {
  font-family:
    -apple-system, BlinkMacSystemFont, "Segoe UI", "Inter", "Roboto", sans-serif;
  color: var(--text-primary);
  line-height: 1.5;
}

/* ========== Tool Header ========== */
.tool-header {
  position: sticky;
  top: 0;
  z-index: 50;
  background: var(--bg-color);
  border-bottom: 1px solid var(--border-color);
}

.tool-nav {
  width: 100%;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 16px;
}

.tool-nav__link {
  text-decoration: none;
  color: var(--text-primary);
  font-size: 0.9rem;
  font-weight: 600;
  letter-spacing: 0.01em;
}

.tool-nav__link:hover {
  color: var(--primary-color);
}

.tool-nav__link:focus-visible,
summary:focus-visible,
.help-icon:focus-visible,
input:focus-visible {
  outline: 3px solid rgba(37, 99, 235, 0.45);
  outline-offset: 2px;
}

/* ========== App Layout ========== */
.app-layout {
  display: grid;
  grid-template-columns: 320px 1fr;
  min-height: 100vh;
  min-width: 0;
}

/* ========== Sidebar ========== */
.sidebar {
  background: var(--surface-color);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow: hidden;
}

.sidebar-header {
  padding: 1.25rem 1rem;
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

.sidebar-header h1 {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 0.125rem;
}

.sidebar-header .subtitle {
  font-size: 0.75rem;
  color: var(--text-secondary);
}

.sidebar-content {
  padding: 1rem;
  flex: 1;
  overflow-y: auto;
}

/* ========== Hero Input (Monthly Minutes) ========== */
.input-section--hero {
  background: #f3f6f8;
  padding: 1rem;
  border-radius: var(--radius-md);
  margin-bottom: 1rem;
  border: 1px solid #dbeafe;
}

.input-label-hero {
  display: block;
  font-size: 0.6875rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
  margin-bottom: 0.375rem;
}

.input-hero {
  width: 100%;
  padding: 0.625rem 0.75rem;
  font-size: 1.125rem;
  font-weight: 600;
  background: var(--surface-color);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  transition:
    border-color var(--transition),
    box-shadow var(--transition);
}

.input-hero:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.time-period-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.5rem;
  font-size: 0.75rem;
  color: var(--text-secondary);
}

.time-period-row input[type="number"] {
  width: 60px;
  padding: 0.375rem 0.5rem;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  font-size: 0.8125rem;
  font-weight: 600;
  background: var(--surface-color);
  text-align: center;
}

.time-period-row input[type="number"]:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.1);
}

.input-hint {
  display: block;
  font-size: 0.6875rem;
  color: var(--text-muted);
  margin-top: 0.375rem;
}

/* ========== Accordion Sections ========== */
.input-accordion {
  margin-bottom: 0.5rem;
  border-radius: var(--radius-md);
  overflow: visible;
}

.accordion-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 0.875rem;
  background: var(--bg-color);
  border-radius: var(--radius-md);
  cursor: pointer;
  list-style: none;
  transition: background var(--transition);
  user-select: none;
}

.accordion-header::-webkit-details-marker {
  display: none;
}

.accordion-header:hover {
  background: #f1f5f9;
}

.accordion-title {
  font-weight: 500;
  font-size: 0.8125rem;
  color: var(--text-primary);
}

.accordion-summary {
  font-size: 0.6875rem;
  font-weight: 600;
  color: var(--primary-color);
  background: transparent;
  padding: 0;
  border-radius: 0;
}

.accordion-badge {
  font-size: 0.625rem;
  font-weight: 500;
  color: var(--text-muted);
  background: transparent;
  padding: 0;
  border-radius: 0;
  border: 0;
}

.input-accordion[open] .accordion-header {
  border-radius: var(--radius-md) var(--radius-md) 0 0;
}

.accordion-content {
  padding: 0.875rem;
  background: var(--bg-color);
  border-radius: 0 0 var(--radius-md) var(--radius-md);
}

.accordion-help {
  font-size: 0.6875rem;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
  line-height: 1.4;
}

/* ========== Mix Validation ========== */
.mix-validation {
  margin-top: 0.75rem;
  padding: 0.5rem 0.75rem;
  border-radius: var(--radius-sm);
  font-size: 0.75rem;
  display: none;
}

.mix-validation.visible {
  display: block;
}

.mix-validation.valid {
  background: rgba(16, 185, 129, 0.1);
  color: var(--success-color);
  border: 1px solid rgba(16, 185, 129, 0.2);
}

.mix-validation.warning {
  background: rgba(245, 158, 11, 0.1);
  color: #b45309;
  border: 1px solid rgba(245, 158, 11, 0.2);
}

.mix-validation.error {
  background: rgba(239, 68, 68, 0.1);
  color: var(--error-color);
  border: 1px solid rgba(239, 68, 68, 0.2);
}

/* ========== Input Rows ========== */
.input-row {
  margin-bottom: 0.75rem;
}

.input-row:last-child {
  margin-bottom: 0;
}

.input-row label {
  display: block;
  font-size: 0.75rem;
  font-weight: 500;
  margin-bottom: 0.375rem;
  color: var(--text-primary);
}

.input-label-with-help {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  margin-bottom: 0.375rem;
}

.input-label-with-help label {
  margin-bottom: 0;
}

.input-row input[type="number"] {
  width: 100%;
  padding: 0.5rem 0.625rem;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  font-size: 0.8125rem;
  background: var(--surface-color);
  transition:
    border-color var(--transition),
    box-shadow var(--transition);
}

.input-row input[type="number"]:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.input-row input[type="number"].input-empty {
  border-color: var(--warning-color);
  background: rgba(245, 158, 11, 0.05);
}

.input-row input[type="number"].input-empty:focus {
  box-shadow: 0 0 0 3px rgba(245, 158, 11, 0.1);
}

input.input-invalid,
input.input-invalid:focus {
  border-color: var(--error-color);
  background: rgba(239, 68, 68, 0.05);
  box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.1);
}

.input-error {
  display: none;
  margin-top: 0.25rem;
  color: #b91c1c;
  font-size: 0.6875rem;
}

.input-error.visible {
  display: block;
}

/* Help Icons */
.help-icon {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 14px;
  height: 14px;
  flex: 0 0 18px;
  margin: 0;
  padding: 0;
  font-size: 0.625rem;
  font-weight: 600;
  color: var(--text-muted);
  background: var(--surface-color);
  border: 1px solid var(--border-color);
  border-radius: 50%;
  cursor: help;
  vertical-align: middle;
  transition: all var(--transition);
}

.help-icon:hover {
  background: var(--primary-color);
  color: white;
  border-color: var(--primary-color);
}

.help-icon:focus-visible {
  background: var(--primary-color);
  color: white;
  border-color: var(--primary-color);
}

.help-icon::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  padding: 8px 12px;
  background: var(--surface-color);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  font-size: 0.75rem;
  font-weight: 400;
  color: var(--text-primary);
  white-space: normal;
  width: max-content;
  max-width: 200px;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.15s;
  pointer-events: none;
  z-index: 1000;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.help-icon:hover::after,
.help-icon:focus-visible::after {
  opacity: 1;
  visibility: visible;
}

/* ========== Slider Rows ========== */
.slider-row {
  display: block;
}

.slider-row label {
  margin-bottom: 0.25rem;
}

.slider-container {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

input[type="range"] {
  flex: 1;
  height: 4px;
  border-radius: 2px;
  background: var(--border-color);
  outline: none;
  -webkit-appearance: none;
  cursor: pointer;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--primary-color);
  cursor: pointer;
  transition:
    transform var(--transition),
    box-shadow var(--transition);
  box-shadow: var(--shadow-sm);
}

input[type="range"]::-webkit-slider-thumb:hover {
  transform: scale(1.1);
  box-shadow: 0 2px 8px rgba(59, 130, 246, 0.3);
}

input[type="range"]::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--primary-color);
  cursor: pointer;
  border: none;
}

.slider-value {
  min-width: 36px;
  text-align: right;
  font-weight: 600;
  color: var(--primary-color);
  font-size: 0.75rem;
}

/* ========== Main Content ========== */
.main-content {
  padding: 1.5rem;
  background: var(--bg-color);
  overflow-y: auto;
  min-height: 100vh;
  min-width: 0;
}

.main-content.results-invalid > :not(.executive-summary) {
  display: none;
}

.invalid-summary {
  color: var(--error-color);
  font-size: 0.875rem;
}

/* ========== Executive Summary ========== */
.executive-summary {
  background: #f4f8f5;
  border: 1px solid #bbf7d0;
  border-radius: var(--radius-lg);
  padding: 1.25rem;
  margin-bottom: 1rem;
}

.summary-recommendation {
  font-size: 1.125rem;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 1rem;
}

.summary-recommendation .highlight {
  color: var(--success-color);
}

.summary-recommendation .option-name {
  font-weight: 700;
}

.summary-costs {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
}

.summary-cost-item {
  background: var(--surface-color);
  padding: 0.875rem;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-color);
}

.summary-cost-item.cheapest {
  border-color: var(--success-color);
  background: rgba(16, 185, 129, 0.05);
}

.summary-cost-label {
  font-size: 0.6875rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
  margin-bottom: 0.25rem;
}

.summary-cost-value {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--text-primary);
}

.summary-cost-item.cheapest .summary-cost-value {
  color: var(--success-color);
}

.summary-cost-annual {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 0.125rem;
}

.summary-savings {
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid #bbf7d0;
  font-size: 0.875rem;
  color: var(--text-secondary);
}

.summary-savings strong {
  color: var(--success-color);
  font-weight: 600;
}

.summary-insight {
  margin-top: 0.75rem;
  padding: 0.75rem 1rem;
  background: var(--info-bg, #eff6ff);
  border-left: 3px solid var(--info-color, #3b82f6);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  font-size: 0.875rem;
  color: var(--text-secondary);
}

.summary-insight.warning {
  background: var(--warning-bg, #fef3c7);
  border-left-color: var(--warning-color, #f59e0b);
}

.summary-insight strong {
  color: var(--text-primary);
  font-weight: 600;
}

/* ========== Chart Section ========== */
.chart-section {
  background: var(--surface-color);
  border-radius: var(--radius-lg);
  padding: 1.25rem;
  box-shadow: var(--shadow-md);
  margin-bottom: 1rem;
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 1rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border-color);
  flex-wrap: wrap;
  gap: 1rem;
}

.chart-header h2 {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0;
}

/* ========== Chart Container ========== */
.chart-container {
  position: relative;
  height: 420px;
}

.chart-container canvas {
  max-height: 100%;
}

.chart-fallback {
  padding: 1rem;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ========== Detail Sections (Collapsible) ========== */
.detail-section {
  background: var(--surface-color);
  border-radius: var(--radius-lg);
  margin-bottom: 0.75rem;
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}

.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.875rem 1.25rem;
  cursor: pointer;
  list-style: none;
  transition: background var(--transition);
}

.detail-header::-webkit-details-marker {
  display: none;
}

.detail-header:hover {
  background: var(--bg-color);
}

.detail-header h3 {
  font-size: 0.875rem;
  font-weight: 600;
  margin: 0;
  color: var(--text-primary);
}

.detail-preview {
  font-size: 0.6875rem;
  font-weight: 500;
  color: var(--primary-color);
  background: transparent;
  padding: 0;
  border-radius: 0;
  transition: opacity var(--transition);
}

.detail-section[open] .detail-preview {
  opacity: 0;
}

.detail-content {
  padding: 0 1.25rem 1.25rem;
}

/* ========== Crossover Points ========== */
.crossover-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 0.75rem;
}

.crossover-item {
  background: var(--bg-color);
  padding: 0.875rem;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-color);
}

.crossover-label {
  font-weight: 600;
  font-size: 0.8125rem;
  color: var(--text-primary);
  margin-bottom: 0.375rem;
}

.crossover-value {
  font-size: 0.9375rem;
  margin-bottom: 0.375rem;
}

.crossover-cost {
  color: var(--text-secondary);
  font-size: 0.75rem;
}

.crossover-insight {
  font-size: 0.6875rem;
  color: var(--text-secondary);
  margin-bottom: 0.375rem;
  line-height: 1.5;
}

.crossover-insight .cheaper {
  font-weight: 600;
  color: var(--success-color);
}

.crossover-you {
  font-size: 0.75rem;
  padding: 0.375rem 0.5rem;
  background: rgba(59, 130, 246, 0.08);
  border-radius: var(--radius-sm);
  color: var(--primary-color);
}

.no-crossover {
  color: var(--text-secondary);
  font-style: italic;
  font-size: 0.8125rem;
}

/* ========== Cost Structure Section ========== */
.cost-structure-section {
  background: var(--surface-color);
  border-radius: var(--radius-lg);
  padding: 1rem 1.25rem;
  margin-bottom: 0.75rem;
  box-shadow: var(--shadow-sm);
}

.cost-structure-section h3 {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0 0 0.75rem;
}

.cost-structure-container {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
}

.cost-structure-item {
  display: flex;
  flex-direction: column;
  gap: 0.125rem;
  padding: 0.5rem 0.75rem;
  background: var(--bg-color);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-color);
  min-width: 180px;
  max-width: 100%;
}

.cost-structure-label {
  font-size: 0.6875rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.025em;
  color: var(--text-secondary);
}

.cost-structure-formula {
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--text-primary);
  font-family: "SF Mono", "Consolas", "Monaco", monospace;
  overflow-wrap: anywhere;
}

/* ========== Rate Comparison Section ========== */
.rate-comparison-section {
  background: var(--surface-color);
  border-radius: var(--radius-lg);
  padding: 1rem 1.25rem;
  margin-bottom: 0.75rem;
  box-shadow: var(--shadow-sm);
}

.rate-comparison-section h3 {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0 0 0.25rem;
}

.section-subtitle {
  font-size: 0.75rem;
  color: var(--text-secondary);
  margin: 0 0 0.75rem;
}

#rate-comparison-table .baseline-row {
  background: rgba(59, 130, 246, 0.05);
  border-top: 2px solid var(--border-color);
}

#rate-comparison-table .baseline-row td {
  padding-top: 0.75rem;
  padding-bottom: 0.75rem;
}

#rate-comparison-table .baseline {
  color: var(--text-muted);
  font-style: italic;
}

#rate-comparison-table .rate-note {
  font-size: 0.625rem;
  color: var(--text-muted);
  font-weight: 400;
}

#rate-comparison-table .no-data {
  color: var(--text-muted);
  font-style: italic;
  text-align: center;
  padding: 1rem;
}

/* ========== Crossover Formula ========== */
.crossover-formula {
  font-size: 0.6875rem;
  color: var(--text-secondary);
  background: rgba(59, 130, 246, 0.05);
  padding: 0.5rem;
  border-radius: var(--radius-sm);
  margin: 0.375rem 0;
  line-height: 1.6;
}

.crossover-formula .formula-label {
  font-weight: 600;
  color: var(--text-primary);
}

.crossover-formula .formula-calc {
  display: block;
  font-family: "SF Mono", "Consolas", "Monaco", monospace;
  color: var(--primary-color);
  margin-top: 0.25rem;
}

/* ========== Data Table ========== */
.table-wrapper {
  overflow-x: auto;
  max-width: 100%;
  overscroll-behavior-inline: contain;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.75rem;
}

thead th {
  background: var(--bg-color);
  padding: 0.625rem 0.75rem;
  text-align: left;
  font-weight: 600;
  font-size: 0.6875rem;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.025em;
  border-bottom: 1px solid var(--border-color);
}

tbody td,
tbody th {
  padding: 0.625rem 0.75rem;
  border-bottom: 1px solid var(--border-color);
  vertical-align: top;
}

tbody th {
  text-align: left;
  font-weight: 500;
}

tbody tr:hover {
  background: var(--bg-color);
}

tfoot td,
tfoot th {
  padding: 0.625rem 0.75rem;
  font-weight: 600;
  border-top: 2px solid var(--border-color);
  background: var(--bg-color);
  text-align: left;
}

.formula {
  color: var(--text-muted);
  font-size: 0.625rem;
  font-style: italic;
  margin-top: 0.125rem;
}

.cost-value {
  font-weight: 600;
  color: var(--text-primary);
}

.diff-positive {
  color: var(--error-color);
}

.diff-negative {
  color: var(--success-color);
}

/* Table Group Headers */
.group-header th {
  background: var(--bg-color);
  font-weight: 600;
  font-size: 0.6875rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border-color);
}

.group-header .group-note {
  font-weight: 400;
  text-transform: none;
  letter-spacing: normal;
  color: var(--text-muted);
  font-style: italic;
}

/* ========== Responsive ========== */
@media (max-width: 1024px) {
  .app-layout {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: relative;
    height: auto;
    max-height: none;
    border-right: none;
    border-bottom: 1px solid var(--border-color);
  }

  .sidebar-content {
    max-height: none;
    overflow-y: visible;
  }

  .main-content {
    min-height: auto;
  }

  .chart-container {
    height: 350px;
  }

  .chart-header {
    flex-direction: column;
    align-items: flex-start;
  }
}

@media (max-width: 640px) {
  .sidebar-header,
  .sidebar-content {
    padding-inline: 0.75rem;
  }

  .main-content {
    padding: 0.75rem;
  }

  .executive-summary,
  .chart-section,
  .cost-structure-section,
  .rate-comparison-section {
    padding: 0.875rem;
  }

  .summary-costs {
    grid-template-columns: minmax(0, 1fr);
  }

  .cost-structure-container,
  .cost-structure-item {
    width: 100%;
    min-width: 0;
  }

  .chart-container {
    height: 280px;
  }

  .crossover-container {
    grid-template-columns: 1fr;
  }

  table {
    font-size: 0.6875rem;
  }

  thead th,
  tbody td,
  tbody th,
  tfoot th,
  tfoot td {
    padding: 0.5rem;
  }

  .detail-header {
    align-items: flex-start;
    gap: 0.5rem;
  }

  .detail-preview {
    max-width: 55%;
    text-align: right;
    white-space: normal;
  }
}
