/* ============================================================
   FrigoSense Responsive Layout — visual PWA patch
   Current DOM: header .header-content + main aside.aside-panel
   ============================================================ */

#legacySensorSection,
#Analysis {
  display: none !important;
}

/* Header device block: keep only device selector, hide thresholds/settings chrome. */
header .aside-panel {
  width: auto;
  max-width: none;
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
  box-shadow: none;
}

header .aside-panel > h2,
header .aside-panel > h3[data-i18n="thresholds.title"],
header .aside-panel > h3[data-i18n="thresholds.title"] + .grid-container,
header .aside-panel .form-group.AWS,
header .aside-panel .btn-group {
  display: none !important;
}

header .aside-panel .form-group.Device {
  margin: 0;
}

header .aside-panel .OrgSelectorRow {
  margin: 0 0 4px;
}

header .aside-panel .OrgTitle[style*="display: none"],
header .aside-panel .OrgSelectorRow[style*="display: none"] {
  display: none !important;
}

header .aside-panel .DevTitle {
  margin: 0 0 2px;
  font-size: 0.68rem;
  line-height: 1;
  color: var(--neutral-text);
}

header .aside-panel .form-group.Device > .device-controls-row {
  display: flex;
  align-items: center;
  gap: 4px;
}

header .aside-panel select,
header .aside-panel #device_id {
  width: 118px;
  max-width: 118px;
  min-width: 0;
  height: 32px;
  padding: 4px 6px;
  font-size: 12px;
}

header .aside-panel #org_id {
  width: 180px;
  max-width: 180px;
}

header .aside-panel .tool_button {
  width: 32px;
  height: 32px;
  min-width: 32px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

#pushToggleBtn[data-compact="true"] {
  min-width: 34px;
  padding: 0.45rem 0.55rem;
  font-size: 0;
}

#pushToggleBtn[data-compact="true"]::before {
  content: "🔔";
  font-size: 14px;
  line-height: 1;
}

main > .chart-section {
  grid-row: auto !important;
  grid-column: auto !important;
}

main > .chart-section {
  display: grid !important;
  grid-template-columns: 1fr !important;
  gap: 0.85rem !important;
}

main > .chart-section > .chart-card {
  min-width: 0;
}

main > .chart-section .chart-container {
  min-width: 0;
}

main > #AiInsight,
main > #AiConciergeSection,
main > #B2BOpsSection,
main > #visualDiagnostics {
  grid-column: 1 / -1;
  width: 100%;
  max-width: none;
}

.section-card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.85rem;
}

.section-card-head h2 {
  margin: 0;
}

.section-card-head p {
  margin: 0.32rem 0 0;
  color: var(--neutral-text);
  line-height: 1.45;
}

.ai-concierge-section,
.b2b-ops-section {
  display: grid !important;
  gap: 0.9rem;
  border-radius: 0.65rem;
  border: 1px solid var(--border, rgba(148, 163, 184, 0.28));
  background: var(--panel, rgba(255, 255, 255, 0.88));
  box-shadow: 0 14px 38px rgba(15, 23, 42, 0.08);
  padding: 1rem;
}

.b2b-ops-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.75rem;
  margin-top: 0.9rem;
}

.b2b-ops-item {
  border-radius: 0.55rem;
  border: 1px solid var(--border, rgba(148, 163, 184, 0.22));
  background: rgba(255, 255, 255, 0.62);
  padding: 0.82rem;
}

.b2b-ops-item strong,
.b2b-ops-item span {
  display: block;
}

.b2b-ops-item span {
  margin-top: 0.3rem;
  color: var(--neutral-text);
  line-height: 1.42;
}

@media (max-width: 767px) {
  main > #visualMain {
    order: 1 !important;
  }

  main > .chart-section {
    order: 2 !important;
  }

  main > #AiInsight {
    grid-column: 1 / -1 !important;
    order: 3 !important;
    width: 100%;
    max-width: none !important;
  }

  main > #AiConciergeSection {
    grid-column: 1 / -1 !important;
    order: 4 !important;
    width: 100%;
    max-width: none !important;
  }

  main > #B2BOpsSection {
    grid-column: 1 / -1 !important;
    order: 5 !important;
    width: 100%;
    max-width: none !important;
  }

  main > #visualDiagnostics {
    grid-column: 1 / -1 !important;
    order: 6 !important;
    width: 100%;
    max-width: none !important;
    margin-top: 0.25rem !important;
  }

  .section-card-head {
    display: grid;
    grid-template-columns: 1fr;
  }

  .section-card-head .dashboard-concierge-btn {
    width: 100%;
  }

  .b2b-ops-grid {
    grid-template-columns: 1fr;
  }
}

.visual-main .bottom-grid,
.visual-diagnostics .bottom-grid {
  display: grid !important;
  gap: 8px !important;
}

