/* ==========================================================================
   MyRinggitCalc — Malaysia 2026 Smart Finance Calculator Suite
   Modern CSS Architecture (Light/Dark auto-theme, Accessible Tabs,
   Container-Query Responsive Tables, Brand-Consistent Forms, :user-invalid)
   ========================================================================== */

:root {
  /* Raw Light/Dark Tokens */
  --bg-light: #f6f8fb;
  --bg-dark: #0b1120;
  --surface-light: #ffffff;
  --surface-dark: #111b2e;
  --surface-alt-light: #eef2f7;
  --surface-alt-dark: #18253d;
  --border-light: #d7e0ec;
  --border-dark: #263652;
  --text-light: #0f172a;
  --text-dark: #e2e8f0;
  --muted-light: #475569;
  --muted-dark: #94a3b8;

  /* Brand & Semantic Accents (>= 3:1 contrast in both schemes) */
  --brand-light: #047857;
  --brand-dark: #34d399;
  --brand-text-light: #ffffff;
  --brand-text-dark: #052e16;
  --info-light: #1d4ed8;
  --info-dark: #60a5fa;
  --warn-light: #b45309;
  --warn-dark: #fbbf24;
  --err-light: #c62828;
  --err-dark: #f87171;
  --err-bg-light: #fdecea;
  --err-bg-dark: #3b1318;

  /* Default Light Assignments (Fallback for browsers without light-dark()) */
  --bg: var(--bg-light);
  --surface: var(--surface-light);
  --surface-alt: var(--surface-alt-light);
  --border: var(--border-light);
  --text: var(--text-light);
  --muted: var(--muted-light);
  --brand: var(--brand-light);
  --brand-text: var(--brand-text-light);
  --info: var(--info-light);
  --warn: var(--warn-light);
  --err: var(--err-light);
  --err-bg: var(--err-bg-light);

  @media (prefers-color-scheme: dark) {
    --bg: var(--bg-dark);
    --surface: var(--surface-dark);
    --surface-alt: var(--surface-alt-dark);
    --border: var(--border-dark);
    --text: var(--text-dark);
    --muted: var(--muted-dark);
    --brand: var(--brand-dark);
    --brand-text: var(--brand-text-dark);
    --info: var(--info-dark);
    --warn: var(--warn-dark);
    --err: var(--err-dark);
    --err-bg: var(--err-bg-dark);
  }

  @supports (color: light-dark(white, black)) {
    --bg: light-dark(var(--bg-light), var(--bg-dark));
    --surface: light-dark(var(--surface-light), var(--surface-dark));
    --surface-alt: light-dark(var(--surface-alt-light), var(--surface-alt-dark));
    --border: light-dark(var(--border-light), var(--border-dark));
    --text: light-dark(var(--text-light), var(--text-dark));
    --muted: light-dark(var(--muted-light), var(--muted-dark));
    --brand: light-dark(var(--brand-light), var(--brand-dark));
    --brand-text: light-dark(var(--brand-text-light), var(--brand-text-dark));
    --info: light-dark(var(--info-light), var(--info-dark));
    --warn: light-dark(var(--warn-light), var(--warn-dark));
    --err: light-dark(var(--err-light), var(--err-dark));
    --err-bg: light-dark(var(--err-bg-light), var(--err-bg-dark));
  }

  color-scheme: light dark;
  accent-color: var(--brand);
}

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

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background-color: var(--bg);
  color: var(--text);
  line-height: 1.55;
}

.visually-hidden:where(:not(:focus-within, :active)) {
  position: absolute !important;
  clip-path: inset(50%) !important;
  overflow: hidden !important;
  width: 1px !important;
  height: 1px !important;
  margin: -1px !important;
  padding: 0 !important;
  border: 0 !important;
  white-space: nowrap !important;
}

.container {
  max-inline-size: 1120px;
  margin-inline: auto;
  padding: 1.25rem 1rem 3rem;
}

