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

:root {
    --bg: #0f1117;
    --surface: #1a1d27;
    --surface2: #242836;
    --border: #2e3347;
    --text: #e1e4ed;
    --text-muted: #8b90a0;
    --accent: #6c8aff;
    --accent-hover: #8da4ff;
    --success: #4ade80;
    --error: #f87171;
    --warning: #fbbf24;
    --radius: 8px;
}

[data-theme="light"] {
    --bg: #f5f6fa;
    --surface: #ffffff;
    --surface2: #ebedf3;
    --border: #d1d5e0;
    --text: #1a1d27;
    --text-muted: #5f6578;
    --accent: #4a6aef;
    --accent-hover: #3a56d4;
    --success: #16a34a;
    --error: #dc2626;
    --warning: #d97706;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    min-height: 100vh;
    -webkit-text-size-adjust: 100%;
}

/* Safe areas for notch/Dynamic Island devices */
.nav {
    display: flex;
    align-items: center;
    gap: 1.5rem;
    padding: 0.75rem 1.5rem;
    padding-left: max(1.5rem, env(safe-area-inset-left));
    padding-right: max(1.5rem, env(safe-area-inset-right));
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

.nav-brand {
    font-weight: 700;
    font-size: 1.1rem;
    color: var(--accent);
}

.nav-links { display: flex; gap: 0.25rem; flex: 1; }

.theme-toggle {
    background: none;
    border: 1px solid var(--border);
    color: var(--text-muted);
    width: 44px;
    height: 44px;
    border-radius: var(--radius);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
    transition: color 0.15s, border-color 0.15s;
    flex-shrink: 0;
    -webkit-appearance: none;
    appearance: none;
}

.theme-toggle:hover { color: var(--text); border-color: var(--text-muted); }

.nav-link {
    color: var(--text-muted);
    text-decoration: none;
    padding: 0.6rem 0.85rem;
    min-height: 44px;
    display: flex;
    align-items: center;
    border-radius: var(--radius);
    font-size: 0.95rem;
    transition: color 0.15s, background 0.15s;
}

.nav-link:hover { color: var(--text); background: var(--surface2); }
.nav-link.active { color: var(--accent); background: var(--surface2); }

.container {
    max-width: 960px;
    margin: 0 auto;
    padding: 1.5rem;
    padding-left: max(1.5rem, env(safe-area-inset-left));
    padding-right: max(1.5rem, env(safe-area-inset-right));
    padding-bottom: max(1.5rem, env(safe-area-inset-bottom));
}

/* Download Form */
.download-form-section { margin-bottom: 2rem; }

.download-form {
    display: flex;
    gap: 0.5rem;
}

/* 16px minimum prevents iOS Safari auto-zoom on focus */
.download-form input[type="url"] {
    flex: 1;
    padding: 0.7rem 0.85rem;
    min-height: 44px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text);
    font-size: 16px;
    outline: none;
    transition: border-color 0.15s;
    -webkit-appearance: none;
    appearance: none;
}

.download-form input[type="url"]:focus { border-color: var(--accent); }

.download-form select {
    padding: 0.7rem 0.85rem;
    min-height: 44px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text);
    font-size: 16px;
    cursor: pointer;
    -webkit-appearance: none;
    appearance: none;
}

.download-form button {
    padding: 0.7rem 1.25rem;
    min-height: 44px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s;
    white-space: nowrap;
    -webkit-appearance: none;
    appearance: none;
}

.download-form button:hover { background: var(--accent-hover); }
.download-form button:disabled { opacity: 0.6; cursor: not-allowed; }

.form-error {
    margin-top: 0.5rem;
    padding: 0.5rem 0.75rem;
    background: rgba(248, 113, 113, 0.1);
    border: 1px solid var(--error);
    border-radius: var(--radius);
    color: var(--error);
    font-size: 0.875rem;
}

.hidden { display: none; }

/* Active Downloads */
.active-downloads h2 {
    font-size: 1.1rem;
    margin-bottom: 0.75rem;
    color: var(--text-muted);
    font-weight: 500;
}