#visualDiagnostics .panel,
#visualDiagnostics .panel-head,
#visualDiagnostics .collapsible-body {
  min-width: 0;
}

#visualDiagnostics .panel {
  transition: padding 0.18s ease, background-color 0.18s ease;
}

#visualDiagnostics .panel-compact .panel-head h3 {
  overflow: hidden;
}

#visualDiagnostics .panel-compact .panel-head h3 span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.visual-diagnostics-label,
.visual-diagnostics-grid {
  grid-column: 1 / -1 !important;
  width: 100%;
}

.visual-diagnostics-label {
  margin-top: 1rem !important;
}

.visual-diagnostics-grid {
  display: grid !important;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 260px), 1fr)) !important;
  gap: 0.75rem !important;
  align-items: stretch;
}

.visual-diagnostics-grid .panel {
  margin-top: 0 !important;
  height: 100%;
}

.visual-diagnostics-grid .collapsible[data-collapsed="true"] .collapsible-body {
  display: none !important;
}

.visual-diagnostics-grid .collapsible[data-collapsed="false"] .panel-head .chev {
  transform: rotate(180deg);
}

.visual-diagnostics-grid .panel-head h3 {
  display: grid !important;
  grid-template-columns: 28px minmax(0, 1fr);
  align-items: center;
  gap: 0.45rem;
  min-height: 34px;
}

.visual-diagnostics-grid .panel-head-icon {
  width: 28px;
  height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.15rem;
  line-height: 1;
  flex: 0 0 28px;
}

.visual-diagnostics-grid .panel-head h3 > span:not(.panel-head-icon) {
  min-width: 0;
  line-height: 1.12;
}

@media (min-width: 768px) {
  .visual-main .bottom-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
  }

  .visual-diagnostics .bottom-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
  }

  .visual-diagnostics-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
  }

  .visual-main .bottom-grid .panel--wide {
    grid-column: auto !important;
  }

  .visual-diagnostics .bottom-grid .panel--wide {
    grid-column: auto !important;
  }

  .visual-diagnostics-grid .panel--wide {
    grid-column: auto !important;
  }
}

@media (min-width: 640px) {
  main {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  main > .chart-section {
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 280px), 1fr)) !important;
    gap: 1rem !important;
  }

  main > .chart-section .chart-card > .in {
    padding: 1rem;
  }

  main > .chart-section .chart-container {
    height: 280px;
  }

  body {
    padding-top: 0.75rem;
  }

  header {
    display: grid;
    grid-template-columns: minmax(220px, auto) minmax(320px, 1fr) auto;
    align-items: center;
    gap: 8px;
    margin-bottom: 1rem;
  }

  header .header-content {
    min-width: 0;
    gap: 0.55rem;
  }

  header .header-content:first-child {
    justify-content: flex-start;
  }

  header .header-content:last-of-type {
    justify-content: flex-end;
    flex-wrap: nowrap;
  }

  header h1 {
    margin: 0;
    font-size: 1.25rem;
    line-height: 1.1;
    white-space: nowrap;
  }

  .last-update {
    font-size: 0.85rem;
    white-space: nowrap;
  }

  .battery-indicator {
    min-width: 58px;
    padding: 0.28rem 0.38rem;
    font-size: 0.75rem;
  }

  .theme-toggle-btn,
  #loginBtn,
  #logoutBtn {
    min-height: 32px;
    padding: 0.38rem 0.5rem;
    font-size: 0.82rem;
  }

  #langSelect {
    width: 7rem;
    min-width: 7rem;
  }
}

@media (min-width: 640px) and (max-width: 1439px) {
  header {
    grid-template-columns: 1fr !important;
    grid-template-rows: auto auto auto;
    align-items: stretch;
    gap: 0.45rem;
    margin-bottom: 0.8rem;
  }

  header .header-content {
    width: 100%;
  }

  header .header-content:first-child {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 0.55rem;
  }

  header .header-content:nth-child(2) {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    flex-wrap: wrap;
    gap: 0.45rem;
  }

  header h1 {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .conn-status {
    white-space: nowrap;
    font-size: 0.82rem;
  }

  .last-update {
    flex: 1 1 240px;
    min-width: 0;
    font-size: 0.78rem;
    color: var(--neutral-text);
    overflow: hidden;
    text-overflow: ellipsis;
  }

  #langSelect {
    width: 7.2rem;
    min-width: 7.2rem;
    height: 34px;
  }

  #logoutBtn,
  #loginBtn {
    min-height: 34px;
    height: 34px;
  }

  header .aside-panel {
    min-width: 0;
    flex: 1 1 320px;
    order: 20;
  }

  header .aside-panel .DevTitle {
    display: none;
  }

  header .aside-panel .form-group.Device > .device-controls-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 32px;
    gap: 0.32rem;
    width: 100%;
  }

  header .aside-panel select,
  header .aside-panel #device_id {
    width: 100%;
    max-width: none;
    height: 34px;
    font-size: 0.82rem;
  }

  header .aside-panel .tool_button {
    width: 32px;
    height: 32px;
    min-width: 32px;
  }
}

