:root {
  /* Responsive sizing variables */
  --rod-height: clamp(140px, 28vh, 220px);
  --rod-width: clamp(32px, 8vw, 56px);
  --bead: clamp(16px, 3.8vw, 26px);
  /* Distance a bead travels to touch the center bar: half rod height minus bead height */
  --bead-shift: calc(var(--rod-height) / 2 - var(--bead));
  --center-bar: clamp(2px, 0.5vh, 4px);
  --label-gap: clamp(2.5rem, 6vh, 5.5rem);
  --abacus-padding: clamp(0.5rem, 2.5vw, 1rem);
  --container-padding: clamp(1rem, 4vw, 2rem);
  --rod-margin-x: clamp(0.25rem, 1.5vw, 1rem);
  /* Size of the triad separator dot on the center bar */
  --group-dot: clamp(6px, 1.5vw, 10px);
  /* Vertical offset for power labels (top and bottom) */
  --power-label-offset: clamp(4rem, 5vh, 5.5rem);
  /* Minimum height reserved for the math notation line (prevents layout shift) */
  --notation-minh: clamp(1.8rem, 3.5vh, 2.6rem);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', Arial, 'Noto Sans', 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', sans-serif;
  background-color: #1a202c;
  color: #e2e8f0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  margin: 0;
  padding: 1rem;
}

.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: #2d3748;
  border-radius: 1rem;
  padding: var(--container-padding);
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
  width: 100%;
  max-width: 600px;
  position: relative;
}

.abacus {
  display: flex;
  align-items: flex-end;
  /* Reserve vertical space above for top decimal labels so they don't overlap the description */
  margin-top: var(--power-label-offset);
  margin-bottom: 2rem;
  position: relative;
  background-color: #4a5568;
  /* Soroban-inspired heaven/earth subtle cue */
  background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0.04) 0 50%, rgba(0, 0, 0, 0.08) 50% 100%);
  border: 4px solid #718096;
  border-radius: 0.5rem;
  padding: var(--abacus-padding);
}

.center-bar {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: var(--center-bar);
  background-color: #e2e8f0;
  transform: translateY(-50%);
}

.rod {
  display: flex;
  flex-direction: column;
  align-items: center;
  height: var(--rod-height);
  width: var(--rod-width);
  position: relative;
  margin: 0 var(--rod-margin-x);
}

.rod-line {
  position: absolute;
  height: 100%;
  width: clamp(1px, 0.3vw, 2px);
  background-color: #a0aec0;
  left: 50%;
  transform: translateX(-50%);
}

.bead {
  width: 0;
  height: 0;
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  cursor: pointer;
  transition: transform 0.3s ease-in-out;
}

.bead.top {
  top: 0;
  border-left: var(--bead) solid transparent;
  border-right: var(--bead) solid transparent;
  border-top: var(--bead) solid #48bb78;
}

.bead.bottom {
  bottom: 0;
  border-left: var(--bead) solid transparent;
  border-right: var(--bead) solid transparent;
  border-bottom: var(--bead) solid #48bb78;
}

.bead.top.active {
  transform: translate(-50%, var(--bead-shift));
  border-top-color: #38a169;
}

.bead.bottom.active {
  transform: translate(-50%, calc(-1 * var(--bead-shift)));
  border-bottom-color: #c53030;
}

.power-label {
  position: absolute;
  bottom: calc(-1 * var(--power-label-offset));
  font-size: 0.875rem;
  color: #a0aec0;
  text-align: center;
  width: 100%;
}

.power-label-top {
  position: absolute;
  top: calc(-1 * var(--power-label-offset));
  font-size: 0.875rem;
  color: #a0aec0;
  text-align: center;
  width: 100%;
}

.abacus-value {
  font-size: 1.5rem;
  font-weight: bold;
  margin-top: 1rem;
  text-align: center;
}

/* Reserve space for MathJax notation to avoid flicker/reflow when it appears */
#math-notation {
  min-height: var(--notation-minh);
  line-height: 1.4;
}

.value-labels {
  position: absolute;
  right: -2rem;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: var(--label-gap);
}

.value-labels span {
  font-size: clamp(0.9rem, 2.8vw, 1.25rem);
  font-weight: bold;
  color: #e2e8f0;
}

/* Soroban-inspired grouping every 3 rods for readability:
   Replace vertical line with a centered black dot on the center bar */
#abacus-rods-container .rod:nth-child(3n+1) {
  /* keep a little spacing for readability */
  padding-left: clamp(0.25rem, 1.2vw, 0.5rem);
}

