/* ===== Phone-only UI (User-Agent) =====
   Applied only when html.is-mobile is set from a phone UA
   (server stamp + js/mobile-boot.js). Desktop and tablet UAs keep
   the desktop chrome — these rules must not leak to them.
   Override: ?device=mobile | ?device=desktop
*/

html:not(.is-mobile) .nav-toggle,
html:not(.is-mobile) .mobile-drawer,
html:not(.is-mobile) .mobile-tabbar {
  display: none !important;
}

html.is-mobile {
  --status-h: 56px;
  --mobile-tab-h: calc(56px + env(safe-area-inset-bottom, 0px));
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

html.is-mobile,
html.is-mobile body {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

html.is-mobile body {
  touch-action: manipulation;
}

/* Hide heavy ambient FX on phones */
html.is-mobile #matrix-canvas,
html.is-mobile #hex-canvas,
html.is-mobile .theme-fx,
html.is-mobile .vignette {
  display: none !important;
}

/* ----- Header ----- */
html.is-mobile .site-header,
html.is-mobile .status-bar {
  height: auto;
  min-height: var(--status-h);
  padding: 0.55rem 0.85rem;
  padding-top: calc(0.45rem + var(--safe-t));
  padding-left: calc(0.85rem + var(--safe-l));
  padding-right: calc(0.85rem + var(--safe-r));
  gap: 0.5rem;
  flex-wrap: nowrap;
}

html.is-mobile .top-nav {
  display: none;
}

html.is-mobile .brand-role {
  display: none;
}

html.is-mobile .brand-name {
  font-size: 0.98rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 52vw;
}

html.is-mobile .header-actions .theme-picker-label,
html.is-mobile .header-actions .theme-picker-caret {
  display: none;
}

html.is-mobile .header-actions #btn-admin,
html.is-mobile .header-actions #btn-logout,
html.is-mobile .header-actions #auth-badge {
  display: none !important;
}

html.is-mobile .theme-picker-btn {
  padding: 0.4rem;
  min-width: 40px;
  min-height: 40px;
  justify-content: center;
}

html.is-mobile .nav-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 42px;
  height: 42px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg-elevated);
  color: var(--text-bright);
  cursor: pointer;
  flex: 0 0 auto;
}

html.is-mobile .nav-toggle-bars,
html.is-mobile .nav-toggle-bars::before,
html.is-mobile .nav-toggle-bars::after {
  display: block;
  width: 16px;
  height: 2px;
  background: currentColor;
  border-radius: 2px;
  transition: transform 0.2s var(--ease), opacity 0.2s var(--ease);
  content: "";
}

html.is-mobile .nav-toggle-bars {
  position: relative;
}

html.is-mobile .nav-toggle-bars::before {
  position: absolute;
  top: -5px;
}

html.is-mobile .nav-toggle-bars::after {
  position: absolute;
  top: 5px;
}

html.is-mobile body.mobile-nav-open .nav-toggle-bars {
  background: transparent;
}

html.is-mobile body.mobile-nav-open .nav-toggle-bars::before {
  top: 0;
  transform: rotate(45deg);
}

html.is-mobile body.mobile-nav-open .nav-toggle-bars::after {
  top: 0;
  transform: rotate(-45deg);
}

.nav-toggle {
  display: none;
}

/* ----- Drawer ----- */
.mobile-drawer {
  display: none;
}

html.is-mobile .mobile-drawer {
  display: block;
  position: fixed;
  inset: 0;
  z-index: 240;
  pointer-events: none;
}

html.is-mobile .mobile-drawer.is-open {
  pointer-events: auto;
}

.mobile-drawer-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.42);
  opacity: 0;
  transition: opacity 0.22s var(--ease);
}

html.is-mobile .mobile-drawer.is-open .mobile-drawer-backdrop {
  opacity: 1;
}

.mobile-drawer-panel {
  position: absolute;
  top: 0;
  right: 0;
  height: 100%;
  width: min(86vw, 340px);
  background: var(--bg-elevated);
  border-left: 1px solid var(--border);
  box-shadow: -12px 0 40px rgba(15, 23, 42, 0.16);
  transform: translateX(104%);
  transition: transform 0.26s var(--ease);
  display: flex;
  flex-direction: column;
  padding:
    calc(0.75rem + var(--safe-t, 0px))
    calc(0.9rem + var(--safe-r, 0px))
    calc(1rem + var(--safe-b, 0px))
    0.9rem;
}

