@import url('https://fonts.googleapis.com/css2?family=DM+Sans:ital,wght@0,400;0,500;0,600;0,700;1,400&family=DM+Serif+Display&display=swap');

:root {
    /* Refined Structogram colors */
    --green: #2D6A4F;
    --green-light: #D8F3DC;
    --green-soft: #95D5B2;

    --red: #C9184A;
    --red-light: #FFE5EC;
    --red-soft: #FF8FA3;

    --blue: #1D4E89;
    --blue-light: #E7F1FF;
    --blue-soft: #90CAF9;

    /* Neutrals */
    --bg: #FAFAF8;
    --bg-warm: #F5F3EF;
    --surface: #FFFFFF;
    --text: #1A1A1A;
    --text-muted: #6B6B6B;
    --text-soft: #9A9A9A;
    --border: #E8E6E1;
    --border-light: #F0EDE8;

    /* Accents */
    --accent: #1D4E89;
    --accent-hover: #15406F;

    /* Shadows */
    --shadow-sm: 0 1px 2px rgba(26, 26, 26, 0.04);
    --shadow-md: 0 4px 12px rgba(26, 26, 26, 0.06), 0 1px 3px rgba(26, 26, 26, 0.04);
    --shadow-lg: 0 12px 40px rgba(26, 26, 26, 0.08), 0 4px 12px rgba(26, 26, 26, 0.04);
    --shadow-xl: 0 24px 60px rgba(26, 26, 26, 0.12), 0 8px 24px rgba(26, 26, 26, 0.06);

    /* Layout */
    --radius: 12px;
    --radius-lg: 20px;
    --radius-sm: 8px;

    /* Transitions */
    --ease-out: cubic-bezier(0.25, 0.46, 0.45, 0.94);
    --ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    scroll-behavior: smooth;
}

body {
    font-family: 'DM Sans', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.7;
    font-size: 16px;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    min-height: 100vh;
}

/* ================================
   Typography
   ================================ */

h1, h2, h3 {
    font-family: 'DM Serif Display', Georgia, serif;
    font-weight: 400;
    letter-spacing: -0.01em;
    line-height: 1.2;
}

h1 { font-size: 2rem; }
h2 { font-size: 1.75rem; }
h3 { font-size: 1.25rem; }

/* ================================
   Layout
   ================================ */

.container {
    max-width: 960px;
    margin: 0 auto;
    padding: 0 24px;
}

@media (max-width: 640px) {
    .container {
        padding: 0 16px;
    }
}

/* ================================
   Header
   ================================ */

header {
    background: var(--surface);
    border-bottom: 1px solid var(--border-light);
    padding: 0;
    position: sticky;
    top: 0;
    z-index: 100;
    backdrop-filter: blur(12px);
    background: rgba(255, 255, 255, 0.92);
}

header .container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 72px;
}

header h1 {
    font-size: 1.35rem;
    display: flex;
    align-items: center;
    gap: 10px;
}

header h1 a {
    text-decoration: none;
    color: var(--text);
    display: flex;
    align-items: center;
    gap: 10px;
    transition: opacity 0.2s var(--ease-out);
}

header h1 a:hover {
    opacity: 0.7;
}

header h1::before {
    content: '';
    width: 32px;
    height: 32px;
    background: linear-gradient(135deg, var(--green) 33%, var(--red) 33%, var(--red) 66%, var(--blue) 66%);
    border-radius: 8px;
    flex-shrink: 0;
}

header nav {
    display: flex;
    align-items: center;
    gap: 8px;
}

header nav a {
    padding: 8px 16px;
    text-decoration: none;
    color: var(--text-muted);
    font-weight: 500;
    font-size: 0.95rem;
    border-radius: var(--radius-sm);
    transition: all 0.2s var(--ease-out);
}

header nav a:hover {
    color: var(--text);
    background: var(--bg-warm);
}

header nav a:last-child {
    color: var(--accent);
}

header nav a:last-child:hover {
    background: var(--blue-light);
}

/* ================================
   Main Content
   ================================ */

main.container {
    padding-top: 40px;
    padding-bottom: 80px;
}