#abacus-rods-container .rod:nth-child(3n+1)::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  width: var(--group-dot);
  height: var(--group-dot);
  background: rgb(0, 92, 220);
  border-radius: 50%;
  transform: translate(-50%, -50%);
  z-index: 2;
  pointer-events: none;
}

.translation-button {
  position: fixed !important;
  top: calc(0.75rem + env(safe-area-inset-top, 0px)) !important;
  left: calc(0.75rem + env(safe-area-inset-left, 0px)) !important;
  background-color: #4a5568;
  color: #e2e8f0;
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 0.5rem;
  cursor: pointer;
  font-weight: bold;
  transition: background-color 0.2s;
  z-index: 2147483647;
  /* ensure always on top */
  pointer-events: auto;
}

.translation-button:hover {
  background-color: #6a768c;
}

.triquetra-logo {
  width: clamp(120px, 25vw, 200px);
  height: auto;
  margin-bottom: 5rem;
  max-width: 100%;
}

/* Reserve space on the left so the centered title doesn't sit under the EN button */
#main-title {
  padding-left: clamp(3rem, 12vw, 6rem);
}

/* Fixed GitHub link at top-right of the page */
.github-link {
  position: fixed !important;
  top: calc(0.75rem + env(safe-area-inset-top, 0px)) !important;
  right: calc(0.75rem + env(safe-area-inset-right, 0px)) !important;
  color: #e2e8f0;
  opacity: 0.9;
  z-index: 2147483647;
  /* ensure always on top */
  transition: color 0.2s ease, opacity 0.2s ease;
  pointer-events: auto;
}

.github-link:hover {
  color: #63b3ed;
  opacity: 1;
}

.github-link svg {
  width: clamp(22px, 3.5vw, 28px);
  height: clamp(22px, 3.5vw, 28px);
  display: block;
}

/* ====== Top controls + menu ====== */
:root {
  --topbar-bg: rgba(17, 24, 39, 0.7);
  /* cinza-900 com transparência */
  --topbar-text: #e5e7eb;
  /* cinza-200 */
  --topbar-border: rgba(255, 255, 255, 0.08);
  --topbar-height: 56px;
  /* altura aproximada da barra superior (botões 36px + padding 10px) */
  --menu-bg: #111827;
  /* cinza-900 */
  --menu-border: rgba(255, 255, 255, 0.12);
  --menu-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
  --menu-item: #e5e7eb;
  --menu-item-muted: #9ca3af;
  /* cinza-400 */
  --menu-item-hover: #34d399;
  /* teal-400 */
  --focus-ring: 0 0 0 3px rgba(52, 211, 153, 0.45);
}

body.dark {
  /* Mantém as cores similares, mas pode ser ajustado conforme seu tema */
  --topbar-bg: rgba(17, 24, 39, 0.75);
  --topbar-text: #e5e7eb;
  --topbar-border: rgba(255, 255, 255, 0.12);
  --menu-bg: #0b1220;
  --menu-border: rgba(255, 255, 255, 0.16);
  --menu-item: #e5e7eb;
  --menu-item-muted: #9ca3af;
  --menu-item-hover: #5eead4;
  /* teal-300 */
}

.top-controls {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  background: var(--topbar-bg);
  color: var(--topbar-text);
  backdrop-filter: saturate(120%) blur(6px);
  -webkit-backdrop-filter: saturate(120%) blur(6px);
  border-bottom: 1px solid var(--topbar-border);
  z-index: 2147483646;
  /* below legacy fixed buttons we will hide */
}

.language-switcher {
  display: flex;
  align-items: center;
}

.m3-selection-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--topbar-border);
  border-radius: 20px;
  /* M3 Chip style */
  padding: 0 12px;
  height: 36px;
  transition: background-color 0.2s, border-color 0.2s;
  cursor: pointer;
}

.m3-selection-wrapper:hover {
  background: rgba(255, 255, 255, 0.08);
  border-color: var(--menu-item-hover);
}

.m3-selection-wrapper:focus-within {
  box-shadow: var(--focus-ring);
  border-color: var(--menu-item-hover);
}

.m3-icon-start {
  margin-right: 6px;
  color: var(--menu-item-hover);
  opacity: 0.9;
}

.m3-icon-end {
  margin-left: 4px;
  opacity: 0.7;
  pointer-events: none;
}

.language-switcher select {
  appearance: none;
  background: transparent;
  color: var(--topbar-text);
  border: none;
  border-radius: 0;
  padding: 0;
  font-size: 13px;
  font-weight: 500;
  letter-spacing: 0.5px;
  cursor: pointer;
  outline: none;
  width: 30px;
  /* Fixed width for EN/PT */
  text-align: center;
}

/* Ensure options are readable in light mode */
.language-switcher select option {
  background: var(--menu-bg);
  color: var(--menu-item);
}