/* Header & Responsive Navbar (modern-web-guidance: navbar) */
.site-header {
  border-block-end: 1px solid var(--border);
  margin-block-end: 1.25rem;
  padding-block-end: 1rem;
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.header-inner > * {
  flex-shrink: 0;
  white-space: nowrap;
}

.site-header.is-measuring,
.site-header.is-measuring * {
  transition: none !important;
}

.site-logo {
  text-decoration: none;
  color: var(--text);
}

.brand-title {
  margin: 0;
  font-size: clamp(1.3rem, 2.3vw, 1.75rem);
  font-weight: 800;
  letter-spacing: -0.02em;
}

.brand-accent {
  color: var(--brand);
}

.brand-sub {
  margin: 0.2rem 0 0;
  font-size: 0.82rem;
  color: var(--muted);
  white-space: normal;
}

.site-nav {
  display: flex;
  align-items: center;
}

.menu-button {
  anchor-name: --menu-button;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem 0.85rem;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  cursor: pointer;
}

.menu-button:focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 2px;
}

.site-menu {
  list-style: none;
  margin: 0;
  padding: 0.5rem;
  position: fixed;
  inset: auto;
  /* Fallback offset before anchor() for browsers without Anchor Positioning */
  inset-block-start: 4.5rem;
  inset-block-start: anchor(--menu-button bottom);
  inset-inline-end: 1rem;
  inline-size: min(82dvw, 22rem);
  max-inline-size: calc(100dvw - 2rem);
  block-size: fit-content;
  max-block-size: calc(100dvh - 6rem);
  margin-block-start: 0.4rem;
  overflow: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.65rem;
  box-shadow: 0 12px 28px rgb(0 0 0 / 0.18);
}

.site-menu:not(:is(:popover-open, .\:popover-open)) {
  display: none;
}

.site-menu:is(:popover-open, .\:popover-open) {
  display: grid;
  gap: 0.2rem;
}

.site-menu a {
  display: block;
  position: relative;
  padding: 0.5rem 0.7rem;
  font-size: 0.83rem;
  font-weight: 600;
  color: var(--muted);
  text-decoration: none;
  border-radius: 0.4rem;
}

.site-menu a:hover {
  color: var(--text);
  background: var(--surface-alt);
}

.site-menu a:focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: -2px;
}

/* Current page indicator uses shape + high-contrast color (WCAG 1.4.1) */
.site-menu a[aria-current="page"] {
  color: var(--brand);
  background: var(--surface-alt);
}

.site-menu a[aria-current="page"]::after {
  content: "";
  position: absolute;
  inset: auto 0.5rem 0.15rem;
  border-block-end: 2px solid var(--brand);
}

.site-header.is-wide .menu-button {
  display: none;
}

.site-header.is-wide .site-menu {
  position: static;
  display: flex;
  align-items: center;
  gap: 0.2rem;
  inline-size: auto;
  max-inline-size: none;
  block-size: auto;
  max-block-size: none;
  overflow: visible;
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
  box-shadow: none;
}

.site-header.is-wide .site-menu::backdrop {
  display: none;
}

.verified-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.76rem;
  font-weight: 600;
  padding: 0.35rem 0.7rem;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--brand);
}

/* SEO Guide & Policy Article Section */
.seo-guide {
  margin-block-start: 1.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.85rem;
  padding: 1.5rem;
}

.seo-guide h2 {
  margin: 0 0 0.75rem;
  font-size: 1.25rem;
}

.seo-guide h3 {
  margin: 1.25rem 0 0.45rem;
  font-size: 1.02rem;
}

.seo-guide p,
.seo-guide li {
  font-size: 0.9rem;
  color: var(--muted);
}

.seo-links-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(290px, 1fr));
  gap: 0.9rem;
  margin-block-start: 0.85rem;
}

.seo-card-link {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 0.45rem;
  padding: 1rem 1.1rem;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 0.7rem;
  color: var(--text);
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 700;
  transition: border-color 0.18s ease, transform 0.18s ease, box-shadow 0.18s ease;
}

