/* Menus, dialogs (palette, account popover, settings sheet, modals) and toasts. */

.layer { position: fixed; inset: 0; z-index: 60; pointer-events: none; }
.layer > * { pointer-events: auto; }

/* -------------------------------------------------------------------- Menu */

.menu {
  position: fixed;
  min-width: 196px;
  max-width: min(320px, calc(100vw - 16px));
  max-height: min(60vh, 420px);
  overflow-y: auto;
  scrollbar-width: thin;
  padding: 4px;
  border-radius: var(--r-md);
  border: 1px solid var(--hairline-2);
  background: rgba(20, 21, 30, .9);
  -webkit-backdrop-filter: blur(24px) saturate(150%);
  backdrop-filter: blur(24px) saturate(150%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .08), var(--shadow-3);
  transform-origin: top left;
  animation: pop-in var(--fast) var(--ease) both;
}
@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  .menu { background: var(--bg-menu); }
}

.menu-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 36px;
  padding: 0 10px;
  border: 0;
  border-radius: var(--r-sm);
  background: none;
  color: var(--text-1);
  font-size: 13px;
  text-align: left;
  cursor: pointer;
}
.menu-item:hover, .menu-item:focus-visible {
  outline: 2px solid transparent;
  outline-offset: -2px;
  background: rgba(255, 255, 255, .08);
}
.menu-item:focus-visible { box-shadow: inset 0 0 0 1.5px var(--accent-line); }
.menu-ico { width: 16px; height: 16px; flex: none; color: var(--text-2); }
.menu-label { flex: 1; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.menu-hint { color: var(--text-3); font-size: 12px; }
.menu-check { width: 16px; height: 16px; color: var(--link); flex: none; }
.menu-item.is-danger { color: var(--danger); }
.menu-item.is-danger .menu-ico { color: var(--danger); }
.menu-item.is-danger:hover, .menu-item.is-danger:focus-visible { background: rgba(255, 110, 125, .12); }
.menu-sep { height: 1px; margin: 5px 4px; background: var(--hairline); }
.menu-heading { padding: 8px 10px 4px; }

@media (max-width: 767.98px) {
  .menu-item { min-height: 44px; font-size: 14px; }
}

/* ----------------------------------------------------------------- Dialogs */

dialog.dlg {
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--text-1);
  max-width: none;
  max-height: none;
  overflow: visible;
}
/* Every panel stays within the visible viewport and scrolls inside, since the
   dialog's own UA scrolling is turned off above. */
.popover-panel, .modal-panel {
  max-height: calc(var(--app-height, 100dvh) - 24px);
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
}
dialog.dlg::backdrop {
  background: rgba(3, 4, 8, .56);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  animation: fade-in var(--dur) var(--ease);
}
dialog.dlg[open] .dlg-panel { animation: pop-in var(--dur) var(--ease) both; }

.dlg-panel {
  position: relative;
  border-radius: var(--r-card);
  border: 1px solid var(--hairline-2);
  background: linear-gradient(180deg, rgba(28, 29, 40, .92) 0%, rgba(18, 19, 27, .94) 100%);
  -webkit-backdrop-filter: blur(28px) saturate(150%);
  backdrop-filter: blur(28px) saturate(150%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .09), var(--shadow-3);
}

/* Modal (rename, confirm, re-auth) */
dialog.modal { width: min(420px, calc(100vw - 32px)); margin: auto; }
.modal-panel { padding: 22px 22px 18px; }
.modal-title {
  margin-bottom: 8px;
  font-family: var(--font-display);
  font-size: 18px;
  font-weight: var(--fw-semibold);
  letter-spacing: -.015em;
}
.modal-body { margin-bottom: 20px; font-size: 14px; line-height: 1.55; color: var(--text-2); overflow-wrap: anywhere; }
.modal-panel .field { margin-bottom: 2px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.modal-actions .btn { height: 40px; }

/* Command palette. Heights follow the visible viewport (--app-height), not vh,
   so the last option is not left under an on-screen keyboard. */
dialog.palette { width: min(640px, calc(100vw - 24px)); margin: min(14vh, 120px) auto auto; }
.palette-panel {
  display: flex;
  flex-direction: column;
  max-height: min(calc(var(--app-height, 100dvh) - min(14vh, 120px) - 16px), 560px);
  overflow: hidden;
}
.palette-search {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 14px 0 18px;
  height: 58px;
  border-bottom: 1px solid var(--hairline);
  color: var(--text-3);
}
.palette-input {
  flex: 1;
  min-width: 0;
  height: 100%;
  border: 0;
  outline: none;
  background: none;
  color: var(--text-1);
  font-size: 16px;
}
.palette-input::placeholder { color: var(--text-3); }
.palette-results {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 6px 8px 10px;
  scrollbar-width: thin;
}
.palette-heading {
  padding: 12px 10px 6px;
  font-size: 11px;
  font-weight: var(--fw-semibold);
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--text-3);
}
.palette-option {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 42px;
  padding: 0 12px;
  border-radius: var(--r-nav);
  color: var(--text-2);
  font-size: 14px;
  cursor: pointer;
}
/* The option Enter will run. Focus stays in the search box, so this ring is the
   focus indicator: well over 3:1 against the panel. */
