/* ============================================
   Digital Anima — Responsive Styles
   ============================================ */

/* ── Base Defaults (hidden on desktop) ──────── */

.hamburger-btn {
  display: none;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: none;
  background: none;
  font-size: 1.4rem;
  color: var(--aw-color-text-primary);
  cursor: pointer;
  border-radius: 6px;
  transition: background 0.15s;
  flex-shrink: 0;
}

.hamburger-btn:hover,
.hamburger-btn:active {
  background: var(--aw-color-bg-hover);
}

.hamburger-btn:focus-visible {
  outline: 2px solid var(--aw-color-accent);
  outline-offset: 2px;
}

.sidebar-collapse-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: none;
  background: none;
  color: var(--aw-color-text-primary);
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s, transform 0.15s;
}

.sidebar-collapse-btn:hover,
.sidebar-collapse-btn:active {
  background: var(--aw-color-bg-hover);
}

.sidebar-collapse-btn:focus-visible {
  outline: 2px solid var(--aw-color-accent);
  outline-offset: 2px;
}

.sidebar-collapse-icon {
  width: 18px;
  height: 18px;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
  fill: none;
}

body.sidebar-collapsed .sidebar-collapse-btn {
  transform: rotate(180deg);
}

.mobile-nav-backdrop {
  display: none;
}

.data-table-wrapper {
  width: 100%;
  overflow-x: auto;
}

/* ── Fluid Typography for Headings ─────────── */

.page-header h2 {
  font-size: clamp(1.05rem, 2vw + 0.5rem, 1.25rem);
}

.login-title {
  font-size: clamp(1.15rem, 2vw + 0.5rem, 1.4rem);
}

/* ── Desktop (< 1100px) ────────────────────── */

@media (max-width: 1100px) {
  .panel-right {
    width: 320px;
    min-width: 320px;
  }
}

/* ── Tablet / Small Desktop (< 900px) ──────── */

@media (max-width: 900px) {
  .app-layout {
    flex-direction: column;
    gap: 0;
  }
  .panel-main {
    min-height: 50vh;
  }
  .panel-right {
    width: 100%;
    min-width: 0;
    max-height: 45vh;
  }
}

/* ── Tablet (< 768px) ─────────────────────── */