html.is-mobile .mobile-drawer.is-open .mobile-drawer-panel {
  transform: none;
}

.mobile-drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.85rem;
}

.mobile-drawer-title {
  font-size: 1.05rem;
  color: var(--text-bright);
}

.mobile-drawer-nav {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.mobile-drawer-nav .nav-link {
  display: flex;
  width: 100%;
  text-align: left;
  padding: 0.85rem 0.95rem;
  border-radius: 12px;
  font-size: 1rem;
  min-height: 48px;
  align-items: center;
}

.mobile-drawer-sep {
  height: 1px;
  background: var(--border);
  margin: 0.65rem 0;
}

html.is-mobile body.mobile-nav-open {
  overflow: hidden;
}

/* ----- Bottom tab bar (phones only) ----- */
.mobile-tabbar {
  display: none;
}

html.is-mobile .mobile-tabbar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 180;
  height: var(--mobile-tab-h);
  padding: 0.28rem 0.2rem var(--safe-b);
  background: color-mix(in srgb, var(--header-glass) 92%, var(--bg-elevated));
  border-top: 1px solid var(--border);
  backdrop-filter: blur(16px) saturate(1.15);
}

html.is-mobile body.view-admin .mobile-tabbar,
html.is-mobile body.view-manage .mobile-tabbar,
html.is-mobile body.view-login .mobile-tabbar,
html.is-mobile body.mobile-nav-open .mobile-tabbar {
  display: none;
}

.mobile-tab {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text-dim);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.15rem;
  font-family: inherit;
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  min-height: 48px;
  padding: 0.15rem 0.2rem 0.2rem;
  border-radius: 10px;
}

.mobile-tab svg {
  width: 22px;
  height: 22px;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.mobile-tab.active {
  color: var(--accent);
}

.mobile-tab.active svg {
  fill: color-mix(in srgb, var(--accent) 16%, transparent);
}

/* ----- Stage / typography ----- */
html.is-mobile .stage {
  padding: 0.9rem 0.85rem 1.4rem;
  padding-left: calc(0.85rem + var(--safe-l));
  padding-right: calc(0.85rem + var(--safe-r));
  max-width: 100%;
}

html.is-mobile .stage {
  padding-bottom: calc(1.15rem + var(--mobile-tab-h));
  min-height: calc(100vh - var(--status-h) - var(--footer-h));
}

html.is-mobile body.view-admin .stage,
html.is-mobile body.view-manage .stage,
html.is-mobile body.view-login .stage {
  padding-bottom: 1.25rem;
}

html.is-mobile .page-header,
html.is-mobile .view-header {
  flex-direction: column;
  align-items: stretch;
  gap: 0.7rem;
}

html.is-mobile .hero {
  padding: 1.15rem 1rem 1.25rem;
}

html.is-mobile .hero-title {
  font-size: clamp(1.45rem, 7vw, 2rem);
  line-height: 1.2;
}

html.is-mobile .hero-desc {
  font-size: 0.98rem;
}

html.is-mobile .hero-actions {
  flex-direction: column;
  align-items: stretch;
}

html.is-mobile .hero-actions .btn {
  width: 100%;
  min-height: 46px;
}

html.is-mobile .hero-stats,
html.is-mobile .pillar-grid,
html.is-mobile .featured-grid,
html.is-mobile .blog-layout,
html.is-mobile .contact-grid,
html.is-mobile .cred-grid {
  grid-template-columns: 1fr;
}

html.is-tablet .hero-stats,
html.is-tablet .pillar-grid,
html.is-tablet .featured-grid {
  grid-template-columns: 1fr 1fr;
}

/* ----- Blog list (phone cards) ----- */
html.is-mobile #view-blog.page-stack {
  gap: 0.85rem;
}

html.is-mobile #view-blog .page-header {
  margin-bottom: 0;
  flex-direction: row;
  align-items: flex-end;
  justify-content: space-between;
}

html.is-mobile #view-blog .view-title {
  font-size: 1.45rem;
  letter-spacing: -0.03em;
}

html.is-mobile #view-blog .section-kicker {
  margin-bottom: 0.1rem;
}

html.is-mobile .blog-toolbar {
  padding: 0.65rem 0.7rem 0.7rem;
  gap: 0.55rem;
  border-radius: 14px;
}

html.is-mobile .blog-toolbar .search-wrap {
  display: none !important;
}

