/* Content must shrink with its container, including long addresses and names. */
html, body { width: 100%; min-width: 0; }
.wrap, .wrap > section, .card, .stat, .modal, .devmain,
.page-heading > div, .card-head > div, .tool .title > div,
.topbar-left, .status-lines, .auth-side, .auth-form { min-width: 0; }
.tools { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.tool .out, .devmeta, .devname, .kv, .mono, .modal .muted,
.account-mini b, .badge, .toast { overflow-wrap: anywhere; }
.devmain { flex: 1; }
.tool-history, #adUserList, #adAudit, #devList { overscroll-behavior: contain; }
.sidebar { max-width: calc(100vw - 52px); overflow-y: auto; overscroll-behavior: contain; }
.account-mini > div:last-child { min-width: 0; }
.account-mini b { display: block; }
.topbar-left { flex: 1; }
.topbar-right { flex-shrink: 0; }
.topbar h1 { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.qrbox { max-width: 100%; }
.qrbox img { max-width: 100%; height: auto; aspect-ratio: 1; }
.modal { overscroll-behavior: contain; overflow-wrap: anywhere; }
.modal input, .modal select { max-width: 100%; }
.modal .devname { white-space: normal; }
body.mobile-nav-open, body:has(.overlay:not(.auth-overlay).open) { overflow: hidden; }

@media (max-width: 960px) {
  .sidebar { height: 100dvh; padding-bottom: max(20px, env(safe-area-inset-bottom)); }
  .mobile-menu { flex-shrink: 0; }
  .topbar-breadcrumb, .topbar-divider { display: none; }
  .wrap { width: 100%; }
}

@media (max-width: 760px) {
  .tools, .overview-grid { grid-template-columns: minmax(0, 1fr); }
  .auth-layout { grid-template-columns: minmax(0, 1fr); }
  .auth-art { display: none; }
  .auth-side { min-height: 100dvh; }
}

@media (max-width: 650px) {
  /* Put the primary action on its own row; keep the title and utility icons readable. */
  header.topbar {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 40px 40px;
    gap: 8px;
    height: auto;
    padding: max(10px, env(safe-area-inset-top)) 14px 10px;
  }
  .topbar-left { grid-column: 1; grid-row: 1; gap: 8px; }
  .topbar h1 { font-size: 15px; }
  .topbar-right { display: contents; }
  .topbar-right .theme-toggle { grid-column: 2; grid-row: 1; width: 40px; height: 40px; padding: 8px; }
  #refreshBtn { grid-column: 3; grid-row: 1; }
  #addBtn { grid-column: 1 / -1; grid-row: 2; width: 100%; min-height: 42px; font-size: 13px; }
  .topbar .round-action { width: 40px; height: 40px; }
  .wrap { padding: 22px 14px 30px; }
  .page-heading { flex-wrap: wrap; gap: 10px; align-items: flex-start; }
  .page-heading > div:first-child { flex: 1 1 180px; }
  .page-heading h2 { font-size: 24px; line-height: 1.25; }
  .page-heading p { max-width: none; font-size: 12px; }
  .page-heading .period-chip { margin-top: 0; flex-shrink: 0; white-space: nowrap; }
  .stats { gap: 10px; }
  .stat { padding: 14px 12px; }
  .stat .lbl { gap: 6px; align-items: flex-start; }
  .stat .val { font-size: clamp(22px, 6vw, 28px); overflow-wrap: anywhere; }
  .card { padding: 16px; }
  .card-head { flex-wrap: wrap; align-items: flex-start; }
  .chart-card .card-head > div { flex: 1 1 155px; }
  .chart-filter { min-height: 36px; }
  .chart-foot { flex-wrap: wrap; }
  .status-card .status-content { gap: 18px; }
  .status-lines { max-width: none; }
  .status-line span { min-width: 0; }
  .section-heading { display: block; }
  .section-heading span { display: block; margin-top: 4px; font-size: 11px; }
  .tool .head { align-items: stretch; flex-direction: column; gap: 14px; }
  .tool .title { flex-wrap: nowrap; }
  .tool .head button { width: 100%; min-height: 42px; margin-left: 0; font-size: 12px; }
  .tool .out, .tool .muted { font-size: 12px; }
  .tool-history:has(.devrow) { max-height: none; overflow: visible; }
  .kv { gap: 8px; }
  .kv .mono { flex: 1 1 100%; }
  .devrow { flex-wrap: wrap; padding: 12px; gap: 10px; }
  .devrow .devmain { flex: 1 1 100%; }
  .devrow .devname { white-space: normal; overflow: visible; font-size: 13px; }
  .devrow > div:last-child:not(.devmain) { flex-wrap: wrap; width: 100%; }
  .devrow button { min-height: 38px; }
  .sms-order-actions { justify-content: flex-start; }
  .sms-order-actions button { flex: 1 1 120px; white-space: normal; }
  .sms-code-row { gap: 10px; }
  .sms-code-row .code { font-size: 24px; }
}

@media (max-width: 960px) {
  /* Render the existing account table as labelled cards, preserving every action. */
  .table-card { padding: 0; }
  .table-card .card-head { padding: 16px; gap: 12px; }
  #checkAllBtn { min-height: 40px; }
  .table-filters { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; padding: 12px 16px; }
  .table-search { width: 100%; min-width: 0; }
  .table-filter { width: 100%; max-width: none; min-height: 42px; }
  .table-scroll { overflow: visible; }
  #accountsSection table { display: block; min-width: 0; width: 100%; }
  #accountsSection thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
  #rows { display: grid; gap: 12px; padding: 14px; }
  #rows tr { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; padding: 16px; border: 1px solid var(--line); border-radius: 9px; min-width: 0; }
  #rows td { display: block; padding: 0; border: 0; min-width: 0; font-size: 12px; overflow-wrap: anywhere; }
  #rows td::before { content: attr(data-label); display: block; margin-bottom: 4px; color: var(--dim); font-size: 10px; font-weight: 500; }
  #rows td:first-child, #rows td:last-child { grid-column: 1 / -1; }
  #rows td:first-child .mono { font-size: 16px; font-weight: 600; }
  #rows .badge { max-width: 100%; white-space: normal; }
  #rows .livedot { display: inline-block; }
  #rows .table-actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; justify-content: stretch; white-space: normal; }
  #rows .table-actions button, #rows a.dl { min-height: 40px; font-size: 11px; white-space: normal; padding: 8px; margin: 0; }
  #rows a.dl { display: flex; align-items: center; justify-content: center; border: 1px solid var(--line); border-radius: 6px; }
  .table-footer, .footer-note { flex-wrap: wrap; gap: 6px 14px; }
}

@media (max-width: 650px) {
  .overlay:not(.auth-overlay) { padding: 12px; }
  .modal { padding: 20px 16px; max-height: calc(100dvh - 24px); }
  .modal .row button { flex: 1 1 100px; min-height: 42px; }
  .qrbox { width: min(100%, 264px); padding: 12px; }
  .qrbox img { width: 100%; }
  #adminOverlay .stats { grid-template-columns: repeat(3, minmax(0, 1fr)) !important; gap: 8px; }
  #adminOverlay .stat { padding: 12px 8px; }
  #adminOverlay .stat .lbl { font-size: 10px; }
  #adminOverlay .stat .val { font-size: 24px; }
  #adNewUser, #adNewPass { flex-basis: 100% !important; min-width: 0 !important; }
  #adNewRole { flex: 1; }
  #adCreateBtn { flex: 1; min-height: 42px; }
  #adUserList, #devList { max-height: none !important; }
  .auth-side { padding: 24px 20px; }
  .auth-top { flex-wrap: wrap; gap: 12px; }
  .auth-form { margin: 48px auto; }
  .auth-footer { flex-wrap: wrap; gap: 8px; }
  input, select, .auth-form input, .table-search input, .table-filter { font-size: 16px; }
  .auth-note { text-align: center; }
  .toast { width: max-content; max-width: calc(100vw - 28px); bottom: max(18px, env(safe-area-inset-bottom)); }
}

@media (min-width: 700px) and (max-width: 960px) {
  #rows { grid-template-columns: repeat(2, minmax(0, 1fr)); align-items: start; }
}

@media (max-width: 360px) {
  .wrap { padding-left: 12px; padding-right: 12px; }
  .period-chip { font-size: 10px; }
  .stat .lbl .icon { display: none; }
  .status-card .status-content { gap: 12px; }
  .status-ring { width: 70px; height: 70px; }
}
