/* App-style back office for phones and tablets (D-098, prototype/app-ui).
   Below 1024px: bottom tab bar, a short top bar with back button and page title, dialogs
   as bottom sheets and save buttons that stay on screen. Desktop keeps the sidebar (D-052). */

.tabbar,
.tb-back,
.tb-title {
  display: none;
}

/* ---- Bottom sheet (all sizes; a centred box on desktop) ---- */
dialog.sheet {
  max-width: 460px;
  padding: 0;
}
.sheet-in {
  padding: 8px 16px 16px;
}
.sheet-grab {
  display: none;
  width: 40px;
  height: 5px;
  margin: 2px auto 6px;
  border-radius: 3px;
  background: var(--line);
}
.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 4px;
}
dialog.sheet h2 {
  margin: 0;
  font-size: 1.1rem;
}
.sheet-list {
  display: grid;
  gap: 2px;
}
.sheet-row {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 64px;
  padding: 10px 8px;
  border-radius: 14px;
  color: var(--ink);
  text-decoration: none;
}
.sheet-row:hover,
.sheet-row:focus-visible {
  background: var(--bg);
}
.sheet-row > svg {
  flex: none;
  color: var(--muted);
}
.sheet-ic,
.set-ic,
.tile-ic {
  display: grid;
  flex: none;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 13px;
  background: var(--soft);
  color: var(--green);
}
.sheet-main,
.set-main {
  flex: 1;
  min-width: 0;
}
.sheet-title,
.sheet-sub,
.set-title,
.set-sub {
  display: block;
}
.sheet-title,
.set-title {
  font-weight: 600;
}
.sheet-sub,
.set-sub {
  color: var(--muted);
  font-size: 0.8rem;
}

