/* ===== Design Tokens ===== */
:root {
    --radius-sm: 6px;
    --radius-md: 8px;
    --radius-lg: 12px;
    --radius-xl: 16px;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.08);
    --shadow-lg: 0 8px 24px rgba(0,0,0,0.12);
    --shadow-xl: 0 12px 40px rgba(0,0,0,0.16);
    --sidebar-width: 256px;
    --sidebar-collapsed-width: 64px;
    --header-height: 52px;
    --transition-fast: 150ms ease;
    --transition-normal: 200ms ease;
    --transition-slow: 300ms ease;
}

/* ===== Base ===== */
body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

/* ===== HTMX Loading Indicators ===== */
.htmx-indicator {
    display: none;
    opacity: 0;
    transition: opacity var(--transition-normal);
}
.htmx-request .htmx-indicator,
.htmx-request.htmx-indicator {
    display: inline-flex;
    opacity: 1;
}

/* HTMX loading overlay for tables */
.htmx-loading-overlay {
    display: none;
    position: absolute;
    inset: 0;
    background: rgba(255,255,255,0.7);
    z-index: 10;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-md);
}
.htmx-request .htmx-loading-overlay {
    display: flex;
}

/* Spinner */
.spinner {
    width: 24px;
    height: 24px;
    border: 3px solid #e2e8f0;
    border-top-color: #8a6145;
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Toggle Switch ===== */
.toggle { display: flex; align-items: center; gap: 10px; cursor: pointer; user-select: none; }
.toggle input { display: none; }
.toggle-slider {
    position: relative;
    width: 44px;
    height: 24px;
    background: #cbd5e1;
    border-radius: 12px;
    transition: background var(--transition-normal);
    flex-shrink: 0;
}
.toggle-slider::after {
    content: '';
    position: absolute;
    width: 20px;
    height: 20px;
    background: white;
    border-radius: 50%;
    top: 2px;
    left: 2px;
    transition: transform var(--transition-normal);
    box-shadow: 0 1px 3px rgba(0,0,0,0.15);
}
.toggle input:checked + .toggle-slider { background: #22c55e; }
.toggle input:checked + .toggle-slider::after { transform: translateX(20px); }
.toggle-label { font-size: 14px; font-weight: 500; }
.toggle.disabled { cursor: not-allowed; opacity: 0.5; }
.toggle.disabled input { pointer-events: none; }

/* ===== Modal Overlay ===== */
.modal-overlay {
    display: none;
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.4);
    z-index: 1000;
    justify-content: center;
    align-items: flex-start;
    padding-top: 5vh;
    backdrop-filter: blur(4px);
    opacity: 0;
    transition: opacity var(--transition-normal);
}
.modal-overlay.active {
    display: flex;
    opacity: 1;
    animation: fadeIn 0.2s ease-out;
}
.modal-overlay > div {
    animation: scaleIn 0.2s ease-out;
}

/* Drawer modal (slides in from right) */
.modal-overlay.drawer {
    justify-content: flex-end;
    align-items: stretch;
    padding-top: 0;
}
.modal-overlay.drawer > div {
    animation: slideInRight 0.3s ease-out;
    border-radius: 0;
    max-height: 100vh;
    height: 100vh;
}

/* ===== Searchable Select ===== */
.search-select { position: relative; }
.search-select-dropdown {
    position: absolute; top: 100%; left: 0; right: 0; z-index: 100;
    background: #fff; border: 1px solid #e2e8f0; border-top: none;
    border-radius: 0 0 var(--radius-md) var(--radius-md);
    max-height: 200px; overflow-y: auto; display: none;
    box-shadow: var(--shadow-md);
}
.search-select.open .search-select-dropdown { display: block; }
.search-select.open .form-control { border-radius: var(--radius-md) var(--radius-md) 0 0; }
.search-select-item {
    padding: 8px 14px; cursor: pointer; font-size: 14px;
    border-bottom: 1px solid #f1f5f9; transition: background var(--transition-fast);
}
.search-select-item:last-child { border-bottom: none; }
.search-select-item:hover, .search-select-item.active { background: #eef2ff; }
.search-select-item .item-name { font-weight: 500; }
.search-select-item .item-id { font-size: 11px; color: #94a3b8; font-family: monospace; }
.search-select-empty,
.search-select-loading { padding: 12px 14px; color: #94a3b8; font-size: 13px; text-align: center; }
.search-select-selected { display: flex; align-items: center; gap: 8px; }
.search-select-selected .selected-tag {
    background: #eef2ff; border: 1px solid #c7d2fe; border-radius: 4px;
    padding: 2px 8px; font-size: 12px; display: inline-flex; align-items: center; gap: 4px;
}
.search-select-selected .selected-clear { cursor: pointer; font-size: 14px; color: #94a3b8; line-height: 1; }
.search-select-selected .selected-clear:hover { color: #334155; }

/* ===== Expandable Detail Row ===== */
.detail-row { display: none; }
.detail-row.open { display: table-row; }
.detail-btn.open { background: #e2e8f0; }

/* ===== Sidebar ===== */
.sidebar-nav {
    width: var(--sidebar-width);
    transition: width var(--transition-slow);
    overflow: hidden;
}

.sidebar-collapsed .sidebar-nav {
    width: var(--sidebar-collapsed-width);
}

.sidebar-nav .nav-text,
.sidebar-nav .section-header,
.sidebar-nav .nav-chevron {
    transition: opacity var(--transition-normal);
    white-space: nowrap;
}

.sidebar-collapsed .sidebar-nav .nav-text,
.sidebar-collapsed .sidebar-nav .section-header,
.sidebar-collapsed .sidebar-nav .nav-chevron {
    opacity: 0;
    width: 0;
    overflow: hidden;
}

.sidebar-collapsed .sidebar-nav a,
.sidebar-collapsed .sidebar-nav .nav-item {
    justify-content: center;
    padding-left: 0;
    padding-right: 0;
}

/* Sidebar tooltip on collapsed state */
.sidebar-collapsed .sidebar-nav [data-tooltip] {
    position: relative;
}
.sidebar-collapsed .sidebar-nav [data-tooltip]:hover::after {
    content: attr(data-tooltip);
    position: absolute;
    left: calc(var(--sidebar-collapsed-width) + 4px);
    top: 50%;
    transform: translateY(-50%);
    background: #1e293b;
    color: white;
    padding: 4px 10px;
    border-radius: var(--radius-sm);
    font-size: 12px;
    white-space: nowrap;
    z-index: 100;
    box-shadow: var(--shadow-md);
}

/* Collapsible nav sections */
.nav-section-items {
    max-height: 500px;
    overflow: hidden;
    transition: max-height var(--transition-slow);
}
.nav-section.collapsed .nav-section-items {
    max-height: 0;
}
.nav-section .nav-chevron {
    transition: transform var(--transition-normal);
}
.nav-section.collapsed .nav-chevron {
    transform: rotate(-90deg);
}

/* ===== Toast Notifications ===== */
#toast-container {
    position: fixed;
    top: 60px;
    right: 16px;
    z-index: 2000;
    display: flex;
    flex-direction: column;
    gap: 8px;
    pointer-events: none;
    max-width: 400px;
}

.toast {
    pointer-events: auto;
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 12px 16px;
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    animation: slideIn 0.25s ease-out;
    position: relative;
    overflow: hidden;
    font-size: 14px;
    min-width: 300px;
    border: 1px solid transparent;
}
.toast-success {
    background: #f0fdf4;
    color: #166534;
    border-color: #bbf7d0;
}
.toast-error {
    background: #fef2f2;
    color: #991b1b;
    border-color: #fecaca;
}
.toast-warning {
    background: #fffbeb;
    color: #92400e;
    border-color: #fde68a;
}
.toast-info {
    background: #eff6ff;
    color: #1e40af;
    border-color: #bfdbfe;
}

.toast-icon { flex-shrink: 0; width: 20px; height: 20px; margin-top: 1px; }
.toast-body { flex: 1; min-width: 0; }
.toast-title { font-weight: 600; font-size: 13px; margin-bottom: 2px; }
.toast-message { font-size: 13px; opacity: 0.9; }

.toast-close {
    flex-shrink: 0;
    cursor: pointer;
    opacity: 0.5;
    transition: opacity var(--transition-fast);
    background: none;
    border: none;
    font-size: 18px;
    line-height: 1;
    padding: 0;
    color: currentColor;
}
.toast-close:hover { opacity: 1; }

.toast-progress {
    position: absolute;
    bottom: 0;
    left: 0;
    height: 3px;
    border-radius: 0 0 var(--radius-md) var(--radius-md);
    animation: toastProgress linear forwards;
}
.toast-success .toast-progress { background: #22c55e; }
.toast-error .toast-progress { background: #ef4444; }
.toast-warning .toast-progress { background: #f59e0b; }
.toast-info .toast-progress { background: #3b82f6; }

@keyframes toastProgress {
    from { width: 100%; }
    to { width: 0%; }
}

.toast-exit {
    animation: fadeOut 0.2s ease-in forwards;
}
@keyframes fadeOut {
    to { opacity: 0; transform: translateX(16px); }
}

/* ===== Breadcrumbs ===== */
.breadcrumb {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: #94a3b8;
    margin-bottom: 16px;
}
.breadcrumb a {
    color: #64748b;
    text-decoration: none;
    transition: color var(--transition-fast);
}
.breadcrumb a:hover { color: #8a6145; }
.breadcrumb .breadcrumb-sep { font-size: 11px; color: #cbd5e1; }
.breadcrumb .breadcrumb-current { color: #1e293b; font-weight: 500; }

/* ===== Table Improvements ===== */
.data-table {
    width: 100%;
    font-size: 14px;
    border-collapse: separate;
    border-spacing: 0;
}
.data-table thead th {
    text-align: left;
    padding: 10px 16px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #64748b;
    background: #f8fafc;
    border-bottom: 2px solid #e2e8f0;
    position: sticky;
    top: 0;
    z-index: 5;
}
.data-table tbody td {
    padding: 10px 16px;
    border-bottom: 1px solid #f1f5f9;
    vertical-align: middle;
}
.data-table tbody tr {
    transition: background var(--transition-fast);
}
.data-table tbody tr:hover { background: #faf7f5; }
.data-table tbody tr:nth-child(even) { background: #fafafa; }
.data-table tbody tr:nth-child(even):hover { background: #faf7f5; }

/* ID cell with copy */
.cell-id {
    font-family: ui-monospace, SFMono-Regular, monospace;
    font-size: 12px;
    color: #64748b;
    max-width: 140px;
    cursor: pointer;
    position: relative;
}
.cell-id:hover { color: #8a6145; }
.cell-id .copy-hint {
    display: none;
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
}
.cell-id:hover .copy-hint { display: inline; }

/* Empty state */
.empty-state {
    text-align: center;
    padding: 48px 16px;
    color: #94a3b8;
}
.empty-state-icon {
    width: 48px;
    height: 48px;
    margin: 0 auto 12px;
    opacity: 0.4;
}
.empty-state-text { font-size: 14px; font-weight: 500; }
.empty-state-sub { font-size: 13px; margin-top: 4px; opacity: 0.7; }

/* ===== Badges/Tags ===== */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 8px;
    border-radius: 9999px;
    font-size: 12px;
    font-weight: 500;
    line-height: 1.5;
}
.badge::before {
    content: '';
    width: 6px;
    height: 6px;
    border-radius: 50%;
    flex-shrink: 0;
}
.badge-green { background: #f0fdf4; color: #166534; }
.badge-green::before { background: #22c55e; }
.badge-red { background: #fef2f2; color: #991b1b; }
.badge-red::before { background: #ef4444; }
.badge-blue { background: #eff6ff; color: #1e40af; }
.badge-blue::before { background: #3b82f6; }
.badge-amber { background: #fffbeb; color: #92400e; }
.badge-amber::before { background: #f59e0b; }
.badge-slate { background: #f1f5f9; color: #475569; }
.badge-slate::before { background: #94a3b8; }
.badge-violet { background: #f5f3ff; color: #5b21b6; }
.badge-violet::before { background: #8b5cf6; }

/* ===== Action Buttons ===== */
.btn-action {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: var(--radius-sm);
    transition: all var(--transition-fast);
    cursor: pointer;
    border: none;
    position: relative;
}
.btn-action:hover { transform: translateY(-1px); }
.btn-action[data-tooltip]:hover::after {
    content: attr(data-tooltip);
    position: absolute;
    bottom: calc(100% + 6px);
    left: 50%;
    transform: translateX(-50%);
    background: #1e293b;
    color: white;
    padding: 3px 8px;
    border-radius: 4px;
    font-size: 11px;
    white-space: nowrap;
    z-index: 50;
}
.btn-action-primary { background: #faf7f5; color: #8a6145; }
.btn-action-primary:hover { background: #f3ede7; }
.btn-action-slate { background: #f1f5f9; color: #475569; }
.btn-action-slate:hover { background: #e2e8f0; }
.btn-action-danger { background: #fef2f2; color: #dc2626; }
.btn-action-danger:hover { background: #fee2e2; }

/* ===== Filter Bar ===== */
.filter-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 10px;
    padding: 12px 16px;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: var(--radius-md);
    margin-bottom: 16px;
}
.filter-bar .filter-icon {
    color: #94a3b8;
    flex-shrink: 0;
}
.filter-bar input,
.filter-bar select {
    padding: 7px 12px;
    border: 1px solid #e2e8f0;
    border-radius: var(--radius-sm);
    font-size: 13px;
    background: white;
    transition: border-color var(--transition-fast);
}
.filter-bar input:focus,
.filter-bar select:focus {
    outline: none;
    border-color: #8a6145;
    box-shadow: 0 0 0 2px rgba(138,97,69,0.1);
}
.filter-bar select {
    cursor: pointer;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%2394a3b8' viewBox='0 0 16 16'%3E%3Cpath d='M8 11L3 6h10l-5 5z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 10px center;
    padding-right: 28px;
}
.filter-clear {
    padding: 7px 12px;
    font-size: 12px;
    color: #64748b;
    background: white;
    border: 1px solid #e2e8f0;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all var(--transition-fast);
}
.filter-clear:hover { background: #fee2e2; color: #dc2626; border-color: #fecaca; }

/* ===== Skeleton Loading ===== */
.skeleton {
    background: linear-gradient(90deg, #f1f5f9 25%, #e2e8f0 50%, #f1f5f9 75%);
    background-size: 200% 100%;
    animation: skeleton 1.5s ease-in-out infinite;
    border-radius: 4px;
}
@keyframes skeleton {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

/* ===== Page Section ===== */
.page-section {
    background: white;
    border-radius: var(--radius-lg);
    border: 1px solid #e2e8f0;
    box-shadow: var(--shadow-sm);
}
.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 24px;
    border-bottom: 1px solid #f1f5f9;
}
.page-header h1 {
    font-size: 18px;
    font-weight: 700;
    color: #0f172a;
}
.page-body {
    padding: 20px 24px;
}

/* ===== Sidebar Mobile ===== */
@media (max-width: 768px) {
    .sidebar-nav { display: none; }
    .sidebar-nav.open { display: block; position: fixed; top: var(--header-height); left: 0; z-index: 40; height: calc(100vh - var(--header-height)); box-shadow: var(--shadow-xl); }
    .sidebar-collapsed .sidebar-nav { width: var(--sidebar-width); }
}

/* ===== Copied toast ===== */
.copy-toast {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: #1e293b;
    color: white;
    padding: 6px 16px;
    border-radius: 9999px;
    font-size: 13px;
    z-index: 3000;
    animation: fadeIn 0.15s ease-out;
    pointer-events: none;
}
