/* ── A11Y HELPERS ── */
.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0);
  white-space: nowrap; border: 0;
}
.skip-link {
  position: absolute;
  top: -40px; left: 8px;
  z-index: 20000;
  background: var(--accent);
  color: white;
  padding: 8px 14px;
  border-radius: 8px;
  text-decoration: none;
  font-family: var(--font-b);
  font-size: 13px;
  font-weight: 600;
  transition: top 0.18s;
}
.skip-link:focus { top: 8px; outline: 2px solid white; outline-offset: 2px; }

/* Global focus-visible for keyboard nav */
*:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
button:focus-visible, a:focus-visible, [role="button"]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ── APP SHELL GRID ── */
body {
  display: grid;
  grid-template-rows: 56px 1fr;
  grid-template-columns: 240px 1fr;
  grid-template-areas: "top top" "side chat";
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

/* ── TOPBAR ── */
.topbar {
  grid-area: top;
  background: var(--topbar-bg);
  color: var(--topbar-fg);
  display: flex;
  align-items: center;
  padding: 0 28px;
  gap: 0;
  border-bottom: 1px solid rgba(255,255,255,0.06);
  position: relative;
  z-index: 10;
}

/* Hamburger toggle — hidden on desktop, shown on mobile */
.sidebar-toggle {
  display: none;
  width: 36px; height: 36px;
  border-radius: 8px;
  background: transparent;
  border: 1px solid rgba(255,255,255,0.12);
  color: rgba(255,255,255,0.7);
  align-items: center;
  justify-content: center;
  cursor: pointer;
  margin-right: 12px;
  transition: background 0.15s, color 0.15s;
}
.sidebar-toggle:hover { background: rgba(255,255,255,0.08); color: white; }

.logo-sub {
  font-size: 12px;
  font-family: var(--font-b);
  font-weight: 400;
  color: var(--ink-dim);
  font-style: normal;
}

.topbar-logo {
  font-family: var(--font-d);
  font-size: 21px;
  font-weight: 900;
  letter-spacing: -0.02em;
  color: var(--topbar-fg);
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
}

.logo-gavel {
  width: 30px; height: 30px;
  border-radius: 50%;
  background: var(--accent-dim);
  border: 1px solid var(--accent-mid);
  display: flex; align-items: center; justify-content: center;
  animation: logoPulse 4s ease-in-out infinite;
}
.logo-gavel svg { width: 15px; height: 15px; }
@keyframes logoPulse {
  0%,100%{box-shadow:0 0 0 0 var(--accent-dim)}
  50%{box-shadow:0 0 0 6px transparent}
}

.logo-accent { color: var(--accent); font-style: italic; }

.topbar-center {
  position: absolute; left: 50%; transform: translateX(-50%);
  display: flex; align-items: center; gap: 6px;
}

.status-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--mint);
  animation: pulse-dot 2.5s ease-in-out infinite;
}
@keyframes pulse-dot {
  0%,100%{opacity:1;transform:scale(1)}
  50%{opacity:0.5;transform:scale(0.8)}
}

.status-label {
  font-size: 11px;
  font-weight: 500;
  color: var(--ink-dim);
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.topbar-right { display: flex; align-items: center; gap: 12px; }

.tweaks-btn {
  width: 30px; height: 30px;
  border-radius: 8px;
  background: transparent;
  border: 1px solid rgba(255,255,255,0.12);
  color: rgba(255,255,255,0.5);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.tweaks-btn:hover { background: rgba(255,255,255,0.08); color: rgba(255,255,255,0.9); border-color: rgba(255,255,255,0.25); }

.topbar-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 6px;
  color: rgba(255,255,255,0.6);
  text-decoration: none;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  transition: color 0.15s, background 0.15s;
}
.topbar-link:hover { color: var(--accent); background: rgba(255,255,255,0.06); }

.user-pill {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 12px 4px 4px;
  border-radius: 20px;
  background: rgba(255,255,255,0.06);
  cursor: pointer;
  transition: background 0.15s;
}
.user-pill:hover { background: rgba(255,255,255,0.12); }

.user-avatar {
  width: 26px; height: 26px;
  border-radius: 50%;
  background: var(--accent-dim);
  border: 1px solid var(--accent-mid);
  display: flex; align-items: center; justify-content: center;
  font-family: var(--font-d);
  font-size: 12px;
  font-weight: 700;
  color: var(--accent);
}
.user-name {
  font-size: 12px;
  font-weight: 500;
  color: rgba(255,255,255,0.9);
  max-width: 110px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.user-pill-caret {
  color: rgba(255,255,255,0.55);
  transition: transform 0.18s, color 0.15s;
  flex-shrink: 0;
}
.user-pill.open .user-pill-caret { transform: rotate(180deg); color: rgba(255,255,255,0.9); }

.user-menu {
  display: none;
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  width: 268px;
  max-width: calc(100vw - 24px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.25);
  padding: 8px;
  z-index: 100;
  animation: menuIn 0.18s cubic-bezier(.34,1.56,.64,1) both;
  cursor: default;
}
.user-pill.open .user-menu { display: block; }
@keyframes menuIn { from { opacity: 0; transform: translateY(-4px); } }

.user-menu-section {
  padding: 10px 10px 12px;
}
.user-menu-section + .user-menu-section { border-top: 1px solid var(--border-2); }

.user-menu-label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--ink-dim);
  margin-bottom: 8px;
}