/* ---- "ฉัน" / account and "เมนู" screens ---- */
.account,
.more {
  max-width: 640px;
  margin: 0 auto;
}
.acc-card {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--paper);
}
.acc-card .tb-avatar {
  width: 64px;
  height: 64px;
}
.acc-who {
  min-width: 0;
}
.acc-name {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
}
.acc-sub {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 0.85rem;
  overflow-wrap: anywhere;
}
.acc-title {
  margin: 22px 4px 8px;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 600;
}
.set-list {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--paper);
}
.set-row {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 62px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  color: var(--ink);
  text-decoration: none;
}
.set-row:last-child {
  border-bottom: 0;
}
a.set-row:hover {
  background: var(--bg);
}
.set-ic {
  width: 36px;
  height: 36px;
  border-radius: 10px;
}
.set-row > svg {
  flex: none;
  color: var(--muted);
}
.switch {
  position: relative;
  flex: none;
  width: 52px;
  height: 32px;
  padding: 0;
  border: 0;
  border-radius: 16px;
  background: var(--line);
  cursor: pointer;
}
.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px #0003;
  transition: transform 0.2s;
}
.switch[aria-checked="true"] {
  background: var(--green);
}
.switch[aria-checked="true"]::after {
  transform: translateX(20px);
}
.switch:focus-visible {
  outline: 2px solid var(--blue);
  outline-offset: 2px;
}
/* A switch that cannot change (a security notice); its row says why. Still focusable. */
.switch[aria-disabled="true"] {
  cursor: not-allowed;
  opacity: 0.55;
}
/* Notification settings: a blocker's icon sits beside its text, not above it. */
.account .notice > span {
  flex: 1;
  min-width: 0;
}
.set-row .fontseg {
  flex: none;
}
.acc-logout {
  justify-content: center;
  gap: 8px;
  width: 100%;
  min-height: 50px;
  margin-top: 24px;
}
.acc-version {
  margin-top: 14px;
  text-align: center;
}
.more-group .acc-title:first-child {
  margin-top: 6px;
}
.tiles {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}
.tile {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 98px;
  padding: 14px 6px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--paper);
  color: var(--ink);
  font-size: 0.8rem;
  line-height: 1.3;
  text-align: center;
  text-decoration: none;
}
.tile:hover {
  border-color: var(--green);
}
.tab-dot,
.tile-dot {
  position: absolute;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--danger);
  color: #fff;
  font-size: 0.68rem;
  font-weight: 700;
  line-height: 18px;
  text-align: center;
}
.tile-dot {
  top: 8px;
  right: 10px;
}
@media (min-width: 600px) {
  .tiles {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

/* ---- Phones and tablets ---- */
@media (max-width: 1023px) {
  :root {
    --tabbar-h: 64px;
    --safe-b: env(safe-area-inset-bottom, 0px);
  }
  .sidebar,
  .sidebar.open,
  .topbar .mobile-nav,
  .topbar .crumb,
  .topbar .user-name,
  .topbar #theme-toggle,
  .topbar .fontseg,
  .topbar .tb-avatar,
  .topbar [data-action="logout"] {
    display: none;
  }
  .workspace {
    margin-left: 0;
  }
  .topbar {
    position: sticky;
    top: 0;
    z-index: 40;
    height: 56px;
    padding: 0 8px 0 10px;
    border-bottom: 1px solid transparent;
    transition: border-color 0.2s;
  }
  .topbar.scrolled {
    border-bottom-color: var(--line);
  }
  .topbar .toolbar:first-child {
    flex: 1;
    min-width: 0;
    gap: 2px;
  }
  .tb-back {
    display: inline-grid;
  }
  .tb-back[hidden] {
    display: none;
  }
  .tb-title {
    display: block;
    padding-left: 6px;
    overflow: hidden;
    font-size: 1.05rem;
    font-weight: 700;
    white-space: nowrap;
    text-overflow: ellipsis;
    opacity: 0;
    transform: translateY(4px);
    transition:
      opacity 0.18s,
      transform 0.18s;
  }
  .topbar.titled .tb-title {
    opacity: 1;
    transform: none;
  }
  .page {
    padding: 12px 16px calc(var(--tabbar-h) + var(--safe-b) + 28px);
  }
  .app.task .page {
    padding-bottom: calc(96px + var(--safe-b));
  }
  .pagehead .eyebrow {
    display: none;
  }

  .tabbar {
    position: fixed;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 45;
    display: grid;
    grid-template-columns: repeat(var(--tabs, 5), minmax(0, 1fr));
    height: calc(var(--tabbar-h) + var(--safe-b));
    padding-bottom: var(--safe-b);
    border-top: 1px solid var(--line);
    background: var(--paper);
    box-shadow: 0 -6px 24px #102a4314;
  }
  .app.task .tabbar {
    display: none;
  }
  .tab {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    min-width: 0;
    padding: 4px 2px;
    border: 0;
    background: none;
    color: var(--muted);
    font: inherit;
    font-size: 0.72rem;
    text-decoration: none;
    cursor: pointer;
  }
  .tab-ic {
    display: grid;
    place-items: center;
    width: 56px;
    height: 30px;
    border-radius: 15px;
    transition: background 0.2s;
  }
  .tab[aria-current="page"] {
    color: var(--ink);
    font-weight: 600;
  }
  .tab[aria-current="page"] .tab-ic {
    background: var(--soft);
    color: var(--green);
  }
  .tab-label {
    max-width: 100%;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
  .tab.main {
    color: var(--ink);
  }
  .tab-fab {
    display: grid;
    place-items: center;
    width: 52px;
    height: 52px;
    margin-top: -24px;
    border-radius: 50%;
    background: var(--gold);
    color: #102a43;
    box-shadow:
      0 6px 16px #102a4333,
      0 0 0 4px var(--paper);
    transition: transform 0.15s;
  }
  .tab.main:active .tab-fab {
    transform: scale(0.94);
  }
  .tab:focus-visible {
    border-radius: 12px;
    outline: 2px solid var(--blue);
    outline-offset: -4px;
  }
  .tab-dot {
    top: 2px;
    left: calc(50% + 8px);
  }
  .toasts {
    bottom: calc(var(--tabbar-h) + var(--safe-b) + 12px);
  }
  body:has(.app.task) .toasts {
    bottom: calc(96px + var(--safe-b));
  }

  /* Save buttons stay on screen while writing. */
  .app.task .editor-layout aside .pub-foot {
    position: fixed;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 44;
    display: flex;
    gap: 8px;
    margin: 0;
    padding: 10px 16px calc(10px + var(--safe-b));
    border-top: 1px solid var(--line);
    background: var(--paper);
    box-shadow: 0 -6px 24px #102a4314;
  }
  .app.task .editor-layout aside .pub-foot .btn {
    flex: 1;
    width: auto;
  }
  /* A panel clips its corners with overflow:hidden, which would stop the bar sticking. */
  .panel:has(.savebar) {
    overflow: visible;
  }
  .savebar {
    position: sticky;
    bottom: 0;
    z-index: 5;
    margin-inline: -24px;
    padding: 10px 24px calc(10px + var(--safe-b));
    border-top: 1px solid var(--line);
    background: var(--paper);
  }
  .savebar .btn {
    width: 100%;
  }

  /* The content area takes focus after each tab change (for screen readers), not a frame. */
  #main:focus {
    outline: none;
  }

  /* Every dialog opens as a sheet from the bottom, within thumb reach. */
  dialog {
    width: 100%;
    max-width: 100%;
    max-height: 90dvh;
    margin: auto 0 0;
    padding: 20px 16px calc(20px + var(--safe-b));
    overflow: auto;
    border: 0;
    border-radius: 20px 20px 0 0;
    animation: sheet-up 0.26s cubic-bezier(0.2, 0.8, 0.2, 1);
  }
  dialog.sheet {
    max-width: 100%;
    padding: 0 0 var(--safe-b);
  }
  .sheet-grab {
    display: block;
  }
  dialog .actions .btn {
    flex: 1;
  }
}

@keyframes sheet-up {
  from {
    transform: translateY(100%);
  }
}

@media (prefers-reduced-motion: reduce) {
  dialog,
  .tb-title,
  .tab-fab,
  .switch::after {
    animation: none;
    transition: none;
  }
}

@media (max-width: 760px) {
  .savebar {
    margin-inline: -18px;
    padding-inline: 18px;
  }
}

/* ---- "บัญชีของฉัน" on phones (D-104) ---- */
.tb-me {
  display: none;
  overflow: hidden;
  border-radius: 50%;
  padding: 0;
}
.tb-me img,
.me-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.tb-me span,
.me-avatar span {
  color: var(--green);
  font-weight: 700;
}
@media (max-width: 1023px) {
  .topbar .tb-me {
    display: inline-grid;
    place-items: center;
    width: 42px;
    height: 42px;
    background: var(--soft);
  }
}
.me-card {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 12px;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--paper);
  color: var(--ink);
}
.me-card > div {
  flex: 1;
  min-width: 0;
}
.me-card-link {
  margin: 0 0 18px;
}
.me-avatar {
  display: inline-grid;
  flex: none;
  place-items: center;
  width: 52px;
  height: 52px;
  overflow: hidden;
  border-radius: 50%;
  background: var(--soft);
  font-size: 1.2rem;
}
.me-name {
  margin: 0;
  font-weight: 700;
}
.me-sub {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 0.85rem;
  overflow-wrap: anywhere;
}
.me-logout {
  justify-content: center;
  gap: 8px;
  width: 100%;
  min-height: 50px;
  margin-top: 14px;
}
