/*
 * Responsive navigation correction
 * Keeps the desktop menu on one line and switches to the hamburger
 * before the navigation can overlap the logo or wrap.
 */

.top-nav {
  box-sizing: border-box;
  width: 100%;
  max-width: 100%;
  min-width: 0;
  gap: clamp(10px, 1.25vw, 20px);
  flex-wrap: nowrap;
}

.top-nav .logo {
  flex: 0 0 auto;
  min-width: 0;
  white-space: nowrap;
}

.top-nav .logo span {
  display: block;
  white-space: nowrap;
}

.top-nav .nav-buttons {
  flex: 1 1 auto;
  min-width: 0;
  flex-wrap: nowrap;
  justify-content: flex-end;
  align-items: center;
  gap: clamp(4px, 0.6vw, 9px);
}

.top-nav .nav-dropdown {
  flex: 0 0 auto;
}

.top-nav .nav-btn,
.top-nav .nav-dropdown-toggle {
  box-sizing: border-box;
  white-space: nowrap;
  line-height: 1.2;
}

.top-nav .nav-btn {
  padding: 10px clamp(8px, 0.85vw, 14px);
  font-size: clamp(12px, 0.86vw, 14px);
}

.top-nav .nav-caret {
  flex: 0 0 auto;
}

/* Keep the final desktop dropdowns inside the viewport. */
.top-nav .typing-dropdown .nav-dropdown-menu,
.top-nav .spacebar-dropdown .nav-dropdown-menu {
  right: 0;
  left: auto;
}

/*
 * Use the compact mobile navigation before desktop items become crowded.
 * This prevents a second navigation line at ordinary 100% browser zoom.
 */
