/* ==========================================================================
   Design tokens
   ========================================================================== */
:root {
    --ink: #0f172a;
    --ink-2: #475569;
    --line: #e2e8f0;
    --surface: #ffffff;
    --canvas: #f8fafc;

    --accent: #1d4ed8;
    --accent-hover: #1e40af;
    --accent-soft: #eff6ff;

    --success: #15803d;
    --success-soft: #dcfce7;
    --warning: #b45309;
    --warning-soft: #fef3c7;
    --danger: #b91c1c;
    --danger-soft: #fee2e2;

    --radius-sm: 8px;
    --radius-md: 12px;
    --shadow: 0 1px 2px rgb(15 23 42 / 0.06);
    --shadow-lg: 0 8px 24px rgb(15 23 42 / 0.12);

    --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
}

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

body {
    font-family: var(--font);
    font-size: 16px;
    line-height: 1.6;
    color: var(--ink);
    background: var(--canvas);
    min-height: 100vh;
}

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

[hidden] {
    display: none !important;
}

.num {
    font-variant-numeric: tabular-nums;
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
}

.icon {
    width: 16px;
    height: 16px;
    flex-shrink: 0;
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* ==========================================================================
   Layout
   ========================================================================== */
.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 32px 16px;
}

.container-narrow {
    max-width: 760px;
}

/* ==========================================================================
   Buttons
   ========================================================================== */
.btn,
.vote-button,
.modal-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 10px 16px;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    font: inherit;
    font-size: 14px;
    font-weight: 600;
    line-height: 1.25;
    text-decoration: none;
    cursor: pointer;
    transition: background-color 0.15s, border-color 0.15s, color 0.15s;
}

.btn-primary,
.vote-button,
.modal-button {
    background: var(--accent);
    color: #fff;
}

.btn-primary:hover:not(:disabled),
.vote-button:hover:not(:disabled),
.modal-button:hover {
    background: var(--accent-hover);
}

.btn-secondary {
    background: var(--surface);
    color: var(--ink);
    border-color: var(--line);
}

.btn-secondary:hover:not(:disabled) {
    background: var(--canvas);
    border-color: var(--ink-2);
}

.btn:disabled {
    opacity: 0.55;
    cursor: not-allowed;
}

.btn-block {
    width: 100%;
}

.vote-button {
    padding: 14px 32px;
    font-size: 16px;
}

.vote-button:disabled {
    opacity: 0.8;
    cursor: progress;
}

.vote-icon {
    width: 20px;
    height: 20px;
}

/* Spinner on a button while a request is in flight */
.loading {
    position: relative;
    pointer-events: none;
}

.btn.loading,
.vote-button.loading {
    padding-left: 40px;
}

.loading::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 16px;
    width: 16px;
    height: 16px;
    margin-top: -8px;
    border: 2px solid rgb(255 255 255 / 0.4);
    border-top-color: #fff;
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}

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

/* ==========================================================================
   Home: category list
   ========================================================================== */
.container > h1 {
    font-size: 32px;
    font-weight: 700;
    line-height: 1.2;
    text-align: center;
    margin-bottom: 8px;
}

.container > .subtitle {
    text-align: center;
    color: var(--ink-2);
    margin-bottom: 40px;
}

.container > p:not(.subtitle) {
    text-align: center;
    padding: 48px 0;
    color: var(--ink-2);
}

.banner {
    display: flex;
    align-items: center;
    gap: 8px;
    max-width: 640px;
    margin: 0 auto 24px;
    padding: 10px 16px;
    border-radius: var(--radius-sm);
    background: var(--success-soft);
    color: var(--ink);
    font-size: 14px;
}

.banner .icon {
    color: var(--success);
}

.banner a {
    color: var(--accent);
}

.group {
    margin-bottom: 48px;
}

.group-title {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 16px;
    padding-bottom: 12px;
    margin-bottom: 16px;
    border-bottom: 1px solid var(--line);
    font-size: 20px;
    font-weight: 600;
    text-transform: capitalize;
}

.group-count {
    color: var(--ink-2);
    font-size: 14px;
    font-weight: 400;
    text-transform: none;
}

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

