/*
 * Компоненты LLM-портала: оболочка (меню + шапка), кнопки, карточки,
 * бейджи, модалки, таблицы, формы, сетка плиток.
 * Внешних ресурсов нет; всё строится на токенах tokens.css.
 */

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

/*
 * UA-стиль [hidden] = display: none перебивается авторскими display
 * (например, .btn { display: inline-flex }), из-за чего гость видел
 * «Выйти». Правило с !important возвращает скрытие.
 */
[hidden] {
  display: none !important;
}

body {
  min-height: 100vh;
  background: hsl(var(--background));
  color: hsl(var(--foreground));
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a {
  color: hsl(var(--primary));
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

h1,
h2,
h3,
h4 {
  margin: 0;
  font-weight: 700;
  line-height: 1.25;
}

p {
  margin: 0;
}

/* ---------- Каркас: тёмное меню слева + шапка ---------- */

.portal-side {
  position: fixed;
  inset: 0 auto 0 0;
  z-index: 40;
  display: flex;
  flex-direction: column;
  width: var(--side-w);
  padding: 12px 10px;
  color: hsl(var(--side-fg));
  background: hsl(var(--side-bg));
  border-right: 1px solid hsl(var(--side-border));
  transition: width 0.16s ease, transform 0.16s ease;
}

.side-top {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 6px 14px;
}

.side-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  color: hsl(var(--side-fg));
  font-weight: 700;
  letter-spacing: 0.01em;
}

.side-brand:hover {
  text-decoration: none;
}

.side-logo {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  background: hsl(var(--primary));
  color: hsl(var(--primary-foreground));
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
}

.side-brand-text {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.side-toggle {
  margin-left: auto;
  flex: 0 0 auto;
  width: 26px;
  height: 26px;
  padding: 0;
  border: 1px solid hsl(var(--side-border));
  border-radius: var(--radius-xs);
  background: transparent;
  color: hsl(var(--side-muted));
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
}

.side-toggle:hover {
  background: hsl(var(--side-hover));
  color: hsl(var(--side-fg));
}

.side-nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow-y: auto;
  padding: 4px 0;
}

.side-group-label {
  margin: 12px 10px 6px;
  color: hsl(var(--side-muted));
  font-family: var(--font-mono);
  font-size: 9.5px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  white-space: nowrap;
  overflow: hidden;
}

.side-link {
  display: flex;
  align-items: center;
  gap: 10px;
  height: 36px;
  padding: 0 10px;
  border-radius: var(--radius-sm);
  color: hsl(var(--side-muted));
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
}

.side-link:hover {
  background: hsl(var(--side-hover));
  color: hsl(var(--side-fg));
  text-decoration: none;
}

.side-link[aria-current='page'] {
  background: hsl(var(--side-active-bg));
  color: hsl(var(--side-active-fg));
  font-weight: 600;
}

.side-link-icon {
  display: inline-flex;
  flex: 0 0 auto;
  width: 18px;
  height: 18px;
}

.side-link-icon svg {
  width: 18px;
  height: 18px;
}

.side-link-text {
  overflow: hidden;
  text-overflow: ellipsis;
}

.side-user {
  margin-top: auto;
  padding: 12px 6px 4px;
  border-top: 1px solid hsl(var(--side-border));
}

.side-user-name {
  overflow: hidden;
  color: hsl(var(--side-fg));
  font-family: var(--font-mono);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.side-user-role {
  margin-top: 2px;
  color: hsl(var(--side-muted));
  font-family: var(--font-mono);
  font-size: 9.5px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.side-logout {
  width: 100%;
  margin-top: 10px;
  height: 30px;
  border: 1px solid hsl(var(--side-border));
  border-radius: var(--radius-xs);
  background: transparent;
  color: hsl(var(--side-muted));
  font-family: var(--font-sans);
  font-size: 12.5px;
  cursor: pointer;
}

.side-logout:hover {
  background: hsl(var(--side-hover));
  color: hsl(var(--side-fg));
}

.portal-navbar {
  position: fixed;
  top: 0;
  right: 0;
  left: var(--side-w);
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 12px;
  height: var(--navbar-h);
  padding: 0 20px;
  background: hsl(var(--card) / 0.92);
  border-bottom: 1px solid hsl(var(--border));
  backdrop-filter: blur(6px);
  transition: left 0.16s ease;
}

.navbar-menu-btn {
  display: none;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius-xs);
  background: transparent;
  color: hsl(var(--foreground));
  cursor: pointer;
}

.navbar-title {
  font-size: 13px;
  font-weight: 600;
  color: hsl(var(--muted-foreground));
}

.navbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-left: auto;
}

.navbar-user {
  font-family: var(--font-mono);
  font-size: 12px;
  color: hsl(var(--muted-foreground));
}

.navbar-guest {
  font-size: 12px;
  color: hsl(var(--muted-foreground));
}

.theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--ctrl-h);
  height: var(--ctrl-h);
  padding: 0;
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius-sm);
  background: hsl(var(--card));
  color: hsl(var(--muted-foreground));
  cursor: pointer;
}

