/* ============================================================
   Design System - PLEX Media Manager
   ============================================================ */

/* --- CSS Custom Properties --- */
:root {
    /* Core palette */
    --bg-base:       #0f0f0f;
    --bg-surface:    #181818;
    --bg-elevated:   #222222;
    --bg-hover:      #2a2a2a;

    /* Text */
    --text-primary:   #e8e8e8;
    --text-secondary: #999999;
    --text-muted:     #666666;

    /* Accent */
    --accent:         #6366f1;   /* indigo */
    --accent-hover:   #818cf8;
    --accent-subtle:  rgba(99, 102, 241, 0.12);

    /* Semantic */
    --success:        #22c55e;
    --success-subtle: rgba(34, 197, 94, 0.12);
    --danger:         #ef4444;
    --danger-subtle:  rgba(239, 68, 68, 0.12);
    --warning:        #f59e0b;
    --warning-subtle: rgba(245, 158, 11, 0.12);
    --info:           #3b82f6;
    --info-subtle:    rgba(59, 130, 246, 0.12);

    /* Borders & shadows */
    --border:         #2a2a2a;
    --border-subtle:  #1f1f1f;
    --shadow-sm:      0 1px 2px rgba(0,0,0,.3);
    --shadow-md:      0 4px 12px rgba(0,0,0,.4);
    --shadow-lg:      0 8px 24px rgba(0,0,0,.5);

    /* Radii */
    --radius-sm:  6px;
    --radius-md:  10px;
    --radius-lg:  16px;
    --radius-full: 9999px;

    /* Transitions */
    --transition-fast: 150ms ease;
    --transition-base: 250ms ease;

    /* Badge colors */
    --badge-admin:  #a855f7;
    --badge-family: #22c55e;
    --badge-friend: #3b82f6;
}

/* --- Reset & Base --- */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background-color: var(--bg-base);
    color: var(--text-primary);
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    min-height: 100vh;
}

a {
    color: var(--accent);
    text-decoration: none;
    transition: color var(--transition-fast);
}

a:hover {
    color: var(--accent-hover);
}

/* --- Typography --- */
h1, h2, h3, h4 {
    font-weight: 600;
    line-height: 1.3;
    color: var(--text-primary);
}

h1 { font-size: 1.5rem; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1.1rem; }

/* --- Top Navigation Bar --- */
.navbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1.5rem;
    height: 60px;
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 100;
    backdrop-filter: blur(12px);
}

.navbar__brand {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 700;
    font-size: 1.1rem;
    color: var(--text-primary);
    letter-spacing: -0.02em;
}

.navbar__brand i {
    color: var(--accent);
    font-size: 1.3rem;
}

.navbar__user {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-size: 0.875rem;
    color: var(--text-secondary);
}

.navbar__actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

/* --- Buttons --- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    padding: 0.5rem 1rem;
    font-size: 0.85rem;
    font-weight: 500;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all var(--transition-fast);
    text-decoration: none;
    white-space: nowrap;
    line-height: 1.4;
    font-family: inherit;
}

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

.btn--primary {
    background: var(--accent);
    color: #fff;
}
.btn--primary:hover {
    background: var(--accent-hover);
    color: #fff;
}

.btn--success {
    background: var(--success);
    color: #fff;
}
.btn--success:hover {
    background: #16a34a;
    color: #fff;
}

.btn--danger {
    background: var(--danger);
    color: #fff;
}
.btn--danger:hover {
    background: #dc2626;
    color: #fff;
}

.btn--ghost {
    background: transparent;
    color: var(--text-secondary);
    border-color: var(--border);
}
.btn--ghost:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.btn--sm {
    padding: 0.3rem 0.65rem;
    font-size: 0.8rem;
}

.btn--icon {
    padding: 0.45rem;
    aspect-ratio: 1;
}

/* --- Cards --- */
.card {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    overflow: hidden;
    transition: border-color var(--transition-fast);
}

.card:hover {
    border-color: #333;
}

.card__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--border);
    background: var(--bg-elevated);
}

