/*
 * CPA UI v5 — Relay Telemetry Deck
 * CSS-only progressive enhancement over v4.
 * Keeps every existing DOM/API contract and the bright pixel palette.
 */

:root {
  --v5-motion-fast: 120ms;
  --v5-motion-base: 220ms;
  --v5-motion-view: 420ms;
  --v5-ease-enter: cubic-bezier(.16, 1, .3, 1);
  --v5-grid-line: rgba(39, 94, 168, .055);
}

/* Flat telemetry surfaces: retain pixel borders and color, remove material shadows. */
body.ui-v5 * {
  box-shadow: none !important;
}

body.ui-v5 :is(.hourly-svg-bar, .hourly-svg-peak-mark) {
  filter: none !important;
}

body.ui-v5 :is(
  .newapi-input,
  .newapi-select,
  .newapi-textarea,
  .search-input,
  .console-key-input-wrap .newapi-input
):focus-visible {
  outline: 3px solid var(--pf-blue);
  outline-offset: 2px;
}

body.ui-v5 :is(
  .view-tab,
  .newapi-button,
  .console-nav,
  .console-key-visibility,
  .admin-workspace-tab,
  .usage-log-page-button,
  .recharge-plan-card,
  .recharge-method-card,
  .news-filter-button,
  .agent-suggestion-chip,
  .refresh-button,
  .search-button,
  .packet-motion-toggle
):hover:not(:disabled) {
  transform: translateY(-1px);
}

body.ui-v5 :is(
  .view-tab,
  .newapi-button,
  .console-nav,
  .console-key-visibility,
  .admin-workspace-tab,
  .usage-log-page-button,
  .recharge-plan-card,
  .recharge-method-card,
  .news-filter-button,
  .agent-suggestion-chip,
  .refresh-button,
  .search-button,
  .packet-motion-toggle
):active:not(:disabled) {
  transform: translateY(1px);
}

/* A subtle station-grid backplane replaces floating-card depth. */
body.ui-v5 .console-content {
  background-color: #edf7e9;
  background-image:
    linear-gradient(var(--v5-grid-line) 1px, transparent 1px),
    linear-gradient(90deg, var(--v5-grid-line) 1px, transparent 1px);
  background-size: 24px 24px;
}

body.ui-v5 :is(
  .newapi-card,
  .mini-leaderboard-panel,
  .usage-log-panel,
  .dashboard-hourly-card,
  .admin-account-detail-page,
  .admin-account-tool-card,
  .news-intel-panel,
  .news-group-card,
  .recharge-page-hero,
  .admin-page-hero,
  .console-page-title
) {
  position: relative;
}

body.ui-v5 :is(
  .newapi-card-head,
  .dashboard-overview .newapi-card-head,
  .console-page-title,
  .recharge-page-hero,
  .admin-page-hero,
  .newspaper-header
) {
  background-image: linear-gradient(
    90deg,
    var(--pf-blue) 0 44px,
    var(--pf-coral) 44px 70px,
    var(--pf-yellow) 70px 96px,
    transparent 96px
  );
  background-position: left top;
  background-repeat: no-repeat;
  background-size: 100% 3px;
}

/* The hourly chart is the primary telemetry surface. */
body.ui-v5 .dashboard-overview > .newapi-card-body {
  grid-template-areas:
    "usage"
    "chart"
    "state"
    "actions";
  grid-template-rows: auto minmax(190px, 1fr) auto auto;
  gap: 8px;
}

body.ui-v5 .dashboard-usage-metrics { grid-area: usage; }
body.ui-v5 .dashboard-hourly-card { grid-area: chart; }
body.ui-v5 .dashboard-state-metrics { grid-area: state; }
body.ui-v5 .dashboard-overview > .newapi-card-body > .newapi-actions { grid-area: actions; }

body.ui-v5 .dashboard-overview {
  overflow: hidden;
}

body.ui-v5 .dashboard-overview::before {
  content: "";
  position: absolute;
  z-index: 4;
  top: 0;
  left: 0;
  width: 100%;
  height: 4px;
  pointer-events: none;
  background: linear-gradient(
    90deg,
    var(--pf-blue) 0 42%,
    var(--pf-mint) 42% 68%,
    var(--pf-coral) 68% 84%,
    var(--pf-yellow) 84% 100%
  );
  transform: scaleX(0);
  transform-origin: left center;
  animation: v5-telemetry-lock 460ms 80ms steps(9, end) both;
}

body.ui-v5 .dashboard-hourly-card {
  overflow: hidden;
  background: var(--pf-yellow-soft);
}

