:root {
    --bg: #0f1115;
    --surface: #1a1d24;
    --surface-2: #22262f;
    --border: #2c313c;
    --text: #e8eaed;
    --text-dim: #9aa0ac;
    --accent: #e6b400;
    --accent-text: #1a1300;
    --danger: #e5484d;
    --radius: 10px;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    overflow-x: hidden;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.4;
}

a { color: inherit; text-decoration: none; }
a:hover { text-decoration: underline; }

h1 { font-size: 1.6rem; margin: 0 0 1rem; }
h2 { font-size: 1.2rem; margin: 2rem 0 1rem; }
h3 { font-size: 1.05rem; margin: 0 0 0.25rem; }
.tmdb-link {
    display: inline-block;
    color: var(--text-dim);
    font-size: 0.85em;
    text-decoration: none;
    vertical-align: middle;
}
.tmdb-link:hover { color: var(--accent); text-decoration: none; }

.site-header {
    border-bottom: 1px solid var(--border);
    background: var(--surface);
    position: sticky;
    top: 0;
    z-index: 10;
    /* Push content below the iOS status bar/notch when installed as a
       standalone web app (viewport-fit=cover + black-translucent status
       bar make the page render edge-to-edge, so we have to reclaim the
       space ourselves). Zero on browsers that don't support env(). */
    padding-top: env(safe-area-inset-top);
}

.site-header-inner {
    max-width: 1100px;
    margin: 0 auto;
    padding: 0.9rem 1.25rem;
    padding-left: max(1.25rem, env(safe-area-inset-left));
    padding-right: max(1.25rem, env(safe-area-inset-right));
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0.75rem;
}

.brand { font-weight: 700; font-size: 1.1rem; }

.nav { display: flex; align-items: center; gap: 1.1rem; font-size: 0.95rem; }
.nav a.active { color: var(--accent); }
.nav-user { color: var(--text-dim); }

.site-main {
    max-width: 1100px;
    margin: 0 auto;
    padding: 1.5rem 1.25rem calc(4rem + env(safe-area-inset-bottom));
    padding-left: max(1.25rem, env(safe-area-inset-left));
    padding-right: max(1.25rem, env(safe-area-inset-right));
}

.flash {
    padding: 0.75rem 1rem;
    border-radius: var(--radius);
    margin-bottom: 1.25rem;
    font-size: 0.95rem;
}
.flash.success { background: #123a1c; color: #7ee2a0; }
.flash.error { background: #3a1212; color: #f28b8b; }

.empty-state, .hint { color: var(--text-dim); }

/* Auth */
.auth-card {
    max-width: 380px;
    margin: 3rem auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.75rem;
}
.auth-card form { display: flex; flex-direction: column; gap: 0.9rem; margin-top: 1rem; }
.auth-card label { display: flex; flex-direction: column; gap: 0.35rem; font-size: 0.9rem; color: var(--text-dim); }
.auth-card input {
    padding: 0.6rem 0.7rem;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--text);
    font-size: 1rem;
}
.auth-card p { font-size: 0.9rem; color: var(--text-dim); }
.error { color: var(--danger); }

/* Buttons */
.btn {
    border: none;
    border-radius: 8px;
    padding: 0.55rem 0.9rem;
    font-size: 0.9rem;
    cursor: pointer;
    font-weight: 600;
}
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-primary:hover { opacity: 0.9; }
.btn-ghost { background: transparent; color: var(--text-dim); border: 1px solid var(--border); }
.btn-ghost:hover { color: var(--text); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn-sm { padding: 0.4rem 0.65rem; font-size: 0.8rem; }
.vote-widget {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 0.15rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.3rem 0.5rem;
}
.vote-arrow {
    display: block;
    width: 100%;
    background: transparent;
    border: none;
    color: var(--text-dim);
    font-size: 0.85rem;
    line-height: 1.2;
    text-align: center;
    cursor: pointer;
    padding: 0.15rem;
    border-radius: 6px;
}
.vote-arrow:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }
.vote-arrow:disabled { opacity: 0.3; cursor: default; }
.vote-arrow.vote-up:not(:disabled) { color: var(--accent); }
.vote-arrow.vote-down:not(:disabled) { color: var(--danger); }
.vote-count { display: block; font-weight: 700; font-size: 0.85rem; line-height: 1.2; text-align: center; }

/* Search */
.search-box { position: relative; max-width: 480px; }
#movie-search {
    width: 100%;
    padding: 0.7rem 0.9rem;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--text);
    font-size: 1rem;
}
.search-results {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    margin-top: 0.4rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    max-height: 420px;
    overflow-y: auto;
    z-index: 20;
}
.search-result {
    display: flex;
    gap: 0.75rem;
    padding: 0.6rem 0.75rem;
    border-bottom: 1px solid var(--border);
    align-items: center;
}
.search-result:last-child { border-bottom: none; }
.search-result img { width: 40px; height: 60px; object-fit: cover; border-radius: 4px; flex-shrink: 0; }
.search-result .sr-info { flex: 1; min-width: 0; }
.search-result .sr-info h4 { margin: 0; font-size: 0.9rem; }
.search-result .sr-info span { font-size: 0.8rem; color: var(--text-dim); }
.search-empty { padding: 0.75rem; color: var(--text-dim); font-size: 0.9rem; }

