:root {
    --primary: #10b981;
    --primary-dark: #047857;
    --primary-light: #d1fae5;
    --accent: #06b6d4;
    --bg-from: #ecfdf5;
    --bg-to: #f0fdfa;
    --surface: #ffffff;
    --surface-soft: #f8fafc;
    --border: #e2e8f0;
    --border-strong: #cbd5e1;
    --text: #0f172a;
    --text-muted: #64748b;
    --highlight: #fef3c7;
    --highlight-border: #f59e0b;
    --highlight-text: #92400e;
    --warning: #fb923c;
    --warning-bg: #ffedd5;
    --warning-text: #9a3412;
    --danger: #ef4444;
    --danger-bg: #fee2e2;
    --danger-text: #991b1b;
    --success: #10b981;
    --success-bg: #d1fae5;
    --success-text: #065f46;
    --shadow-sm: 0 1px 2px rgba(15, 23, 42, .04);
    --shadow-md: 0 4px 6px -1px rgba(15, 23, 42, .07), 0 2px 4px -2px rgba(15, 23, 42, .05);
    --shadow-xl: 0 20px 40px -10px rgba(15, 23, 42, .12);
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-xl: 24px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --primary: #34d399;
        --primary-dark: #6ee7b7;
        --primary-light: #064e3b;
        --accent: #22d3ee;
        --bg-from: #022c22;
        --bg-to: #0f172a;
        --surface: #0f172a;
        --surface-soft: #1e293b;
        --border: #1e293b;
        --border-strong: #334155;
        --text: #f1f5f9;
        --text-muted: #94a3b8;
        --highlight: #422006;
        --highlight-border: #f59e0b;
        --highlight-text: #fde68a;
        --warning: #fb923c;
        --warning-bg: #2c1003;
        --warning-text: #fdba74;
        --danger: #f87171;
        --danger-bg: #2a0a0a;
        --danger-text: #fca5a5;
        --success: #34d399;
        --success-bg: #053126;
        --success-text: #6ee7b7;
        --shadow-xl: 0 20px 40px -10px rgba(0, 0, 0, .55);
    }
}

* { box-sizing: border-box; }

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Inter', system-ui, sans-serif;
    color: var(--text);
    background: linear-gradient(135deg, var(--bg-from) 0%, var(--bg-to) 100%);
    min-height: 100vh;
    margin: 0;
    padding: 24px 16px;
    line-height: 1.55;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

.container {
    max-width: 720px;
    margin: 0 auto;
    background: var(--surface);
    padding: 32px;
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-xl);
    border: 1px solid var(--border);
    position: relative;
}

.header {
    display: flex;
    align-items: center;
    gap: 14px;
    padding-bottom: 18px;
    border-bottom: 1px solid var(--border);
    margin-bottom: 4px;
}

.header__icon {
    width: 48px;
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, var(--primary), var(--accent));
    color: white;
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-md);
    flex-shrink: 0;
}

.header h1 {
    margin: 0;
    font-size: 22px;
    font-weight: 700;
    letter-spacing: -0.02em;
    color: var(--text);
}

.header p {
    margin: 2px 0 0 0;
    font-size: 13.5px;
    color: var(--text-muted);
}

.info {
    background: var(--surface-soft);
    border-left: 4px solid var(--accent);
    border-radius: var(--radius-sm);
    padding: 14px 18px;
    margin: 16px 0;
    font-size: 14.5px;
    color: var(--text-muted);
}

.info--important {
    background: var(--highlight);
    border-left-color: var(--highlight-border);
    color: var(--text);
}

.highlight {
    font-weight: 600;
    color: var(--primary-dark);
}

.info--important .highlight {
    color: var(--highlight-text);
}

a {
    color: var(--primary-dark);
    text-decoration: none;
    border-bottom: 1px dashed currentColor;
    transition: color 0.15s ease, border-color 0.15s ease;
}

a:hover {
    color: var(--accent);
    border-bottom-style: solid;
}

.table-wrapper {
    margin-top: 20px;
    border-radius: var(--radius-md);
    overflow: hidden;
    border: 1px solid var(--border);
    background: var(--surface);
    box-shadow: var(--shadow-sm);
}

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

th {
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: white;
    font-weight: 600;
    font-size: 12px;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    padding: 14px 12px;
    text-align: center;
}

th:first-child { text-align: left; padding-left: 18px; }

/* "Semester " prefix hidden on small screens via .th-full */
.th-full { }

td {
    padding: 12px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
    text-align: center;
    vertical-align: middle;
}

tr:last-child td { border-bottom: none; }
tr:nth-child(even) td { background: var(--surface-soft); }

td:first-child {
    text-align: left;
    padding-left: 18px;
    font-weight: 500;
    color: var(--text);
}