@media (max-width: 768px) {
  /* --- Layout: stack sidebar below --- */
  .app-shell {
    flex-direction: column;
  }

  /* --- Sidebar Nav: hidden by default, overlay drawer --- */
  .sidebar-nav {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 280px;
    z-index: 1100;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
  }

  .mobile-nav-open .sidebar-nav {
    transform: translateX(0);
  }

  /* --- Mobile nav backdrop --- */
  .mobile-nav-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 1050;
  }

  .mobile-nav-open .mobile-nav-backdrop {
    display: block;
  }

  /* --- Hamburger button visible --- */
  .hamburger-btn {
    display: flex;
  }

  .sidebar-collapse-btn {
    display: none;
  }

  /* --- Right panel: full width stacked below --- */
  .panel-right {
    width: 100%;
    min-width: 0;
    max-height: none;
  }

  /* --- Main content: reduce padding --- */
  .main-content {
    padding: 12px;
  }

  /* Chat page: zero padding for full-bleed unified header */
  .main-content:has(.chat-page-layout) {
    padding: 0;
  }

  /* --- Header: reduce padding --- */
  .app-header {
    padding: 0 12px;
    gap: 8px;
  }

  .app-title {
    font-size: 1rem;
  }

  .person-selector {
    display: none;
  }

  /* --- Login card: compact padding --- */
  .login-card {
    padding: 20px 16px;
    margin: 0 12px;
  }

  /* --- Card grid: smaller min --- */
  .card-grid {
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 12px;
  }

  /* --- Tap targets: 44px minimum --- */
  .btn-logout {
    min-height: 44px;
    padding: 8px 14px;
    font-size: 0.85rem;
    display: inline-flex;
    align-items: center;
  }

  .nav-item {
    min-height: 44px;
    padding: 12px 20px;
    font-size: 0.95rem;
  }

  .page-tab {
    min-height: 44px;
    padding: 10px 12px;
  }

  .memory-tab {
    min-height: 44px;
    padding: 10px 6px;
  }

  .right-tab {
    min-height: 44px;
    padding: 10px 6px;
  }

  .memory-file-item {
    min-height: 44px;
    padding: 10px;
    display: flex;
    align-items: center;
  }

  .session-item {
    min-height: 44px;
    padding: 10px;
  }

  .btn-primary,
  .btn-secondary {
    min-height: 44px;
  }

  .user-btn {
    min-height: 44px;
    padding: 12px 16px;
  }

  .btn-guest {
    min-height: 44px;
    display: inline-flex;
    align-items: center;
  }

  .activity-entry {
    min-height: 44px;
    padding: 8px;
  }

  /* --- Font size minimums (no sub-12px) --- */
  .notification-toast-header {
    font-size: 0.85rem;
  }

  .session-item-meta {
    font-size: 0.8rem;
  }

  .history-turn-meta {
    font-size: 0.8rem;
  }

  .activity-time {
    font-size: 0.8rem;
  }

  .session-section-header {
    font-size: 0.8rem;
  }

  .state-content {
    font-size: 0.85rem;
  }

  .stat-card-label {
    font-size: 0.85rem;
  }

  .tool-indicator {
    font-size: 0.85rem;
  }

  .bootstrap-indicator {
    font-size: 0.85rem;
  }

  .status-badge {
    font-size: 0.85rem;
  }

  .data-table th {
    font-size: 0.85rem;
  }

  .history-section-label {
    font-size: 0.8rem;
  }

  .history-summary-label {
    font-size: 0.8rem;
  }

  /* --- Hide app-header when chat page is active (unified header replaces it) --- */
  body:has(.chat-page-layout) > .app-header {
    display: none;
  }

  /* --- Hide original thread tabs on mobile (replaced by dropdown) --- */
  .thread-tabs {
    display: none !important;
  }

  /* --- Unified header: single-row layout --- */
  .chat-anima-tabs-header {
    flex-wrap: nowrap;
    align-items: center;
    padding: 0 0.4rem;
    gap: 0.3rem;
    height: 44px;
    background: var(--aw-color-bg-primary);
    border-bottom: 1px solid var(--aw-color-border);
  }

  /* Hamburger */
  .chat-unified-hamburger {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: none;
    background: none;
    font-size: 1.25rem;
    color: var(--aw-color-text-primary);
    cursor: pointer;
    border-radius: 6px;
    flex-shrink: 0;
  }
  .chat-unified-hamburger:hover,
  .chat-unified-hamburger:active {
    background: var(--aw-color-bg-hover);
  }

  /* Anima avatar tabs: horizontal scroll */
  .chat-anima-tabs-header .anima-tabs {
    flex: 1;
    min-width: 0;
    min-height: 0;
    flex-wrap: nowrap;
    padding: 0;
    gap: 3px;
    overflow-x: auto;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }
  .chat-anima-tabs-header .anima-tabs::-webkit-scrollbar { display: none; }

  .chat-anima-tabs-header .anima-tab-name {
    display: none;
  }

  .chat-anima-tabs-header .anima-tab-wrap {
    position: relative;
  }

  .chat-anima-tabs-header .anima-tab-close {
    position: absolute;
    top: -5px;
    right: -5px;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 18px;
    height: 18px;
    padding: 0;
    font-size: 0.7rem;
    line-height: 1;
    border-radius: 50%;
    background: var(--aw-color-bg-secondary);
    border: 1px solid var(--aw-color-border);
    color: var(--aw-color-text-muted);
    z-index: 2;
  }

  .chat-anima-tabs-header .anima-tab-close:active {
    background: var(--aw-color-bg-hover);
    color: var(--aw-color-text-primary);
  }

  .chat-anima-tabs-header .anima-tab {
    padding: 2px;
    gap: 0;
    border-radius: 50%;
    flex-shrink: 0;
  }

  .chat-anima-tabs-header .anima-tab-avatar {
    width: 28px;
    height: 28px;
  }

  /* Flatten header-actions so + button flows inline */
  .chat-header-actions {
    display: contents;
  }

  .chat-add-conversation {
    margin-left: 0;
    flex-shrink: 0;
  }

  .chat-add-conversation-btn {
    width: 28px;
    height: 28px;
    padding: 0;
    border-radius: 999px;
    font-size: 0;
    line-height: 0;
  }

  .chat-add-conversation-btn::before {
    content: "+";
    font-size: 1rem;
    line-height: 1;
    color: var(--aw-color-text-primary);
  }

  /* Thread dropdown */
  .chat-thread-dropdown {
    display: flex;
    position: relative;
    flex-shrink: 0;
  }

  .chat-thread-dropdown-btn {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    padding: 4px 8px;
    font-size: 0.78rem;
    font-family: inherit;
    border: 1px solid var(--aw-color-border-medium);
    border-radius: 6px;
    background: var(--aw-color-bg-secondary);
    color: var(--aw-color-text-primary);
    cursor: pointer;
    white-space: nowrap;
    max-width: 100px;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .chat-thread-dropdown-btn:hover,
  .chat-thread-dropdown-btn:active {
    background: var(--aw-color-bg-hover);
  }

  .chat-thread-dropdown-chevron {
    flex-shrink: 0;
    stroke: var(--aw-color-text-muted);
    transition: transform 0.15s;
  }
  .chat-thread-dropdown.open .chat-thread-dropdown-chevron {
    transform: rotate(180deg);
  }

  .chat-thread-dropdown-label {
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .chat-thread-dropdown-menu {
    display: none;
    position: absolute;
    top: calc(100% + 4px);
    right: 0;
    min-width: 180px;
    max-width: 260px;
    max-height: 300px;
    overflow-y: auto;
    background: var(--aw-color-bg-primary);
    border: 1px solid var(--aw-color-border-medium);
    border-radius: 8px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.15);
    z-index: 100;
    padding: 4px 0;
  }
  .chat-thread-dropdown.open .chat-thread-dropdown-menu {
    display: block;
  }

  .chat-thread-dd-item {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 8px 12px;
    font-size: 0.85rem;
    cursor: pointer;
    color: var(--aw-color-text-secondary);
  }
  .chat-thread-dd-item:hover,
  .chat-thread-dd-item:active {
    background: var(--aw-color-bg-hover);
  }
  .chat-thread-dd-item.active {
    color: var(--aw-color-accent-hover);
    font-weight: 600;
    background: color-mix(in srgb, var(--aw-color-accent) 10%, transparent 90%);
  }
  .chat-thread-dd-item.is-streaming {
    color: var(--aw-color-accent-hover);
  }
  .chat-thread-dd-item.has-unread::before {
    content: "";
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--aw-color-accent);
    flex-shrink: 0;
  }
  .chat-thread-dd-item.archived {
    color: var(--aw-color-text-muted);
    font-style: italic;
  }

  .chat-thread-dd-label {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .chat-thread-dd-close {
    padding: 0 4px;
    font-size: 1rem;
    line-height: 1;
    border: none;
    border-radius: 4px;
    background: transparent;
    color: var(--aw-color-text-muted);
    cursor: pointer;
    flex-shrink: 0;
  }
  .chat-thread-dd-close:hover { color: var(--aw-color-text-primary); }

  .chat-thread-dd-restore {
    font-size: 0.85rem;
    color: var(--aw-color-text-muted);
    flex-shrink: 0;
  }

  .chat-thread-dd-sep {
    height: 1px;
    background: var(--aw-color-border-light);
    margin: 4px 8px;
  }

  .chat-thread-dd-new {
    padding: 8px 12px;
    font-size: 0.85rem;
    color: var(--aw-color-accent);
    cursor: pointer;
    font-weight: 500;
  }
  .chat-thread-dd-new:hover {
    background: var(--aw-color-bg-hover);
  }

  /* Info panel button */
  .chat-unified-info-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border-radius: 6px;
    border: none;
    background: none;
    color: var(--aw-color-text-muted);
    cursor: pointer;
    flex-shrink: 0;
  }
  .chat-unified-info-btn:hover,
  .chat-unified-info-btn:active {
    background: var(--aw-color-bg-hover);
    color: var(--aw-color-text-primary);
  }

  /* User icon */
  .chat-unified-user-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    border: 1.5px solid var(--aw-color-border-medium);
    background: var(--aw-color-bg-secondary);
    cursor: pointer;
    flex-shrink: 0;
    position: relative;
  }
  .chat-unified-user-btn:hover,
  .chat-unified-user-btn:active {
    border-color: var(--aw-color-accent);
  }

  .chat-unified-user-initial {
    font-size: 0.72rem;
    font-weight: 700;
    color: var(--aw-color-text-primary);
    text-transform: uppercase;
    line-height: 1;
  }

  /* User menu dropdown */
  .chat-unified-user-menu {
    display: none;
    position: absolute;
    top: 44px;
    right: 0.4rem;
    min-width: 160px;
    background: var(--aw-color-bg-primary);
    border: 1px solid var(--aw-color-border-medium);
    border-radius: 8px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.15);
    z-index: 200;
    padding: 8px 0;
  }
  .chat-unified-user-menu.open {
    display: block;
  }

  .chat-unified-user-name {
    padding: 6px 14px;
    font-size: 0.88rem;
    font-weight: 600;
    color: var(--aw-color-text-primary);
  }

  .chat-unified-user-status {
    padding: 2px 14px 6px;
    font-size: 0.78rem;
    color: var(--aw-color-text-muted);
  }

  .chat-unified-user-sep {
    border: none;
    height: 1px;
    background: var(--aw-color-border-light);
    margin: 4px 8px;
  }

  .chat-unified-user-logout {
    display: block;
    width: calc(100% - 16px);
    margin: 0 8px;
    padding: 8px 6px;
    font-size: 0.82rem;
    font-family: inherit;
    border: none;
    border-radius: 6px;
    background: transparent;
    color: var(--aw-color-error, #dc2626);
    cursor: pointer;
    text-align: left;
  }
  .chat-unified-user-logout:hover {
    background: var(--aw-color-bg-hover);
  }

  /* --- Chat input: 2x2 grid buttons, uniform 32px --- */
  .chat-input-form {
    padding: 8px;
  }

  .chat-input {
    max-height: 140px;
  }

  .chat-input-actions {
    grid-template-columns: repeat(2, 32px);
    grid-auto-rows: 32px;
    gap: 4px;
  }

  .chat-send-btn,
  .chat-attach-btn,
  .chat-queue-btn {
    width: 32px;
    height: 32px;
  }

  .voice-controls-slot {
    width: 32px;
    height: 32px;
  }

  .chat-input-actions .voice-controls {
    width: 32px;
    height: 32px;
    min-width: 32px;
    min-height: 32px;
    margin-right: 0;
  }

  .chat-input-actions .voice-mic-btn {
    width: 32px;
    height: 32px;
  }

  .chat-input-actions .voice-mic-btn svg {
    width: 16px;
    height: 16px;
  }

  .chat-bubble.user {
    max-width: 90%;
  }

  /* --- Toast notifications: full width --- */
  #notificationToasts {
    left: 8px;
    right: 8px;
    max-width: none;
  }

  /* --- Data table scroll wrapper --- */
  .data-table-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* --- Page header --- */
  .page-header {
    margin-bottom: 16px;
  }

  /* --- Card body/header: compact --- */
  .card-header {
    padding: 12px 14px;
  }

  .card-body {
    padding: 12px 14px;
  }

  /* --- Stat card: compact --- */
  .stat-card {
    padding: 14px;
  }

  /* --- Empty state: compact --- */
  .empty-state {
    padding: 32px 16px;
  }

  /* --- Body scroll for mobile layout --- */
  body.mobile-nav-open {
    overflow: hidden;
  }
}