html.is-mobile .blog-toolbar-row {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-end;
  gap: 0.4rem;
}

html.is-mobile .sort-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin-left: 0;
  width: auto;
}

html.is-mobile .sort-label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

html.is-mobile #sort-select {
  width: auto;
  min-height: 36px;
  padding: 0.35rem 0.55rem;
  font-size: 16px;
  border-radius: 10px;
}

html.is-mobile .category-bar {
  display: flex;
  flex-wrap: nowrap;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  gap: 0.35rem;
  margin: 0;
  padding: 0.05rem 0;
  scrollbar-width: none;
}

html.is-mobile .category-bar::-webkit-scrollbar {
  display: none;
}

html.is-mobile .category-chip {
  flex: 0 0 auto;
  padding: 0.38rem 0.78rem;
  font-size: 0.78rem;
}

html.is-mobile .blog-layout {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

html.is-mobile .blog-sidebar {
  position: static;
  order: 3;
  gap: 0.75rem;
}

html.is-mobile .blog-tags-card {
  display: none;
}

html.is-mobile .blog-sidebar-card {
  padding: 0.8rem 0.85rem;
  border-radius: 14px;
}

html.is-mobile .blog-sidebar-sub {
  display: none;
}

html.is-mobile .tag-bar-sidebar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

html.is-mobile .recommended-list {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

html.is-mobile .post-list {
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  border: none;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
  overflow: visible;
}

html.is-mobile .post-row {
  flex-direction: column;
  align-items: stretch;
  gap: 0;
  padding: 0;
  border: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  border-radius: 16px;
  background: var(--bg-panel-solid);
  box-shadow: var(--shadow-card);
  overflow: hidden;
}

html.is-mobile .post-row:last-child {
  border-bottom: 1px solid var(--border);
}

html.is-mobile .post-row:hover {
  background: var(--bg-panel-solid);
}

html.is-mobile .post-row:active {
  transform: scale(0.992);
}

html.is-mobile .post-row-cover {
  flex: none;
  align-self: stretch;
  width: 100%;
  height: 158px;
  min-height: 158px;
  border: none;
  border-radius: 0;
  background: var(--bg-muted);
}

html.is-mobile .post-row-cover img {
  width: 100%;
  height: 158px;
  min-height: 158px;
  max-height: 158px;
  object-fit: cover;
}

html.is-mobile .post-row-main {
  padding: 0.8rem 0.9rem 0.95rem;
  gap: 0.35rem;
}

html.is-mobile .post-row-meta {
  font-size: 0.7rem;
  gap: 0.28rem 0.4rem;
}

html.is-mobile .post-row-meta .pc-sev {
  margin-left: auto;
}

html.is-mobile .post-row-title {
  font-size: 1.08rem;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

html.is-mobile .post-row-title-btn {
  font: inherit;
  color: inherit;
  text-align: left;
  width: 100%;
}

html.is-mobile .post-row-excerpt {
  font-size: 0.88rem;
  line-height: 1.45;
  -webkit-line-clamp: 2;
  max-width: none;
}

html.is-mobile .post-row-tags {
  display: none;
}

html.is-mobile .post-row-aside,
html.is-mobile .post-row-arrow {
  display: none;
}

html.is-mobile .blog-pagination {
  margin-top: 0.35rem;
  margin-bottom: 0.25rem;
}

html.is-tablet .blog-sidebar .recommended-list {
  grid-template-columns: 1fr 1fr;
}

html.is-mobile .about-hero {
  grid-template-columns: 1fr;
}

html.is-mobile .about-hero-main {
  flex-direction: column;
  border-right: none;
  border-bottom: 1px solid var(--border);
  padding: 1.15rem 1rem 1.2rem;
}

html.is-mobile .about-hero-side {
  display: block;
  grid-template-columns: 1fr;
}

html.is-mobile .auth-card {
  max-width: 100%;
}

html.is-mobile .theme-dropdown {
  z-index: 260;
  right: 0;
}

/* Article */
html.is-mobile #view-post.active,
html.is-mobile #view-post.active.has-toc-sidebar,
html.is-mobile #view-post.active.has-rec-sidebar,
html.is-mobile #view-post.active.has-toc-sidebar.has-rec-sidebar {
  max-width: 100%;
}

html.is-mobile .post-layout.has-toc-sidebar,
html.is-mobile .post-layout.has-rec-sidebar,
html.is-mobile .post-layout.has-toc-sidebar.has-rec-sidebar {
  grid-template-columns: 1fr;
  gap: 0.85rem;
}

html.is-mobile .post-toc-sidebar {
  position: relative;
  top: 0;
  max-height: 14rem;
  overflow: auto;
  order: -1;
}

html.is-mobile .post-rec-sidebar {
  position: static;
  top: auto;
  max-height: none;
  overflow: visible;
  padding: 0 0 0.5rem;
  animation: none;
}

html.is-mobile .post-rec-sidebar-inner {
  padding: 0.85rem 0.8rem 0.9rem;
  border-radius: 14px;
}

html.is-mobile .post-rec-sub {
  display: none;
}

html.is-mobile .post-rec-sidebar .recommended-list {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

html.is-mobile .post-article.news-article {
  padding: 0.15rem 0 1.25rem;
}

html.is-mobile .post-title {
  font-size: clamp(1.4rem, 6.4vw, 1.85rem);
  line-height: 1.22;
}

html.is-mobile .post-body.prose {
  font-size: 1.02rem;
  overflow-x: auto;
}

html.is-mobile .post-body.prose pre,
html.is-mobile .post-body.prose table {
  max-width: 100%;
  overflow-x: auto;
}

html.is-mobile .post-meta-share-row,
html.is-mobile .share-bar.share-bar-inline {
  flex-direction: column;
  align-items: flex-start;
  width: 100%;
}

html.is-mobile .comment-form-row {
  grid-template-columns: 1fr;
}

/* Forms — 16px to avoid iOS focus zoom */
html.is-mobile input,
html.is-mobile select,
html.is-mobile textarea,
html.is-mobile button,
html.is-mobile .btn {
  font-size: 16px;
}

html.is-mobile .btn,
html.is-mobile .icon-btn,
html.is-mobile .link-btn {
  min-height: 42px;
}

html.is-mobile .admin-form-row {
  grid-template-columns: 1fr;
}

html.is-mobile .page-edit-header,
html.is-mobile .console-card-head {
  flex-direction: column;
  align-items: stretch;
}

html.is-mobile .page-edit-header-actions,
html.is-mobile .console-card-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

/* Console / manage */
html.is-mobile .console-shell,
html.is-mobile .admin-layout {
  grid-template-columns: 1fr;
}

html.is-mobile .console-sidebar {
  position: static;
  max-height: none;
}

html.is-mobile .console-nav {
  display: flex;
  overflow-x: auto;
  gap: 0.4rem;
  -webkit-overflow-scrolling: touch;
}

html.is-mobile .console-nav-label {
  display: none;
}

html.is-mobile .console-nav-item {
  flex: 0 0 auto;
  min-width: 9.5rem;
}

html.is-mobile .console-nav-desc {
  display: none;
}

html.is-mobile .admin-side {
  height: auto;
  max-height: 46vh;
  min-height: 220px;
}

html.is-mobile .editor-header-actions,
html.is-mobile .editor-top-bar,
html.is-mobile .editor-stats-bar {
  flex-wrap: wrap;
}

html.is-mobile .editor-surface {
  min-height: 280px;
  max-height: 52vh;
}

html.is-mobile .stage:has(#view-admin.active),
html.is-mobile .stage:has(#view-manage.active),
html.is-mobile .stage.stage-wide {
  padding-left: 0.65rem;
  padding-right: 0.65rem;
}

/* Footer */
html.is-mobile .footer-strip {
  flex-wrap: wrap;
  height: auto;
  min-height: var(--footer-h);
  padding: 0.45rem 0.75rem;
  padding-bottom: calc(0.45rem + var(--mobile-tab-h));
  text-align: center;
}

html.is-mobile body.view-admin .footer-strip,
html.is-mobile body.view-manage .footer-strip,
html.is-mobile body.view-login .footer-strip {
  padding-bottom: 0.45rem;
}

html.is-tablet .footer-strip {
  flex-wrap: wrap;
  height: auto;
  padding: 0.5rem 1rem;
}

html.is-mobile .ui-modal-dialog {
  width: calc(100vw - 1.2rem);
  max-width: 100%;
  margin: 0.6rem;
}

html.is-mobile .toast {
  left: 0.75rem;
  right: 0.75rem;
  bottom: calc(0.75rem + var(--mobile-tab-h));
  width: auto;
}