@media (max-width: 1180px) {
  .top-nav {
    position: sticky !important;
    top: 0;
    padding: 12px 18px;
    min-height: 64px;
    justify-content: space-between;
    align-items: center;
  }

  .top-nav .logo {
    max-width: calc(100% - 58px);
    font-size: clamp(16px, 2.5vw, 21px);
  }

  .top-nav .logo-icon {
    flex: 0 0 auto;
  }

  .top-nav .hamburger {
    display: flex !important;
    flex: 0 0 42px;
    width: 42px;
    height: 40px;
    padding: 6px 4px;
    margin-left: auto;
    align-items: center;
    justify-content: center;
    gap: 5px;
    cursor: pointer;
    border: 0;
    background: transparent;
    position: relative;
    z-index: 3100;
  }

  .top-nav .hamburger span {
    display: block;
    width: 30px;
    height: 3px;
    flex: 0 0 3px;
    border-radius: 999px;
    background: #fff;
  }

  .top-nav .nav-buttons {
    display: none !important;
  }

  .top-nav .nav-buttons.open {
    display: flex !important;
    position: absolute;
    top: calc(100% + 8px);
    right: 12px;
    left: auto;
    width: min(390px, calc(100vw - 24px));
    max-width: calc(100vw - 24px);
    max-height: calc(100dvh - 88px);
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    box-sizing: border-box;
    flex-direction: column;
    flex-wrap: nowrap;
    align-items: stretch;
    justify-content: flex-start;
    gap: 8px;
    padding: 12px;
    margin: 0;
    border: 1px solid rgba(255, 255, 255, 0.14);
    border-radius: 14px;
    background: rgba(5, 8, 18, 0.98);
    box-shadow: 0 18px 45px rgba(0, 0, 0, 0.45);
    z-index: 3000;
  }

  .top-nav .nav-buttons.open > .nav-btn,
  .top-nav .nav-buttons.open > .nav-dropdown,
  .top-nav .nav-buttons.open > .nav-dropdown > .nav-dropdown-toggle {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
  }

  .top-nav .nav-buttons.open .nav-btn,
  .top-nav .nav-buttons.open .nav-dropdown-toggle {
    min-height: 44px;
    padding: 11px 14px;
    font-size: 14px;
    white-space: nowrap;
  }

  .top-nav .nav-buttons.open > .nav-btn {
    display: flex;
    align-items: center;
  }

  .top-nav .nav-dropdown {
    display: block;
  }

  .top-nav .nav-dropdown-toggle {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .top-nav .nav-dropdown-menu,
  .top-nav .typing-dropdown .nav-dropdown-menu,
  .top-nav .spacebar-dropdown .nav-dropdown-menu {
    position: static;
    inset: auto;
    width: 100%;
    min-width: 0;
    max-width: 100%;
    box-sizing: border-box;
    margin: 8px 0 0;
    padding: 8px;
  }

  .top-nav .nav-dropdown-menu a {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
    white-space: normal;
    overflow-wrap: anywhere;
  }
}

@media (max-width: 520px) {
  .top-nav {
    padding: 10px 12px;
    min-height: 60px;
  }

  .top-nav .logo {
    gap: 8px;
    font-size: clamp(14px, 4.25vw, 18px);
    max-width: calc(100% - 52px);
  }

  .top-nav .logo-icon {
    width: 34px;
    height: 34px;
  }

  .top-nav .hamburger {
    flex-basis: 40px;
    width: 40px;
    height: 38px;
  }

  .top-nav .nav-buttons.open {
    top: calc(100% + 6px);
    right: 8px;
    width: calc(100vw - 16px);
    max-width: calc(100vw - 16px);
    max-height: calc(100dvh - 76px);
  }
}

@media (max-width: 360px) {
  .top-nav .logo {
    font-size: 13.5px;
  }

  .top-nav .logo-icon {
    width: 32px;
    height: 32px;
  }

  .top-nav .nav-buttons.open .nav-btn,
  .top-nav .nav-buttons.open .nav-dropdown-toggle {
    font-size: 13px;
    padding-right: 12px;
    padding-left: 12px;
  }
}
/* Small-screen width safety for the tool cards beneath the header. */
@media (max-width: 520px) {
  .container,
  .test-section,
  .stats-container,
  .click-area,
  .typing-area,
  .spacebar-area,
  .scroll-area,
  .recent-results-panel {
    min-width: 0 !important;
    max-width: 100% !important;
    box-sizing: border-box !important;
  }

  .stats-container {
    width: 100% !important;
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
    gap: 6px !important;
  }

  .stats-container > *,
  .stat-box,
  .stat-label,
  .stat-value {
    min-width: 0 !important;
    max-width: 100% !important;
    box-sizing: border-box !important;
  }

  .stat-box {
    padding-right: 6px !important;
    padding-left: 6px !important;
  }

  .stat-label,
  .stat-value {
    overflow-wrap: anywhere;
  }

  .space-key {
    max-width: 100% !important;
    box-sizing: border-box !important;
    overflow: hidden;
  }

  pre,
  .semantic-formula {
    max-width: 100% !important;
    box-sizing: border-box !important;
    white-space: pre-wrap !important;
    overflow-wrap: anywhere;
    word-break: normal;
  }
}

/* Phase V2: readable desktop buttons and a consistent three-line hamburger */

/*
 * At wide desktop sizes the full menu remains visible with readable controls.
 * We no longer shrink the menu text merely to force it into a crowded row.
 */
@media (min-width: 1481px) {
  .top-nav {
    padding: 15px 24px;
    gap: 18px;
  }

  .top-nav .logo {
    font-size: 22px;
  }

  .top-nav .nav-buttons {
    display: flex !important;
    flex: 1 1 auto;
    flex-wrap: nowrap;
    justify-content: flex-end;
    align-items: center;
    gap: 9px;
  }

  .top-nav .nav-btn,
  .top-nav .nav-dropdown-toggle {
    min-height: 44px !important;
    padding: 12px 16px !important;
    font-size: 15px !important;
    line-height: 1.2;
    white-space: nowrap;
  }

  .top-nav .hamburger {
    display: none !important;
  }
}

/*
 * Switch to the hamburger before full-size desktop buttons can overlap.
 * This threshold also handles browser zoom because zoom reduces the effective
 * CSS viewport width.
 */
@media (max-width: 1480px) {
  .top-nav {
    position: sticky !important;
    top: 0;
    min-height: 66px;
    padding: 12px 18px;
    gap: 12px;
    justify-content: space-between;
    align-items: center;
    flex-wrap: nowrap;
  }

  .top-nav .logo {
    flex: 0 1 auto;
    max-width: calc(100% - 60px);
    font-size: clamp(16px, 2.15vw, 21px);
  }

  .top-nav .nav-buttons {
    display: none !important;
  }

  .top-nav .hamburger {
    display: flex !important;
    flex-direction: column !important;
    flex: 0 0 44px;
    width: 44px;
    height: 42px;
    min-width: 44px;
    min-height: 42px;
    padding: 7px;
    margin: 0 0 0 auto;
    align-items: center;
    justify-content: center;
    gap: 5px;
    border: 0;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.06);
    cursor: pointer;
    position: relative;
    z-index: 3100;
  }

  .top-nav .hamburger span {
    display: block !important;
    flex: 0 0 3px;
    width: 28px;
    height: 3px;
    min-height: 3px;
    margin: 0;
    border-radius: 999px;
    background: #ffffff;
    opacity: 1;
    transform: none;
  }

  .top-nav .nav-buttons.open {
    display: flex !important;
    position: absolute;
    top: calc(100% + 8px);
    right: 12px;
    left: auto;
    width: min(410px, calc(100vw - 24px));
    max-width: calc(100vw - 24px);
    max-height: calc(100dvh - 90px);
    box-sizing: border-box;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    flex-direction: column;
    flex-wrap: nowrap;
    align-items: stretch;
    justify-content: flex-start;
    gap: 9px;
    padding: 13px;
    margin: 0;
    border: 1px solid rgba(255, 255, 255, 0.14);
    border-radius: 14px;
    background: rgba(5, 8, 18, 0.98);
    box-shadow: 0 18px 45px rgba(0, 0, 0, 0.45);
    z-index: 3000;
  }

  .top-nav .nav-buttons.open > .nav-btn,
  .top-nav .nav-buttons.open > .nav-dropdown,
  .top-nav .nav-buttons.open > .nav-dropdown > .nav-dropdown-toggle {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
  }

  .top-nav .nav-buttons.open .nav-btn,
  .top-nav .nav-buttons.open .nav-dropdown-toggle {
    min-height: 46px !important;
    padding: 12px 15px !important;
    font-size: 15px !important;
    line-height: 1.25;
    white-space: nowrap;
  }

  .top-nav .nav-buttons.open > .nav-btn {
    display: flex;
    align-items: center;
  }

  .top-nav .nav-dropdown {
    display: block;
  }

  .top-nav .nav-dropdown-toggle {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .top-nav .nav-dropdown-menu,
  .top-nav .typing-dropdown .nav-dropdown-menu,
  .top-nav .spacebar-dropdown .nav-dropdown-menu {
    position: static;
    inset: auto;
    width: 100%;
    min-width: 0;
    max-width: 100%;
    box-sizing: border-box;
    margin: 8px 0 0;
    padding: 8px;
  }

  .top-nav .nav-dropdown-menu a {
    width: 100%;
    max-width: 100%;
    min-height: 42px;
    box-sizing: border-box;
    white-space: normal;
    overflow-wrap: anywhere;
  }
}

@media (max-width: 520px) {
  .top-nav {
    min-height: 60px;
    padding: 9px 11px;
  }

  .top-nav .logo {
    max-width: calc(100% - 54px);
    gap: 8px;
    font-size: clamp(13px, 4.4vw, 18px);
  }

  .top-nav .logo-icon {
    width: 34px;
    height: 34px;
  }

  .top-nav .hamburger {
    flex-basis: 42px;
    width: 42px;
    min-width: 42px;
    height: 40px;
    min-height: 40px;
    padding: 6px;
  }

  .top-nav .hamburger span {
    width: 27px;
  }

  .top-nav .nav-buttons.open {
    top: calc(100% + 6px);
    right: 8px;
    width: calc(100vw - 16px);
    max-width: calc(100vw - 16px);
    max-height: calc(100dvh - 76px);
  }
}

/* Phase V3: full desktop navigation from laptop widths upward */

/*
 * Desktop and laptop layout:
 * - logo stays in its own column
 * - six navigation items share the remaining width equally
 * - labels stay at 15px and controls stay 46px high
 * - no wrapping, overlap, truncation, or compressed mini-buttons
 */
@media (min-width: 1201px) {
  .top-nav {
    display: grid !important;
    grid-template-columns: clamp(230px, 20vw, 270px) minmax(0, 1fr);
    align-items: center;
    width: 100%;
    max-width: 100%;
    min-height: 70px;
    padding: 12px 18px;
    gap: 12px;
    box-sizing: border-box;
  }

  .top-nav .logo {
    width: 100%;
    max-width: 270px;
    min-width: 0;
    font-size: 20px;
    gap: 9px;
    white-space: nowrap;
    overflow: visible;
  }

  .top-nav .logo-icon {
    flex: 0 0 40px;
    width: 40px;
    height: 40px;
  }

  .top-nav .nav-buttons,
  .top-nav .nav-buttons.open {
    display: grid !important;
    position: static !important;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    align-items: stretch;
    justify-content: stretch;
    width: 100%;
    min-width: 0;
    max-width: 100%;
    height: auto;
    max-height: none;
    padding: 0;
    margin: 0;
    gap: 8px;
    overflow: visible;
    border: 0;
    border-radius: 0;
    background: transparent;
    box-shadow: none;
  }

  .top-nav .nav-buttons > .nav-btn,
  .top-nav .nav-buttons > .nav-dropdown {
    width: 100%;
    min-width: 0;
    max-width: 100%;
    height: 100%;
    box-sizing: border-box;
  }

  .top-nav .nav-dropdown {
    position: relative;
    display: block;
  }

  .top-nav .nav-btn,
  .top-nav .nav-dropdown-toggle,
  .top-nav .nav-buttons.open .nav-btn,
  .top-nav .nav-buttons.open .nav-dropdown-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    min-width: 0;
    max-width: 100%;
    min-height: 46px !important;
    height: 46px;
    padding: 10px 6px !important;
    box-sizing: border-box;
    font-size: 15px !important;
    line-height: 1.15;
    text-align: center;
    white-space: nowrap;
    overflow: visible;
  }

  .top-nav .nav-dropdown-toggle {
    gap: 5px;
  }

  .top-nav .nav-caret {
    flex: 0 0 auto;
    font-size: 11px;
  }

  .top-nav .hamburger {
    display: none !important;
  }

  .top-nav .nav-dropdown-menu,
  .top-nav .typing-dropdown .nav-dropdown-menu,
  .top-nav .spacebar-dropdown .nav-dropdown-menu {
    position: absolute !important;
    top: calc(100% + 8px);
    bottom: auto;
    width: max-content;
    min-width: 240px;
    max-width: min(320px, calc(100vw - 24px));
    margin: 0;
    padding: 10px;
    box-sizing: border-box;
  }

  .top-nav .cps-dropdown .nav-dropdown-menu {
    left: 0;
    right: auto;
  }

  .top-nav .typing-dropdown .nav-dropdown-menu,
  .top-nav .spacebar-dropdown .nav-dropdown-menu {
    left: auto;
    right: 0;
  }
}