.seo-card-link:hover {
  border-color: var(--brand);
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgb(0 0 0 / 0.14);
}

.seo-card-link span {
  display: block;
  font-size: 0.79rem;
  font-weight: 400;
  color: var(--muted);
  line-height: 1.45;
}

.seo-card-link .card-cta {
  font-size: 0.76rem;
  font-weight: 700;
  color: var(--brand);
  margin-block-start: 0.25rem;
}

.pill-tag {
  display: inline-flex;
  align-items: center;
  padding: 0.12rem 0.45rem;
  border-radius: 0.35rem;
  font-size: 0.7rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  background: var(--surface);
  border: 1px solid var(--brand);
  color: var(--brand);
  vertical-align: middle;
  margin-inline-end: 0.3rem;
}

.lang-switch {
  display: inline-flex;
  align-items: center;
  gap: 0.2rem;
  padding: 0.2rem;
  border-radius: 999px;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  font-size: 0.74rem;
  font-weight: 700;
}

.lang-switch a {
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  text-decoration: none;
  color: var(--muted);
}

.lang-switch a.active {
  background: var(--brand);
  color: var(--brand-text);
}

/* AdSense Ready Placeholder (Remove dashed border once AdSense live) */
.ad-slot {
  background: var(--surface-alt);
  border: 1px dashed var(--border);
  border-radius: 0.5rem;
  padding: 0.9rem;
  text-align: center;
  font-size: 0.75rem;
  color: var(--muted);
  margin-block: 1rem;
}

/* ==========================================================================
   Accessible Single-Row Scrollable Tabs (modern-web-guidance: tabs)
   ========================================================================== */
@property --fade-start {
  syntax: "<length>";
  inherits: false;
  initial-value: 0px;
}
@property --fade-end {
  syntax: "<length>";
  inherits: false;
  initial-value: 0px;
}

.tablist-wrap {
  position: relative;
  display: flex;
  align-items: center;
  border-block-end: 1px solid var(--border);
  margin-block-end: 1.25rem;
}

.tablist-scroller {
  flex: 1;
  min-inline-size: 0;
  display: flex;
  overflow-x: auto;
  gap: 0.15rem;
  scroll-snap-type: inline proximity;
  scroll-padding-inline: 2.5rem;
  scroll-behavior: smooth;
  scrollbar-width: none;

  --fade-size: 2.2rem;
  mask:
    linear-gradient(black, black) no-clip subtract,
    linear-gradient(to right, black, transparent) 0 0 / var(--fade-start) 100% no-repeat no-clip,
    linear-gradient(to right, transparent, black) 100% 0 / var(--fade-end) 100% no-repeat no-clip;
  animation: reveal-fade-start linear both, hide-fade-end linear both;
  animation-timeline: scroll(self inline);
  animation-range: 0 var(--fade-size), calc(100% - var(--fade-size)) 100%;
}
.tablist-scroller::-webkit-scrollbar {
  display: none;
}

.tab-scroll-btn {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  inline-size: 2rem;
  block-size: 2rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--brand);
  font-size: 1.15rem;
  font-weight: 800;
  line-height: 1;
  cursor: pointer;
  margin-inline: 0.25rem;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.tab-scroll-btn:hover {
  background: var(--surface-alt);
  border-color: var(--brand);
}

.tab-scroll-btn[hidden] {
  display: none;
}

@keyframes reveal-fade-start {
  to { --fade-start: var(--fade-size); }
}
@keyframes hide-fade-end {
  from { --fade-end: var(--fade-size); }
}

.tab-btn {
  flex: none;
  scroll-snap-align: start;
  position: relative;
  background: transparent;
  border: 0;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 600;
  padding: 0.7rem 0.75rem;
  cursor: pointer;
  white-space: nowrap;
  border-radius: 0.4rem 0.4rem 0 0;
}

.tab-btn:hover {
  color: var(--text);
  background: var(--surface-alt);
}

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