/* ── Mobile (< 640px) ─────────────────────── */

@media (max-width: 640px) {
  .card-grid {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .page-tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .page-tab {
    white-space: nowrap;
    flex-shrink: 0;
    padding: 10px;
    font-size: 0.85rem;
  }

  .chat-messages {
    padding: 10px 12px;
  }

  .stat-card-value {
    font-size: 1.3rem;
  }

  .memory-tabs {
    padding: 8px 8px 0;
  }

  .right-tabs {
    padding: 8px 8px 0;
  }

  .app-title {
    font-size: 0.95rem;
  }
}

/* ── Small Phone (< 480px) ────────────────── */

@media (max-width: 480px) {
  .main-content {
    padding: 8px;
  }

  .app-header {
    padding: 0 8px;
    height: 44px;
  }

  .login-card {
    padding: 16px 12px;
    margin: 0 8px;
  }

  .chat-input-form {
    padding: 6px;
    gap: 6px;
  }

  .chat-bubble {
    padding: 8px 12px;
    font-size: 0.88rem;
  }
  .chat-bubble.user {
    max-width: 95%;
  }

  .card-header {
    padding: 10px 12px;
    font-size: 0.88rem;
  }

  .card-body {
    padding: 10px 12px;
  }

  .page-header h2 {
    font-size: 1.1rem;
  }

  .system-status {
    display: none;
  }
}

/* ── Chat Page Layout (desktop defaults) ──── */

.chat-page-layout {
  display: flex;
  gap: 1rem;
  height: 100%;
  min-height: 400px;
}

.chat-page-main {
  flex: 1;
  min-width: 0;
}

.chat-page-sidebar {
  width: 340px;
  flex-shrink: 0;
  border-left: 1px solid var(--aw-color-border-light);
  overflow-y: auto;
}

.chat-page-sidebar .right-tabs {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--aw-color-bg-primary);
}

