  /* ===== PHONE LANDSCAPE FIT ===== */
  @media screen and (orientation: landscape), screen and (max-aspect-ratio: 99/100) {
    #topbar {
      height: clamp(52px, 14vh, 64px);
      padding: 0 clamp(8px, 2vw, 16px);
      gap: 8px;
    }

    #pages {
      top: clamp(52px, 14vh, 64px);
    }

    #logo {
      font-size: clamp(14px, 2.2vw, 19px);
    }

    #topbarLeft {
      gap: clamp(6px, 1.4vw, 14px);
    }

    #sectionSelector {
      padding: 7px 11px;
      font-size: 10px;
    }

    #topbarCenter {
      gap: 2px;
    }

    .tool-group {
      gap: 2px;
    }

    .toolbtn {
      width: clamp(42px, 6vw, 54px);
      height: clamp(42px, 12vh, 54px);
      font-size: 9px;
      gap: 2px;
    }

    .toolbtn svg {
      width: 17px;
      height: 17px;
    }

    .toolbar-divider {
      height: 30px;
      margin: 0 2px;
    }

    #themeToggle {
      width: 36px;
      height: 36px;
    }

    #sidebar,
    #wfValuesPanel {
      width: clamp(180px, 23vw, 240px);
      padding: 10px;
    }

    .sidebar-item {
      padding: 9px 11px;
      margin-bottom: 7px;
      border-radius: 10px;
      font-size: 12px;
      gap: 9px;
    }

    .sidebar-item svg {
      width: 28px;
      height: 15px;
    }

    #wfChartArea {
      padding: 10px;
    }

    #wfChartWrap {
      max-height: calc(100vh - 100px);
      width: auto;
      height: 100%;
      aspect-ratio: 1 / 1;
    }
  }


  /* ===== LANDSCAPE V2: MATCH SIM PALETTE TO WAVEFORM + FIT GRAPH ===== */
  @media screen and (orientation: landscape), screen and (max-aspect-ratio: 99/100) {

    /* Simulation palette gets the same white-card treatment as Values panel */
    #sidebar {
      background: var(--bg);
      border-right: 1px solid var(--panel-line);
      padding: 10px;
    }

    #sidebar::before {
      content: "";
      position: absolute;
      pointer-events: none;
    }

    .sidebar-item {
      background: var(--panel);
      border: 1px solid var(--panel-line);
      border-radius: 14px;
      box-shadow: none;
    }

    /* Keep both left panels visually consistent */
    #sidebar,
    #wfValuesPanel {
      width: clamp(180px, 23vw, 240px);
    }

    /* Waveform page: consume exactly the remaining viewport */
    .wf-layout {
      width: 100%;
      min-width: 0;
      overflow: hidden;
    }

    #wfValuesPanel {
      height: 100%;
      flex: 0 0 clamp(180px, 23vw, 240px);
      overflow-y: auto;
    }

    #wfChartArea {
      flex: 1 1 0;
      min-width: 0;
      min-height: 0;
      height: 100%;
      padding: 10px;
      overflow: hidden;
    }

    #wfLegend {
      flex: 0 0 auto;
      margin-bottom: 8px;
    }

    /* Remove the old square constraint that could push the graph off-screen */
    #wfChartWrap {
      flex: 1 1 0;
      min-width: 0;
      min-height: 0;
      width: 100%;
      height: auto;
      max-width: none;
      max-height: none;
      aspect-ratio: auto;
      overflow: hidden;
      padding: 10px;
    }

    #wfChartWrap canvas,
    #resultChart {
      display: block !important;
      width: 100% !important;
      height: 100% !important;
      max-width: 100% !important;
      max-height: 100% !important;
    }
  }


  /* ===== V2: GRAPH 73% / VALUES 27% ===== */
  @media screen and (orientation: landscape), screen and (max-aspect-ratio: 99/100) {
    .wf-layout {
      display: grid;
      grid-template-columns: minmax(0, 27fr) minmax(0, 73fr);
      width: 100%;
      height: 100%;
      min-width: 0;
      overflow: hidden;
    }

    #wfValuesPanel {
      width: auto;
      min-width: 0;
      height: 100%;
      padding: 8px;
      overflow: hidden;
    }

    .wf-card {
      padding: 10px;
      height: 100%;
      overflow: hidden;
    }

    .wf-card h3 { font-size: 20px; margin-bottom: 3px; }
    #wfStatus { font-size: 11px; min-height: 0; margin-bottom: 3px; }
    #wfStatus:empty { display: none; }
    #wfTraceSelect { padding: 7px 8px; font-size: 13px; margin-bottom: 6px; }
    .wf-stat-row { padding: 6px 2px; font-size: 13px; }
    #wfDownloadRow { margin-top: 7px; }
    #wfDownloadRow button { padding: 6px; font-size: 11px; }

    #wfChartArea {
      width: auto;
      min-width: 0;
      min-height: 0;
      height: 100%;
      padding: 8px;
      overflow: hidden;
    }

    #wfChartWrap {
      width: 100%;
      height: 100%;
      min-width: 0;
      min-height: 0;
      max-width: 100%;
      max-height: 100%;
      aspect-ratio: auto;
      overflow: hidden;
    }

    #wfChartWrap canvas, #resultChart {
      width: 100% !important;
      height: 100% !important;
      max-width: 100% !important;
      max-height: 100% !important;
    }
  }


  /* ===== REFINED PHONE LANDSCAPE UI ===== */
  @media screen and (orientation: landscape), screen and (max-aspect-ratio: 99/100) {

    /* Slimmer top bar */
    #topbar {
      height: clamp(44px, 11vh, 52px);
      padding: 0 9px;
      gap: 5px;
    }

    #pages {
      top: clamp(44px, 11vh, 52px);
    }

    #logo { font-size: 16px; }
    #topbarLeft { gap: 9px; }

    #sectionSelector {
      padding: 6px 10px;
      font-size: 9px;
    }

    .toolbtn {
      width: 43px;
      height: 42px;
      font-size: 8px;
      gap: 1px;
      border-radius: 9px;
    }

    .toolbtn svg {
      width: 16px;
      height: 16px;
    }

    /* Settings + Export live at top-right, followed by light/dark mode */
    #topbarCenter > .tool-group:last-child {
      position: absolute;
      right: 47px;
      top: 50%;
      transform: translateY(-50%);
      gap: 2px;
      z-index: 35;
    }

    #topbarRight {
      position: absolute;
      right: 7px;
      top: 50%;
      transform: translateY(-50%);
      z-index: 36;
    }

    #themeToggle {
      width: 34px;
      height: 34px;
      border-radius: 9px;
    }

    #themeToggle svg {
      width: 16px;
      height: 16px;
    }

    /* Remove the vertical separator between Values and graph */
    #wfValuesPanel {
      border-right: none !important;
    }

    /* Same top inset for both waveform cards */
    .wf-layout {
      padding: 10px;
      gap: 10px;
      background: var(--bg);
      grid-template-columns: minmax(0, 27fr) minmax(0, 73fr);
    }

    #wfValuesPanel,
    #wfChartArea {
      padding: 0 !important;
      height: 100%;
      min-height: 0;
    }

    /* Values palette */
    .wf-card {
      height: 100%;
      border-radius: 14px;
      padding: 10px;
      overflow: hidden;
    }

    /* Graph palette starts at exactly the same Y position as Values */
    #wfChartArea {
      display: flex;
      flex-direction: column;
      overflow: hidden;
    }

    #wfLegend {
      flex: 0 0 auto;
      margin: 0 0 6px 0;
      min-height: 18px;
    }

    #wfChartWrap {
      flex: 1 1 auto;
      width: 100%;
      min-width: 0;
      min-height: 0;
      height: auto;
      max-width: 100%;
      max-height: 100%;
      border-radius: 14px;
      padding: 8px;
      overflow: hidden;
    }

    #wfChartWrap canvas,
    #resultChart {
      width: 100% !important;
      height: 100% !important;
      max-width: 100% !important;
      max-height: 100% !important;
    }
  }


  /* ===== ALIGN WAVEFORM GRAPH CARD WITH VALUES CARD ===== */
  @media screen and (orientation: landscape), screen and (max-aspect-ratio: 99/100) {
    #wfChartArea {
      position: relative;
    }

    #wfLegend {
      position: absolute;
      top: 8px;
      left: 12px;
      right: 12px;
      z-index: 3;
      margin: 0 !important;
      min-height: 18px;
      pointer-events: auto;
    }

    #wfChartWrap {
      flex: 1 1 auto;
      height: 100%;
      padding-top: 34px;
    }
  }


  /* ===== SIMULATION: HAMBURGER + MATCHED WHITE PANELS ===== */
  #componentsMenuBtn {
    display: none;
    width: 36px;
    height: 36px;
    flex: 0 0 36px;
    align-items: center;
    justify-content: center;
    padding: 0;
    border: 1px solid var(--panel-line);
    border-radius: 9px;
    background: var(--panel);
    color: var(--text);
    cursor: pointer;
  }
  #componentsMenuBtn svg { width: 19px; height: 19px; }

  @media screen and (orientation: landscape), screen and (max-aspect-ratio: 99/100) {
    /* Stable logo position across all pages: reserve the hamburger slot always. */
    #topbarLeft {
      position: relative;
      padding-left: 43px;
    }

    #componentsMenuBtn {
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      z-index: 38;
    }

    body.simulation-page #componentsMenuBtn {
      display: flex;
    }

    /* Simulation gets its own spacing; no waveform height/proportion rules reused. */
    #page-simulation {
      padding: 10px;
      background: var(--bg);
    }

    #page-simulation .sim-layout {
      position: relative;
      height: 100%;
      min-height: 0;
      gap: 10px;
    }

    /* Components are contained in one white palette panel. */
    #sidebar {
      position: relative;
      width: clamp(180px, 23vw, 240px);
      flex: 0 0 clamp(180px, 23vw, 240px);
      height: 100%;
      min-height: 0;
      padding: 10px;
      overflow-y: auto;
      background: var(--panel);
      border: 1px solid var(--panel-line);
      border-radius: 14px;
      transition: width .2s ease, flex-basis .2s ease, padding .2s ease,
                  opacity .15s ease, border-width .2s ease;
    }

    /* Items now live inside the single white Components palette. */
    #sidebar .sidebar-item {
      background: var(--bg);
      border: 1px solid var(--panel-line);
      box-shadow: none;
    }

    /* Canvas is a separate white panel, sized only for the simulation page. */
    #canvas-area {
      flex: 1 1 0;
      min-width: 0;
      min-height: 0;
      height: 100%;
      background: var(--panel);
      border: 1px solid var(--panel-line);
      border-radius: 14px;
      overflow: hidden;
    }

    #canvas-wrap {
      inset: 0;
      background: var(--panel);
      border-radius: inherit;
    }

    /* Hide/show the whole Components palette with the hamburger. */
    #page-simulation.components-collapsed #sidebar {
      width: 0;
      flex-basis: 0;
      min-width: 0;
      padding-left: 0;
      padding-right: 0;
      border-width: 0;
      opacity: 0;
      overflow: hidden;
      pointer-events: none;
    }
  }


  /* ===== USER UPDATE: toolbar spacing ===== */
  @media screen and (orientation: landscape), screen and (max-aspect-ratio: 99/100) {
    #topbarCenter {
      transform: translateX(-12px);
      padding-right: 12px;
    }
  }


  /* Selected component tab: same filled accent treatment as Simulate */
  #sidebar .sidebar-item.active {
    background: var(--accent) !important;
    border-color: var(--accent) !important;
    color: #fff !important;
    box-shadow: 0 3px 12px rgba(193,117,46,.28);
  }
  #sidebar .sidebar-item.active svg {
    color: #fff !important;
  }
  [data-theme="dark"] #sidebar .sidebar-item.active {
    background: var(--accent) !important;
    border-color: var(--accent-strong) !important;
    color: #17130b !important;
    box-shadow: 0 3px 14px rgba(216,166,63,.30);
  }
  [data-theme="dark"] #sidebar .sidebar-item.active svg {
    color: #17130b !important;
  }


  /* BUG AUDIT: consistent selected-component feedback for every symbol type */
  #compsLayer g.is-selected .comp-body,
  #compsLayer g.is-selected .gnd-symbol,
  #compsLayer g.is-selected > g {
    stroke: var(--accent) !important;
  }
  #compsLayer g.is-selected .transistor-arrow {
    fill: var(--accent) !important;
  }