.card__header h2 {
    font-size: 1rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.card__header h2 i {
    color: var(--accent);
    font-size: 1rem;
}

.card__body {
    padding: 1.25rem;
}

/* --- Form Controls --- */
.form-group {
    margin-bottom: 1rem;
}

.form-label {
    display: block;
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 0.35rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.form-input,
.form-select {
    width: 100%;
    padding: 0.6rem 0.85rem;
    font-size: 0.9rem;
    font-family: inherit;
    color: var(--text-primary);
    background: var(--bg-base);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
    outline: none;
}

.form-input:focus,
.form-select:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-subtle);
}

.form-input::placeholder {
    color: var(--text-muted);
}

.form-select {
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='%23999' viewBox='0 0 16 16'%3E%3Cpath d='M4.646 5.646a.5.5 0 0 1 .708 0L8 8.293l2.646-2.647a.5.5 0 0 1 .708.708l-3 3a.5.5 0 0 1-.708 0l-3-3a.5.5 0 0 1 0-.708z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0.75rem center;
    padding-right: 2.25rem;
}

.form-row {
    display: flex;
    gap: 0.75rem;
    align-items: flex-end;
}

.form-row > * {
    flex: 1;
}

.form-row > .form-row__action {
    flex: 0 0 auto;
}

/* --- Inline form (single row with inputs + button) --- */
.form-inline {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.form-inline .form-select,
.form-inline .form-input {
    width: auto;
    flex: 0 0 auto;
}

.form-inline .form-input {
    flex: 1;
    min-width: 200px;
}

/* --- Badges --- */
.badge {
    display: inline-flex;
    align-items: center;
    padding: 0.15rem 0.55rem;
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    border-radius: var(--radius-full);
}

.badge--admin  { background: rgba(168,85,247,.15); color: var(--badge-admin); }
.badge--family { background: rgba(34,197,94,.15);  color: var(--badge-family); }
.badge--friend { background: rgba(59,130,246,.15); color: var(--badge-friend); }
.badge--success { background: var(--success-subtle); color: var(--success); }
.badge--danger  { background: var(--danger-subtle);  color: var(--danger); }

/* --- Toast Notifications --- */
.toast-container {
    position: fixed;
    top: 1rem;
    right: 1rem;
    z-index: 1000;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    pointer-events: none;
}

.toast {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    padding: 0.75rem 1rem;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-lg);
    font-size: 0.875rem;
    color: var(--text-primary);
    pointer-events: auto;
    animation: toastIn 0.3s ease forwards;
    max-width: 380px;
}

.toast--success { border-left: 3px solid var(--success); }
.toast--error   { border-left: 3px solid var(--danger); }
.toast--info    { border-left: 3px solid var(--info); }

.toast.removing {
    animation: toastOut 0.25s ease forwards;
}

@keyframes toastIn {
    from { opacity: 0; transform: translateX(20px); }
    to   { opacity: 1; transform: translateX(0); }
}

@keyframes toastOut {
    from { opacity: 1; transform: translateX(0); }
    to   { opacity: 0; transform: translateX(20px); }
}

/* --- Content Layout --- */
.page {
    max-width: 1200px;
    margin: 0 auto;
    padding: 1.5rem;
}

.page--narrow {
    max-width: 800px;
}

.section {
    margin-bottom: 1.5rem;
}

/* --- Filter / Sort Toolbar --- */
.toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0.75rem;
    padding: 0.75rem 1rem;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    margin-bottom: 1rem;
}

.toolbar__group {
    display: flex;
    align-items: center;
    gap: 0.35rem;
}

.toolbar__label {
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-right: 0.25rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    font-weight: 500;
}

.chip {
    display: inline-flex;
    align-items: center;
    padding: 0.3rem 0.7rem;
    font-size: 0.8rem;
    font-weight: 500;
    border: 1px solid var(--border);
    border-radius: var(--radius-full);
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all var(--transition-fast);
    font-family: inherit;
}

.chip:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.chip.active {
    background: var(--accent-subtle);
    color: var(--accent-hover);
    border-color: var(--accent);
}

/* --- Media Items --- */
.media-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.85rem 1rem;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    transition: all var(--transition-fast);
    gap: 1rem;
}

.media-item:hover {
    border-color: #333;
    background: var(--bg-elevated);
}

.media-item + .media-item {
    margin-top: 0.4rem;
}