@media (min-width: 640px) and (max-width: 899px) {
  .last-update {
    flex-basis: 100%;
    order: 1;
  }

  .battery-indicator,
  #themeToggleBtn,
  #pwaInstallBtn,
  #helpBtn,
  #langSelect,
  #loginBtn,
  #logoutBtn,
  #pushToggleBtn {
    order: 2;
  }
}

@media (min-width: 1024px) {
  main {
    display: grid !important;
    grid-template-columns: minmax(420px, 0.95fr) minmax(420px, 1.05fr) !important;
    gap: 1rem !important;
    align-items: start;
  }

  main > #visualMain {
    grid-column: 1 !important;
    width: 100%;
    max-width: none !important;
    margin: 0 !important;
  }

  main > .chart-section {
    grid-column: 2 !important;
    width: 100%;
    min-width: 0;
    grid-template-columns: 1fr !important;
    gap: 0.85rem !important;
  }

  main > .chart-section .chart-card > .in {
    padding: 0.9rem;
  }

  main > .chart-section .chart-card h2 {
    font-size: clamp(1rem, 1.4vw, 1.35rem);
    line-height: 1.15;
    overflow-wrap: anywhere;
  }

  main > .chart-section .chart-container {
    height: clamp(210px, 24vh, 300px);
  }

  #visualMain .top-grid {
    grid-template-columns: minmax(180px, 230px) minmax(0, 1fr) !important;
    gap: 1rem !important;
    align-items: start !important;
  }

  #visualMain .col-state {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 0.8rem;
  }

  #visualMain .fresh-hero {
    padding: 0.15rem 0 0;
  }

  #visualMain .gauge,
  #visualMain .gauge svg {
    width: 168px;
    height: 168px;
    margin-inline: auto;
  }

  #visualMain .gauge-emoji {
    font-size: 2rem;
  }

  #visualMain .gauge-state {
    font-size: 1.15rem;
  }

  #visualMain .fresh-sub {
    max-width: 250px;
    font-size: 0.82rem;
  }

  #visualMain .outlook {
    margin-top: 0;
    padding: 0.68rem 0.72rem;
  }

  #visualMain .outlook-row {
    align-items: flex-start;
  }

  #visualMain .tile {
    padding: 0.55rem 0.65rem;
  }

  #visualMain .minigauge {
    width: 42px;
    height: 42px;
    flex-basis: 42px;
  }

  #visualMain .minigauge svg {
    width: 42px;
    height: 42px;
  }

  #visualMain .bottom-grid {
    grid-column: 1 / -1;
  }

  #visualMain .actions {
    margin-top: 0.55rem;
  }

  #visualDiagnostics {
    width: 100%;
    max-width: none;
    margin: 0;
  }

  #visualDiagnostics .sec-label {
    margin: 0.1rem 0 0.35rem;
    font-size: 0.62rem;
    letter-spacing: 0.04em;
  }

  #visualDiagnostics .bottom-grid {
    grid-template-columns: 1fr !important;
    gap: 0.38rem !important;
  }

  #visualDiagnostics .panel {
    padding: 0.5rem 0.6rem;
    border-radius: 0.55rem;
  }

  #visualDiagnostics .panel-head h3 {
    font-size: 0.72rem;
    line-height: 1.15;
  }

  .visual-diagnostics-label {
    margin-top: 0.25rem !important;
  }
}

