/* Design Tokens and Theme Variables */
:root {
  /* Warm Paper Palette (Recommended & Approved) */
  --bg: #faf8f5;
  --surface: #ffffff;
  --surface-raised: #f3f0eb;
  --border: #e4dfd6;
  --border-focus: #c2410c;
  --text: #1c1917;
  --text-muted: #6b7280;
  
  /* Primary Interactive Accents (Burnt Orange) */
  --accent: #c2410c;
  --accent-hover: #9a3412;
  --accent-subtle: #fff7ed;
  
  /* Semantic Validation States */
  --error: #d93025;
  --error-bg: #fce8e6;
  --success: #188038;
  --success-bg: #e6f4ea;
  
  /* Radius Scale */
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  
  /* Typography Scale */
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Monaco, Consolas, monospace;
  
  /* Text scale adjusted dynamically via JavaScript button click */
  --text-scale: 1;
}

/* Reset & Accessibility Defaults */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: calc(16px * var(--text-scale));
  scroll-behavior: smooth;
  font-family: var(--font-sans);
  background-color: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* Accessibility Skip Link */
.skip-link {
  position: absolute;
  top: -100px;
  left: 1rem;
  background-color: var(--accent);
  color: var(--surface);
  padding: 0.75rem 1.5rem;
  border-radius: 0 0 var(--radius-sm) var(--radius-sm);
  font-weight: 600;
  text-decoration: none;
  z-index: 1000;
  transition: top 0.2s ease;
}

.skip-link:focus {
  top: 0;
}

/* Header & Navigation Styles */
.app-header {
  background-color: var(--surface);
  border-bottom: 2px solid var(--border);
  padding: 1.5rem 1rem;
}

.header-container {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

@media (min-width: 768px) {
  .header-container {
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
  }
}

.header-text h1 {
  font-size: 1.8rem;
  font-weight: 800;
  letter-spacing: -0.025em;
  color: var(--text);
  margin-bottom: 0.25rem;
  display: inline-flex;
  align-items: center;
  gap: 0.6rem;
}

.header-logo {
  height: 2.2rem;
  width: 2.2rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}

.header-text .subtitle {
  font-size: 1.05rem;
  color: var(--text-muted);
}

/* Font Size Scaling Control widget */
.text-scaler {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  background-color: var(--surface-raised);
  padding: 0.5rem 0.75rem;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  align-self: flex-start;
}

@media (min-width: 768px) {
  .text-scaler {
    align-self: auto;
  }
}

.scaler-title {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text-muted);
}

.scaler-buttons {
  display: flex;
  gap: 0.25rem;
}

.scaler-buttons button {
  background-color: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.35rem 0.75rem;
  font-size: 1rem;
  font-weight: 700;
  border-radius: var(--radius-sm);
  cursor: pointer;
  min-height: 38px;
  min-width: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s ease;
}

.scaler-buttons button:hover {
  background-color: var(--surface-raised);
  border-color: var(--text-muted);
}

.scaler-buttons button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* Main Layout Grid */
.app-main {
  flex: 1;
  padding: 1.5rem 1rem;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
}

.main-container {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1.5rem;
  margin-bottom: 2rem;
}

@media (min-width: 900px) {
  .main-container {
    grid-template-columns: 1.1fr 0.9fr;
    align-items: start;
  }
}

/* Section Cards styling */
.section-card {
  background-color: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.5rem;
  box-shadow: 0 2px 4px rgba(28, 25, 23, 0.02);
}

@media (min-width: 768px) {
  .section-card {
    padding: 2rem;
  }
}

.section-title {
  font-size: 1.4rem;
  font-weight: 700;
  margin-bottom: 1.5rem;
  border-bottom: 2px solid var(--surface-raised);
  padding-bottom: 0.75rem;
}

/* Accessible Forms and Fields */
.form-group {
  margin-bottom: 1.5rem;
  display: flex;
  flex-direction: column;
}

.form-group:last-of-type {
  margin-bottom: 2rem;
}

label, .field-label {
  display: block;
  font-weight: 600;
  font-size: 1.1rem;
  margin-bottom: 0.4rem;
  color: var(--text);
}

.field-helper {
  font-size: 0.9rem;
  color: var(--text-muted);
  margin-top: 0.3rem;
}

.optional-tag {
  font-weight: normal;
  font-size: 0.85rem;
  color: var(--text-muted);
}

