/* Browser build: the Windows title-bar buttons have no meaning in a tab. */
#winMinBtn,
#winMaxBtn,
#winCloseBtn {
  display: none !important;
}

/* The frameless desktop shell reserves drag space for the native window. */
body.frameless .topbar {
  -webkit-app-region: no-drag;
  app-region: no-drag;
}

/* ------------------------------------------------------------------ */
/* Phone layout: the same desk, refitted.                              */
/* The chart gets the whole width, the icon rail moves under it, and   */
/* the watchlist, flyouts, ticket and trade drawer slide over the      */
/* chart instead of shrinking it. Every control stays where it was.    */
/* ------------------------------------------------------------------ */
@media (max-width: 760px) {
  .terminal { grid-template-rows: 44px minmax(0, 1fr); }

  .topbar { gap: 4px; padding: 0 6px; }
  .topbar .logo { padding-right: 2px; }
  .topbar .logo-text { display: none; }
  .topbar .icon-btn { width: 30px; height: 30px; flex: 0 0 auto; }
  .topbar .tf-btn,
  .topbar .btn { padding-left: 7px; padding-right: 7px; }

  .workspace,
  body.watchlist-closed .workspace,
  body.ticket-open .workspace,
  body.ticket-open.watchlist-closed .workspace {
    grid-template-columns: minmax(0, 1fr) !important;
    grid-template-rows: minmax(0, 1fr) 30px 32px 36px !important;
  }
  .chart-area { grid-column: 1 !important; grid-row: 1 !important; }
  .trade-quickbar { grid-column: 1 !important; grid-row: 2 !important; overflow-x: auto; }
  .bottombar { grid-column: 1 !important; grid-row: 3 !important; }
  .utility-rail {
    grid-column: 1 !important;
    grid-row: 4 !important;
    flex-direction: row !important;
    align-items: center;
    justify-content: space-between;
    width: auto !important;
    height: 36px;
    border-left: 0;
    border-top: 1px solid var(--border);
    overflow-x: auto;
  }
  .utility-rail .rail-btn { width: 34px; height: 34px; flex: 0 0 auto; }

  /* Panels sit over the chart instead of squeezing it. */
  body.watchlist-closed .watchlist { display: none !important; }
  body:not(.watchlist-closed) .watchlist {
    position: fixed;
    top: 44px;
    right: 0;
    bottom: 98px;
    left: auto;
    width: min(340px, 92vw);
    z-index: 50;
    display: flex;
    opacity: 1;
    pointer-events: auto;
    border-left: 1px solid var(--border-strong);
    box-shadow: -14px 0 34px rgba(0, 0, 0, 0.4);
  }
  .flyout {
    position: fixed !important;
    top: 44px;
    right: 0;
    bottom: 98px;
    left: auto !important;
    width: min(360px, 94vw) !important;
    z-index: 50;
    box-shadow: -14px 0 34px rgba(0, 0, 0, 0.4);
  }
  body.ticket-open .chart-ticket {
    position: fixed !important;
    top: 44px;
    right: 0;
    bottom: 0;
    left: 0 !important;
    width: auto !important;
    z-index: 55;
    box-shadow: 0 -10px 34px rgba(0, 0, 0, 0.4);
  }

  /* The trade drawer spans the phone and never covers the price scale. */
  body.desk-drawer,
  body.desk-drawer-full { --drawer-right: 0px !important; }
  body.desk-drawer .trade-desk,
  body.desk-drawer-full .trade-desk {
    left: 0 !important;
    right: 0 !important;
    width: auto !important;
    max-height: 78vh;
  }
  #deskTabs { overflow-x: auto; flex-wrap: nowrap; scrollbar-width: none; }
  #deskTabs::-webkit-scrollbar { display: none; }

  /* The quick bar, the range bar and the rail are swipeable strips. */
  .trade-quickbar,
  .bottombar { overflow-x: auto; overflow-y: hidden; flex-wrap: nowrap; scrollbar-width: none; }
  .trade-quickbar::-webkit-scrollbar,
  .bottombar::-webkit-scrollbar { display: none; }
  .utility-rail { scrollbar-width: none; }
  .utility-rail::-webkit-scrollbar { display: none; }

  /* Chart overlays: keep them inside the screen. */
  .chart-header { left: 6px; right: 6px; gap: 6px; }
  .legend-main { flex-wrap: wrap; row-gap: 1px; column-gap: 4px; max-width: 62vw; }
  .legend-main h1 { font-size: 15px; }
  .legend-main .provider,
  .legend-main .market-open,
  .legend-main .refresh,
  .legend-main .source-badge { display: none; }
  .legend-main .pill { font-size: 8.5px; padding: 1px 5px; }
  .legend-main .ohlc-line { font-size: 8px; gap: 4px; }
  .legend-main .price-delta { font-size: 8.5px; gap: 4px; }
  .legend-right { flex-direction: column; align-items: flex-end; gap: 1px; }
  .legend-right .current-price { font-size: 17px; }
  .legend-right .spread-pill { font-size: 8.5px; }
  .draw-palette { top: 58px; left: 6px; max-height: calc(100% - 116px); }
  .chart-nav { bottom: 6px; right: 6px; }
  .chart-logo { display: none; }

  /* Menus and pop-ups must never run off the screen. */
  .menu,
  .dropdown,
  [class*="menu"] {
    max-width: calc(100vw - 12px);
  }
  .modal-card,
  .dialog,
  [class*="modal"] > * {
    max-width: calc(100vw - 16px) !important;
    max-height: calc(100vh - 40px);
    overflow: auto;
  }
}