.innovation-card {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow);
    color: inherit;
    text-decoration: none;
    transition: border-color 0.15s, box-shadow 0.15s;
}

.innovation-card:hover {
    border-color: var(--accent);
    box-shadow: var(--shadow-lg);
}

.card-thumb {
    aspect-ratio: 16 / 9;
    overflow: hidden;
    background: var(--canvas);
    border-bottom: 1px solid var(--line);
}

.card-thumb img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: center;
}

.card-body {
    display: flex;
    flex: 1;
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
    padding: 16px;
}

.card-body h3 {
    font-size: 16px;
    font-weight: 600;
    line-height: 1.4;
}

.division {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 6px;
    background: var(--canvas);
    color: var(--ink-2);
    font-size: 12px;
    font-weight: 500;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}

/* ==========================================================================
   Innovation / vote page
   ========================================================================== */
.back-link-top {
    display: inline-block;
    margin-bottom: 16px;
    color: var(--ink-2);
    font-size: 14px;
    text-decoration: none;
}

.back-link-top:hover {
    color: var(--accent);
}

.framed .back-link-top {
    display: none;
}

.innovation-page {
    padding: 32px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow);
}

.apo-hero {
    display: flex;
    justify-content: center;
    margin-bottom: 24px;
}

.apo-hero picture,
.apo-hero img {
    display: block;
}

.apo-hero img {
    width: 100%;
    max-width: 260px;
    height: auto;
}

.innovation-header {
    text-align: center;
    margin-bottom: 24px;
    padding-bottom: 24px;
    border-bottom: 1px solid var(--line);
}

.innovation-header h1 {
    font-size: 28px;
    font-weight: 700;
    line-height: 1.25;
    margin-bottom: 8px;
}

.innovation-header .division {
    padding: 0;
    background: none;
}

.innovation-header .description {
    margin-top: 16px;
    color: var(--ink-2);
    line-height: 1.7;
}

.vote-section {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    text-align: center;
}

.vote-rule {
    color: var(--ink-2);
    font-size: 14px;
}

.vote-done {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 20px;
    border-radius: var(--radius-sm);
    background: var(--success-soft);
    text-align: left;
}

.vote-done-icon {
    width: 28px;
    height: 28px;
    flex-shrink: 0;
    color: var(--success);
}

.vote-done strong,
.vote-done span {
    display: block;
}

.vote-done span {
    color: var(--ink-2);
    font-size: 14px;
}

.vote-done a {
    color: var(--accent);
}

/* ==========================================================================
   Dialogs
   ========================================================================== */
.modal {
    position: fixed;
    top: 50%;
    left: 50%;
    z-index: 1000;
    width: 90%;
    max-width: 400px;
    margin: 0;
    padding: 0;
    border: none;
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    transform: translate(-50%, -50%);
}

.modal::backdrop {
    background: rgb(15 23 42 / 0.5);
}

.modal-content {
    padding: 32px 24px 24px;
    text-align: center;
}

.modal-icon,
.message-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 56px;
    height: 56px;
    margin: 0 auto 16px;
    border-radius: 50%;
    background: var(--accent-soft);
    color: var(--accent);
}

.modal-icon svg,
.message-icon svg {
    width: 28px;
    height: 28px;
}

.modal-icon.success { background: var(--success-soft); color: var(--success); }
.modal-icon.warning,
.message-icon.warning { background: var(--warning-soft); color: var(--warning); }
.modal-icon.error,
.message-icon.danger { background: var(--danger-soft); color: var(--danger); }

.modal-content h2 {
    font-size: 20px;
    font-weight: 600;
    margin-bottom: 8px;
}

.modal-content p {
    color: var(--ink-2);
    margin-bottom: 8px;
}

.modal-content p strong {
    color: var(--ink);
}

.modal-content .modal-note {
    font-size: 14px;
}

.modal-button {
    margin-top: 16px;
    min-width: 96px;
}

.modal-actions {
    display: flex;
    gap: 8px;
    margin-top: 16px;
}

.modal-actions .btn {
    flex: 1;
}