/* Selected indicator drawn via border on ::after (preserved in Forced Colors Mode) */
.tab-btn[aria-selected="true"] {
  color: var(--brand);
}

.tab-btn[aria-selected="true"]::after {
  content: "";
  position: absolute;
  inset: auto 0 0;
  border-block-end: 3px solid var(--brand);
}

/* Place all panels in the same grid cell so hidden="until-found" panels never add gap */
.panels-stack {
  display: grid;
}

/* MANDATORY: Zero padding/margin/border/background on [role="tabpanel"] itself */
.panels-stack > [role="tabpanel"] {
  grid-area: 1 / 1;
  position: relative;
  z-index: 1;
}

/* Prevent hidden="until-found" panels in the same grid cell from blocking clicks */
.panels-stack > [role="tabpanel"][hidden] {
  pointer-events: none;
  z-index: 0;
}

.panel-surface {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.85rem;
  padding: 1.5rem;
}

.panel-header {
  margin-block-end: 1.25rem;
}

.panel-header h2 {
  margin: 0 0 0.25rem;
  font-size: 1.25rem;
}

.panel-header p {
  margin: 0;
  font-size: 0.875rem;
  color: var(--muted);
}

/* Two-column Calculator Grid */
.calc-grid {
  display: grid;
  grid-template-columns: minmax(280px, 1fr) minmax(300px, 1.15fr);
  gap: 1.5rem;
  align-items: start;
}

@media (max-width: 780px) {
  .calc-grid {
    grid-template-columns: 1fr;
  }
}

/* ==========================================================================
   Accessible Forms & :user-invalid Validation (modern-web-guidance)
   ========================================================================== */
.calc-form {
  display: grid;
  gap: 1rem;
}

.field {
  display: flex;
  flex-direction: column;
}

.field label {
  font-size: 0.875rem;
  font-weight: 600;
  margin-block-end: 0.2rem;
}

.hint {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
  margin-block-end: 0.35rem;
}

.field input[type="number"],
.field select {
  inline-size: 100%;
  padding: 0.6rem 0.75rem;
  font-size: 0.95rem;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 0.5rem;
}

.field input:focus-visible,
.field select:focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 1px;
}

.error-msg {
  display: none;
  color: var(--err);
  font-size: 0.8rem;
  margin-block-start: 0.3rem;
  font-weight: 500;
}

/* :is() keeps fallback working even if browser lacks :user-invalid */
:is(input:user-invalid, input.user-invalid-fallback) {
  border-color: var(--err);
  background-color: var(--err-bg);
}

:is(input:user-invalid, input.user-invalid-fallback) + .error-msg {
  display: block;
}

:is(input:user-valid, input.user-valid-fallback) {
  border-color: var(--brand);
}

.checkbox-label {
  display: inline-flex;
  align-items: center;
  gap: 0.55rem;
  font-size: 0.88rem;
  font-weight: 500;
  cursor: pointer;
}

/* Progressive fallback if accent-color is unsupported */
@supports not (accent-color: #047857) {
  .checkbox-label {
    position: relative;
    padding-inline-start: 1.75rem;
  }
  .checkbox-label input[type="checkbox"] {
    position: absolute !important;
    clip-path: inset(50%) !important;
    overflow: hidden !important;
    width: 1px !important;
    height: 1px !important;
  }
  .checkbox-label input[type="checkbox"] + span::before {
    content: "";
    position: absolute;
    inset-inline-start: 0;
    inset-block-start: 50%;
    transform: translateY(-50%);
    width: 1.15rem;
    height: 1.15rem;
    border: 2px solid var(--border);
    border-radius: 4px;
    background: var(--surface);
  }
  .checkbox-label input[type="checkbox"]:checked + span::before {
    background: var(--brand);
    border-color: var(--brand);
  }
  .checkbox-label input[type="checkbox"]:checked + span::after {
    content: "✓";
    position: absolute;
    inset-inline-start: 0.22rem;
    inset-block-start: 50%;
    transform: translateY(-50%);
    color: var(--brand-text);
    font-size: 0.85rem;
    font-weight: 700;
  }
}