.palette-option[aria-selected="true"] {
  background: rgba(156, 140, 255, .14);
  color: var(--text-1);
  box-shadow: inset 0 0 0 1.5px var(--accent-line);
}
.palette-ico { width: 16px; height: 16px; flex: none; color: var(--text-3); }
.palette-option[aria-selected="true"] .palette-ico { color: var(--text-1); }
.palette-label { flex: 1; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.palette-meta { flex: none; font-size: 12px; color: var(--text-3); }
.palette-option[aria-selected="true"] .palette-meta { color: var(--text-2); }
.palette-empty { padding: 26px 16px 10px; text-align: center; overflow-wrap: anywhere; }
.palette-foot {
  display: flex;
  gap: 18px;
  padding: 10px 18px;
  border-top: 1px solid var(--hairline);
  font-size: 12px;
  color: var(--text-3);
}
.palette-foot span { display: inline-flex; align-items: center; gap: 4px; }

/* Account popover */
dialog.popover {
  position: fixed;
  inset: auto auto 76px 12px;
  width: 300px;
  margin: 0;
}
dialog.popover::backdrop { background: transparent; -webkit-backdrop-filter: none; backdrop-filter: none; }
dialog.popover[open] .dlg-panel { transform-origin: bottom left; }
.popover-panel { padding: 8px; border-radius: var(--r-card); }
.popover-head { display: flex; align-items: center; gap: 12px; padding: 10px 10px 12px; }
.popover-id { min-width: 0; }
.popover-name, .popover-email { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.popover-name { font-size: 14px; font-weight: var(--fw-semibold); color: var(--text-1); }
.popover-email { font-size: 12px; color: var(--text-3); }
.popover-section { padding: 10px 10px 12px; border-top: 1px solid var(--hairline); }
.popover-section > .section-label { margin-bottom: 8px; }
.popover-actions { padding: 6px 0 0; border-top: 1px solid var(--hairline); }

.account-list { list-style: none; display: flex; flex-direction: column; gap: 2px; margin: 0 -6px; }
.account-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 38px;
  padding: 0 8px;
  border: 0;
  border-radius: var(--r-nav);
  background: none;
  color: var(--text-2);
  font-size: 13px;
  text-align: left;
  cursor: pointer;
}
.account-btn:hover { background: rgba(255, 255, 255, .06); color: var(--text-1); }
.account-btn[aria-current="true"] { color: var(--text-1); background: rgba(255, 255, 255, .05); }
.account-ico { width: 16px; height: 16px; flex: none; color: var(--text-3); }
.account-name { flex: 1; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.account-check { width: 16px; height: 16px; flex: none; color: var(--link); }

.menu-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  height: 38px;
  padding: 0 12px;
  border: 0;
  border-radius: var(--r-nav);
  background: none;
  color: var(--text-1);
  font-size: 13px;
  cursor: pointer;
}
.menu-row .ico { color: var(--text-2); }
.menu-row:hover { background: rgba(255, 255, 255, .06); }

/* Usage meter */
.meter-top { display: flex; align-items: baseline; gap: 6px; }
.meter-big { font-family: var(--font-display); font-size: 22px; font-weight: var(--fw-semibold); letter-spacing: -.02em; color: var(--text-1); }
.meter-of { font-size: 12px; color: var(--text-3); }
.meter {
  position: relative;
  height: 6px;
  margin: 10px 0 8px;
  border-radius: 999px;
  background: rgba(255, 255, 255, .07);
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, .35);
  overflow: hidden;
}
.meter-fill {
  --p: 0;
  position: absolute;
  inset: 0;
  border-radius: inherit;
  /* Monochrome: the accent is kept for actions, focus and links. */
  background: linear-gradient(90deg, rgba(255, 255, 255, .55), rgba(255, 255, 255, .9));
  box-shadow: none;
  transform: scaleX(var(--p));
  transform-origin: left center;
  transition: transform 600ms var(--ease);
}
.meter.is-warn .meter-fill { background: linear-gradient(90deg, #ffcf8a, #ffb37a); box-shadow: 0 0 10px rgba(255, 190, 120, .5); }
.meter.is-full .meter-fill { background: linear-gradient(90deg, #ff9aa2, #ff7d8a); box-shadow: 0 0 10px rgba(255, 120, 130, .5); }
.meter-text { margin-top: 6px; font-size: 13px; line-height: 1.45; color: var(--text-2); }

/* Settings sheet */
dialog.sheet-right {
  position: fixed;
  inset: 0 0 0 auto;
  width: min(440px, 100vw);
  height: 100%;
  margin: 0;
}
dialog.sheet-right[open] .dlg-panel { animation: sheet-in var(--slow) var(--ease) both; }
.sheet-panel {
  display: flex;
  flex-direction: column;
  height: 100%;
  border-radius: var(--r-comp) 0 0 var(--r-comp);
  border-right: 0;
}
.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px max(16px, env(safe-area-inset-right)) 12px 24px;
}
.sheet-title { font-family: var(--font-display); font-size: 20px; font-weight: var(--fw-semibold); letter-spacing: -.02em; }
.sheet-body { flex: 1; min-height: 0; overflow-y: auto; padding: 4px max(24px, env(safe-area-inset-right)) max(28px, env(safe-area-inset-bottom)) 24px; scrollbar-width: thin; }

.settings-group + .settings-group { margin-top: 22px; }
.settings-group > .section-label { margin: 0 2px 8px; }
.settings-card {
  padding: 14px;
  border-radius: var(--r-card);
  border: 1px solid var(--hairline);
  background: rgba(255, 255, 255, .03);
  box-shadow: var(--top-light);
}
.settings-row { display: flex; align-items: center; gap: 14px; }
.settings-id { min-width: 0; }
.settings-strong { display: block; font-size: 14px; font-weight: var(--fw-semibold); color: var(--text-1); overflow-wrap: anywhere; }
.settings-muted { display: block; font-size: 13px; line-height: 1.5; color: var(--text-2); overflow-wrap: anywhere; }
.settings-card > .settings-muted + .settings-muted { margin-top: 6px; }
.settings-note {
  margin-top: 12px;
  padding: 10px 12px;
  border-radius: var(--r-md);
  border: 1px solid rgba(255, 208, 138, .28);
  background: rgba(255, 208, 138, .06);
  font-size: 13px;
  line-height: 1.5;
  color: var(--text-1);
}
.settings-plan { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.settings-card .meter-block { margin-bottom: 10px; }
.settings-accounts { list-style: none; display: flex; flex-direction: column; gap: 4px; margin: -4px; }
.settings-account {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 10px;
  border: 1px solid transparent;
  border-radius: var(--r-md);
  background: none;
  text-align: left;
}
button.settings-account { cursor: pointer; }
button.settings-account:hover { background: rgba(255, 255, 255, .05); }
.settings-account[aria-pressed="true"] { border-color: var(--hairline-2); background: rgba(255, 255, 255, .04); }
.settings-account-text { flex: 1; min-width: 0; }
.settings-foot { margin-top: 10px; font-size: 12px; color: var(--text-3); text-align: center; }

/* Password */
.settings-form { display: flex; flex-direction: column; }
.settings-form > .settings-muted { margin-bottom: 14px; }
.settings-form .field { height: 44px; }
.settings-form .pw-toggle { top: 2px; }
@media (max-width: 767.98px) {
  .settings-form .pw-toggle { top: 0; } /* the 44px phone toggle fills the 44px field */
}
.settings-form .btn-block { margin-top: 14px; }
.settings-verify { display: flex; flex-direction: column; margin-top: 16px; }
.settings-verify > .settings-muted { margin-bottom: 10px; }
.settings-verify .field-code { font-variant-numeric: tabular-nums; letter-spacing: .18em; }
.settings-verify .btn-sm { align-self: flex-start; margin-top: 8px; }

@keyframes sheet-in {
  from { opacity: 0; transform: translateX(24px); }
  to { opacity: 1; transform: none; }
}

/* ------------------------------------------------------------------ Toasts */

.toasts {
  position: fixed;
  left: 50%;
  bottom: max(24px, env(safe-area-inset-bottom));
  z-index: 70;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  width: max-content;
  max-width: calc(100vw - 24px);
  transform: translateX(-50%);
  pointer-events: none;
}
.toast {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  padding: 6px 6px 6px 14px;
  border-radius: var(--r-md);
  border: 1px solid var(--hairline-2);
  background: rgba(24, 25, 35, .9);
  -webkit-backdrop-filter: blur(24px) saturate(150%);
  backdrop-filter: blur(24px) saturate(150%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .08), var(--shadow-3);
  color: var(--text-1);
  font-size: 13px;
  pointer-events: auto;
  animation: rise var(--dur) var(--ease) both;
}
.toast.is-leaving { opacity: 0; transform: translateY(4px); transition: opacity 160ms var(--ease), transform 160ms var(--ease); }
.toast-ico { width: 16px; height: 16px; flex: none; color: var(--text-2); }
.toast-text { min-width: 0; max-width: 46ch; overflow-wrap: anywhere; }
.toast-action {
  height: 30px;
  padding: 0 10px;
  border: 0;
  border-radius: var(--r-sm);
  background: rgba(255, 255, 255, .08);
  color: var(--link);
  font-size: 13px;
  font-weight: var(--fw-semibold);
  cursor: pointer;
}
.toast-action:hover { background: rgba(255, 255, 255, .13); }
.toast-close {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border: 0;
  border-radius: var(--r-sm);
  background: none;
  color: var(--text-3);
  cursor: pointer;
}
.toast-close svg { width: 14px; height: 14px; }
.toast-close:hover { color: var(--text-1); background: rgba(255, 255, 255, .06); }

.view-app[data-page="chat"] + .toasts { bottom: 128px; }

/* Centred on the main column, not the window: the sidebar takes the left. */
@media (min-width: 768px) {
  .view-app:not([hidden]) + .toasts { left: calc(50% + var(--sidebar-w) / 2); max-width: calc(100vw - var(--sidebar-w) - 24px); }
  .view-app.is-rail:not([hidden]) + .toasts { left: calc(50% + var(--rail-w) / 2); max-width: calc(100vw - var(--rail-w) - 24px); }
}

/* A toast raised inside an open dialog (everything outside it is inert). */
.toasts-dialog { bottom: max(24px, env(safe-area-inset-bottom)); }

/* ------------------------------------------------------------- Responsive */

@media (max-width: 767.98px) {
  dialog.popover,
  dialog.popover[open] {
    inset: auto 8px max(8px, env(safe-area-inset-bottom)) 8px !important;
    width: auto;
  }
  dialog.popover[open] .dlg-panel { transform-origin: bottom center; }
  .account-btn, .menu-row { min-height: 44px; }

  /* Rename: sit near the top, so the field and Save stay above the keyboard. */
  dialog.modal { margin: 16px auto auto; }

  dialog.palette { margin-top: 12px; width: calc(100vw - 16px); }
  .palette-panel { max-height: calc(var(--app-height, 100dvh) - 24px); }
  .palette-foot { display: none; }
  .palette-option { min-height: 48px; }

  dialog.sheet-right { width: 100vw; }
  .sheet-panel { border-radius: 0; border-left: 0; }
  .sheet-head { padding-top: max(14px, env(safe-area-inset-top)); }
  .sheet-body { padding-bottom: max(24px, env(safe-area-inset-bottom)); }

  .toasts { bottom: max(16px, env(safe-area-inset-bottom)); }
  .view-app[data-page="chat"] + .toasts,
  .view-app[data-page="home"] + .toasts { bottom: 150px; }
  .modal-actions .btn { height: 44px; }
  .toast-action { height: 44px; padding: 0 14px; }
  .toast-close { width: 44px; height: 44px; }
}