/* ==========================================================================
   Message pages (closed, error, not found)
   ========================================================================== */
.message-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 16px;
}

.message-card {
    width: 100%;
    max-width: 520px;
    padding: 40px 32px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow);
    text-align: center;
}

.message-card .message-icon {
    width: 64px;
    height: 64px;
    margin-bottom: 24px;
}

.message-card .message-icon svg {
    width: 32px;
    height: 32px;
}

.message-card h1 {
    font-size: 24px;
    font-weight: 700;
    line-height: 1.25;
    margin-bottom: 12px;
}

.message-card p {
    color: var(--ink-2);
    margin-bottom: 24px;
}

.message-card p.message-note {
    margin-bottom: 0;
}

.message-card p:last-child {
    margin-bottom: 0;
}

/* ==========================================================================
   Admin: login
   ========================================================================== */
body.admin {
    background: var(--canvas);
}

.login-wrap {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 16px;
}

.login-card {
    width: 100%;
    max-width: 400px;
    padding: 32px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow);
}

.login-card h1 {
    font-size: 24px;
    font-weight: 700;
    line-height: 1.25;
    margin-bottom: 8px;
    text-align: center;
}

.login-card .subtitle {
    margin-bottom: 24px;
    color: var(--ink-2);
    font-size: 14px;
    text-align: center;
}

.form-group {
    margin-bottom: 16px;
}

.form-group label {
    display: block;
    margin-bottom: 8px;
    font-size: 14px;
    font-weight: 500;
}

.form-group input {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--ink-2);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--ink);
    font: inherit;
    font-size: 16px;
}

.form-group input[aria-invalid='true'] {
    border-color: var(--danger);
}

.field-error {
    margin-top: 8px;
    color: var(--danger);
    font-size: 14px;
}

.alert-error {
    margin-bottom: 16px;
    padding: 12px 16px;
    border-radius: var(--radius-sm);
    background: var(--danger-soft);
    color: var(--danger);
    font-size: 14px;
}

.back-link {
    display: block;
    margin-top: 16px;
    color: var(--ink-2);
    font-size: 14px;
    text-align: center;
    text-decoration: none;
}

.back-link:hover {
    color: var(--accent);
}

/* ==========================================================================
   Admin: dashboard
   ========================================================================== */
.admin-page {
    max-width: 1100px;
    margin: 0 auto;
    padding: 32px 16px;
}

.admin-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 24px;
}

.admin-header h1 {
    font-size: 24px;
    font-weight: 700;
    line-height: 1.25;
}

.admin-meta {
    color: var(--ink-2);
    font-size: 14px;
}

.admin-meta a {
    color: var(--accent);
}

.admin-actions {
    display: flex;
    gap: 8px;
}

.admin-loading {
    padding: 48px 0;
    text-align: center;
    color: var(--ink-2);
}

.spinner {
    width: 32px;
    height: 32px;
    margin: 0 auto 16px;
    border: 3px solid var(--line);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

.stat-strip {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    margin-bottom: 24px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow);
}

.stat {
    padding: 16px 20px;
}

.stat + .stat {
    border-left: 1px solid var(--line);
}

.stat-label {
    color: var(--ink-2);
    font-size: 13px;
}

.stat-value {
    font-size: 28px;
    font-weight: 700;
    line-height: 1.2;
}

.panel {
    margin-bottom: 16px;
    padding: 20px 24px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow);
}

.panel-head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 16px;
}

.panel-head h2 {
    font-size: 16px;
    font-weight: 600;
}

.panel-total {
    flex-shrink: 0;
    color: var(--ink-2);
    font-size: 14px;
    white-space: nowrap;
}

.empty {
    padding: 8px 0;
    color: var(--ink-2);
    font-size: 14px;
}

.rank-list {
    display: grid;
    gap: 4px;
    list-style: none;
}

.rank-row {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
}

.rank-row.is-leader {
    background: var(--accent-soft);
}

.rank-pos {
    width: 24px;
    flex-shrink: 0;
    color: var(--ink-2);
    font-size: 14px;
    text-align: center;
}