.user-menu-hint {
  font-size: 11.5px;
  color: var(--ink-faint);
  line-height: 1.4;
  margin-top: 2px;
}

.user-menu-toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.user-menu-toggle-row > div { flex: 1; min-width: 0; }
.user-menu-toggle-row .user-menu-label { margin-bottom: 0; }

.user-menu-divider {
  height: 1px;
  background: var(--border-2);
  margin: 4px 0;
}

.user-menu-logout {
  width: 100%;
  padding: 9px 12px;
  background: transparent;
  border: 0;
  text-align: left;
  font-family: var(--font-b);
  font-size: 13px;
  color: var(--ink);
  cursor: pointer;
  border-radius: 7px;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: background 0.12s;
}
.user-menu-logout:hover { background: var(--surface-2); }

.theme-toggle {
  width: 36px; height: 20px;
  border-radius: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  position: relative;
  cursor: pointer;
  transition: background 0.3s;
  /* button reset (was a div in the topbar) */
  padding: 0;
  appearance: none;
  -webkit-appearance: none;
}
.theme-toggle::after {
  content: '';
  position: absolute;
  top: 2px; left: 2px;
  width: 14px; height: 14px;
  border-radius: 50%;
  background: var(--accent);
  transition: transform 0.3s cubic-bezier(.34,1.56,.64,1);
}
[data-theme="dark"] .theme-toggle::after { transform: translateX(16px); }

.theme-label {
  font-size: 11px;
  color: var(--ink-dim);
  font-weight: 500;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

/* ── SIDEBAR ── */
.sidebar {
  grid-area: side;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: background 0.4s;
}

.sidebar-top { padding: 20px 16px 16px; border-bottom: 1px solid var(--border-2); }

.new-btn {
  width: 100%;
  padding: 10px 14px;
  background: var(--accent-dim);
  border: 1px solid var(--accent-mid);
  border-radius: 8px;
  color: var(--accent);
  font-family: var(--font-b);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center; gap: 7px;
  transition: background 0.18s, transform 0.12s;
  letter-spacing: 0.01em;
}
.new-btn:hover { background: var(--accent-mid); transform: translateY(-1px); }
.new-btn:active { transform: translateY(0); }

/* ── SIDEBAR SEARCH ── */
.sidebar-search {
  position: relative;
  padding: 10px 14px;          /* symmetric → 50% lines up with the input center */
  border-bottom: 1px solid var(--border-2);
  display: flex;               /* aligns the input with the absolute children */
  align-items: center;
}
.hist-search-icon {
  position: absolute;
  left: 22px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--ink-faint);
  pointer-events: none;
}
.hist-search-input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 28px 7px 30px;
  font-family: var(--font-b);
  font-size: 12.5px;
  line-height: 1.4;
  color: var(--ink);
  outline: none;
  transition: border-color 0.18s, box-shadow 0.18s;
  -webkit-appearance: none;
  appearance: none;
}
.hist-search-input::-webkit-search-cancel-button { display: none; }
.hist-search-input:focus {
  border-color: var(--accent-mid);
  box-shadow: 0 0 0 3px var(--accent-dim);
}
.hist-search-input::placeholder { color: var(--ink-faint); }

.hist-search-clear {
  position: absolute;
  right: 19px;
  top: 50%;
  transform: translateY(-50%);
  width: 18px; height: 18px;
  border-radius: 50%;
  background: var(--ink-faint);
  border: 0;
  color: var(--surface);
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: background 0.15s;
}
.hist-search-clear:hover { background: var(--ink-dim); }

.hist-item-preview {
  display: block;
  font-size: 11.5px;
  line-height: 1.4;
  color: var(--ink-faint);
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.hist-item-preview mark {
  background: var(--accent-dim);
  color: var(--accent);
  padding: 0 2px;
  border-radius: 3px;
  font-weight: 500;
}
.hist-item.active .hist-item-preview { color: var(--ink-mid); }

.sidebar-section {
  padding: 16px 16px 8px;
}
.sidebar-section-label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin-bottom: 8px;
}