@media (max-width: 480px) {
  .topbar .logo img,
  .topbar .logo svg { width: 26px; height: 26px; }
  .chart-header .chart-price { font-size: 18px; }
  .chart-header .chart-ohlc { font-size: 9px; gap: 5px; }
}

/* Phone: give the UTC time field a full row under price and bar, otherwise the
   native date/time control clips inside the drawing settings dialog. */
@media (max-width: 760px) {
  .coord-head { display: none; }
  .coord-row { flex-wrap: wrap; row-gap: 5px; }
  .coord-row input[data-coord-price] { flex: 1 1 60px; }
  .coord-row input[data-coord-bar] { flex: 0 0 58px; }
  .coord-row input[data-coord-time] { order: 9; flex: 1 0 calc(100% - 31px); margin-left: 31px; }
}


/* ------------------------------------------------------------------ */
/* Phone chrome: every bar is optional, and the sheets that toggle it. */
/* ------------------------------------------------------------------ */
.phone-only { display: none !important; }

/* A finger on the drawing surface must never scroll the page or be turned
   into a browser gesture halfway through a stroke. */
#drawCanvas { touch-action: none; -webkit-user-select: none; user-select: none; }
.chart-area { -webkit-tap-highlight-color: transparent; }

.phone-sheet {
  position: fixed;
  left: 8px;
  right: 8px;
  bottom: 8px;
  z-index: 95;
  max-height: 72vh;
  overflow: auto;
  padding: 10px 12px 12px;
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  background: var(--panel);
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.5);
  display: grid;
  gap: 2px;
}
.phone-sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding-bottom: 6px; }
.phone-sheet-head strong { font-size: 13px; }
.phone-sheet-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 9px 2px; border-bottom: 1px solid var(--border); font-size: 12.5px;
}
.phone-sheet-row input { width: 18px; height: 18px; accent-color: var(--accent); }
.phone-sheet-action,
.phone-more-item {
  display: flex; align-items: center; gap: 8px; width: 100%;
  padding: 10px 8px; border: 0; border-radius: 8px; background: transparent;
  color: var(--text); font: inherit; text-align: left;
}
.phone-sheet-action:active,
.phone-more-item:active { background: rgba(127, 127, 127, 0.16); }
.phone-more-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 2px; }
.phone-more-item svg { width: 15px; height: 15px; opacity: 0.8; }

@media (max-width: 760px) {
  .phone-only { display: inline-flex !important; }

  /* Secondary top-bar actions live in the More sheet on a phone, so the
     header fits without sideways scrolling. */
  #demoBtn, #replayBtn, #undoBtn, #redoBtn, #saveNowBtn, #chartTicketBtn,
  #masterSettingsBtn, #riskBtn, #fullscreenBtn, #screenshotBtn, #publishBtn,
  #strategyBtn, #analyticsBtn, #backtestsBtn, #multiChartBtn, #themeBtn { display: none !important; }

  body.hide-legend .chart-header,
  body.hide-quickbar .trade-quickbar,
  body.hide-bottombar .bottombar,
  body.hide-rail .utility-rail,
  body.hide-toolbar .floating-toolbar,
  body.hide-palette .draw-palette,
  body.hide-palette .draw-rail-toggle,
  body.hide-nav .chart-nav { display: none !important; }

  /* Hidden bars collapse their rows instead of leaving empty strips. */
  .workspace,
  body.watchlist-closed .workspace,
  body.ticket-open .workspace,
  body.ticket-open.watchlist-closed .workspace {
    grid-template-rows: minmax(0, 1fr) auto auto auto !important;
  }

  .draw-select-bar { max-width: calc(100vw - 12px); overflow-x: auto; }
}