.is-leader .rank-pos {
    color: var(--accent);
    font-weight: 700;
}

.rank-body {
    flex: 1;
    min-width: 0;
}

.rank-name {
    font-size: 15px;
    font-weight: 500;
}

.rank-bar {
    height: 4px;
    margin-top: 6px;
    overflow: hidden;
    border-radius: 999px;
    background: var(--line);
}

.rank-bar-fill {
    height: 100%;
    background: var(--ink-2);
    transition: width 0.3s ease;
}

.is-leader .rank-bar-fill {
    background: var(--accent);
}

.rank-count {
    min-width: 40px;
    font-size: 18px;
    font-weight: 700;
    text-align: right;
}

.filter {
    max-width: 100%;
}

.filter select {
    max-width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--line);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--ink);
    font: inherit;
    font-size: 14px;
}

.table-container {
    max-height: 70vh;
    overflow: auto;
    border: 1px solid var(--line);
    border-radius: var(--radius-sm);
}

.results-table {
    width: 100%;
    border-collapse: collapse;
}

.results-table th,
.results-table td {
    padding: 12px 16px;
    border-bottom: 1px solid var(--line);
    font-size: 14px;
    text-align: left;
}

.results-table thead th {
    position: sticky;
    top: 0;
    z-index: 1;
    background: var(--canvas);
    color: var(--ink-2);
    font-weight: 600;
}

.results-table th.num,
.results-table td.num {
    text-align: right;
    white-space: nowrap;
}

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

.results-table td.num {
    font-weight: 600;
}

.sort {
    padding: 0;
    border: none;
    background: none;
    color: inherit;
    cursor: pointer;
    font: inherit;
    font-weight: 600;
}

.sort:hover {
    color: var(--accent);
}

th[aria-sort='ascending'] .sort::after { content: ' ↑'; }
th[aria-sort='descending'] .sort::after { content: ' ↓'; }

/* ==========================================================================
   Responsive
   ========================================================================== */
@media (max-width: 640px) {
    .container {
        padding: 24px 12px;
    }

    .container > h1 {
        font-size: 24px;
    }

    .container > .subtitle {
        margin-bottom: 32px;
    }

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

    .group-title {
        font-size: 18px;
    }

    /* Vote action stays in reach at the bottom of the screen */
    .innovation-page {
        padding: 20px 16px 104px;
    }

    .innovation-header h1 {
        font-size: 22px;
    }

    .apo-hero img {
        max-width: 70vw;
    }

    .vote-section {
        position: fixed;
        right: 0;
        bottom: 0;
        left: 0;
        z-index: 10;
        padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
        background: var(--surface);
        border-top: 1px solid var(--line);
        box-shadow: 0 -4px 12px rgb(15 23 42 / 0.06);
    }

    .vote-button {
        width: 100%;
    }

    .vote-done {
        width: 100%;
    }

    .message-card {
        padding: 32px 20px;
    }

    .admin-header {
        flex-direction: column;
    }

    .stat {
        padding: 12px;
    }

    .stat-value {
        font-size: 22px;
    }

    .panel {
        padding: 16px;
    }

    .panel-head {
        flex-wrap: wrap;
    }

    .filter,
    .filter select {
        width: 100%;
    }

    .rank-row {
        gap: 12px;
        padding: 10px 4px;
    }

    .table-container {
        max-height: none;
        border: none;
    }

    .results-table thead {
        display: none;
    }

    .results-table,
    .results-table tbody,
    .results-table tr,
    .results-table td {
        display: block;
    }

    .results-table tr {
        margin-bottom: 8px;
        padding: 8px 12px;
        border: 1px solid var(--line);
        border-radius: var(--radius-sm);
    }

    .results-table td {
        display: flex;
        justify-content: space-between;
        gap: 16px;
        padding: 4px 0;
        border: none;
        text-align: right;
    }

    .results-table td[data-label]:not(:first-child)::before {
        content: attr(data-label);
        color: var(--ink-2);
        text-align: left;
    }

    .results-table td:first-child {
        justify-content: flex-start;
        text-align: left;
    }
}