.theme-toggle:hover {
  color: hsl(var(--foreground));
  border-color: hsl(var(--primary) / 0.45);
}

.theme-toggle svg {
  width: 16px;
  height: 16px;
}

/* Свёрнутое меню — рельс */
body.side-collapsed .portal-side {
  width: var(--side-cw);
}

body.side-collapsed .portal-navbar {
  left: var(--side-cw);
}

body.side-collapsed #view {
  margin-left: var(--side-cw);
}

body.side-collapsed .side-brand-text,
body.side-collapsed .side-group-label,
body.side-collapsed .side-link-text,
body.side-collapsed .side-user-name,
body.side-collapsed .side-user-role,
body.side-collapsed .side-logout {
  display: none;
}

body.side-collapsed .side-top {
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

body.side-collapsed .side-toggle {
  margin-left: 0;
}

body.side-collapsed .side-link {
  justify-content: center;
  padding: 0;
}

body.side-collapsed .side-user {
  display: flex;
  justify-content: center;
}

body.side-collapsed .side-user::after {
  content: '';
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: hsl(var(--ok));
}

/* ---------- Контент ---------- */

#view {
  margin-left: var(--side-w);
  padding: calc(var(--navbar-h) + 24px) 24px 40px;
  transition: margin-left 0.16s ease;
}

.page-header {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 20px;
}

.page-title {
  font-size: 24px;
  letter-spacing: -0.01em;
}

.page-subtitle {
  margin-top: 4px;
  color: hsl(var(--muted-foreground));
  font-size: 13px;
}

.page-actions {
  display: flex;
  gap: 8px;
  margin-left: auto;
}

.section-title {
  margin: 24px 0 12px;
  color: hsl(var(--muted-foreground));
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

/* ---------- Кнопки ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: var(--ctrl-h);
  padding: 0 14px;
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius-sm);
  background: hsl(var(--card));
  color: hsl(var(--foreground));
  font-family: var(--font-sans);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease;
}

.btn:hover {
  border-color: hsl(var(--primary) / 0.45);
  text-decoration: none;
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn-sm {
  height: 26px;
  padding: 0 10px;
  border-radius: var(--radius-xs);
  font-size: 12px;
}

.btn-primary {
  border-color: transparent;
  background: hsl(var(--primary));
  color: hsl(var(--primary-foreground));
}

.btn-primary:hover {
  background: hsl(var(--primary) / 0.9);
  border-color: transparent;
}

.btn-ghost {
  border-color: transparent;
  background: transparent;
  color: hsl(var(--muted-foreground));
}

.btn-ghost:hover {
  background: hsl(var(--muted));
  color: hsl(var(--foreground));
}

.btn-destructive {
  border-color: transparent;
  background: transparent;
  color: hsl(var(--destructive));
}

.btn-destructive:hover {
  background: hsl(var(--bad) / 0.12);
  border-color: hsl(var(--bad) / 0.4);
}

/* ---------- Карточки ---------- */

.card {
  background: hsl(var(--card));
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius);
}