/*
 * Compact/mobile layout:
 * the hamburger appears only when the full six-button row no longer has
 * enough room. It remains a true three-horizontal-line icon.
 */
@media (max-width: 1200px) {
  .top-nav {
    display: flex !important;
    min-height: 64px;
    padding: 11px 16px;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: nowrap;
  }

  .top-nav .logo {
    flex: 0 1 auto;
    width: auto;
    max-width: calc(100% - 58px);
    min-width: 0;
    font-size: clamp(15px, 2.4vw, 20px);
  }

  .top-nav .nav-buttons {
    display: none !important;
  }

  .top-nav .hamburger {
    display: flex !important;
    flex-direction: column !important;
    flex: 0 0 44px;
    width: 44px;
    min-width: 44px;
    height: 42px;
    min-height: 42px;
    padding: 7px;
    margin-left: auto;
    align-items: center;
    justify-content: center;
    gap: 5px;
    border: 0;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.06);
  }

  .top-nav .hamburger span {
    display: block !important;
    flex: 0 0 3px;
    width: 28px;
    height: 3px;
    min-height: 3px;
    margin: 0;
    border-radius: 999px;
    background: #ffffff;
    opacity: 1;
    transform: none;
  }

  .top-nav .nav-buttons.open {
    display: flex !important;
    position: absolute !important;
    top: calc(100% + 8px);
    right: 12px;
    left: auto;
    width: min(410px, calc(100vw - 24px));
    max-width: calc(100vw - 24px);
    max-height: calc(100dvh - 88px);
    padding: 13px;
    margin: 0;
    gap: 9px;
    flex-direction: column;
    flex-wrap: nowrap;
    align-items: stretch;
    justify-content: flex-start;
    overflow-x: hidden;
    overflow-y: auto;
    box-sizing: border-box;
    border: 1px solid rgba(255, 255, 255, 0.14);
    border-radius: 14px;
    background: rgba(5, 8, 18, 0.98);
    box-shadow: 0 18px 45px rgba(0, 0, 0, 0.45);
  }

  .top-nav .nav-buttons.open > .nav-btn,
  .top-nav .nav-buttons.open > .nav-dropdown,
  .top-nav .nav-buttons.open > .nav-dropdown > .nav-dropdown-toggle {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
  }

  .top-nav .nav-buttons.open .nav-btn,
  .top-nav .nav-buttons.open .nav-dropdown-toggle {
    min-height: 46px !important;
    padding: 12px 15px !important;
    font-size: 15px !important;
    line-height: 1.25;
    white-space: nowrap;
  }

  .top-nav .nav-dropdown-menu,
  .top-nav .typing-dropdown .nav-dropdown-menu,
  .top-nav .spacebar-dropdown .nav-dropdown-menu {
    position: static !important;
    inset: auto !important;
    width: 100%;
    min-width: 0;
    max-width: 100%;
    margin: 8px 0 0;
  }
}

