/* App Shell (PLANO-IA D1): rail · sidebar do produto · área de trabalho · painel à direita. Rail branca e 2 px (A1). */
.os {
  --rail-w: var(--rail-expanded);
  display: grid;
  grid-template-columns: var(--rail-w) auto minmax(0, 1fr) auto;
  grid-template-rows: minmax(0, 1fr);
  gap: var(--gap);
  height: 100vh;
  height: 100dvh;
  padding: var(--gap);
  overflow: hidden;
  background: var(--bg);
}
.os[data-rail="compact"] { --rail-w: var(--rail-compact); }
.os-topbar { display: none; }
.os-bottombar { display: none; }

/* No desktop, só a área de trabalho e a sidebar rolam. O documento externo
   não pode acompanhar scrollIntoView e deslocar o shell para fora da viewport. */
@media (min-width: 768px) {
  html, body { overflow: hidden; }
}

/* ---------- rail ---------- */
.os-rail {
  grid-column: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding-bottom: 12px;
  overflow-x: hidden;
  overflow-y: auto;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text-strong);
}
.rail-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; padding: 16px 12px 0 18px; }
.rail-brand { display: inline-flex; border-radius: var(--radius); color: var(--text-strong); }
.rail-brand:hover { background: var(--hover); }
.sub-symbol { display: block; flex: none; width: 36px; height: 45px; }
.rail-toggle,
.os-icon-btn {
  display: inline-grid;
  flex: none;
  place-items: center;
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-strong);
}
.rail-toggle:hover,
.os-icon-btn:hover { background: var(--hover); }
.os[data-rail-locked="true"] .rail-toggle { display: none; }

.rail-new-wrap { padding: 20px 12px 12px; }
.rail-new {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 40px;
  padding: 4px;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-strong);
  font-size: 14px;
  font-weight: 500;
  line-height: 20px;
  text-align: left;
  cursor: default;
}
.rail-new-square {
  display: inline-grid;
  flex: 0 0 32px;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: var(--radius);
  background: var(--sub-action-bg);
  color: var(--sub-action-ink);
}

.rail-list { display: flex; flex-direction: column; gap: 2px; padding: 0 12px; }
.rail-item {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  min-height: 40px;
  padding: 8px 10px 8px 12px;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-strong);
  font-size: 14px;
  font-weight: 500;
  line-height: 20px;
  text-align: left;
}
.rail-item:hover { background: var(--hover); }
.rail-item[aria-current="page"],
.rail-item[aria-expanded="true"] { background: var(--selected); font-weight: 600; }
.rail-item.is-disabled { font-weight: 400; cursor: default; }
.rail-item.is-disabled:hover { background: transparent; }
.rail-copy { display: flex; flex-direction: column; min-width: 0; }
.rail-note { color: var(--text-secondary); font-size: 11px; font-weight: 400; line-height: 14px; }
.rail-sep { height: 1px; margin: 12px 22px; background: var(--border-soft); }
.rail-foot { margin-top: auto; padding-top: 12px; }

/* compacta: só ícones; o nome segue acessível e aparece no hover e no foco */
.os[data-rail="compact"] .os-rail { overflow: visible; }
.os[data-rail="compact"] .rail-top { flex-direction: column; align-items: center; padding: 16px 0 0; }
.os[data-rail="compact"] .rail-new-wrap { display: flex; justify-content: center; padding: 16px 0 8px; }
.os[data-rail="compact"] .rail-new { justify-content: center; width: 40px; }
.os[data-rail="compact"] .rail-list { padding: 0 16px; }
.os[data-rail="compact"] .rail-item { justify-content: center; padding: 0; }
.os[data-rail="compact"] .rail-sep { margin: 12px 20px; }
.os[data-rail="compact"] .rail-copy {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}
.os[data-rail="compact"] .os-rail [data-tip] { position: relative; }
.os[data-rail="compact"] .os-rail [data-tip]:is(:hover, :focus-visible)::after {
  content: attr(data-tip);
  position: absolute;
  top: 50%;
  left: calc(100% + 12px);
  z-index: var(--z-popover);
  padding: 4px 8px;
  border-radius: var(--radius);
  background: var(--text-strong);
  color: var(--surface);
  font-size: 12px;
  font-weight: 500;
  line-height: 16px;
  white-space: nowrap;
  transform: translateY(-50%);
  pointer-events: none;
}