.field-row-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
}

/* Results Summary Cards */
.results-box {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.hero-metric {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-inline-start: 4px solid var(--brand);
  border-radius: 0.65rem;
  padding: 1rem 1.15rem;
}

.hero-metric .metric-label {
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.hero-metric .metric-value {
  font-size: clamp(1.6rem, 3vw, 2.15rem);
  font-weight: 800;
  color: var(--brand);
  font-variant-numeric: tabular-nums;
  margin-block: 0.15rem;
}

.hero-metric .metric-sub {
  font-size: 0.82rem;
  color: var(--muted);
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(135px, 1fr));
  gap: 0.65rem;
}

.kpi-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 0.55rem;
  padding: 0.7rem 0.8rem;
}

.kpi-card .kpi-label {
  display: block;
  font-size: 0.74rem;
  color: var(--muted);
}

.kpi-card .kpi-val {
  display: block;
  font-size: 1.02rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  margin-block-start: 0.15rem;
}

.callout-info {
  font-size: 0.82rem;
  padding: 0.75rem 0.9rem;
  border-radius: 0.5rem;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  color: var(--text);
}

/* ==========================================================================
   Responsive Tables (modern-web-guidance: responsive-table)
   ========================================================================== */
.table-wrapper {
  overflow: auto;
  max-inline-size: 100%;
  max-block-size: min(360px, 65vh);
  container-type: inline-size;
  border: 1px solid var(--border);
  border-radius: 0.6rem;
}

.table-wrapper:focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 2px;
}

.data-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
}

.data-table caption {
  text-align: start;
  font-weight: 700;
  font-size: 0.82rem;
  padding: 0.6rem 0.75rem;
  color: var(--muted);
  background: var(--surface-alt);
  border-block-end: 1px solid var(--border);
}

.data-table thead {
  position: sticky;
  inset-block-start: 0;
  z-index: 1;
}

.data-table thead th {
  background: var(--surface-alt);
  color: var(--muted);
  font-weight: 700;
  text-align: start;
  padding: 0.55rem 0.75rem;
  border-block-end: 1px solid var(--border);
  white-space: nowrap;
}

.data-table tbody th,
.data-table tbody td {
  padding: 0.55rem 0.75rem;
  border-block-end: 1px solid var(--border);
  text-align: start;
}

.data-table tbody th {
  position: sticky;
  inset-inline-start: 0;
  background: var(--surface);
  font-weight: 600;
}

.data-table tbody tr:last-child th,
.data-table tbody tr:last-child td {
  border-block-end: 0;
}

@container (width < 460px) {
  .data-table,
  .data-table thead,
  .data-table tbody,
  .data-table tr,
  .data-table th,
  .data-table td {
    display: block;
  }

  .data-table thead {
    display: none;
  }

  .data-table tbody tr {
    margin: 0.6rem;
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    overflow: hidden;
  }

  .data-table tbody th {
    position: sticky;
    inset-block-start: 0;
    z-index: 2;
    background: var(--surface-alt);
  }

  .data-table td::before {
    content: attr(data-label) ": ";
    font-weight: 600;
    color: var(--muted);
  }
}

/* Official Sources & FAQ Section (SEO & AdSense E-E-A-T Trust) */
.trust-section {
  margin-block-start: 2rem;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(290px, 1fr));
  gap: 1.25rem;
}

.trust-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  padding: 1.25rem;
  font-size: 0.87rem;
}

.trust-card h3 {
  margin: 0 0 0.65rem;
  font-size: 1rem;
}

.trust-card ul {
  margin: 0;
  padding-inline-start: 1.15rem;
  color: var(--muted);
}

.trust-card li {
  margin-block-end: 0.4rem;
}

.site-footer {
  margin-block-start: 2.5rem;
  padding-block-start: 1.25rem;
  border-block-start: 1px solid var(--border);
  font-size: 0.8rem;
  color: var(--muted);
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 0.75rem;
}