@media (min-width: 768px) and (max-width: 1439px) {
  main {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) !important;
    gap: 1rem !important;
    align-items: stretch;
  }

  main > #visualMain,
  main > .chart-section,
  main > #AiInsight,
  main > #visualDiagnostics {
    grid-column: 1 / -1 !important;
    width: 100%;
    max-width: none !important;
    margin-left: 0 !important;
    margin-right: 0 !important;
  }

  main > #visualMain {
    order: 1;
  }

  main > .chart-section {
    order: 2;
  }

  main > #AiInsight {
    order: 3;
  }

  main > #visualDiagnostics {
    order: 4;
  }

  main > .chart-section {
    display: grid !important;
    grid-template-columns: 1fr !important;
    gap: 1rem !important;
  }

  main > .chart-section .chart-container {
    height: clamp(260px, 34vh, 380px);
  }

  main > #visualDiagnostics {
    margin-top: 0.15rem !important;
  }

  #visualDiagnostics .sec-label {
    margin: 0.15rem 0 0.45rem;
    font-size: 0.72rem;
  }

  #visualDiagnostics .bottom-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
    gap: 0.65rem !important;
  }

  #visualDiagnostics.has-open.active-service .bottom-grid {
    grid-template-columns: minmax(320px, 2.2fr) minmax(120px, 0.7fr) minmax(120px, 0.7fr) !important;
  }

  #visualDiagnostics.has-open.active-technical .bottom-grid {
    grid-template-columns: minmax(120px, 0.7fr) minmax(390px, 2.4fr) minmax(120px, 0.7fr) !important;
  }

  #visualDiagnostics.has-open.active-json .bottom-grid {
    grid-template-columns: minmax(120px, 0.7fr) minmax(120px, 0.7fr) minmax(430px, 2.6fr) !important;
  }

  #visualDiagnostics.has-open .panel-compact {
    padding-left: 0.58rem;
    padding-right: 0.58rem;
  }

  #visualDiagnostics .panel {
    min-height: 44px;
    padding: 0.65rem 0.75rem;
  }

  #visualDiagnostics .panel-head h3 {
    font-size: 0.82rem;
  }
}

@media (min-width: 900px) and (max-width: 1439px) {
  #visualMain .top-grid {
    grid-template-columns: minmax(180px, 250px) minmax(0, 1fr) !important;
    gap: 1rem !important;
    align-items: stretch !important;
  }

  #visualMain .col-state,
  #visualMain .col-env {
    min-height: 0;
  }
}

@media (min-width: 1440px) {
  main {
    grid-template-columns: minmax(620px, 0.92fr) minmax(720px, 1.08fr) !important;
    align-items: stretch;
  }

  main > #visualMain {
    display: block;
    align-self: stretch;
  }

  main > #visualMain .top-grid,
  main > .chart-section > .chart-card {
    min-height: 400px;
  }

  main > #visualMain .top-grid {
    height: 100%;
    align-items: stretch !important;
    overflow: hidden;
  }

  main > #visualMain.diag-technical-open .top-grid,
  main > #visualMain.diag-json-open .top-grid {
    grid-template-columns: minmax(360px, 420px) minmax(0, 1fr) !important;
  }

  main > #visualMain.diag-service-open .top-grid {
    grid-template-columns: minmax(300px, 340px) minmax(0, 1fr) !important;
  }

  #visualMain .col-state,
  #visualMain .col-env {
    min-height: 400px;
    max-height: 400px;
    min-width: 0;
  }

  #visualMain .col-state {
    overflow: hidden;
  }

  #visualMain.diag-open .col-state {
    gap: 0.5rem;
  }

  #visualMain.diag-open .fresh-hero {
    padding-top: 0;
  }

  #visualMain.diag-open .gauge,
  #visualMain.diag-open .gauge svg {
    width: 104px;
    height: 104px;
  }

  #visualMain.diag-open .gauge-state {
    font-size: 0.95rem;
    max-width: 112px;
  }

  #visualMain.diag-open .gauge-emoji {
    font-size: 1.25rem;
  }

  #visualMain.diag-open .outlook {
    display: none;
  }

  #visualMain.diag-open .outlook-h {
    font-size: 0.86rem;
  }

  #visualDiagnostics {
    min-height: 0;
  }

  #visualDiagnostics .panel-active {
    display: flex;
    flex-direction: column;
    min-height: 0;
  }

  #visualDiagnostics .panel-active .collapsible-body {
    min-height: 0;
    max-height: 150px;
    overflow: auto;
    padding-right: 0.15rem;
    overscroll-behavior: contain;
  }

  #visualDiagnostics .tech-note {
    margin: 0.45rem 0 0.55rem;
    padding: 0.55rem 0.62rem;
    font-size: 0.78rem;
  }

  #visualDiagnostics .kv {
    grid-template-columns: minmax(92px, auto) minmax(0, 1fr);
    gap: 0.24rem 0.55rem;
    font-size: 0.76rem;
  }

  #visualDiagnostics table.tech {
    font-size: 0.72rem;
  }

  #visualDiagnostics table.tech th,
  #visualDiagnostics table.tech td {
    padding: 0.25rem 0.3rem;
  }

  #visualDiagnostics pre.json {
    max-height: 176px;
    font-size: 0.68rem;
  }

  #visualMain .col-env {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) minmax(58px, auto) 40px;
    gap: 0.58rem;
  }

  #visualMain .col-env .sec-label {
    margin-bottom: 0;
  }

  #visualMain .col-env .env-grid {
    align-content: stretch;
    gap: 0.58rem;
  }

  #visualMain .col-env .tile {
    min-height: 0;
  }

  #visualMain .col-env .ai-line {
    min-height: 58px;
    margin-top: 0;
    align-items: center;
  }

  #visualMain .col-env .actions {
    margin-top: 0;
  }

  main > .chart-section {
    grid-template-columns: repeat(3, minmax(220px, 1fr)) !important;
    align-items: stretch;
    align-self: stretch;
  }

  main > .chart-section > .chart-card {
    display: flex;
    height: 100%;
  }

  main > .chart-section .chart-card > .in {
    width: 100%;
    display: flex;
    flex-direction: column;
  }

  main > .chart-section .chart-container {
    flex: 1 1 auto;
    min-height: 0;
    height: auto;
  }
}