.media-item__info {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    flex: 1;
    min-width: 0;
}

.media-item__icon {
    flex-shrink: 0;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-sm);
    font-size: 0.9rem;
}

.media-item__icon--movies       { background: var(--info-subtle);    color: var(--info); }
.media-item__icon--tv_shows     { background: var(--success-subtle); color: var(--success); }
.media-item__icon--anime_movies { background: var(--warning-subtle); color: var(--warning); }
.media-item__icon--anime_tv_shows { background: rgba(168,85,247,.12); color: var(--badge-admin); }

.media-item__details {
    min-width: 0;
}

.media-item__name {
    font-weight: 500;
    font-size: 0.9rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.media-item__meta {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.78rem;
    color: var(--text-muted);
    margin-top: 0.15rem;
}

.media-item__actions {
    flex-shrink: 0;
}

/* --- Series (Collapsible) --- */
.series {
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    overflow: hidden;
    margin-top: 0.4rem;
    transition: border-color var(--transition-fast);
}

.series:hover {
    border-color: #333;
}

.series__header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem 1rem;
    background: var(--bg-surface);
    cursor: pointer;
    user-select: none;
    transition: background var(--transition-fast);
}

.series__header:hover {
    background: var(--bg-elevated);
}

.series__toggle {
    font-size: 0.65rem;
    color: var(--text-muted);
    transition: transform var(--transition-base);
    width: 16px;
    text-align: center;
}

.series__toggle.expanded {
    transform: rotate(90deg);
}

.series__title {
    flex: 1;
    font-weight: 500;
    font-size: 0.9rem;
}

.series__count {
    font-size: 0.78rem;
    color: var(--text-muted);
}

.series__episodes {
    display: none;
    border-top: 1px solid var(--border);
}

.series__episodes.expanded {
    display: block;
}

.episode {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.6rem 1rem 0.6rem 2.75rem;
    border-bottom: 1px solid var(--border-subtle);
    font-size: 0.85rem;
    transition: background var(--transition-fast);
}

.episode:last-child {
    border-bottom: none;
}

.episode:hover {
    background: var(--bg-hover);
}

.episode__name {
    font-weight: 400;
}

.episode__date {
    color: var(--text-muted);
    font-size: 0.78rem;
}

/* --- Download Status --- */
.download-status {
    padding: 0;
    margin-top: 0.75rem;
    display: none;
}

.download-status:not(:empty) {
    display: block;
}

.torrent-progress {
    background: var(--info-subtle);
    border: 1px solid rgba(59, 130, 246, 0.2);
    border-radius: var(--radius-sm);
    padding: 0.85rem 1rem;
    font-size: 0.85rem;
}

.torrent-progress__header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.5rem;
    color: var(--text);
    font-weight: 500;
}