/* ---------- sidebar do produto ---------- */
.os-sidebar {
  grid-column: 2;
  display: flex;
  flex-direction: column;
  width: var(--sidebar-w);
  min-height: 0;
  border-radius: var(--radius);
  background: var(--surface);
}
.os[data-sidebar="closed"] .os-sidebar { display: none; }
.os-side-head { display: flex; flex: none; align-items: center; justify-content: space-between; gap: 8px; height: var(--head-h); padding: 0 12px 0 20px; border-bottom: 1px solid var(--border-soft); }
.os-side-title { color: var(--text-strong); font-size: 15px; font-weight: 600; }
.os-side-body { flex: 1; min-height: 0; overflow: auto; padding: 16px 12px 24px; }
.os-side-label { margin: 4px 0 6px; padding: 0 10px; color: var(--text-secondary); font-size: 11px; font-weight: 600; letter-spacing: 0.06em; line-height: 16px; text-transform: uppercase; }

/* ---------- área de trabalho ---------- */
.os-work {
  grid-column: 3;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  border-radius: var(--radius);
  background: var(--surface);
}
.os-head { display: flex; flex: none; align-items: center; gap: 12px; height: var(--head-h); padding: 0 24px; border-bottom: 1px solid var(--border-soft); }
.os-side-show { display: none; margin-left: -8px; }
.os[data-sidebar="closed"] .os-side-show { display: inline-grid; }
.os-crumbs { display: flex; align-items: center; gap: 8px; min-width: 0; overflow: hidden; color: var(--text-secondary); font-size: 13px; white-space: nowrap; }
.os-crumbs > * { flex: 0 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.os-crumbs > .sep,
.os-crumbs > a:first-child { flex: none; }
.os-crumbs a:hover { color: var(--text); text-decoration: underline; }
.os-crumbs [aria-current="page"] { color: var(--text-strong); font-weight: 500; }
/* as telas dos produtos se ajustam ao espaço da área de trabalho (rail, sidebar e Brain mudam a largura) */
.os-body { flex: 1; min-height: 0; overflow: auto; container: os-body / inline-size; }

/* ---------- painel à direita (Brain) ---------- */
.os-panel {
  grid-column: 4;
  display: flex;
  flex-direction: column;
  width: var(--panel-w);
  min-height: 0;
  border-radius: var(--radius);
  background: var(--surface);
}
.os-panel[hidden] { display: none; }
.os-panel:focus { outline: none; }
.os-panel:focus-visible { outline: 2px solid var(--text-strong); outline-offset: -2px; }
.os-panel-head { display: flex; flex: none; align-items: center; justify-content: space-between; height: var(--head-h); padding: 0 12px 0 20px; border-bottom: 1px solid var(--border-soft); }
.os-panel-title { display: flex; align-items: center; gap: 10px; color: var(--text-strong); font-size: 15px; font-weight: 600; }
.os-panel-body { display: flex; flex-direction: column; gap: 12px; overflow: auto; padding: 20px; color: var(--text); font-size: 14px; line-height: 22px; }
.os-badge { align-self: flex-start; padding: 1px 8px; border: 1px solid var(--border); border-radius: var(--radius); color: var(--text-strong); font-size: 12px; font-weight: 600; }
.os-panel-ctx { margin-top: 8px; padding-top: 12px; border-top: 1px solid var(--border-soft); }
.os-panel-ctx dt { color: var(--text-secondary); font-size: 12px; line-height: 16px; }
.os-panel-ctx dd { color: var(--text-strong); font-weight: 600; }

/* ---------- Configurações (tema) ---------- */
.os-pop {
  position: fixed;
  bottom: 16px;
  left: calc(var(--rail-w) + var(--gap) * 2);
  z-index: var(--z-popover);
  width: 240px;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-popover);
}
.os-pop[hidden] { display: none; }
.os-pop-title { color: var(--text-strong); font-size: 14px; font-weight: 600; }
.os-pop-label { margin-top: 10px; color: var(--text-secondary); font-size: 12px; }
.os-seg { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; margin-top: 6px; padding: 3px; border-radius: var(--radius); background: var(--surface-sunken); }
.os-seg-btn { display: flex; align-items: center; justify-content: center; gap: 6px; min-height: 32px; border: 0; border-radius: var(--radius); background: transparent; color: var(--text); font-size: 13px; font-weight: 500; }
.os-seg-btn[aria-pressed="true"] { background: var(--surface); color: var(--text-strong); font-weight: 600; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12); }