.empty-state {
    color: var(--text-muted);
    font-size: 0.9rem;
    padding: 1rem 0;
}

.dl-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.85rem;
    margin-bottom: 0.5rem;
}

.dl-card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.5rem;
    gap: 0.5rem;
}

.dl-card-title {
    font-size: 0.9rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
    min-width: 0;
}

.dl-card-status {
    font-size: 0.8rem;
    color: var(--text-muted);
    flex-shrink: 0;
}

.progress-bar {
    height: 6px;
    background: var(--surface2);
    border-radius: 3px;
    overflow: hidden;
    margin-bottom: 0.4rem;
}

.progress-fill {
    height: 100%;
    background: var(--accent);
    border-radius: 3px;
    transition: width 0.3s ease;
    width: 0%;
}

.dl-card-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.8rem;
    color: var(--text-muted);
}

.dl-card-info {
    font-size: 0.8rem;
    color: var(--text-muted);
}

.btn-cancel {
    background: none;
    border: 1px solid var(--error);
    color: var(--error);
    padding: 0.5rem 0.85rem;
    min-height: 44px;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 0.875rem;
    transition: background 0.15s;
    -webkit-appearance: none;
    appearance: none;
}

.btn-cancel:hover { background: rgba(248, 113, 113, 0.15); }

/* Filters */
.filters {
    display: flex;
    gap: 0.35rem;
    margin-bottom: 1rem;
    flex-wrap: wrap;
}

.filter-btn {
    padding: 0.5rem 0.85rem;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-muted);
    text-decoration: none;
    font-size: 0.875rem;
    transition: all 0.15s;
}

.filter-btn:hover { color: var(--text); border-color: var(--text-muted); }
.filter-btn.active { color: var(--accent); border-color: var(--accent); background: rgba(108, 138, 255, 0.1); }

.filter-spacer { flex: 1; }

.btn-clear-history {
    padding: 0.5rem 0.85rem;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    background: none;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-muted);
    font-size: 0.875rem;
    cursor: pointer;
    transition: color 0.15s, border-color 0.15s;
    -webkit-appearance: none;
    appearance: none;
}

.btn-clear-history:hover { color: var(--error); border-color: var(--error); }

.clear-history-confirm {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
}

/* History */
.history-section h2 {
    font-size: 1.1rem;
    margin-bottom: 0.75rem;
    font-weight: 500;
}

/* Desktop table */
.history-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}

.history-table th {
    text-align: left;
    padding: 0.6rem 0.75rem;
    border-bottom: 1px solid var(--border);
    color: var(--text-muted);
    font-weight: 500;
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.history-table td {
    padding: 0.6rem 0.75rem;
    border-bottom: 1px solid var(--border);
}

.title-cell {
    max-width: 400px;
}
.title-url {
    font-size: 0.75rem;
    opacity: 0.6;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 400px;
}
.title-url a {
    color: inherit;
    text-decoration: none;
}
.title-url a:hover {
    text-decoration: underline;
}

.status {
    padding: 0.15rem 0.5rem;
    border-radius: 4px;
    font-size: 0.8rem;
    font-weight: 500;
    white-space: nowrap;
}

.status-completed { color: var(--success); background: rgba(74, 222, 128, 0.1); }
.status-failed { color: var(--error); background: rgba(248, 113, 113, 0.1); }
.status-cancelled { color: var(--warning); background: rgba(251, 191, 36, 0.1); }
.status-queued, .status-downloading { color: var(--accent); background: rgba(108, 138, 255, 0.1); }
.status-processing { color: var(--warning); background: rgba(251, 191, 36, 0.1); }

/* Mobile card layout for history */
.history-cards { display: none; }

.history-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.85rem;
    margin-bottom: 0.5rem;
}

.history-card-title {
    font-size: 0.9rem;
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    margin-bottom: 0.4rem;
}

.history-card-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    align-items: center;
    font-size: 0.8rem;
    color: var(--text-muted);
}