/* Form Input Elements */
.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  width: 100%;
}

input[type="number"],
input[type="month"] {
  width: 100%;
  font-size: 1.15rem;
  font-family: inherit;
  padding: 0.75rem 1rem;
  min-height: 56px; /* Senior-friendly tap height */
  border: 2px solid var(--border);
  border-radius: var(--radius-md);
  color: var(--text);
  background-color: var(--surface);
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

input[type="number"]:hover,
input[type="month"]:hover {
  border-color: var(--text-muted);
}

input[type="number"]:focus-visible,
input[type="month"]:focus-visible {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px var(--accent-subtle);
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* Prefixes & Suffixes inside inputs */
.currency-input input {
  padding-left: 2rem;
}

.percent-input input {
  padding-right: 2rem;
}

.input-prefix,
.input-suffix {
  position: absolute;
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--text-muted);
  user-select: none;
}

.input-prefix {
  left: 0.85rem;
}

.input-suffix {
  right: 0.85rem;
}

/* Radio Group (Years/Months Toggle) */
.radio-group {
  display: flex;
  gap: 1.5rem;
  margin-bottom: 0.75rem;
}

.radio-label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
  font-size: 1.1rem;
  font-weight: 500;
  padding: 0.5rem 0;
}

.radio-label input[type="radio"] {
  appearance: none;
  background-color: var(--surface);
  margin: 0;
  font: inherit;
  color: var(--accent);
  width: 1.5em;
  height: 1.5em;
  border: 2px solid var(--border);
  border-radius: 50%;
  display: grid;
  place-content: center;
  transition: border-color 0.15s ease;
  cursor: pointer;
}

.radio-label input[type="radio"]::before {
  content: "";
  width: 0.75em;
  height: 0.75em;
  border-radius: 50%;
  transform: scale(0);
  transition: 120ms transform ease-in-out;
  background-color: var(--accent);
}

.radio-label input[type="radio"]:checked {
  border-color: var(--accent);
}

.radio-label input[type="radio"]:checked::before {
  transform: scale(1);
}

.radio-label input[type="radio"]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Validation Feedback via :has() selector */
.error-msg {
  display: none;
  font-size: 0.95rem;
  color: var(--error);
  font-weight: 600;
  margin-top: 0.4rem;
}

/* Match validation state of input elements and update borders/colors */
.form-group:has(input:user-invalid) input {
  border-color: var(--error);
  background-color: var(--error-bg);
}

.form-group:has(input:user-invalid) .error-msg {
  display: block;
}

.form-group:has(input:user-invalid) .field-helper {
  display: none; /* Hide standard helper when error is showing */
}

.form-group:has(input:user-valid) input {
  border-color: var(--success);
}

/* Buttons and Actions styling */
.form-actions {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-top: 1rem;
}

@media (min-width: 480px) {
  .form-actions {
    flex-direction: row;
  }
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  font-weight: 700;
  padding: 0.9rem 1.5rem;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
  min-height: 56px;
  text-decoration: none;
  border: none;
  flex: 1;
}

.btn:active {
  transform: translateY(1px);
}

.btn-primary {
  background-color: var(--accent);
  color: var(--surface);
}

.btn-primary:hover {
  background-color: var(--accent-hover);
}

.btn-primary:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.btn-secondary {
  background-color: var(--surface-raised);
  color: var(--text);
  border: 2px solid var(--border);
}

.btn-secondary:hover {
  background-color: var(--border);
}

.btn-secondary:focus-visible {
  outline: 3px solid var(--text);
  outline-offset: 2px;
}

/* Results Section styling */
.results-card {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.results-placeholder {
  text-align: center;
  padding: 3rem 1rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  flex: 1;
}

.placeholder-icon {
  font-size: 3.5rem;
}

.results-placeholder p {
  color: var(--text-muted);
  font-size: 1.1rem;
  max-width: 280px;
  line-height: 1.6;
}

/* Dynamic display block for results */
.results-content {
  display: flex;
  flex-direction: column;
  gap: 1.75rem;
  animation: fadeIn 0.3s ease-out;
}

.emi-large-display {
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: var(--accent-subtle);
  border: 2px solid var(--border);
  padding: 1.75rem;
  border-radius: var(--radius-md);
  text-align: center;
}

.emi-label {
  font-size: 1.05rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--accent-hover);
  margin-bottom: 0.5rem;
}