@media (max-width: 639px) {
  main > .chart-section {
    grid-template-columns: 1fr !important;
  }

  body {
    padding: 0.55rem 0.65rem 0.65rem;
    padding-top: max(1.25rem, calc(env(safe-area-inset-top, 0px) + 0.85rem));
  }

  header {
    display: grid;
    grid-template-rows: auto auto;
    align-items: stretch;
    gap: 0.38rem;
    margin-bottom: 0.55rem;
  }

  header .header-content {
    gap: 0.42rem;
    flex-wrap: wrap;
  }

  header .header-content:first-child {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
  }

  header .header-content:last-of-type {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto auto auto auto;
    align-items: center;
  }

  header h1 {
    margin: 0;
    min-width: 0;
    font-size: 1.08rem;
    line-height: 1.15;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .conn-status,
  .last-update {
    font-size: 0.74rem;
  }

  .conn-status {
    gap: 0.22rem;
  }

  .conn-dot {
    margin-right: 0.22rem;
  }

  .last-update {
    grid-column: 1 / -1;
    width: auto;
    order: -1;
    color: var(--neutral-text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .battery-indicator {
    min-width: 62px;
    min-height: 32px;
    padding: 0.22rem 0.34rem;
    font-size: 0.72rem;
  }

  .theme-toggle-btn,
  #loginBtn,
  #logoutBtn {
    width: 34px;
    min-width: 34px;
    min-height: 32px;
    padding: 0;
    border-radius: 999px;
    font-size: 0.82rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  #langSelect {
    display: none !important;
  }

  header #loginBtn,
  header #logoutBtn,
  header #pushToggleBtn[data-compact="true"] {
    font-size: 0 !important;
    color: transparent;
  }

  header #loginBtn::before {
    content: "\f2f6";
    font: var(--fa-font-solid);
    font-size: 0.9rem;
    color: #fff;
  }

  header #logoutBtn::before {
    content: "\f2f5";
    font: var(--fa-font-solid);
    font-size: 0.9rem;
    color: #fff;
  }

  header #pushToggleBtn[data-compact="true"] {
    width: 34px;
    min-width: 34px;
    min-height: 32px;
    padding: 0;
  }

  header #pushToggleBtn[data-compact="true"]::before {
    font-size: 0.9rem;
    color: #fff;
  }

  header .aside-panel {
    justify-self: stretch;
  }

  header .aside-panel .form-group.Device {
    display: grid;
    grid-template-columns: 1fr;
    align-items: center;
    gap: 6px;
  }

  header .aside-panel .DevTitle {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
  }

  header .aside-panel .form-group.Device > .device-controls-row {
    width: 100%;
    display: grid;
    grid-template-columns: minmax(0, 1fr) 34px;
    gap: 4px;
  }

  header .aside-panel select,
  header .aside-panel #device_id {
    width: 100%;
    max-width: none;
    height: 36px;
    font-size: 0.85rem;
  }

  header .aside-panel #device_ping_status,
  header .aside-panel #enter_new_device_id {
    display: none !important;
  }

  header .aside-panel .tool_button {
    width: 34px;
    height: 34px;
    min-width: 34px;
    padding: 0;
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
  }

  #visualMain {
    margin-top: 0.25rem;
  }

  #visualMain .gauge,
  #visualMain .gauge svg {
    width: 156px;
    height: 156px;
  }

  #visualMain .fresh-hero {
    padding-top: 0;
  }

  #visualMain .outlook {
    margin-top: 0.45rem;
    padding: 0.7rem 0.78rem;
  }
}

@media (max-width: 379px) {
  body {
    padding: 0.5rem;
    padding-top: max(1.35rem, calc(env(safe-area-inset-top, 0px) + 0.8rem));
  }

  header {
    gap: 5px;
  }

  header h1 {
    font-size: 1.05rem;
  }

  .theme-toggle-btn,
  #loginBtn,
  #logoutBtn {
    min-height: 28px;
    padding: 0.28rem 0.38rem;
    font-size: 0.72rem;
  }

  #pushToggleBtn[data-compact="true"] {
    min-width: 30px;
    padding: 0.32rem 0.42rem;
  }

  header .aside-panel .tool_button {
    width: 28px;
    height: 28px;
    min-width: 28px;
  }

  header .aside-panel select,
  header .aside-panel #device_id {
    height: 28px;
    font-size: 11px;
  }

  .visual-main .bottom-grid {
    grid-template-columns: 1fr !important;
  }
}

