/* Custom properties */
:root {
    --color-primary: #3498DB;
    --color-primary-dark: #2471A3;
    --color-primary-hover: #2E86C1;
    --color-primary-light: #5DADE2;
    --color-primary-bg: #EBF5FB;
    --color-primary-border-light: #A9CCE3;
}

/* Reset and base styles */
*, *::before, *::after {
    box-sizing: border-box;
}

body {
    font-family: "DM Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    line-height: 1.6;
    color: #333;
    max-width: 1200px;
    margin: 0 auto;
    padding: 1rem;
    background: #f5f5f5;
}

/* Header */
header {
    text-align: center;
    margin-bottom: 2rem;
    padding: 1rem;
    background: linear-gradient(135deg, var(--color-primary) 0%, var(--color-primary-dark) 100%);
    color: white;
    border-radius: 8px;
}

header h1 {
    margin: 0;
    font-size: 2rem;
}

header .subtitle {
    margin: 0.5rem 0 0;
    opacity: 0.9;
}

/* Main sections */
main {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

section {
    background: white;
    border-radius: 8px;
    padding: 1.5rem;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

h2 {
    margin-top: 0;
    color: #444;
    border-bottom: 2px solid var(--color-primary);
    padding-bottom: 0.5rem;
}

h3 {
    color: #555;
    margin-top: 0;
}

/* Input section */
.input-section {
    max-width: 600px;
    margin: 0 auto;
    width: 100%;
}

.input-lead {
    margin-top: 0;
}

.mode-toggle {
    float: right;
    color: #888;
    font-size: 0.8rem;
    margin-top: 0.35rem;
}

.tab-content {
    display: none;
}

.tab-content.active {
    display: block;
}

/* Forms */
form {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

label {
    font-weight: 600;
    color: #555;
}

input {
    font-family: "DM Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

input[type="url"],
input[type="file"] {
    padding: 0.75rem;
    border: 2px solid #ddd;
    border-radius: 4px;
    font-size: 1rem;
    width: 100%;
}

input[type="url"]:focus {
    border-color: var(--color-primary);
    outline: none;
}

.hint {
    font-size: 0.875rem;
    color: #666;
    margin: 0;
}

button {
    font-family: "DM Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

button[type="submit"] {
    padding: 0.75rem 1.5rem;
    background: var(--color-primary);
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 1rem;
    cursor: pointer;
    transition: background 0.2s;
}

button[type="submit"]:hover {
    background: var(--color-primary-hover);
}

button[type="submit"]:disabled {
    background: #ccc;
    cursor: not-allowed;
}

/* Loading state */
.loading {
    text-align: center;
    padding: 2rem;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 4px solid #ddd;
    border-top-color: var(--color-primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto 1rem;
}

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

/* Error state */
.error {
    background: #fee;
    border: 1px solid #fcc;
}

.error h3 {
    color: #c00;
}

#error-message p {
    margin: 0.5rem 0;
}

#error-message ul {
    margin: 0.5rem 0;
    padding-left: 1.5rem;
}

#error-message li {
    margin: 0.25rem 0;
    word-break: break-all;
}

/* Results */
.results {
    overflow-x: auto;
}

.meta {
    color: #666;
    margin-bottom: 1rem;
}

.results-table-wrapper {
    overflow-x: auto;
    margin-bottom: 1.5rem;
}

.results-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
    table-layout: fixed;
}

.results-table th,
.results-table td {
    padding: 0.75rem;
    text-align: center;
    border: 1px solid #ddd;
}

.results-table th {
    background: var(--color-primary);
    color: white;
    font-weight: 600;
}

.results-table th:first-child {
    text-align: left;
}

.results-table td:first-child {
    text-align: left;
    font-weight: 500;
}

.results-table tbody tr:nth-child(even) {
    background: #f9f9f9;
}

.results-table tbody tr:hover {
    background: var(--color-primary-bg);
}

/* Placement change triangles */
.placement-up {
    color: #28a745;
    font-size: 0.75em;
}

.placement-down {
    color: #dc3545;
    font-size: 0.75em;
}

.results-table td.winner {
    background: #d4edda;
    font-weight: 600;
}

/* Voting system detail blocks */
.voting-details {
    margin-top: 1.5rem;
}

.voting-detail-block {
    background: #f9f9f9;
    border-radius: 4px;
    margin-bottom: 0.75rem;
    border: 1px solid #e0e0e0;
}

.voting-detail-block summary {
    padding: 0.75rem 1rem;
    cursor: pointer;
    font-weight: 600;
    color: var(--color-primary);
    list-style: none;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.voting-detail-block summary::-webkit-details-marker {
    display: none;
}

.voting-detail-block > summary::before {
    content: '\25B6';
    font-size: 0.7em;
    transition: transform 0.2s;
}

.voting-detail-block[open] > summary::before {
    transform: rotate(90deg);
}

.detail-content {
    padding: 0 1rem 1rem;
}

.detail-description {
    color: #666;
    font-size: 0.9rem;
    margin: 0 0 0.75rem 0;
}

/* Nested "Learn more" expandable */
details.learn-more {
    margin-bottom: 1rem;
    background: var(--color-primary-bg);
    border-radius: 0.3rem;
}

details.learn-more > summary {
    font-size: 0.85rem;
    font-weight: 400;
    color: #666;
    cursor: pointer;
    list-style: none;
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.4rem 0.6em;
}

details.learn-more > summary::-webkit-details-marker {
    display: none;
}

details.learn-more > summary::before {
    content: '\25B6';
    font-size: 0.55em;
    transition: transform 0.2s;
}

details.learn-more[open] > summary::before {
    transform: rotate(90deg);
}

.learn-more-content {
    padding: 0 1.25rem 0.5rem 1.25rem;
    font-size: 0.85rem;
    color: #555;
    line-height: 1.5;
}

.learn-more-content p {
    margin: 0 0 0.5rem 0;
}

.learn-more-content p:last-child {
    margin-bottom: 0;
}

.learn-more-content ul {
    margin: 0.25rem 0 0.5rem 0;
    padding-left: 1.25rem;
}

.learn-more-content li {
    margin-bottom: 0.25rem;
}

/* Detail working tables (shared by RP, Borda, Schulze) */
.detail-table-wrapper {
    overflow-x: auto;
    margin-bottom: 1rem;
}

.detail-table {
    border-collapse: collapse;
    font-size: 0.85rem;
    white-space: nowrap;
}

.detail-table th,
.detail-table td {
    padding: 0.4rem 0.6rem;
    border: 1px solid #ccc;
    text-align: center;
}

.detail-table th {
    background: var(--color-primary);
    color: white;
    font-weight: 600;
    font-size: 0.8rem;
}

.detail-table td:first-child {
    text-align: left;
    font-weight: 500;
}

.detail-table tbody tr:nth-child(even) {
    background: #f4f4f4;
}

.detail-table tbody tr:hover {
    background: var(--color-primary-bg);
}

/* Special cell colours override row hover (slightly darker than non-hover) */
.detail-table tbody tr:hover .cell-wins   { background-color: #cbe9d2; }
.detail-table tbody tr:hover .cell-loses  { background-color: #f4ccd0; }
.detail-table tbody tr:hover .cell-ties   { background-color: #ffeeb9; }
.detail-table tbody tr:hover .cell-diagonal { background-color: #e3e6ea; }
.detail-table tbody tr:hover .rp-first-majority { background: #cbe9d2; }
.detail-table tbody tr:hover .rp-highlight { background: #cbe9d2; }

/* RP table: separator between judge and cumulative columns */
.detail-table .rp-separator {
    border-left: 3px solid var(--color-primary);
}

/* RP table: result column */
.detail-table .rp-result {
    border-left: 3px solid var(--color-primary);
    font-weight: 600;
}

/* RP quality-of-majority tiebreak annotation */
.rp-quality {
    font-size: 0.85em;
    color: #666;
}

/* RP table: first majority in progression */
.rp-first-majority {
    background: #d4edda;
}

/* RP table: highlight judge cells on count hover */
.rp-highlight {
    background: #d4edda !important;
}

/* Borda tiebreak section header within unified table */
/* Need specificity to override .detail-table td and .detail-table td:first-child */
.detail-table td.borda-section-header:first-child,
.detail-table td.borda-section-header,
.borda-section-header {
    color: var(--color-primary);
    background: #f9f9f9;
    font-weight: 600;
    font-size: 0.95rem;
    text-align: left;
    border-left: none;
    border-right: none;
}

/* Schulze matrix cell colours */
.cell-wins {
    background-color: #d4edda;
}

.cell-loses {
    background-color: #f8d7da;
}

.cell-ties {
    background-color: #fff3cd;
}

.cell-diagonal {
    background-color: #e9ecef;
}

.schulze-wins-col {
    font-weight: 600;
    border-left: 3px solid var(--color-primary);
}

/* Sequential IRV narrative */
.irv-placement-round {
    margin-bottom: 1rem;
}

.irv-placement-round strong {
    color: #555;
    font-size: 0.95rem;
}

.irv-round-step {
    margin: 0.25rem 0;
    padding-left: 1rem;
    font-size: 0.9rem;
    color: #444;
}

.irv-eliminated {
    color: #dc3545;
}

.irv-winner {
    color: #28a745;
    font-weight: 600;
}

/* Instant CSS tooltips (no delay) */
.has-tooltip {
    position: relative;
    cursor: default;
}

.has-tooltip::after {
    content: attr(data-tooltip);
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    padding: 0.3rem 0.5rem;
    background: #333;
    color: #fff;
    font-size: 0.75rem;
    font-weight: 400;
    white-space: nowrap;
    border-radius: 3px;
    pointer-events: none;
    opacity: 0;
    z-index: 10;
}

.has-tooltip:hover::after {
    opacity: 1;
}

/* Tooltips in thead: show below to avoid overflow clipping from wrapper */
thead .has-tooltip::after {
    bottom: auto;
    top: 100%;
    margin-top: 2px;
}

thead .has-tooltip:hover {
    z-index: 100;
}

/* IRV note for excluded zero-vote candidates */
.irv-note {
    color: #888;
    font-style: italic;
}

/* Tiebreak details block */
.irv-tiebreak {
    margin: 0.25rem 0 0.5rem 1rem;
    padding: 0.4rem 0.6rem;
    border-left: 2px solid var(--color-primary-light);
    background: var(--color-primary-bg);
    font-size: 0.85rem;
    color: #555;
}

.irv-tiebreak-header {
    font-weight: 600;
    color: var(--color-primary-dark);
    margin-bottom: 0.2rem;
    font-size: 0.85rem;
}

.irv-tiebreak-step {
    margin: 0.15rem 0;
}

.irv-tiebreak-line {
    margin: 0.15rem 0;
    padding-left: 0.5rem;
    font-size: 0.85rem;
}

/* Detail section subheadings */
.detail-content h4 {
    color: #555;
    margin: 1rem 0 0.5rem 0;
    font-size: 0.95rem;
}

.detail-content h4:first-of-type {
    margin-top: 0;
}

/* Privacy notice */
.privacy-notice {
    background: var(--color-primary-bg);
    border: 1px solid var(--color-primary-border-light);
    font-size: 0.9rem;
}

.privacy-notice h3 {
    margin-bottom: 0.5rem;
}

.privacy-notice p {
    margin: 0;
    color: #555;
}

/* Footer */
footer {
    text-align: center;
    padding: 1rem;
    color: #666;
    font-size: 0.9rem;
}

footer a {
    color: var(--color-primary);
}

/* Utility classes */
.hidden {
    display: none !important;
}

/* Responsive */
@media (max-width: 600px) {
    body {
        padding: 0.5rem;
    }

    header h1 {
        font-size: 1.5rem;
    }

    section {
        padding: 1rem;
    }

    .results-table {
        font-size: 0.8rem;
    }

    .results-table th,
    .results-table td {
        padding: 0.5rem;
    }

    .detail-table {
        font-size: 0.75rem;
    }

    .detail-table th,
    .detail-table td {
        padding: 0.3rem 0.4rem;
    }

    .mode-toggle {
        float: none;
        display: block;
        text-align: right;
    }
}