.torrent-progress__percent {
    font-variant-numeric: tabular-nums;
    font-weight: 600;
    color: var(--primary, #3b82f6);
}

.torrent-progress__track {
    width: 100%;
    height: 10px;
    background: rgba(255, 255, 255, 0.08);
    border-radius: 999px;
    overflow: hidden;
    position: relative;
}

.torrent-progress__fill {
    height: 100%;
    border-radius: 999px;
    background: linear-gradient(90deg, #3b82f6, #6366f1);
    transition: width 0.6s cubic-bezier(0.22, 1, 0.36, 1);
    position: relative;
    min-width: 0;
}

.torrent-progress__fill::after {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: 999px;
    background: linear-gradient(
        90deg,
        rgba(255,255,255,0) 0%,
        rgba(255,255,255,0.25) 50%,
        rgba(255,255,255,0) 100%
    );
    animation: shimmer 1.8s ease-in-out infinite;
    background-size: 200% 100%;
}

@keyframes shimmer {
    0%   { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

.torrent-progress__status {
    margin-top: 0.4rem;
    font-size: 0.78rem;
    color: var(--text-muted);
}

.torrent-progress--ready .torrent-progress__fill {
    background: linear-gradient(90deg, #22c55e, #16a34a);
}

.torrent-progress--error {
    border-color: rgba(239, 68, 68, 0.3);
    background: rgba(239, 68, 68, 0.06);
}

.torrent-progress--error .torrent-progress__status {
    color: var(--danger);
}

/* --- Spinner --- */
.spinner {
    display: inline-block;
    width: 16px;
    height: 16px;
    border: 2px solid rgba(255,255,255,.2);
    border-radius: 50%;
    border-top-color: #fff;
    animation: spin 0.7s linear infinite;
    vertical-align: middle;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* --- Tables --- */
.table-wrap {
    overflow-x: auto;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.875rem;
}

thead {
    background: var(--bg-elevated);
}

th {
    padding: 0.75rem 1rem;
    text-align: left;
    font-weight: 600;
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-secondary);
    border-bottom: 1px solid var(--border);
}

td {
    padding: 0.65rem 1rem;
    border-bottom: 1px solid var(--border-subtle);
}

tbody tr:hover {
    background: var(--bg-hover);
}

tbody tr:last-child td {
    border-bottom: none;
}

/* --- Admin Grid --- */
.admin-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 0.75rem;
    margin-top: 1rem;
}

.admin-grid .btn {
    padding: 0.85rem 1rem;
    justify-content: flex-start;
    font-size: 0.9rem;
}

/* --- Alert / Message boxes --- */
.alert {
    padding: 0.75rem 1rem;
    border-radius: var(--radius-sm);
    font-size: 0.875rem;
    display: none;
    margin-bottom: 0.75rem;
}

.alert--error {
    background: var(--danger-subtle);
    color: var(--danger);
    border: 1px solid rgba(239, 68, 68, 0.2);
}

.alert--success {
    background: var(--success-subtle);
    color: var(--success);
    border: 1px solid rgba(34, 197, 94, 0.2);
}

.alert.visible {
    display: block;
}

/* --- Auth Pages --- */
.auth-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 1.5rem;
    background: var(--bg-base);
}

.auth-card {
    width: 100%;
    max-width: 400px;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 2.5rem 2rem 2rem;
    box-shadow: var(--shadow-lg);
}

.auth-card__logo {
    text-align: center;
    margin-bottom: 2rem;
}

.auth-card__logo i {
    font-size: 2.5rem;
    color: var(--accent);
    margin-bottom: 0.75rem;
    display: block;
}

.auth-card__logo h1 {
    font-size: 1.25rem;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.auth-card__logo p {
    color: var(--text-muted);
    font-size: 0.85rem;
    margin-top: 0.25rem;
}

.auth-card .form-input {
    padding: 0.75rem 0.85rem;
}

.auth-card .btn {
    width: 100%;
    padding: 0.75rem;
    font-size: 0.95rem;
    margin-top: 0.5rem;
}

.auth-card__footer {
    text-align: center;
    margin-top: 1.5rem;
    font-size: 0.85rem;
    color: var(--text-muted);
}

.auth-error {
    background: var(--danger-subtle);
    color: var(--danger);
    padding: 0.65rem 0.85rem;
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
    margin-bottom: 1rem;
    border: 1px solid rgba(239, 68, 68, 0.2);
}

/* --- Library Selector --- */
.library-selector {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
}

.library-selector .form-label {
    margin-bottom: 0;
    white-space: nowrap;
}

.library-selector .form-select {
    width: auto;
}

/* --- Responsive --- */
@media (max-width: 768px) {
    .navbar {
        padding: 0 1rem;
        height: auto;
        min-height: 56px;
        flex-wrap: wrap;
        gap: 0.5rem;
        padding-top: 0.5rem;
        padding-bottom: 0.5rem;
    }

    .navbar__user {
        display: none;
    }

    .page {
        padding: 1rem;
    }

    .form-inline {
        flex-direction: column;
        align-items: stretch;
    }

    .form-inline .form-select,
    .form-inline .form-input {
        width: 100%;
    }

    .toolbar {
        flex-direction: column;
        align-items: flex-start;
    }

    .media-item {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.5rem;
    }

    .media-item__actions {
        align-self: flex-end;
    }
}

@media (max-width: 480px) {
    .auth-card {
        padding: 1.75rem 1.25rem 1.5rem;
    }

    .navbar__actions {
        flex-wrap: wrap;
        justify-content: flex-end;
    }
}
