@layer reset, tokens, base, layout, components;

@layer components {
  .sidebar-frame {
    grid-column: 1;
    grid-row: 1 / 3;
    display: flex;
    flex-direction: column;
    width: clamp(220px, 20vw, 280px);
    min-width: 0;
    min-height: 0;
    overflow: hidden;
    padding-block: var(--space-4);
    border-right: 1px solid var(--line);
  }
  .sidebar-frame.folded {
    width: max-content;
    overflow: hidden;
    padding-inline: 0;
  }
  .sidebar-frame-content {
    flex: 1 1 auto;
    min-width: 0;
    min-height: 0;
    overflow-y: auto;
    padding-inline: var(--space-3);
  }
  .sidebar-frame-content,
  .global-sidebar,
  .workspace-sidebar {
    min-width: 0;
  }
  .global-sidebar,
  .global-sidebar-section,
  .workspace-sidebar {
    display: grid;
    gap: var(--space-2);
  }
  .sidebar-header {
    display: grid;
    gap: var(--space-2);
    margin-bottom: var(--space-2);
    min-width: 0;
  }
  .sidebar-control-row {
    display: flex;
    justify-content: flex-end;
    gap: var(--space-1);
  }
  .sidebar-control-row {
    margin-top: auto;
    padding: var(--space-4) var(--space-3) 0;
  }
  .sidebar-frame.folded .sidebar-control-row {
    justify-content: center;
    padding-inline: var(--space-2);
    padding-top: 0;
  }
  .workspace-label {
    flex: 1 1 auto;
    min-width: 0;
  }
  .workspace-name {
    display: block;
    min-width: 0;
    overflow: hidden;
    border-radius: var(--radius-soft);
    padding: 0.45rem 0.6rem;
    color: var(--text-strong);
    font-size: 1.05rem;
    font-weight: 800;
    line-height: 1.25;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .workspace-menu {
    position: relative;
    min-width: 0;
  }
  .workspace-menu-trigger {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2);
    width: 100%;
    min-width: 0;
    border: 0;
    border-radius: var(--radius-soft);
    padding: 0.45rem 0.6rem;
    background: transparent;
    color: var(--text-strong);
    font: inherit;
    font-size: 1.05rem;
    font-weight: 800;
    line-height: 1.25;
    text-align: left;
    cursor: pointer;
  }
  .workspace-menu-trigger > span {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .workspace-menu-trigger > svg,
  .workspace-menu-item > svg {
    flex: 0 0 auto;
    width: 1rem;
    height: 1rem;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
  }
  .workspace-menu-trigger > svg {
    transition: transform 140ms ease;
  }
  .workspace-menu-trigger[aria-expanded="true"] > svg {
    transform: rotate(180deg);
  }
  .workspace-menu-trigger:hover,
  .workspace-menu-trigger:focus-visible {
    background: var(--interactive-hover);
  }
  .workspace-menu-trigger:focus-visible,
  .workspace-menu-add:focus-visible,
  .workspace-menu-item:focus-visible {
    outline: 1px solid var(--accent);
    outline-offset: 1px;
  }
  .workspace-menu-popover {
    position: absolute;
    z-index: 20;
    inset-inline: 0;
    top: calc(100% + var(--space-1));
    display: grid;
    gap: var(--space-1);
    min-width: 0;
    max-height: min(26rem, calc(100vh - 6rem));
    overflow-y: auto;
    border: 1px solid var(--line);
    border-radius: calc(var(--radius-soft) + 0.2rem);
    padding: var(--space-2);
    background: var(--bg-raised);
    box-shadow: var(--shadow-soft);
  }
  .workspace-menu-item {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    min-width: 0;
    border-radius: var(--radius-soft);
    padding: var(--space-2);
    color: var(--text-strong);
    font-size: 0.82rem;
    font-weight: 650;
    line-height: 1.3;
    text-decoration: none;
  }
  .workspace-menu-item > span {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .workspace-menu-item:hover,
  .workspace-menu-item:focus-visible,
  .workspace-menu-item.current {
    background: var(--interactive-hover);
  }
  .workspace-menu-item.current {
    color: var(--accent);
  }
  .workspace-menu-separator {
    height: 1px;
    margin: var(--space-1) calc(-1 * var(--space-2));
    background: var(--line);
  }
  .workspace-menu-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2);
    padding: var(--space-1) var(--space-2) 0;
    color: var(--text-faint);
    font-size: 0.68rem;
    font-weight: 750;
    letter-spacing: 0.08em;
    text-transform: uppercase;
  }
  .workspace-menu-add {
    display: grid;
    width: 1.5rem;
    height: 1.5rem;
    place-items: center;
    border-radius: var(--radius-soft);
    color: var(--text-muted);
    font-size: 1rem;
    line-height: 1;
    text-decoration: none;
  }
  .workspace-menu-add:hover,
  .workspace-menu-add:focus-visible {
    background: var(--interactive-hover);
    color: var(--text-strong);
  }
  .workspace-menu-list {
    display: grid;
    gap: 1px;
  }
  .workspace-menu-workspace {
    justify-content: space-between;
  }
  .workspace-menu-check {
    margin-left: auto;
  }
  .workspace-menu-status {
    margin: var(--space-1) var(--space-2);
    color: var(--text-muted);
    font-size: 0.72rem;
    line-height: 1.35;
  }
  .workspace-menu-status.error {
    color: var(--danger);
  }
  .workspace-status {
    margin: var(--space-1) 0 0;
    color: var(--text-muted);
    font-size: 0.78rem;
    line-height: 1.35;
  }
  .workspace-status.error,
  .section-state.error,
  .error {
    color: var(--danger);
  }
  .sidebar-fold-button {
    flex: 0 0 auto;
    display: grid;
    place-items: center;
    width: 32px;
    height: 32px;
    border-radius: var(--radius-soft);
    color: var(--text-muted);
    text-decoration: none;
  }
  .sidebar-fold-button {
    border: 1px solid var(--line);
    background: var(--bg-raised);
    box-shadow: var(--shadow-soft);
    color: var(--text-strong);
    cursor: pointer;
  }
  .sidebar-icon {
    width: 18px;
    height: 18px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
  }
  .sidebar-fold-button:hover,
  .sidebar-fold-button:focus-visible {
    background: var(--interactive-hover);
    color: var(--text-muted);
  }
  .sidebar-sections {
    display: grid;
    gap: var(--space-3);
    min-width: 0;
  }
  .nav-section {
    display: grid;
    gap: var(--space-2);
  }
  .section-heading-row,
  .section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
  }
  .section-heading-row h2,
  .section-header,
  .sidebar-section-label {
    color: var(--text-faint);
    font-size: 0.72rem;
    font-weight: 750;
    letter-spacing: 0.11em;
    text-transform: uppercase;
  }
  .section-heading-row h2,
  .sidebar-section-label {
    margin: 0;
  }
  .section-heading-link {
    color: inherit;
    text-decoration: none;
  }
  .section-heading-link:hover,
  .section-heading-link:focus-visible,
  .section-heading-link.active {
    color: var(--accent);
  }
  .section-count {
    color: var(--text-muted);
    font-size: 0.72rem;
    line-height: 1;
  }
  .nav-list,
  .sidebar-list {
    display: grid;
    gap: var(--space-1);
    margin: 0;
    padding: 0;
    list-style: none;
  }
  .primary-nav-link,
  .nav-item,
  .objective-link,
  .sidebar-link {
    display: grid;
    gap: 3px;
    min-width: 0;
    margin-inline: calc(-1 * var(--space-2));
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-soft);
    color: inherit;
    text-align: left;
    text-decoration: none;
    transition: background-color 140ms ease, color 140ms ease;
  }
  .primary-nav-link {
    color: var(--text-strong);
    font-size: 0.9rem;
    font-weight: 700;
  }
  a.primary-nav-link:hover,
  a.primary-nav-link:focus-visible,
  a.nav-item:hover,
  a.nav-item:focus-visible,
  a.objective-link:hover,
  a.objective-link:focus-visible,
  a.sidebar-link:hover,
  a.sidebar-link:focus-visible {
    background: var(--interactive-hover);
  }
  a.primary-nav-link.active,
  a.nav-item.active,
  a.objective-link.active,
  a.sidebar-link.active {
    background: var(--interactive-selected);
  }
  a.primary-nav-link.active,
  a.nav-item.active .item-title,
  a.objective-link.active .item-title,
  a.sidebar-link.active {
    color: var(--accent);
  }
  .item-title,
  .item-meta {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .item-title {
    color: var(--text-strong);
    font-weight: 650;
  }
  .item-meta {
    color: var(--text-muted);
    font-size: 0.82rem;
  }
  .worker-nav-link {
    display: grid;
    grid-template-columns: 0.75rem minmax(0, 1fr);
    grid-template-rows: auto auto;
    column-gap: var(--space-2);
    row-gap: 0.1rem;
    margin: 0.0625rem 0;
    padding: var(--space-2);
    border-radius: var(--radius-soft);
    color: var(--text-muted);
    text-decoration: none;
  }
  .worker-nav-link:hover {
    background: var(--interactive-hover);
    color: var(--text-strong);
  }
  .worker-nav-link.active {
    background: var(--interactive-selected);
    color: var(--accent);
  }
  .worker-status-indicator {
    grid-column: 1;
    grid-row: 1;
    display: grid;
    width: 0.75rem;
    min-height: 1.1rem;
    place-items: center;
  }
  .worker-status-dot {
    width: 0.5rem;
    height: 0.5rem;
    border-radius: 50%;
    background: var(--success);
  }
  .worker-status-spinner {
    --spinner-color: var(--success);

    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 0.75rem;
    font-size: 0.7rem;
    line-height: 1;
  }
  .worker-status-spinner.is-subworker {
    --spinner-color: var(--tui-magenta);
  }
  .worker-nav-label {
    grid-column: 2;
    grid-row: 1;
    min-width: 0;
    overflow: hidden;
    color: inherit;
    font-size: 0.78rem;
    font-weight: 600;
    line-height: 1.1rem;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .worker-nav-meta {
    grid-column: 2;
    grid-row: 2;
    min-width: 0;
    overflow: hidden;
    color: var(--text-muted);
    font-family: var(--font-mono);
    font-size: 0.66rem;
    line-height: 1rem;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .worker-overflow-toggle {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
    gap: var(--space-2);
    width: 100%;
    margin: var(--space-1) 0 0;
    padding: var(--space-1) var(--space-2);
    align-items: center;
    border: 0;
    border-radius: var(--radius-soft);
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
  }
  .worker-overflow-toggle:hover,
  .worker-overflow-toggle:focus-visible {
    background: var(--interactive-hover);
    color: var(--text-strong);
  }
  .worker-overflow-toggle:focus-visible {
    outline: 1px solid var(--accent);
    outline-offset: 1px;
  }
  .worker-overflow-line {
    height: 1px;
    background: currentColor;
    opacity: 0.45;
  }
  .worker-overflow-chevron {
    width: 0.875rem;
    height: 0.875rem;
    fill: none;
    stroke: currentColor;
    stroke-linecap: round;
    stroke-linejoin: round;
    stroke-width: 1.75;
    transition: transform 140ms ease;
  }
  .worker-overflow-toggle[aria-expanded="true"] .worker-overflow-chevron {
    transform: rotate(180deg);
  }

  @media (max-width: 760px) {
    .sidebar-frame,
    .sidebar-frame.folded {
      grid-column: 1;
      grid-row: 1;
      width: auto;
      border-right: 0;
      border-bottom: 1px solid var(--line);
    }
  }
}