@media (max-width: 520px) {
  .top-nav {
    min-height: 60px;
    padding: 9px 11px;
  }

  .top-nav .logo {
    max-width: calc(100% - 54px);
    gap: 8px;
    font-size: clamp(13px, 4.4vw, 18px);
  }

  .top-nav .logo-icon {
    width: 34px;
    height: 34px;
  }

  .top-nav .hamburger {
    flex-basis: 42px;
    width: 42px;
    min-width: 42px;
    height: 40px;
    min-height: 40px;
  }

  .top-nav .nav-buttons.open {
    top: calc(100% + 6px);
    right: 8px;
    width: calc(100vw - 16px);
    max-width: calc(100vw - 16px);
    max-height: calc(100dvh - 76px);
  }
}

/* Phase V4: remove the desktop hover gap between buttons and dropdowns */
@media (min-width: 1201px) {
  .top-nav .nav-dropdown-menu,
  .top-nav .typing-dropdown .nav-dropdown-menu,
  .top-nav .spacebar-dropdown .nav-dropdown-menu {
    top: 100% !important;
    margin-top: 0 !important;
  }

  /*
   * A one-pixel overlap prevents sub-pixel rendering from creating a
   * hover-breaking seam on scaled Windows displays.
   */
  .top-nav .nav-dropdown-menu {
    transform: translateY(-1px);
  }
}