/* ================================
   Cards
   ================================ */

.card {
    background: var(--surface);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-lg);
    padding: 32px;
    margin-bottom: 24px;
    box-shadow: var(--shadow-md);
    transition: box-shadow 0.3s var(--ease-out), transform 0.3s var(--ease-out);
}

.card:hover {
    box-shadow: var(--shadow-lg);
}

@media (max-width: 640px) {
    .card {
        padding: 24px;
        border-radius: var(--radius);
    }
}

/* ================================
   Buttons
   ================================ */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 14px 28px;
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    font-family: inherit;
    font-size: 1rem;
    font-weight: 600;
    text-decoration: none;
    transition: all 0.25s var(--ease-out);
    position: relative;
    overflow: hidden;
}

.btn::after {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(180deg, rgba(255,255,255,0.15) 0%, rgba(255,255,255,0) 50%);
    pointer-events: none;
}

.btn:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-lg);
}

.btn:active {
    transform: translateY(0);
}

.btn-primary {
    background: var(--accent);
    color: #fff;
}

.btn-primary:hover {
    background: var(--accent-hover);
}

.btn-green {
    background: var(--green);
    color: #fff;
}

.btn-green:hover {
    background: #245840;
}

.btn-outline {
    background: transparent;
    border: 2px solid var(--border);
    color: var(--text);
}

.btn-outline:hover {
    border-color: var(--accent);
    color: var(--accent);
    background: var(--blue-light);
}

/* ================================
   Form Elements
   ================================ */

label {
    display: block;
    margin-bottom: 8px;
    font-weight: 600;
    font-size: 0.9rem;
    color: var(--text);
    letter-spacing: 0.01em;
}

input[type="text"],
input[type="password"],
input[type="date"] {
    width: 100%;
    padding: 14px 18px;
    border: 2px solid var(--border);
    border-radius: var(--radius);
    font-family: inherit;
    font-size: 1rem;
    margin-bottom: 16px;
    background: var(--surface);
    color: var(--text);
    transition: all 0.2s var(--ease-out);
}

input[type="text"]::placeholder,
input[type="password"]::placeholder {
    color: var(--text-soft);
}

input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 4px var(--blue-light);
}

/* Custom checkbox */
input[type="checkbox"] {
    appearance: none;
    -webkit-appearance: none;
    width: 22px;
    height: 22px;
    border: 2px solid var(--border);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    transition: all 0.2s var(--ease-out);
    flex-shrink: 0;
    background: var(--surface);
}

input[type="checkbox"]:hover {
    border-color: var(--text-soft);
}

input[type="checkbox"]:checked {
    border-color: transparent;
}

input[type="checkbox"]:checked::after {
    content: '✓';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: white;
    font-weight: 700;
    font-size: 14px;
}

/* Color-specific checkboxes */
input[type="checkbox"][name="green"]:checked {
    background: var(--green);
}

input[type="checkbox"][name="red"]:checked {
    background: var(--red);
}

input[type="checkbox"][name="blue"]:checked {
    background: var(--blue);
}

input[type="checkbox"]:focus {
    box-shadow: 0 0 0 3px var(--blue-light);
}

/* ================================
   Flash Messages
   ================================ */

.flash {
    padding: 16px 20px;
    border-radius: var(--radius);
    margin-bottom: 20px;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 12px;
    animation: slideDown 0.4s var(--ease-spring);
}