/* Movie grid (suggestions) */
.movie-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 1.25rem;
}
.movie-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}
.poster { width: 100%; aspect-ratio: 2/3; object-fit: cover; display: block; background: var(--surface-2); }
.poster-placeholder { display: flex; align-items: center; justify-content: center; color: var(--text-dim); font-size: 0.85rem; }
.movie-info { padding: 0.9rem; display: flex; flex-direction: column; gap: 0.3rem; flex: 1; }
.meta { font-size: 0.82rem; color: var(--text-dim); margin: 0; }
.overview { font-size: 0.85rem; color: var(--text-dim); margin: 0.2rem 0; flex: 1; }
.suggested-by { font-size: 0.78rem; color: var(--text-dim); margin: 0; }
.card-actions { display: flex; align-items: center; flex-wrap: wrap; gap: 0.5rem; margin-top: 0.5rem; }

/* Watch list */
.watch-list { display: flex; flex-direction: column; gap: 0.75rem; }
.watch-row {
    display: flex;
    align-items: center;
    gap: 1rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.75rem;
}
.watch-row.sortable-ghost { opacity: 0.4; }
.watch-row.sortable-chosen { border-color: var(--accent); }
.drag-handle { cursor: grab; color: var(--text-dim); font-size: 1.2rem; padding: 0 0.25rem; }
.watch-position { color: var(--text-dim); font-weight: 700; width: 1.5rem; text-align: center; flex-shrink: 0; }
.poster-sm { width: 60px; height: 90px; flex-shrink: 0; border-radius: 6px; }
.watch-info { flex: 1; min-width: 0; }
.watch-actions { flex-shrink: 0; display: flex; flex-direction: column; gap: 0.4rem; }

@media (max-width: 640px) {
    .site-header-inner {
        padding-top: 0.75rem;
        padding-bottom: 0.75rem;
        padding-left: max(1rem, env(safe-area-inset-left));
        padding-right: max(1rem, env(safe-area-inset-right));
    }
    .nav { gap: 0.65rem; font-size: 0.85rem; }
    .nav-user { display: none; }

    .site-main {
        padding-top: 1.25rem;
        padding-bottom: calc(3rem + env(safe-area-inset-bottom));
        padding-left: max(0.9rem, env(safe-area-inset-left));
        padding-right: max(0.9rem, env(safe-area-inset-right));
    }

    .watch-row { flex-wrap: wrap; }
    .watch-info { order: 3; width: 100%; }
    .watch-actions { order: 4; width: 100%; flex-direction: row; }
    .overview { display: none; }
}

/* Invites */
.invite-form {
    display: flex;
    align-items: flex-end;
    gap: 1rem;
    flex-wrap: wrap;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem 1.25rem;
    margin-top: 1rem;
}
.invite-form label { display: flex; flex-direction: column; gap: 0.35rem; font-size: 0.85rem; color: var(--text-dim); }
.invite-form input {
    padding: 0.55rem 0.7rem;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--text);
    font-size: 0.95rem;
}
.invite-form input[name="expires_days"] { width: 6rem; }
.new-invite-link {
    margin-top: 1rem;
    padding: 0.9rem 1.1rem;
    background: var(--surface-2);
    border: 1px solid var(--accent);
    border-radius: var(--radius);
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-wrap: wrap;
}
.new-invite-link code { font-size: 0.9rem; word-break: break-all; }

/* Generic admin data tables (Invites, Users) */
.table-wrap { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; margin-top: 0.5rem; }
.data-table th, .data-table td {
    text-align: left;
    vertical-align: middle;
    padding: 0.6rem 0.75rem;
    border-bottom: 1px solid var(--border);
    font-size: 0.88rem;
    white-space: nowrap;
}
.data-table th { color: var(--text-dim); font-weight: 600; }
.row-actions { display: flex; gap: 0.4rem; }