.chat-memory-section {
  border-top: 1px solid var(--aw-color-border-light);
  margin-top: 0.75rem;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

/* Mobile tabs: hidden on desktop */
.chat-mobile-tabs {
  display: none;
}

/* ── Chat Page Mobile (< 768px) ──────────── */

@media (max-width: 768px) {
  /* Hide mobile tab bar — use desktop-style toggle button instead */
  .chat-mobile-tabs {
    display: none;
  }

  .chat-add-conversation-menu {
    right: 0;
    left: auto;
    width: min(320px, 80vw);
    max-height: 280px;
  }

  /* Right pane toggle: hidden on mobile (accessible via user menu or swipe) */
  .chat-right-pane-handle {
    display: none;
  }

  .chat-page-layout {
    height: 100%;
    gap: 0;
    position: relative;
  }

  .chat-page-main {
    flex: 1;
    display: flex;
    flex-direction: column;
  }

  /* Sidebar as full overlay on mobile */
  .chat-page-sidebar {
    position: absolute;
    top: 44px;
    left: 0;
    right: 0;
    bottom: 0;
    width: auto;
    z-index: 10;
    background: var(--aw-color-bg-primary);
    border-left: none;
    overflow-y: auto;
  }

  /* sidebar-hidden controls visibility on mobile */
  .chat-page-layout.sidebar-hidden .chat-page-sidebar {
    display: none !important;
  }

  .chat-page-layout:not(.sidebar-hidden) .chat-page-sidebar {
    display: flex !important;
    flex-direction: column;
  }

  .chat-page-layout:not(.sidebar-hidden) .chat-unified-info-btn {
    color: var(--aw-color-accent);
    background: var(--aw-color-bg-hover);
  }

  .chat-page-main.mobile-hidden {
    display: none !important;
  }
}

/* ── Desktop-only Hover Effects ───────────── */

@media (hover: hover) and (pointer: fine) {
  .data-table tr:hover td {
    background: var(--aw-color-bg-subtle);
  }

  .activity-entry:hover {
    background: var(--aw-color-bg-subtle);
  }
}

.data-table tr:active td {
  background: var(--aw-color-bg-subtle);
}

.activity-entry:active {
  background: var(--aw-color-bg-subtle);
}

/* ── Data Table Keyboard Accessibility ──── */
.data-table-wrapper :focus-visible {
  outline: 2px solid var(--aw-color-info-text-alt);
  outline-offset: -2px;
}