.cat-list { display: flex; flex-direction: column; gap: 2px; }
.cat-item {
  padding: 8px 10px;
  border-radius: 7px;
  font-size: 13px;
  color: var(--ink-dim);
  cursor: pointer;
  display: flex; align-items: center; gap: 9px;
  transition: background 0.15s, color 0.15s;
  font-weight: 400;
  border: 1px solid transparent;
}
.cat-item:hover { background: var(--surface-2); color: var(--ink-mid); }
.cat-item.active { background: var(--accent-dim); color: var(--accent); border-color: var(--accent-mid); font-weight: 500; }

.cat-pip { width: 6px; height: 6px; border-radius: 50%; background: currentColor; flex-shrink: 0; opacity: 0.7; }

.hist-scroll { flex: 1; overflow-y: auto; padding: 4px 10px 16px; }
.hist-scroll::-webkit-scrollbar { width: 3px; }
.hist-scroll::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

.hist-item {
  padding: 6px 8px;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 5px;
  transition: background 0.13s;
  position: relative;
}
.hist-item:hover { background: var(--surface-2); }
.hist-item.active { background: var(--accent-dim); }

.hist-item-info { flex: 1; min-width: 0; }
.hist-item-title {
  display: block;
  font-size: 12.5px;
  line-height: 1.35;
  color: var(--ink-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: color 0.13s;
}
.hist-item:hover .hist-item-title { color: var(--ink-mid); }
.hist-item.active .hist-item-title { color: var(--ink); font-weight: 500; }

.hist-item-age {
  display: block;
  font-size: 10px;
  line-height: 1.2;
  color: var(--ink-faint);
  margin-top: 1px;
}

.hist-edit,
.hist-del {
  width: 20px; height: 20px;
  border-radius: 4px;
  background: none;
  border: none;
  color: var(--ink-dim);
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  line-height: 1;
  flex-shrink: 0;
  transition: background 0.12s, color 0.12s;
  padding: 0;
}
.hist-del { font-size: 15px; }
.hist-item:hover .hist-edit,
.hist-item:hover .hist-del { display: flex; }
.hist-edit:hover { background: var(--accent-dim); color: var(--accent); }
.hist-del:hover { background: var(--rose-dim); color: var(--rose); }

/* Inline rename input replaces .hist-item-title while editing */
.hist-item.editing { background: var(--surface-2); }
.hist-item-rename {
  flex: 1; min-width: 0;
  font: inherit;
  font-size: 12.5px;
  background: var(--bg);
  color: var(--ink);
  border: 1px solid var(--accent-mid);
  border-radius: 5px;
  padding: 3px 6px;
  outline: none;
  width: 100%;
}
.hist-item-rename:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-dim);
}

.hist-empty {
  padding: 8px 10px;
  font-size: 12px;
  color: var(--ink-faint);
  font-style: italic;
}

/* Suppress animation when loading historic messages */
.msg-wrap.no-anim { animation: none !important; }

/* ── CHAT AREA (grid slot) ── */
.chat-wrap {
  grid-area: chat;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
  transition: background 0.4s;
}

/* ── SIDEBAR BACKDROP (mobile drawer) ── */
.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 56px 0 0 0;
  background: rgba(0,0,0,0.45);
  z-index: 14;
  opacity: 0;
  transition: opacity 0.22s;
}
body.sidebar-open .sidebar-backdrop {
  display: block;
  opacity: 1;
}

/* ── RESPONSIVE ── */
@media (max-width: 680px) {
  body {
    grid-template-columns: 1fr;
    grid-template-areas: "top" "chat";
  }

  .sidebar-toggle { display: inline-flex; }
  .topbar { padding: 0 12px; }
  .topbar-center { display: none; }
  .logo-sub { display: none; }
  .topbar-logo { font-size: 18px; gap: 8px; flex: 0 0 auto; }
  .theme-label { display: none; }
  .topbar-link span { display: none; }
  .topbar-right { gap: 8px; }
  .user-name { max-width: 70px; }

  /* Sidebar becomes a drawer over the chat */
  .sidebar {
    grid-area: side;
    position: fixed;
    top: 56px;
    left: 0;
    bottom: 0;
    width: min(320px, 85vw);
    z-index: 15;
    transform: translateX(-100%);
    transition: transform 0.24s cubic-bezier(.34,1.06,.64,1);
    box-shadow: 4px 0 24px rgba(0,0,0,0.18);
  }
  body.sidebar-open .sidebar {
    transform: translateX(0);
  }

  /* Tweaks panel — full-width sheet on small screens */
  .tweaks-panel {
    bottom: 0; right: 0;
    left: 0;
    width: 100%;
    border-radius: 14px 14px 0 0;
  }
}