.subject-badge {
    display: inline-block;
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.06em;
    padding: 2px 7px;
    border-radius: 999px;
    margin-left: 8px;
    background: var(--primary-light);
    color: var(--primary-dark);
    vertical-align: 2px;
}

/* Input + grade label as one fused card */
.input-wrap {
    display: inline-flex;
    flex-direction: column;
    align-items: stretch;
    gap: 0;
}

/* Default: label hidden, input fully rounded */
.grade-label {
    display: none;
    font-size: 11px;
    font-weight: 700;
    text-align: center;
    padding: 4px 4px;
    line-height: 1;
    letter-spacing: 0.02em;
    background: var(--surface-soft);
    color: var(--text-muted);
    border: 1.5px solid var(--border-strong);
    border-top: none;
    border-radius: 0 0 var(--radius-sm) var(--radius-sm);
    transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

/* Show label + flatten input bottom only when value is entered */
.input-wrap.has-label input[type="number"] {
    border-radius: var(--radius-sm) var(--radius-sm) 0 0;
    border-bottom-color: transparent;
}

.input-wrap.has-label .grade-label {
    display: block;
}

/* Color states — input and label share the same palette */
.grade-label.is-included {
    background: var(--highlight);
    border-color: var(--highlight-border);
    color: var(--highlight-text);
}

.grade-label.is-deficit {
    background: var(--warning-bg);
    border-color: var(--warning);
    color: var(--warning-text);
}

.grade-label.is-zero {
    background: var(--danger-bg);
    border-color: var(--danger);
    color: var(--danger-text);
}

input[type="number"] {
    width: 64px;
    min-height: 44px;
    padding: 10px 6px;
    border: 1.5px solid var(--border-strong);
    border-radius: var(--radius-sm);
    background: var(--surface);
    text-align: center;
    font-size: 16px;    /* stays 16px — prevents iOS auto-zoom */
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    color: var(--text);
    transition: border-color 0.15s ease, box-shadow 0.15s ease, background-color 0.15s ease;
    -moz-appearance: textfield;
    touch-action: manipulation;
}

input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

input[type="number"]:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-light);
}

/* When focused, extend the green border to the label strip */
.input-wrap.has-label:has(input:focus) .grade-label {
    border-color: var(--primary);
}

input[type="number"].is-included {
    background: var(--highlight);
    border-color: var(--highlight-border);
    color: var(--highlight-text);
}

input[type="number"].is-deficit {
    background: var(--warning-bg);
    border-color: var(--warning);
    color: var(--warning-text);
}

input[type="number"].is-zero {
    background: var(--danger-bg);
    border-color: var(--danger);
    color: var(--danger-text);
}

.legend {
    display: flex;
    flex-wrap: wrap;
    gap: 10px 18px;
    margin-top: 14px;
    padding: 0 4px;
    font-size: 12.5px;
    color: var(--text-muted);
}

.legend__item {
    display: inline-flex;
    align-items: center;
    gap: 7px;
}

.legend__swatch {
    width: 14px;
    height: 14px;
    border-radius: 4px;
    border: 1.5px solid var(--border-strong);
}

.legend__swatch--included { background: var(--highlight); border-color: var(--highlight-border); }
.legend__swatch--deficit  { background: var(--warning-bg); border-color: var(--warning); }
.legend__swatch--zero     { background: var(--danger-bg); border-color: var(--danger); }

.result {
    margin-top: 24px;
    padding: 16px 20px;
    border-radius: var(--radius-md);
    font-size: 15px;
    background: var(--surface-soft);
    border-left: 4px solid var(--text-muted);
    color: var(--text);
    display: none;
    animation: slideIn 0.25s ease;
}

@keyframes slideIn {
    from { opacity: 0; transform: translateY(-6px); }
    to   { opacity: 1; transform: translateY(0); }
}

.result--success { background: var(--success-bg); border-left-color: var(--success); color: var(--success-text); }
.result--success .highlight { color: var(--success-text); }

.result--warning { background: var(--warning-bg); border-left-color: var(--warning); color: var(--warning-text); }
.result--warning .highlight { color: var(--warning-text); }

.result--error { background: var(--danger-bg); border-left-color: var(--danger); color: var(--danger-text); }
.result--error .highlight { color: var(--danger-text); }

.result__icon {
    display: inline-block;
    margin-right: 8px;
    vertical-align: -5px;
}

.result__hint {
    margin-top: 10px;
    font-size: 13.5px;
    opacity: 0.85;
}

.breakdown {
    margin-top: 14px;
    width: 100%;
    border-collapse: collapse;
    font-size: 13.5px;
    font-variant-numeric: tabular-nums;
}

.breakdown td {
    padding: 6px 0;
    border: none;
    background: transparent;
    color: inherit;
    text-align: left;
}

.breakdown td:nth-child(2) {
    color: inherit;
    opacity: 0.75;
    text-align: right;
    padding-right: 14px;
    font-feature-settings: "tnum";
}