.pagination {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    justify-content: center;
    padding: 1rem 0;
    font-size: 0.9rem;
}

.pagination a {
    color: var(--accent);
    text-decoration: none;
    padding: 0.5rem 0.85rem;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

/* Library */
.library-section h2 {
    font-size: 1.1rem;
    margin-bottom: 0.75rem;
    font-weight: 500;
}

.library-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: 1rem;
}

.library-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
}

.library-thumb img {
    width: 100%;
    aspect-ratio: 16/9;
    object-fit: cover;
    display: block;
}

.thumb-placeholder {
    width: 100%;
    aspect-ratio: 16/9;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--surface2);
    color: var(--text-muted);
    font-size: 0.85rem;
}

.library-info { padding: 0.65rem 0.75rem; }

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

.library-meta {
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-top: 0.2rem;
}

.library-player {
    padding: 0 0.75rem 0.5rem;
}

.library-player audio,
.library-player video {
    width: 100%;
    border-radius: 4px;
}

.library-actions {
    display: flex;
    gap: 0.5rem;
    padding: 0 0.75rem 0.75rem;
}

.btn-play, .btn-download, .btn-delete {
    padding: 0.5rem 0.85rem;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    border-radius: var(--radius);
    font-size: 0.875rem;
    cursor: pointer;
    text-decoration: none;
    border: 1px solid var(--border);
    background: var(--surface2);
    color: var(--text);
    transition: border-color 0.15s;
    -webkit-appearance: none;
    appearance: none;
}

.btn-play:hover, .btn-download:hover { border-color: var(--accent); }
.btn-delete:hover { border-color: var(--error); color: var(--error); }

.confirm-text {
    font-size: 0.875rem;
    color: var(--warning);
    display: inline-flex;
    align-items: center;
}

.btn-confirm-yes {
    padding: 0.5rem 0.85rem;
    min-height: 44px;
    border-radius: var(--radius);
    font-size: 0.875rem;
    cursor: pointer;
    border: 1px solid var(--error);
    background: var(--error);
    color: #fff;
    -webkit-appearance: none;
    appearance: none;
}

.btn-confirm-no {
    padding: 0.5rem 0.85rem;
    min-height: 44px;
    border-radius: var(--radius);
    font-size: 0.875rem;
    cursor: pointer;
    border: 1px solid var(--border);
    background: var(--surface2);
    color: var(--text);
    -webkit-appearance: none;
    appearance: none;
}

/* Login */
.login-page {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg);
    padding: 1rem;
}

.login-card {
    width: 100%;
    max-width: 380px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 2rem;
}

.login-brand {
    font-weight: 700;
    font-size: 1.1rem;
    color: var(--accent);
    margin-bottom: 1.5rem;
}

.login-title {
    font-size: 1.25rem;
    font-weight: 600;
    margin-bottom: 1.25rem;
}

.login-label {
    display: block;
    font-size: 0.875rem;
    color: var(--text-muted);
    margin-bottom: 0.35rem;
}

.login-input {
    width: 100%;
    padding: 0.7rem 0.85rem;
    min-height: 44px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text);
    font-size: 16px;
    outline: none;
    margin-bottom: 1rem;
    transition: border-color 0.15s;
    -webkit-appearance: none;
    appearance: none;
}

.login-input:focus { border-color: var(--accent); }

.login-button {
    width: 100%;
    padding: 0.7rem 1.25rem;
    min-height: 44px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s;
    margin-top: 0.25rem;
    -webkit-appearance: none;
    appearance: none;
}

.login-button:hover { background: var(--accent-hover); }

.login-card .form-error { margin-bottom: 1rem; }

/* Mobile breakpoint */
@media (max-width: 640px) {
    .container { padding: 1rem; }

    .download-form { flex-direction: column; }

    .library-grid { grid-template-columns: 1fr; }

    /* Switch table to card layout */
    .history-table { display: none; }
    .history-cards { display: block; }
}