/* ---------- larguras ---------- */
@media (max-width: 1180px) {
  .os { --sidebar-w: 248px; }
  /* o painel passa a sobrepor a área de trabalho em vez de espremê-la */
  .os-panel { position: fixed; top: var(--gap); right: var(--gap); bottom: var(--gap); z-index: var(--z-panel); width: min(var(--panel-w), calc(100vw - var(--gap) * 2)); box-shadow: var(--shadow-popover); }
}

@media (max-width: 767px) {
  .os { display: block; height: auto; min-height: 100vh; padding: 0; overflow: visible; }
  .os-topbar {
    position: fixed;
    top: 0;
    right: 0;
    left: 0;
    z-index: var(--z-panel);
    display: flex;
    align-items: center;
    gap: 4px;
    height: var(--head-h);
    padding: 0 8px;
    border-bottom: 1px solid var(--border-soft);
    background: var(--surface);
  }
  .os-top-brand { display: inline-flex; padding: 4px; border-radius: var(--radius); color: var(--text-strong); }
  .os-top-brand .sub-symbol { width: 22px; height: 28px; }
  .os-top-title { display: flex; flex: 1; align-items: center; justify-content: center; gap: 6px; min-width: 0; min-height: 40px; padding: 0 8px; border: 0; border-radius: var(--radius); background: transparent; color: var(--text-strong); font-size: 15px; font-weight: 600; white-space: nowrap; }
  button.os-top-title:hover { background: var(--hover); }
  .os-side-menu[aria-expanded="true"] .icon { transform: rotate(180deg); }

  .os-rail {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: var(--z-drawer);
    width: min(300px, 86vw);
    border-radius: 0;
    box-shadow: var(--shadow-popover);
    visibility: hidden;
    transform: translateX(-100%);
    transition: transform var(--dur) ease, visibility 0s linear var(--dur);
  }
  .os[data-drawer="open"] .os-rail { visibility: visible; transform: none; transition: transform var(--dur) ease; }
  .os[data-drawer="open"]::after { content: ""; position: fixed; inset: 0; z-index: calc(var(--z-drawer) - 1); background: rgba(16, 16, 16, 0.32); }

  .os-sidebar,
  .os[data-sidebar="closed"] .os-sidebar { display: none; }
  .os[data-side-menu="open"] .os-sidebar {
    position: fixed;
    top: var(--head-h);
    right: 0;
    left: 0;
    z-index: var(--z-panel);
    display: flex;
    width: auto;
    max-height: calc(100vh - var(--head-h));
    max-height: calc(100dvh - var(--head-h));
    border-bottom: 1px solid var(--border);
    border-radius: 0;
    box-shadow: var(--shadow-popover);
  }
  .os-side-head { display: none; }
  .os-side-body { padding: 12px 8px 20px; }

  .os-work { display: block; min-height: 100vh; padding-top: var(--head-h); padding-bottom: calc(var(--head-h) + env(safe-area-inset-bottom)); overflow: visible; border-radius: 0; background: var(--surface-raised); }
  .os-head { display: none; }
  .os-body { overflow: visible; }

  .os-bottombar {
    position: fixed;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: var(--z-panel);
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    height: var(--head-h);
    padding-bottom: env(safe-area-inset-bottom);
    border-top: 1px solid var(--border-soft);
    border-radius: var(--radius) var(--radius) 0 0;
    background: var(--surface);
  }
  .os-tab { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; min-width: 0; color: var(--text-secondary); font-size: 11px; line-height: 14px; text-align: center; }
  .os-tab .icon { flex: 0 0 20px; width: 20px; height: 20px; }
  .os-tab.is-current { color: var(--text-strong); font-weight: 600; }

  .os-panel { position: fixed; inset: 0; z-index: calc(var(--z-drawer) + 1); width: auto; border-radius: 0; box-shadow: none; }
  .os-pop { right: 16px; left: 16px; width: auto; z-index: calc(var(--z-drawer) + 2); }
}

@media (prefers-reduced-motion: reduce) {
  .os-rail { transition: none !important; }
}