.breakdown td:last-child {
    text-align: right;
    font-weight: 700;
    width: 56px;
}

.breakdown tfoot td {
    border-top: 1.5px solid currentColor;
    padding-top: 8px;
    font-weight: 700;
}

.breakdown tfoot td:first-child {
    text-transform: uppercase;
    letter-spacing: 0.06em;
    font-size: 12px;
}

.actions {
    margin-top: 18px;
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 8px;
}

button {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 18px;
    background: var(--surface);
    color: var(--text-muted);
    border: 1.5px solid var(--border-strong);
    border-radius: 999px;
    font-size: 14px;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    transition: all 0.15s ease;
}

button:hover {
    background: var(--surface-soft);
    color: var(--text);
    border-color: var(--text-muted);
    transform: translateY(-1px);
}

button:active {
    transform: scale(0.97);
    background: var(--surface-soft);
}

button.btn-secondary {
    background: transparent;
    border-color: var(--border);
}

.toast {
    position: fixed;
    left: 50%;
    bottom: 32px;
    transform: translate(-50%, 20px);
    background: var(--text);
    color: var(--surface);
    padding: 12px 20px;
    border-radius: 999px;
    font-size: 14px;
    font-weight: 500;
    box-shadow: var(--shadow-xl);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease, transform 0.2s ease;
    z-index: 1000;
}

.toast--visible {
    opacity: 1;
    transform: translate(-50%, 0);
}

@media (max-width: 540px) {
    body { padding: 10px 6px; }
    .container { padding: 20px 16px; border-radius: var(--radius-lg); }

    .header { gap: 10px; }
    .header__icon { width: 40px; height: 40px; }
    .header h1 { font-size: 18px; }
    .header p { font-size: 12.5px; }

    /* Compact info boxes: collapse to single accordion-like blocks */
    .info { padding: 10px 12px; font-size: 13px; margin: 10px 0; }

    /* Table */
    th { font-size: 11px; padding: 10px 4px; letter-spacing: 0.03em; }
    th:first-child { padding-left: 10px; }
    td { padding: 8px 4px; }
    td:first-child { padding-left: 10px; font-size: 13px; }

    /* Hide "Semester " prefix — only show the number */
    .th-full { display: none; }

    /* Inputs: keep width tight but maintain 44px tap height */
    input[type="number"] {
        width: 52px;
        min-height: 44px;
        padding: 8px 4px;
        font-size: 16px; /* never below 16px — prevents iOS zoom */
        border-radius: 6px;
    }

    .grade-label { font-size: 10px; }
    .subject-badge { font-size: 9px; padding: 1px 6px; margin-left: 5px; }

    .legend { font-size: 11.5px; gap: 6px 12px; margin-top: 10px; }

    /* Buttons: full width, equal split */
    .actions { gap: 6px; }
    .actions button { flex: 1; justify-content: center; padding: 12px 8px; }

    /* Result */
    .result { padding: 14px 14px; font-size: 14px; }

    /* Breakdown: hide the formula column on small screens */
    .breakdown { font-size: 12px; }
    .breakdown td:nth-child(2) { display: none; }
    .breakdown tfoot td:nth-child(2) { display: none; }
}

/* Very small phones (320px) */
@media (max-width: 360px) {
    .container { padding: 16px 10px; }
    input[type="number"] { width: 46px; }
    td:first-child { font-size: 12px; }
    .actions button { padding: 12px 4px; font-size: 12px; gap: 5px; }
    .btn-label { display: none; }  /* icon-only on tiny screens */
}

@media print {
    body {
        background: white;
        color: black;
        padding: 0;
        font-size: 11pt;
    }
    .container {
        box-shadow: none;
        border: none;
        max-width: none;
        padding: 0;
        background: white;
    }
    .header { border-bottom-color: #999; padding-bottom: 8pt; }
    .header__icon, .actions, .legend, .toast, .info--important { display: none !important; }
    .info {
        background: #f5f5f5;
        color: black;
        border-left-color: #999;
    }
    th {
        background: #e5e5e5 !important;
        color: black !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }
    td { background: white !important; color: black; }
    input[type="number"] {
        border: 1px solid #999;
        background: white !important;
        color: black !important;
        font-weight: 600;
    }
    input[type="number"].is-included {
        background: #fff8dc !important;
        border-color: #999 !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }
    .result {
        display: block !important;
        background: #f5f5f5 !important;
        color: black !important;
        border-left-color: #999;
        page-break-inside: avoid;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }
    .result--success, .result--warning, .result--error {
        background: #f5f5f5 !important;
        color: black !important;
        border-left-color: #999;
    }
    .result__icon { display: none; }
    .breakdown td { color: black; }
    a { color: black; border-bottom: none; }
}