.emi-value {
  font-size: 2.5rem;
  font-weight: 800;
  color: var(--accent);
  word-break: break-all;
}

.summary-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}

@media (min-width: 480px) {
  .summary-grid {
    grid-template-columns: 1fr 1fr;
  }
  .total-payable {
    grid-column: span 2;
  }
}

.summary-item {
  background-color: var(--surface-raised);
  border: 1px solid var(--border);
  padding: 1rem 1.25rem;
  border-radius: var(--radius-md);
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.summary-label {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text-muted);
}

.summary-val {
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--text);
}

.highlight-interest {
  color: var(--border-focus);
}

.total-payable {
  background-color: var(--surface);
  border: 2px solid var(--text);
}

.total-payable .summary-val {
  font-size: 1.6rem;
  font-weight: 800;
}

/* Amortization Breakdown Table styling */
.breakdown-section {
  width: 100%;
}

.breakdown-card {
  background-color: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.5rem;
  margin-top: 1rem;
}

@media (min-width: 768px) {
  .breakdown-card {
    padding: 2rem;
  }
}

.breakdown-header {
  margin-bottom: 1.5rem;
}

.breakdown-header h2 {
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 0.25rem;
}

.breakdown-header .subtitle {
  font-size: 1rem;
  color: var(--text-muted);
}

.table-container {
  overflow-x: auto;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background-color: var(--surface);
}

.table-placeholder {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--text-muted);
  font-size: 1.1rem;
}

/* Amortization Table Rules */
table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
  font-size: 1.05rem;
}

th, td {
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}

th {
  background-color: var(--surface-raised);
  font-weight: 700;
  color: var(--text);
}

tr:last-child td {
  border-bottom: none;
}

tr:nth-child(even) {
  background-color: var(--bg);
}

/* Right-align values for structured reading */
.num-col {
  text-align: right;
  font-family: var(--font-mono);
}

th.num-col {
  text-align: right;
}

/* Mobile responsive card-view table fallback */
@media (max-width: 767px) {
  .table-container {
    border: none;
  }
  
  table, thead, tbody, th, td, tr {
    display: block;
  }
  
  thead {
    position: absolute;
    top: -9999px;
    left: -9999px;
  }
  
  tr {
    border: 2px solid var(--border);
    border-radius: var(--radius-md);
    background-color: var(--surface);
    margin-bottom: 1rem;
    padding: 0.5rem 1rem;
  }
  
  tr:nth-child(even) {
    background-color: var(--surface);
  }
  
  td {
    border: none;
    border-bottom: 1px solid var(--surface-raised);
    position: relative;
    padding-left: 50%;
    text-align: right;
    padding-top: 0.75rem;
    padding-bottom: 0.75rem;
    font-size: 1.1rem;
  }
  
  td:last-child {
    border-bottom: none;
  }
  
  td::before {
    content: attr(data-label);
    position: absolute;
    left: 0;
    width: 45%;
    text-align: left;
    font-weight: 700;
    color: var(--text-muted);
  }
  
  .num-col {
    text-align: right;
  }
}

/* Utility / State Helper Classes */
.hidden {
  display: none !important;
}

/* Fade-in Animation */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(5px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Footer Styling */
.app-footer {
  text-align: center;
  padding: 2rem 1rem;
  font-size: 0.95rem;
  color: var(--text-muted);
  border-top: 2px solid var(--border);
  background-color: var(--surface);
  margin-top: auto;
}

/* Export Button and Title Alignment */
.breakdown-title-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  margin-bottom: 0.5rem;
}

.btn-export {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.95rem;
  font-weight: 700;
  padding: 0.55rem 1rem;
  min-height: 44px;
  background-color: var(--surface);
  border: 2px solid var(--border);
  color: var(--accent);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all 0.15s ease;
}

.btn-export:hover {
  background-color: var(--accent-subtle);
  border-color: var(--accent);
}

.btn-export:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn-export:active {
  transform: translateY(1px);
}

@media (max-width: 480px) {
  .breakdown-title-container {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.75rem;
  }
  .btn-export {
    width: 100%;
    justify-content: center;
  }
}

/* Indian Terms Number Word Display styling */
.amount-words-display {
  display: block;
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--accent);
  margin-top: 0.35rem;
  min-height: 1.5rem; /* Prevents layout shifting when typing */
  line-height: 1.4;
  word-break: break-word;
}