.right-controls {
  display: flex;
  align-items: center;
  gap: 10px;
}

.icon-button,
.github-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  border: 1px solid var(--topbar-border);
  background: transparent;
  color: var(--topbar-text);
  cursor: pointer;
  text-decoration: none;
}

.icon-button:hover,
.github-button:hover {
  background: rgba(255, 255, 255, 0.06);
}

.icon-button:focus-visible,
.github-button:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

.menu-wrapper {
  position: relative;
}

.menu-dropdown {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  min-width: 260px;
  padding: 8px;
  background: var(--menu-bg);
  border: 1px solid var(--menu-border);
  border-radius: 12px;
  box-shadow: var(--menu-shadow);
}

.menu-item {
  display: block;
  padding: 10px 12px;
  border-radius: 8px;
  color: var(--menu-item);
  text-decoration: none;
  font-size: 14px;
}

.menu-item:hover,
.menu-item:focus {
  background: rgba(255, 255, 255, 0.06);
  color: var(--menu-item-hover);
}

/* Utilitários de acessibilidade */
.visually-hidden,
.sr-only {
  position: absolute !important;
  height: 1px;
  width: 1px;
  overflow: hidden;
  clip: rect(1px, 1px, 1px, 1px);
  white-space: nowrap;
  clip-path: inset(50%);
}

/* Responsividade básica */
@media (max-width: 480px) {
  .menu-dropdown {
    min-width: 220px;
  }
}

/* Hide legacy floating controls to avoid overlap with new top bar */
.github-link,
.translation-button {
  display: none !important;
}

/* Reserve space for the fixed top bar so content (e.g., logo) isn't hidden underneath */
body {
  padding-top: calc(var(--topbar-height) + 8px + env(safe-area-inset-top, 0px));
}



/* ===== Light theme overrides ===== */
body.light {
  /* Page base colors */
  background-color: #f8fafc;
  /* slate-50 */
  color: #111827;
  /* gray-900 */
}

/* Light theme variables for top bar and menu */
body.light {
  --topbar-bg: rgba(255, 255, 255, 0.75);
  --topbar-text: #111827;
  /* gray-900 */
  --topbar-border: rgba(0, 0, 0, 0.12);
  --menu-bg: #ffffff;
  --menu-border: rgba(0, 0, 0, 0.12);
  --menu-item: #111827;
  --menu-item-muted: #6b7280;
  /* gray-500 */
  --menu-item-hover: #0f766e;
  /* teal-800 */
}

/* Component overrides in light mode */
body.light .container {
  background-color: #ffffff;
  color: #111827;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.08);
}

body.light .abacus {
  background-color: #e5e7eb;
  /* gray-200 */
  border-color: #94a3b8;
  /* slate-400 */
}

body.light .center-bar {
  background-color: #111827;
}

body.light .rod-line {
  background-color: #64748b;
}

body.light .power-label,
body.light .power-label-top {
  color: #475569;
}

body.light .value-labels span {
  color: #111827;
}

/* Common text blocks in light mode */
body.light #main-title,
body.light #total-title,
body.light #footer-sum-title {
  color: #0f766e;
}

body.light #description,
body.light #math-notation,
body.light .text-gray-400,
body.light .text-gray-300 {
  color: #475569 !important;
}

/* Buttons (clear/invert) retain contrast in light mode */
body.light #clear-button,
body.light #invert-button {
  background-color: #e5e7eb;
  /* gray-200 */
  color: #111827;
  border-color: #cbd5e1;
  /* slate-300 */
}

body.light #clear-button:hover,
body.light #invert-button:hover {
  background-color: #cbd5e1;
  /* slate-300 */
}

/* Collapsible sections backgrounds */
body.light #tutorial-content,
body.light #sum-content {
  background-color: #f1f5f9;
  /* slate-100 */
  color: #111827;
}

/* Override Tailwind utility classes used in HTML so they look good in light */
body.light .bg-gray-800 {
  background-color: #ffffff !important;
}

body.light .bg-gray-700 {
  background-color: #f1f5f9 !important;
}

body.light .text-gray-100 {
  color: #111827 !important;
}

body.light .text-white {
  color: #111827 !important;
}

body.light .text-gray-200 {
  color: #1f2937 !important;
}

/* Triquetra logo spacing unchanged; ensure it contrasts with light bg via surrounding colors */
body.light .triquetra-logo {
  filter: none;
}

/* Top controls buttons inherit text color; ensure hover backgrounds are subtle in light */
body.light .icon-button:hover,
body.light .github-button:hover {
  background: rgba(0, 0, 0, 0.06);
}