@keyframes slideDown {
    from {
        opacity: 0;
        transform: translateY(-12px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.flash::before {
    font-size: 1.2rem;
}

.flash-error {
    background: var(--red-light);
    color: var(--red);
    border: 1px solid var(--red-soft);
}

.flash-error::before {
    content: '⚠';
}

.flash-success {
    background: var(--green-light);
    color: var(--green);
    border: 1px solid var(--green-soft);
}

.flash-success::before {
    content: '✓';
}

/* ================================
   Keys Table
   ================================ */

.keys-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    margin: 20px 0;
}

.keys-table th,
.keys-table td {
    padding: 14px 16px;
    text-align: left;
    border-bottom: 1px solid var(--border-light);
}

.keys-table th {
    font-weight: 600;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    background: var(--bg-warm);
}

/* Sticky header */
.keys-table.sticky-header thead th {
    position: sticky;
    top: 72px;
    z-index: 40;
    box-shadow: 0 1px 0 var(--border-light);
}

/* Counter in header */
.keys-table th b {
    display: inline-block;
    min-width: 20px;
    padding: 2px 6px;
    margin-left: 4px;
    background: rgba(255,255,255,0.6);
    border-radius: 10px;
    font-size: 0.8rem;
}

.keys-table th:first-child {
    border-radius: var(--radius-sm) 0 0 0;
}

.keys-table th:last-child {
    border-radius: 0 var(--radius-sm) 0 0;
}

.keys-table th.green,
.keys-table th.red,
.keys-table th.blue {
    text-align: center;
}

.keys-table th.green {
    background: var(--green-light);
    color: var(--green);
}

.keys-table th.red {
    background: var(--red-light);
    color: var(--red);
}

.keys-table th.blue {
    background: var(--blue-light);
    color: var(--blue);
}

.keys-table tbody tr {
    transition: background 0.15s var(--ease-out);
}

.keys-table tbody tr:hover {
    background: var(--bg-warm);
}

.keys-table tbody tr:last-child td {
    border-bottom: none;
}

.keys-table td {
    vertical-align: middle;
}

.keys-table td:first-child {
    font-weight: 600;
    color: var(--text-soft);
    font-size: 0.9rem;
    text-align: center;
}

/* Key cells with checkbox + label */
.keys-table td.key-cell {
    text-align: center;
    padding: 8px 4px;
}

.keys-table td.key-cell label {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    cursor: pointer;
    margin: 0;
}

.keys-table td.key-cell span {
    font-size: 0.75rem;
    color: var(--text-muted);
    line-height: 1.2;
}

/* Block separator rows */
.keys-table tr.block-row td {
    background: var(--bg-warm);
    font-weight: 600;
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-muted);
    padding: 10px 16px;
    border-bottom: 1px solid var(--border-light);
}

.keys-table tr.block-row:hover {
    background: var(--bg-warm);
}

/* Responsive table */
@media (max-width: 768px) {
    .keys-table {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .keys-table th,
    .keys-table td {
        padding: 12px;
        font-size: 0.9rem;
    }
}

/* ================================
   Result Page
   ================================ */

.result-header {
    text-align: center;
    margin-bottom: 32px;
    padding-bottom: 24px;
    border-bottom: 1px solid var(--border-light);
}

.result-header h2 {
    font-size: 2rem;
    margin-bottom: 8px;
    color: var(--text);
}

.result-header p {
    color: var(--text-soft);
    font-size: 0.95rem;
}

.chart-container {
    max-width: 320px;
    margin: 0 auto 32px;
    padding: 24px;
    background: var(--bg-warm);
    border-radius: var(--radius-lg);
}

.qr-container {
    text-align: center;
    margin-top: 32px;
    padding: 32px;
    background: linear-gradient(135deg, var(--bg-warm) 0%, var(--surface) 100%);
    border-radius: var(--radius-lg);
    border: 1px solid var(--border-light);
}

.qr-container h3 {
    margin-bottom: 20px;
    color: var(--text);
}

.qr-container img {
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    background: white;
    padding: 12px;
}

.qr-container p {
    margin-top: 16px;
    color: var(--text-soft);
    font-size: 0.9rem;
}

/* Result table checkmarks */
.keys-table td:nth-child(3):not(:empty),
.keys-table td:nth-child(4):not(:empty),
.keys-table td:nth-child(5):not(:empty) {
    font-size: 0.9rem;
}

/* ================================
   Search Page
   ================================ */

.search-results {
    list-style: none;
    margin-top: 20px;
}

.search-results li {
    padding: 20px;
    border: 1px solid var(--border-light);
    border-radius: var(--radius);
    margin-bottom: 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--surface);
    transition: all 0.2s var(--ease-out);
}

.search-results li:hover {
    border-color: var(--border);
    box-shadow: var(--shadow-sm);
    transform: translateX(4px);
}

.search-results li strong {
    font-size: 1.05rem;
}

.search-results li span {
    color: var(--text-soft);
    font-size: 0.9rem;
}

/* Empty state */
.empty-state {
    text-align: center;
    padding: 48px 24px;
    color: var(--text-soft);
}

.empty-state::before {
    content: '📋';
    font-size: 3rem;
    display: block;
    margin-bottom: 16px;
    opacity: 0.5;
}

/* ================================
   Hero / Landing
   ================================ */

.hero {
    text-align: center;
    padding: 64px 24px;
    background: linear-gradient(180deg, var(--bg-warm) 0%, var(--bg) 100%);
    border-radius: var(--radius-lg);
    margin-bottom: 32px;
    position: relative;
    overflow: hidden;
}

.hero::before {
    content: '';
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background:
        radial-gradient(circle at 30% 20%, var(--green-light) 0%, transparent 25%),
        radial-gradient(circle at 70% 60%, var(--blue-light) 0%, transparent 25%),
        radial-gradient(circle at 50% 80%, var(--red-light) 0%, transparent 20%);
    opacity: 0.6;
    animation: heroFloat 20s ease-in-out infinite;
}

@keyframes heroFloat {
    0%, 100% { transform: translate(0, 0) rotate(0deg); }
    33% { transform: translate(2%, 1%) rotate(1deg); }
    66% { transform: translate(-1%, 2%) rotate(-1deg); }
}

.hero > * {
    position: relative;
    z-index: 1;
}

.hero h2 {
    font-size: 2.5rem;
    margin-bottom: 16px;
    color: var(--text);
}

.hero p {
    font-size: 1.15rem;
    color: var(--text-muted);
    margin-bottom: 32px;
    max-width: 540px;
    margin-left: auto;
    margin-right: auto;
    line-height: 1.7;
}

.hero .btn {
    padding: 16px 36px;
    font-size: 1.05rem;
}

@media (max-width: 640px) {
    .hero {
        padding: 48px 20px;
    }

    .hero h2 {
        font-size: 1.85rem;
    }

    .hero p {
        font-size: 1rem;
    }
}

/* ================================
   How it works section
   ================================ */

.card h3 {
    font-size: 1.35rem;
    margin-bottom: 20px;
    color: var(--text);
}

.card ol {
    padding-left: 0;
    list-style: none;
    counter-reset: steps;
}

.card ol li {
    counter-increment: steps;
    padding: 16px 0 16px 56px;
    position: relative;
    border-bottom: 1px solid var(--border-light);
    font-size: 1.05rem;
}

.card ol li:last-child {
    border-bottom: none;
}

.card ol li::before {
    content: counter(steps);
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 36px;
    height: 36px;
    background: var(--accent);
    color: white;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 0.95rem;
}

.card ol li:nth-child(1)::before { background: var(--green); }
.card ol li:nth-child(2)::before { background: var(--red); }
.card ol li:nth-child(3)::before { background: var(--blue); }
.card ol li:nth-child(4)::before { background: var(--accent); }

/* ================================
   Form Header (trainer page)
   ================================ */

.form-header {
    display: flex;
    align-items: baseline;
    gap: 16px;
    margin-bottom: 16px;
}

.form-header h2 {
    margin: 0;
    white-space: nowrap;
}

.form-header input[type="text"] {
    margin-left: auto;
    width: 220px;
    padding: 8px 14px;
    font-size: 1rem;
}

@media (max-width: 640px) {
    .form-header {
        flex-direction: column;
        align-items: stretch;
        gap: 12px;
    }

    .form-header input[type="text"] {
        margin-left: 0;
        width: 100%;
    }
}

/* Form actions with inline error */
.form-actions {
    margin-top: 24px;
    text-align: center;
}

.form-error {
    margin-top: 16px;
    padding: 12px 20px;
    background: var(--red-light);
    color: var(--red);
    border: 1px solid var(--red-soft);
    border-radius: var(--radius);
    font-weight: 500;
    animation: shake 0.4s ease-in-out;
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    20%, 60% { transform: translateX(-6px); }
    40%, 80% { transform: translateX(6px); }
}

/* ================================
   Key Counts (legacy)
   ================================ */

.key-counts {
    display: flex;
    gap: 12px;
    justify-content: center;
    flex-wrap: wrap;
    margin: 20px 0;
}

.key-counts span {
    padding: 10px 20px;
    border-radius: 100px;
    font-weight: 600;
    font-size: 0.95rem;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    transition: transform 0.2s var(--ease-spring);
}

.key-counts span:hover {
    transform: scale(1.05);
}

.key-counts .green {
    background: var(--green-light);
    color: var(--green);
}

.key-counts .red {
    background: var(--red-light);
    color: var(--red);
}

.key-counts .blue {
    background: var(--blue-light);
    color: var(--blue);
}

/* ================================
   Login Page
   ================================ */

.login-card {
    max-width: 420px;
    margin: 60px auto;
    text-align: center;
}

.login-card h2 {
    margin-bottom: 8px;
}

.login-card .subtitle {
    color: var(--text-soft);
    margin-bottom: 32px;
    font-size: 0.95rem;
}

.login-card .icon {
    width: 64px;
    height: 64px;
    background: var(--bg-warm);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 24px;
    font-size: 1.5rem;
}

.login-card form {
    text-align: left;
}

.login-card .btn {
    width: 100%;
    margin-top: 8px;
}

/* ================================
   Error Page
   ================================ */

.error-card {
    text-align: center;
    padding: 64px 32px;
}

.error-card .code {
    font-family: 'DM Serif Display', Georgia, serif;
    font-size: 6rem;
    color: var(--border);
    line-height: 1;
    margin-bottom: 16px;
}

.error-card p {
    font-size: 1.15rem;
    color: var(--text-muted);
    margin-bottom: 32px;
}

/* ================================
   Page transitions
   ================================ */

main.container > * {
    animation: fadeInUp 0.5s var(--ease-out) backwards;
}

main.container > *:nth-child(1) { animation-delay: 0s; }
main.container > *:nth-child(2) { animation-delay: 0.1s; }
main.container > *:nth-child(3) { animation-delay: 0.2s; }

@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* ================================
   Utilities
   ================================ */

.text-center { text-align: center; }
.text-muted { color: var(--text-muted); }
.mt-4 { margin-top: 24px; }
.mb-4 { margin-bottom: 24px; }

/* Key hint tooltip */
.key-hint {
    cursor: help;
    border-bottom: 1px dotted currentColor;
    position: relative;
}

.key-hint::after {
    content: attr(data-hint);
    position: absolute;
    bottom: calc(100% + 8px);
    left: 50%;
    transform: translateX(-50%);
    background: var(--text);
    color: white;
    padding: 10px 14px;
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
    font-weight: 400;
    line-height: 1.4;
    width: max-content;
    max-width: 280px;
    white-space: normal;
    text-align: left;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.15s, visibility 0.15s;
    z-index: 100;
    box-shadow: var(--shadow-lg);
    pointer-events: none;
}

.key-hint::before {
    content: '';
    position: absolute;
    bottom: calc(100% + 2px);
    left: 50%;
    transform: translateX(-50%);
    border: 6px solid transparent;
    border-top-color: var(--text);
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.15s, visibility 0.15s;
    z-index: 100;
}

.key-hint:hover::after,
.key-hint:hover::before {
    opacity: 1;
    visibility: visible;
}

/* Tooltip position adjustments for edge cells */
.keys-table td:nth-child(3) .key-hint::after {
    left: 0;
    transform: none;
}
.keys-table td:nth-child(3) .key-hint::before {
    left: 20px;
    transform: none;
}
.keys-table td:nth-child(5) .key-hint::after {
    left: auto;
    right: 0;
    transform: none;
}
.keys-table td:nth-child(5) .key-hint::before {
    left: auto;
    right: 20px;
    transform: none;
}

/* Focus visible for accessibility */
:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* Selection */
::selection {
    background: var(--blue-light);
    color: var(--blue);
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: var(--bg);
}

::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-soft);
}