@media (max-width: 639px) {
  header .header-content:last-of-type {
    grid-template-columns: minmax(0, 1fr) auto auto auto auto auto auto !important;
  }

  #langSelect.theme-toggle-btn {
    display: inline-block !important;
    width: 44px !important;
    min-width: 44px !important;
    max-width: 44px !important;
    height: 32px !important;
    min-height: 32px !important;
    padding: 0 0.2rem !important;
    border-radius: 999px !important;
    font-size: 0.68rem !important;
    line-height: 1 !important;
    text-align: center;
  }

  #pwaInstallBtn.theme-toggle-btn {
    display: inline-flex;
    width: 34px;
    min-width: 34px;
  }
}

/* 20260706-mobilefull1: mobile controls should occupy the full viewport width. */
@media (max-width: 639px) {
  header,
  header .header-content,
  header + main,
  main > .aside-panel,
  header .aside-panel,
  header .aside-panel .form-group.Device {
    width: 100% !important;
    max-width: none !important;
    min-width: 0 !important;
    justify-self: stretch !important;
  }

  header .header-content:last-of-type {
    display: flex !important;
    width: 100% !important;
    flex-wrap: wrap !important;
    align-items: center !important;
    justify-content: flex-start !important;
    gap: 0.48rem !important;
  }

  header .header-content:last-of-type .last-update {
    flex: 1 0 100% !important;
    max-width: 100% !important;
  }

  header .aside-panel .form-group.Device > .device-controls-row {
    display: grid !important;
    width: 100% !important;
    grid-template-columns: minmax(0, 1fr) 38px !important;
    gap: 0.42rem !important;
    align-items: center !important;
  }

  header .aside-panel select,
  header .aside-panel #device_id {
    width: 100% !important;
    max-width: none !important;
    min-width: 0 !important;
  }

  header .aside-panel .tool_button {
    width: 38px !important;
    min-width: 38px !important;
    height: 38px !important;
  }
}

@media (max-width: 379px) {
  header .aside-panel .form-group.Device > .device-controls-row {
    grid-template-columns: minmax(0, 1fr) 32px !important;
    gap: 0.32rem !important;
  }

  header .aside-panel .tool_button {
    width: 32px !important;
    min-width: 32px !important;
    height: 32px !important;
  }
}

/* 20260706-mobiletop1: keep the first header row below iPhone status bar / Dynamic Island. */
@media (max-width: 639px) {
  body {
    padding-top: max(4.65rem, calc(env(safe-area-inset-top, 0px) + 1.65rem)) !important;
  }
}

@media (max-width: 379px) {
  body {
    padding-top: max(4.85rem, calc(env(safe-area-inset-top, 0px) + 1.75rem)) !important;
  }
}

/* 20260706-overlap1: prevent the upper dashboard modules from touching the next content row. */
@media (min-width: 1024px) {
  main {
    row-gap: 1.65rem !important;
  }

  main > #visualMain {
    margin-bottom: 0.9rem !important;
  }

  #visualMain .dashboard-ai-actions {
    padding-bottom: 0.25rem;
  }
}

@media (min-width: 1440px) {
  main > #visualMain .top-grid {
    min-height: 430px !important;
    height: auto !important;
    overflow: visible !important;
  }

  #visualMain .col-state,
  #visualMain .col-env {
    min-height: 430px !important;
    max-height: none !important;
  }

  #visualMain .col-env {
    grid-template-rows: auto minmax(0, 1fr) minmax(64px, auto) minmax(44px, auto) !important;
  }
}