.card-pad {
  padding: 18px 20px;
}

.card-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 20px;
  border-bottom: 1px solid hsl(var(--border));
}

.card-title {
  font-size: 15px;
  font-weight: 650;
}

.card-body {
  padding: 18px 20px;
}

.card-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 14px 20px;
  border-top: 1px solid hsl(var(--border));
}

.empty-state {
  padding: 40px 24px;
  border: 1px dashed hsl(var(--border));
  border-radius: var(--radius);
  background: hsl(var(--card) / 0.5);
  color: hsl(var(--muted-foreground));
  text-align: center;
}

/* ---------- Бейджи статусов ---------- */

.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 9px;
  border-radius: var(--radius-badge);
  font-family: var(--font-mono);
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  white-space: nowrap;
}

.badge-ok,
.badge-work {
  background: hsl(var(--ok) / 0.13);
  color: hsl(var(--ok));
}

.badge-warn,
.badge-new {
  background: hsl(var(--warn) / 0.14);
  color: hsl(var(--warn));
}

.badge-bad,
.badge-archived {
  background: hsl(var(--bad) / 0.13);
  color: hsl(var(--bad));
}

.badge-blue {
  background: hsl(var(--blue) / 0.13);
  color: hsl(var(--blue));
}

.badge-purple {
  background: hsl(var(--purple) / 0.13);
  color: hsl(var(--purple));
}

.badge-teal {
  background: hsl(var(--teal) / 0.13);
  color: hsl(var(--teal));
}

.badge-primary {
  background: hsl(var(--primary) / 0.12);
  color: hsl(var(--primary));
}

/* ---------- Сетка плиток проектов ---------- */

.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 12px;
}

.tile {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 16px;
  background: hsl(var(--card));
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius-tile);
  color: inherit;
  cursor: pointer;
  transition: border-color 0.12s ease, background 0.12s ease;
}

.tile:hover {
  border-color: hsl(var(--primary) / 0.45);
  background: hsl(var(--primary) / 0.04);
  text-decoration: none;
}

.tile-head {
  display: flex;
  align-items: flex-start;
  gap: 10px;
}

.tile-name {
  overflow: hidden;
  font-family: var(--font-mono);
  font-size: 13px;
  font-weight: 600;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tile-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: auto;
  color: hsl(var(--muted-foreground));
  font-family: var(--font-mono);
  font-size: 10.5px;
  letter-spacing: 0.04em;
}

/* ---------- Стартовая страница: группы и превью ---------- */

.project-group-head {
  display: flex;
  align-items: center;
  gap: 10px;
}

.project-group-head .section-title {
  margin-bottom: 0;
}

.project-group {
  margin-bottom: 22px;
}

.project-group .tiles {
  margin-top: 12px;
}

.tile-owner::after {
  content: "·";
  margin-left: 8px;
}

.preview-facts {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 6px 14px;
  align-items: baseline;
  font-size: 13px;
}

.preview-label {
  color: hsl(var(--muted-foreground));
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.preview-about,
.memory-content {
  margin: 0;
  padding: 14px 16px;
  overflow-x: auto;
  background: hsl(var(--muted) / 0.45);
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius);
  color: hsl(var(--foreground));
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
}

.memory-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.memory-title {
  margin: 0;
  color: hsl(var(--muted-foreground));
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.memory-content.muted {
  color: hsl(var(--muted-foreground));
  font-style: italic;
}

/* ---------- Таблицы ---------- */

.table-wrap {
  overflow-x: auto;
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius);
  background: hsl(var(--card));
}