body.ui-v5 .dashboard-hourly-card::before {
  content: "";
  position: absolute;
  z-index: 2;
  top: 0;
  left: 0;
  width: 100%;
  height: 3px;
  pointer-events: none;
  background: linear-gradient(90deg, var(--pf-coral), var(--pf-blue), var(--pf-mint));
  transform: scaleX(0);
  transform-origin: left center;
  animation: v5-telemetry-lock 360ms 180ms steps(8, end) both;
}

body.ui-v5 .hourly-chart {
  background-color: #e7f6ff;
  background-image:
    linear-gradient(rgba(39, 94, 168, .06) 1px, transparent 1px),
    linear-gradient(90deg, rgba(39, 94, 168, .06) 1px, transparent 1px);
  background-size: 28px 28px;
}

body.ui-v5 .hourly-svg-bar {
  transform-box: fill-box;
  transform-origin: center bottom;
  animation: v5-bar-lock 260ms var(--v5-ease-enter) both;
}

body.ui-v5 .hourly-svg-bar:nth-of-type(6n + 2) { animation-delay: 18ms; }
body.ui-v5 .hourly-svg-bar:nth-of-type(6n + 3) { animation-delay: 36ms; }
body.ui-v5 .hourly-svg-bar:nth-of-type(6n + 4) { animation-delay: 54ms; }
body.ui-v5 .hourly-svg-bar:nth-of-type(6n + 5) { animation-delay: 72ms; }
body.ui-v5 .hourly-svg-bar:nth-of-type(6n + 6) { animation-delay: 90ms; }

body.ui-v5 .hourly-svg-glow {
  opacity: .1;
}

/* Navigation locks to the selected station without drop shadows. */
body.ui-v5 .console-nav {
  position: relative;
  overflow: hidden;
  transition:
    transform var(--v5-motion-fast) steps(2, end),
    background-color var(--v5-motion-fast) linear,
    border-color var(--v5-motion-fast) linear;
}

body.ui-v5 .console-nav::after {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 3px;
  background: linear-gradient(90deg, var(--pf-blue), var(--pf-coral), var(--pf-yellow));
  transform: scaleX(0);
  transform-origin: left center;
}

body.ui-v5 .console-nav.active::after,
body.ui-v5 .console-nav[aria-selected="true"]::after {
  animation: v5-signal-scan 180ms steps(4, end) both;
}

body.ui-v5 .packet-route-track > i::after {
  left: 0;
  width: 100%;
  background: linear-gradient(
    90deg,
    transparent 0 calc(50% - 5px),
    var(--pf-coral) calc(50% - 5px) calc(50% + 5px),
    transparent calc(50% + 5px) 100%
  );
  transform: translateX(-100%);
}

body.ui-v5 .packet-route-ribbon.is-routing .packet-route-track > i::after {
  animation: v5-route-packet 420ms steps(7, end) both;
}

body.ui-v5 .packet-route-ribbon.is-routing .packet-route-track > i:nth-of-type(2)::after {
  animation-delay: 70ms;
}

@keyframes v5-telemetry-lock {
  0% { transform: scaleX(0); opacity: .25; }
  72% { transform: scaleX(1); opacity: 1; }
  100% { transform: scaleX(1); opacity: .78; }
}

@keyframes v5-signal-scan {
  from { transform: scaleX(0); }
  to { transform: scaleX(1); }
}

@keyframes v5-route-packet {
  0% { transform: translateX(-100%); }
  50% { opacity: 1; }
  100% { transform: translateX(100%); opacity: .8; }
}

@keyframes v5-bar-lock {
  from { opacity: .22; transform: scaleY(.04); }
  to { opacity: 1; transform: scaleY(1); }
}