/* 20260706-topcompact1: keep all header action controls in one compact iPhone row. */
@media (max-width: 639px) {
  header .header-content:last-of-type {
    display: flex !important;
    flex-wrap: nowrap !important;
    gap: 0.32rem !important;
    align-items: center !important;
    overflow-x: visible !important;
  }

  header .header-content:last-of-type > * {
    flex-grow: 0 !important;
    flex-shrink: 0 !important;
  }

  header .header-content:last-of-type .last-update {
    flex: 1 0 100% !important;
    order: -1 !important;
  }

  header .battery-indicator {
    flex-basis: 74px !important;
    width: 74px !important;
    min-width: 74px !important;
    max-width: 74px !important;
    height: 34px !important;
    min-height: 34px !important;
    padding: 0.2rem 0.32rem !important;
  }

  header .theme-toggle-btn,
  header #pwaInstallBtn.theme-toggle-btn,
  header #helpBtn.theme-toggle-btn,
  header #loginBtn,
  header #logoutBtn,
  header #pushToggleBtn {
    flex-basis: 34px !important;
    width: 34px !important;
    min-width: 34px !important;
    max-width: 34px !important;
    height: 34px !important;
    min-height: 34px !important;
    padding: 0 !important;
    border-radius: 999px !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    font-size: 0 !important;
    line-height: 1 !important;
    color: transparent !important;
  }

  header #langSelect.theme-toggle-btn {
    flex-basis: 44px !important;
    width: 44px !important;
    min-width: 44px !important;
    max-width: 44px !important;
    height: 34px !important;
    min-height: 34px !important;
    padding: 0 0.18rem !important;
    font-size: 0.62rem !important;
    color: inherit !important;
    text-align: center !important;
  }

  header #planBillingBtn {
    flex-basis: 48px !important;
    width: 48px !important;
    min-width: 48px !important;
    max-width: 48px !important;
    height: 34px !important;
    min-height: 34px !important;
    padding: 0 !important;
    border-radius: 10px !important;
    font-size: 0.62rem !important;
    line-height: 1 !important;
    white-space: nowrap !important;
  }

  header #loginBtn::before,
  header #logoutBtn::before,
  header #pushToggleBtn::before {
    color: #fff !important;
    font-size: 0.92rem !important;
    line-height: 1 !important;
  }

  header #pushToggleBtn::before {
    content: "🔔" !important;
  }

  header #themeToggleBtn i,
  header #pwaInstallBtn i,
  header #helpBtn i {
    font-size: 0.92rem !important;
    color: inherit !important;
  }

  header #pwaInstallBtn[hidden] {
    display: none !important;
  }
}

@media (max-width: 379px) {
  header .header-content:last-of-type {
    gap: 0.24rem !important;
  }

  header .battery-indicator {
    flex-basis: 68px !important;
    width: 68px !important;
    min-width: 68px !important;
    max-width: 68px !important;
  }

  header .theme-toggle-btn,
  header #pwaInstallBtn.theme-toggle-btn,
  header #helpBtn.theme-toggle-btn,
  header #loginBtn,
  header #logoutBtn,
  header #pushToggleBtn {
    flex-basis: 32px !important;
    width: 32px !important;
    min-width: 32px !important;
    max-width: 32px !important;
    height: 32px !important;
    min-height: 32px !important;
  }

  header #langSelect.theme-toggle-btn {
    flex-basis: 38px !important;
    width: 38px !important;
    min-width: 38px !important;
    max-width: 38px !important;
  }

  header #planBillingBtn {
    flex-basis: 42px !important;
    width: 42px !important;
    min-width: 42px !important;
    max-width: 42px !important;
    font-size: 0.58rem !important;
  }
}

/* 20260706-topcompact2: safe compact header row, no disappearing controls on iPhone. */
@media (max-width: 639px) {
  header .header-content:last-of-type {
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 0.28rem !important;
    align-items: center !important;
    justify-content: flex-start !important;
    overflow: visible !important;
  }

  header .header-content:last-of-type .last-update {
    flex: 0 0 100% !important;
    width: 100% !important;
    order: -1 !important;
  }

  header .battery-indicator {
    flex: 0 0 68px !important;
    width: 68px !important;
    min-width: 68px !important;
    max-width: 68px !important;
    height: 32px !important;
    min-height: 32px !important;
  }

  header .theme-toggle-btn,
  header #pwaInstallBtn.theme-toggle-btn,
  header #helpBtn.theme-toggle-btn,
  header #loginBtn,
  header #logoutBtn,
  header #pushToggleBtn {
    flex: 0 0 32px !important;
    width: 32px !important;
    min-width: 32px !important;
    max-width: 32px !important;
    height: 32px !important;
    min-height: 32px !important;
  }

  header #langSelect.theme-toggle-btn {
    flex: 0 0 40px !important;
    width: 40px !important;
    min-width: 40px !important;
    max-width: 40px !important;
    height: 32px !important;
    min-height: 32px !important;
    font-size: 0.58rem !important;
  }

  header #planBillingBtn {
    flex: 0 0 42px !important;
    width: 42px !important;
    min-width: 42px !important;
    max-width: 42px !important;
    height: 32px !important;
    min-height: 32px !important;
    font-size: 0.56rem !important;
  }

  header #pwaInstallBtn[hidden] {
    display: none !important;
  }
}

/* 20260706-iconfix1: compact header icons must stay visible after text hiding. */
@media (max-width: 639px) {
  header #themeToggleBtn i,
  header #pwaInstallBtn i,
  header #helpBtn i {
    color: #0b74ff !important;
    opacity: 1 !important;
    visibility: visible !important;
    font-size: 0.95rem !important;
  }

  header #themeToggleBtn #moonIcon {
    color: #0b74ff !important;
  }

  header #themeToggleBtn #sunIcon {
    color: #f5b301 !important;
  }

  header #pwaInstallBtn[hidden] {
    display: none !important;
  }
}