table.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.table th {
  padding: 10px 14px;
  border-bottom: 1px solid hsl(var(--border));
  color: hsl(var(--muted-foreground));
  font-family: var(--font-mono);
  font-size: 9.5px;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-align: left;
  text-transform: uppercase;
  white-space: nowrap;
}

.table td {
  padding: 11px 14px;
  border-bottom: 1px solid hsl(var(--border));
  vertical-align: middle;
}

.table tbody tr:last-child td {
  border-bottom: 0;
}

.table tbody tr:hover {
  background: hsl(var(--muted) / 0.45);
}

.table .mono {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

/* ---------- Формы ---------- */

.form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field-label {
  color: hsl(var(--muted-foreground));
  font-family: var(--font-mono);
  font-size: 9.5px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.input,
.select,
.textarea {
  width: 100%;
  min-height: var(--ctrl-h);
  padding: 6px 11px;
  border: 1px solid hsl(var(--input));
  border-radius: var(--radius-sm);
  background: hsl(var(--card));
  color: hsl(var(--foreground));
  font-family: var(--font-sans);
  font-size: 13px;
  outline: none;
}

.input:focus,
.select:focus,
.textarea:focus {
  border-color: hsl(var(--ring));
  box-shadow: 0 0 0 3px hsl(var(--ring) / 0.16);
}

.textarea {
  min-height: 96px;
  resize: vertical;
}

.form-hint {
  color: hsl(var(--muted-foreground));
  font-size: 12px;
}

.form-error {
  padding: 8px 11px;
  border: 1px solid hsl(var(--bad) / 0.4);
  border-radius: var(--radius-sm);
  background: hsl(var(--bad) / 0.1);
  color: hsl(var(--bad));
  font-size: 12.5px;
}

/* ---------- Админка ---------- */

.admin-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(260px, 340px);
  gap: 16px;
  align-items: start;
}

@media (max-width: 860px) {
  .admin-grid {
    grid-template-columns: minmax(0, 1fr);
  }
}

/* ---------- Модалки ---------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: hsl(var(--side-bg) / 0.55);
}

.modal {
  width: 100%;
  max-width: 440px;
  max-height: calc(100vh - 48px);
  overflow-y: auto;
  background: hsl(var(--card));
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius-lg);
}

.modal-lg {
  max-width: 720px;
}

.modal-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 20px;
  border-bottom: 1px solid hsl(var(--border));
}

.modal-title {
  font-size: 16px;
  font-weight: 700;
}

.modal-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  margin-left: auto;
  padding: 0;
  border: 1px solid transparent;
  border-radius: var(--radius-xs);
  background: transparent;
  color: hsl(var(--muted-foreground));
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
}

.modal-close:hover {
  background: hsl(var(--muted));
  color: hsl(var(--foreground));
}

.modal-body {
  padding: 20px;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 14px 20px;
  border-top: 1px solid hsl(var(--border));
}

/* ---------- Служебные утилиты ---------- */

.mono {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.muted {
  color: hsl(var(--muted-foreground));
}

.stack {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.spacer {
  flex: 1;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ---------- Телефон ---------- */

@media (max-width: 820px) {
  .portal-side {
    transform: translateX(-100%);
  }

  body.side-open .portal-side {
    transform: translateX(0);
  }

  body.side-open::after {
    content: '';
    position: fixed;
    inset: 0;
    z-index: 35;
    background: hsl(var(--side-bg) / 0.45);
  }

  .portal-navbar {
    left: 0;
  }

  #view,
  body.side-collapsed #view {
    margin-left: 0;
    padding: calc(var(--navbar-h) + 18px) 16px 32px;
  }

  .navbar-menu-btn {
    display: inline-flex;
  }

  body.side-collapsed .portal-side {
    width: var(--side-w);
  }

  body.side-collapsed .side-brand-text,
  body.side-collapsed .side-group-label,
  body.side-collapsed .side-link-text,
  body.side-collapsed .side-user-name,
  body.side-collapsed .side-user-role,
  body.side-collapsed .side-logout {
    display: initial;
  }
}