/* Tablet and phone: use the document as the single scrolling owner. */
@media (max-width: 980px) {
  html {
    height: auto !important;
    min-height: 100%;
    overflow-x: hidden !important;
    overflow-y: auto !important;
  }

  body.ui-v5 {
    height: auto !important;
    min-height: 100svh;
    overflow: visible !important;
  }

  body.ui-v5 .app,
  body.ui-v5 .console-shell-panel,
  body.ui-v5 .console-shell-panel > .panel-body,
  body.ui-v5 .console-shell,
  body.ui-v5 .console-content,
  body.ui-v5 .console-page,
  body.ui-v5 .console-page.active,
  body.ui-v5 .dashboard-page,
  body.ui-v5 .dashboard-grid,
  body.ui-v5 .dashboard-overview {
    height: auto !important;
    min-height: 0;
    overflow: visible !important;
  }

  body.ui-v5.packet-authenticated .console-shell-panel > .panel-head {
    display: none;
  }

  body.ui-v5.packet-authenticated .topbar {
    min-height: 55px;
    grid-template-rows: 52px;
  }

  body.ui-v5.packet-authenticated .title-copy {
    height: 52px;
    min-height: 52px;
    padding: 5px 8px;
  }

  body.ui-v5.packet-authenticated .view-switch {
    min-width: 0;
    min-height: 40px;
    height: 40px;
    padding: 3px;
  }

  body.ui-v5.packet-authenticated .view-tab {
    min-height: 32px;
  }

  body.ui-v5.packet-authenticated .console-sidebar {
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: auto;
    grid-template-areas: "nav foot";
    gap: 6px;
    min-height: 52px;
    padding: 5px 6px;
  }

  body.ui-v5.packet-authenticated .console-brand,
  body.ui-v5.packet-authenticated .console-sidebar-identity {
    display: none;
  }

  body.ui-v5.packet-authenticated .console-nav-list {
    grid-area: nav;
    min-height: 42px;
    height: 42px;
    padding: 0 2px 2px 0;
  }

  body.ui-v5.packet-authenticated .console-nav {
    min-width: 104px;
    min-height: 40px;
    height: 40px;
    padding: 4px 7px;
  }

  body.ui-v5.packet-authenticated .console-nav-icon {
    width: 23px;
    height: 23px;
  }

  body.ui-v5.packet-authenticated .console-sidebar-foot {
    grid-area: foot;
    display: flex;
    align-items: center;
    min-height: 42px;
    padding: 0;
    border: 0;
    background: transparent;
  }

  body.ui-v5.packet-authenticated .console-sidebar-foot .newapi-button.danger {
    width: auto;
    min-width: 72px;
    min-height: 40px;
    padding: 6px 9px;
  }

  body.ui-v5 .console-content {
    gap: 6px;
    padding: 6px;
  }

  body.ui-v5 .packet-route-ribbon {
    min-height: 34px;
    padding: 4px 8px;
  }

  body.ui-v5 .dashboard-overview .newapi-card-head {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 8px;
    min-height: 64px;
    padding: 8px 10px;
  }

  body.ui-v5 .dashboard-overview .newapi-card-head > div:first-child {
    min-width: 0;
  }

  body.ui-v5 .newapi-account-title-row,
  body.ui-v5 .dashboard-overview .newapi-card-title {
    min-width: 0;
  }

  body.ui-v5 .dashboard-overview .newapi-card-title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  body.ui-v5 .display-name-policy-note {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  body.ui-v5 .dashboard-head-actions {
    width: auto;
    display: flex;
    flex-wrap: nowrap;
    gap: 6px;
  }

  body.ui-v5 .dashboard-head-actions .newapi-status-pill {
    width: auto;
    min-width: 104px;
    min-height: 34px;
  }

  body.ui-v5 .dashboard-overview > .newapi-card-body {
    grid-template-rows: auto auto auto auto;
    gap: 8px;
    padding: 9px 10px;
  }

  body.ui-v5 .dashboard-usage-metrics,
  body.ui-v5 .dashboard-state-metrics {
    gap: 7px;
  }

  body.ui-v5 .dashboard-usage-metrics .newapi-metric,
  body.ui-v5 .dashboard-state-metrics .newapi-metric {
    min-height: 62px;
    padding: 7px 9px;
  }

  body.ui-v5 .dashboard-hourly-card {
    min-height: 0;
    padding: 7px;
  }

  body.ui-v5 .dashboard-hourly-head {
    flex-direction: row;
    align-items: center;
    gap: 6px;
  }

  body.ui-v5 .dashboard-hourly-total {
    flex: 0 1 auto;
    justify-content: flex-end;
    max-width: 52%;
    min-width: 0;
    margin-left: auto;
    text-align: right;
  }
}

@media (min-width: 640px) and (max-width: 980px) {
  body.ui-v5 .dashboard-usage-metrics,
  body.ui-v5 .dashboard-state-metrics {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  body.ui-v5 .dashboard-usage-metrics .newapi-metric strong {
    font-size: clamp(14px, 2.2vw, 18px);
  }
}

@media (max-width: 639px) {
  body.ui-v5 .dashboard-usage-metrics,
  body.ui-v5 .dashboard-state-metrics {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  body.ui-v5 .display-name-policy-note {
    display: none;
  }

  body.ui-v5 .dashboard-overview .usage-update-stamp {
    margin-top: 3px;
    padding: 3px 6px;
  }

  body.ui-v5 .dashboard-head-actions .newapi-status-pill {
    min-width: 98px;
  }

  body.ui-v5 .dashboard-hourly-head {
    flex-direction: row;
    align-items: center;
  }

  body.ui-v5 .dashboard-hourly-total {
    justify-content: flex-end;
    max-width: 48%;
    text-align: right;
  }

  body.ui-v5 .dashboard-overview .newapi-card-head:has(.display-name-inline-editor:not([hidden])) {
    grid-template-columns: minmax(0, 1fr);
    align-items: stretch;
  }

  body.ui-v5 .dashboard-overview .newapi-card-head:has(.display-name-inline-editor:not([hidden]))
    > div:first-child {
    width: 100%;
  }

  body.ui-v5 .dashboard-overview .newapi-card-head:has(.display-name-inline-editor:not([hidden]))
    .dashboard-head-actions {
    justify-content: flex-end;
  }

  body.ui-v5 .display-name-inline-editor:not([hidden]) {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto auto;
    gap: 6px;
  }

  body.ui-v5 .display-name-inline-editor .newapi-input {
    width: 100%;
    min-width: 0;
  }

  body.ui-v5 .display-name-inline-editor .newapi-button,
  body.ui-v5 .display-name-inline-editor .newapi-button.slim {
    width: auto;
    min-width: 52px;
    padding-inline: 8px;
  }
}

/* Short devices get a true telemetry-first composition. */
@media (max-width: 980px) and (max-height: 620px) {
  body.ui-v5.packet-authenticated .topbar {
    min-height: 51px;
    grid-template-rows: 48px;
  }

  body.ui-v5.packet-authenticated .title-copy {
    height: 48px;
    min-height: 48px;
    padding-block: 4px;
  }

  body.ui-v5.packet-authenticated .view-switch {
    min-height: 36px;
    height: 36px;
  }

  body.ui-v5.packet-authenticated .view-tab {
    min-height: 28px;
  }

  body.ui-v5.packet-authenticated .console-sidebar {
    min-height: 48px;
    padding-block: 3px;
  }

  body.ui-v5.packet-authenticated .console-nav-list,
  body.ui-v5.packet-authenticated .console-sidebar-foot {
    min-height: 40px;
    height: 40px;
  }

  body.ui-v5.packet-authenticated .console-nav,
  body.ui-v5.packet-authenticated .console-sidebar-foot .newapi-button.danger {
    min-height: 38px;
    height: 38px;
  }

  body.ui-v5 .packet-route-ribbon {
    min-height: 30px;
    padding-block: 2px;
  }

  body.ui-v5 .dashboard-overview .newapi-card-head {
    min-height: 56px;
    padding-block: 6px;
  }

  body.ui-v5 .newapi-account-title-row {
    min-height: 30px;
    gap: 6px;
  }

  body.ui-v5 .newapi-account-title-row .newapi-card-title {
    min-height: 28px;
    font-size: 16px;
  }

  body.ui-v5 .dashboard-overview .usage-update-stamp {
    min-height: 17px;
    margin-top: 2px;
    padding: 2px 6px;
  }

  body.ui-v5 .display-name-policy-note {
    display: none;
  }

  body.ui-v5 .dashboard-overview > .newapi-card-body {
    gap: 6px;
    padding-block: 7px;
  }

  body.ui-v5 .dashboard-usage-metrics .newapi-metric,
  body.ui-v5 .dashboard-state-metrics .newapi-metric {
    min-height: 56px;
    padding-block: 6px;
  }

  body.ui-v5 .dashboard-hourly-card {
    gap: 4px;
    padding: 6px;
  }

  body.ui-v5 .hourly-chart {
    min-height: 140px;
  }

  body.ui-v5 .hourly-svg {
    height: 140px;
  }
}

body.ui-v5.packet-static .dashboard-overview::before,
body.ui-v5.packet-static .dashboard-hourly-card::before,
body.ui-v5.packet-static .hourly-svg-bar,
body.ui-v5.packet-static .console-nav.active::after,
body.ui-v5.packet-static .console-nav[aria-selected="true"]::after,
body.ui-v5.packet-static .packet-route-ribbon.is-routing .packet-route-track > i::after,
body.ui-v5.packet-motion-paused .dashboard-overview::before,
body.ui-v5.packet-motion-paused .dashboard-hourly-card::before,
body.ui-v5.packet-motion-paused .hourly-svg-bar,
body.ui-v5.packet-motion-paused .console-nav.active::after,
body.ui-v5.packet-motion-paused .console-nav[aria-selected="true"]::after,
body.ui-v5.packet-motion-paused .packet-route-ribbon.is-routing .packet-route-track > i::after {
  animation: none !important;
  transform: none !important;
}

@media (prefers-reduced-motion: reduce) {
  body.ui-v5 .dashboard-overview::before,
  body.ui-v5 .dashboard-hourly-card::before,
  body.ui-v5 .hourly-svg-bar,
  body.ui-v5 .console-nav.active::after,
  body.ui-v5 .console-nav[aria-selected="true"]::after,
  body.ui-v5 .packet-route-ribbon.is-routing .packet-route-track > i::after {
    animation: none !important;
    transform: none !important;
  }
}