/* 20260708-ui85a1: single mobile-first dashboard hierarchy. */
main > #visualMain { order: 1 !important; }
main > .chart-section { order: 2 !important; }
main > #AiInsight { order: 3 !important; }
main > #AiConciergeSection { order: 4 !important; }
main > #B2BOpsSection { order: 5 !important; }
main > #visualDiagnostics { order: 6 !important; }

@media (max-width: 1023px) {
  main > #AiInsight,
  main > #AiConciergeSection,
  main > #B2BOpsSection,
  main > #visualDiagnostics {
    grid-column: 1 / -1 !important;
  }
}

/* 20260708-theme-sync1: modules follow the in-app html.dark toggle, not OS preference. */
:root {
  --fgs-module-bg: #ffffff;
  --fgs-module-panel: #f8fafc;
  --fgs-module-panel-soft: #f1f5f9;
  --fgs-module-border: rgba(15, 23, 42, 0.14);
  --fgs-module-text: #111827;
  --fgs-module-muted: #64748b;
  --fgs-module-accent: #4d8588;
  --fgs-module-accent-soft: rgba(77, 133, 136, 0.1);
  --fgs-module-shadow: 0 14px 38px rgba(15, 23, 42, 0.08);
}

html.dark {
  --fgs-module-bg: #262626;
  --fgs-module-panel: rgba(36, 36, 36, 0.96);
  --fgs-module-panel-soft: rgba(48, 48, 48, 0.98);
  --fgs-module-border: rgba(148, 163, 184, 0.28);
  --fgs-module-text: #f5f5f5;
  --fgs-module-muted: #b7c0cc;
  --fgs-module-accent: #9bc9cc;
  --fgs-module-accent-soft: rgba(155, 201, 204, 0.14);
  --fgs-module-shadow: 0 14px 38px rgba(0, 0, 0, 0.28);
}

.ai-concierge-section,
.b2b-ops-section,
.ai-chat-box {
  background: var(--fgs-module-bg) !important;
  color: var(--fgs-module-text) !important;
  border-color: var(--fgs-module-border) !important;
  box-shadow: var(--fgs-module-shadow) !important;
}

.ai-context-card,
.ai-concierge-card,
.ai-concierge-log,
.ai-concierge-note,
.ai-concierge-disclaimer,
.ai-chat-messages,
.b2b-ops-item {
  background: var(--fgs-module-panel) !important;
  color: var(--fgs-module-text) !important;
  border-color: var(--fgs-module-border) !important;
}

.ai-concierge-evidence-item {
  background: var(--fgs-module-panel-soft) !important;
  color: var(--fgs-module-text) !important;
}

.section-card-head p,
.ai-chat-head p,
.ai-chat-note,
.ai-context-lead,
.ai-context-disclaimer,
.ai-context-item span:not(.ai-context-icon),
.ai-concierge-device,
.ai-concierge-body,
.ai-concierge-note,
.ai-concierge-disclaimer,
.ai-concierge-evidence-item span,
.b2b-ops-item span {
  color: var(--fgs-module-muted) !important;
}

.section-card-head h2,
.ai-chat-head h2,
.ai-context-title,
.ai-context-item strong,
.ai-concierge-title-wrap h3,
.ai-concierge-section-title,
.ai-concierge-log-line,
.ai-concierge-evidence-item strong,
.b2b-ops-item strong {
  color: var(--fgs-module-text) !important;
}

.ai-concierge-audience {
  background: var(--fgs-module-accent-soft) !important;
  border-color: color-mix(in srgb, var(--fgs-module-accent) 42%, transparent) !important;
}

.ai-concierge-audience button,
.ai-concierge-copy,
.dashboard-concierge-btn {
  color: var(--fgs-module-accent) !important;
  border-color: color-mix(in srgb, var(--fgs-module-accent) 58%, transparent) !important;
}

.ai-concierge-audience button.is-active,
.dashboard-concierge-btn:hover,
.dashboard-concierge-btn:focus-visible,
.ai-concierge-copy:hover,
.ai-concierge-copy:focus-visible {
  background: var(--fgs-module-accent) !important;
  color: #071313 !important;
}

/* Native iOS shell has its own bottom tab bar outside the WebView. Keep the
   last dashboard tiles above that bar without adding extra space to browser PWA. */
body.NativeShell {
  padding-bottom: calc(8.75rem + env(safe-area-inset-bottom, 0px)) !important;
}

body.NativeShell main {
  padding-bottom: 1.5rem;
}
