:root {
    --bg: #0f172a;
    --surface: #1e293b;
    --surface-light: #334155;
    --text: #f8fafc;
    --text-muted: #94a3b8;
    --primary: #3b82f6;
    --primary-hover: #2563eb;
    --accent: #22d3ee;
    --success: #22c55e;
    --warning: #f59e0b;
    --danger: #ef4444;
    --radius: 12px;
    --shadow: 0 4px 6px -1px rgba(0,0,0,0.3);
    --shadow-lg: 0 20px 25px -5px rgba(0,0,0,0.4);
}

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

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
    min-height: 100vh;
}

.container {
    max-width: 1280px;
    margin: 0 auto;
    padding: 2rem 1rem;
}

header {
    text-align: center;
    margin-bottom: 2.5rem;
}

header h1 {
    font-size: 2rem;
    font-weight: 700;
    background: linear-gradient(135deg, var(--primary), var(--accent));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 0.5rem;
}

.subtitle {
    color: var(--text-muted);
    font-size: 1rem;
}

.calculator {
    display: grid;
    grid-template-columns: 1fr 400px;
    gap: 1.5rem;
}

@media (max-width: 1024px) {
    .calculator {
        grid-template-columns: 1fr;
    }
}

.input-section {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 1.25rem;
    align-content: start;
}

.card {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 1.25rem;
    border: 1px solid rgba(255,255,255,0.05);
    box-shadow: var(--shadow);
    transition: transform 0.2s, box-shadow 0.2s, opacity 0.3s;
}

.card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-lg);
}

.card.disabled {
    opacity: 0.45;
    transform: none;
    box-shadow: none;
    filter: grayscale(0.6);
}

.card.disabled .card-body {
    pointer-events: none;
    opacity: 0.5;
}

.card.disabled .card-header {
    pointer-events: auto;
    opacity: 1;
}

.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 1rem;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid rgba(255,255,255,0.05);
}

.card-title {
    display: flex;
    align-items: center;
    gap: 0.6rem;
}

.card-header .icon {
    font-size: 1.4rem;
}

.card-header h2 {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--text);
}

/* Toggle Switch */
.toggle {
    position: relative;
    display: inline-block;
    width: 44px;
    height: 24px;
    cursor: pointer;
    flex-shrink: 0;
}

.toggle input {
    opacity: 0;
    width: 0;
    height: 0;
}

.toggle-slider {
    position: absolute;
    inset: 0;
    background: var(--surface-light);
    border-radius: 24px;
    transition: background 0.2s;
}

.toggle-slider::before {
    content: '';
    position: absolute;
    height: 18px;
    width: 18px;
    left: 3px;
    bottom: 3px;
    background: var(--text);
    border-radius: 50%;
    transition: transform 0.2s;
}

.toggle input:checked + .toggle-slider {
    background: var(--primary);
}

.toggle input:checked + .toggle-slider::before {
    transform: translateX(20px);
    background: white;
}

.toggle input:focus + .toggle-slider {
    box-shadow: 0 0 0 3px rgba(59,130,246,0.3);
}

/* Segmented Control */
.segmented-control {
    display: flex;
    gap: 0;
    background: var(--bg);
    border-radius: 8px;
    padding: 3px;
    border: 1px solid var(--surface-light);
}

.segment {
    flex: 1;
    padding: 0.45rem 0.6rem;
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    border-radius: 6px;
    transition: all 0.15s;
    font-family: inherit;
}

.segment.active {
    background: var(--surface-light);
    color: var(--text);
    box-shadow: 0 1px 2px rgba(0,0,0,0.2);
}

.segment:hover:not(.active) {
    color: var(--text);
}

/* Card hint text */
.card-hint {
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-bottom: 0.75rem;
    line-height: 1.4;
}

/* Dynamic Items List */
.items-list {
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
    margin-bottom: 0.75rem;
}

.item-row {
    display: grid;
    grid-template-columns: 1fr 70px 90px 32px;
    gap: 0.5rem;
    align-items: center;
    background: var(--bg);
    padding: 0.5rem;
    border-radius: 8px;
    border: 1px solid var(--surface-light);
}

.item-row input {
    width: 100%;
    padding: 0.45rem 0.5rem;
    background: var(--surface);
    border: 1px solid var(--surface-light);
    border-radius: 6px;
    color: var(--text);
    font-size: 0.85rem;
    outline: none;
    font-family: inherit;
}

.item-row input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(59,130,246,0.15);
}

.item-row input::placeholder {
    color: var(--text-muted);
    opacity: 0.5;
    font-size: 0.8rem;
}

.item-row .item-total {
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--accent);
    text-align: right;
    white-space: nowrap;
}

.item-row .item-remove {
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 1.1rem;
    padding: 0.2rem;
    border-radius: 4px;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s;
}

.item-row .item-remove:hover {
    color: var(--danger);
    background: rgba(239,68,68,0.1);
}

.btn-add-item {
    width: 100%;
    padding: 0.55rem;
    background: var(--bg);
    border: 1px dashed var(--surface-light);
    border-radius: 8px;
    color: var(--text-muted);
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    font-family: inherit;
    transition: all 0.2s;
}

.btn-add-item:hover {
    border-color: var(--primary);
    color: var(--primary);
    background: rgba(59,130,246,0.05);
}

@media (max-width: 480px) {
    .item-row {
        grid-template-columns: 1fr 60px 80px 32px;
        gap: 0.35rem;
        padding: 0.4rem;
    }
    .item-row input {
        padding: 0.4rem 0.45rem;
        font-size: 0.82rem;
    }
}

.form-group {
    margin-bottom: 0.9rem;
}

.form-group label {
    display: block;
    font-size: 0.82rem;
    font-weight: 500;
    color: var(--text-muted);
    margin-bottom: 0.35rem;
}

.form-group input {
    width: 100%;
    padding: 0.6rem 0.8rem;
    background: var(--bg);
    border: 1px solid var(--surface-light);
    border-radius: 8px;
    color: var(--text);
    font-size: 0.95rem;
    transition: border-color 0.2s;
    outline: none;
}

.form-group input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(59,130,246,0.15);
}

.form-group input.synced {
    opacity: 0.6;
    cursor: not-allowed;
}

.form-group small {
    display: block;
    margin-top: 0.2rem;
    font-size: 0.75rem;
    color: var(--text-muted);
}

.result-inline {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--accent);
    margin-top: 0.4rem;
    text-align: right;
    min-height: 1.4rem;
}

.card-highlight {
    border: 1px solid rgba(34, 211, 238, 0.2);
    background: linear-gradient(180deg, var(--surface), rgba(34, 211, 238, 0.03));
}

/* RESULTS */
.results-section {
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
    position: sticky;
    top: 1.5rem;
    height: fit-content;
}

.result-card {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 1.5rem;
    text-align: center;
    border: 1px solid rgba(255,255,255,0.05);
    box-shadow: var(--shadow);
}

.result-card.total {
    background: linear-gradient(135deg, var(--primary), #7c3aed);
    border: none;
}

.result-card h3 {
    font-size: 0.9rem;
    font-weight: 500;
    opacity: 0.9;
    margin-bottom: 0.5rem;
}

.result-card .amount {
    font-size: 2.4rem;
    font-weight: 700;
}

.result-card .detail {
    font-size: 0.9rem;
    opacity: 0.85;
    margin-top: 0.25rem;
}

.breakdown-card {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 1.25rem;
    border: 1px solid rgba(255,255,255,0.05);
    box-shadow: var(--shadow);
}

.breakdown-card h3 {
    font-size: 1rem;
    margin-bottom: 1rem;
    text-align: center;
}

.chart-container {
    display: flex;
    justify-content: center;
    margin-bottom: 1rem;
}

.legend {
    list-style: none;
    display: grid;
    gap: 0.4rem;
}

.legend li {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 0.82rem;
    color: var(--text-muted);
}

.legend .dot {
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    margin-right: 0.5rem;
}

.legend .value {
    font-weight: 600;
    color: var(--text);
    margin-left: auto;
}

.result-details {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 1.25rem;
    border: 1px solid rgba(255,255,255,0.05);
    box-shadow: var(--shadow);
}

.result-details h3 {
    font-size: 1rem;
    margin-bottom: 1rem;
    text-align: center;
}

.detail-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.55rem 0;
    font-size: 0.88rem;
    border-bottom: 1px solid rgba(255,255,255,0.03);
}

.detail-row.hidden {
    display: none;
}

.detail-row span {
    color: var(--text-muted);
}

.detail-row strong {
    color: var(--text);
    font-weight: 500;
}

.detail-row.divider {
    border-top: 2px solid rgba(255,255,255,0.08);
    margin-top: 0.5rem;
    padding-top: 0.75rem;
    font-weight: 600;
}

.detail-row.divider span,
.detail-row.divider strong {
    color: var(--text);
}

.detail-row.highlight {
    background: rgba(34, 211, 238, 0.08);
    margin: 0.5rem -0.5rem 0;
    padding: 0.75rem 0.5rem;
    border-radius: 8px;
    border: none;
}

.detail-row.highlight span,
.detail-row.highlight strong {
    color: var(--accent);
    font-weight: 700;
}

.actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.6rem;
    justify-content: center;
}

.actions button {
    padding: 0.7rem 1.2rem;
    border-radius: 8px;
    font-size: 0.88rem;
    font-weight: 600;
    cursor: pointer;
    border: none;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 0.35rem;
    white-space: nowrap;
}

.btn-primary {
    background: var(--primary);
    color: white;
}

.btn-primary:hover {
    background: var(--primary-hover);
}

.btn-secondary {
    background: var(--surface-light);
    color: var(--text);
}

.btn-secondary:hover {
    background: #475569;
}

.btn-ghost {
    background: transparent;
    color: var(--text-muted);
    border: 1px solid var(--surface-light);
}

.btn-ghost:hover {
    color: var(--text);
    border-color: var(--text-muted);
}

footer {
    margin-top: 3rem;
    text-align: center;
    color: var(--text-muted);
    font-size: 0.82rem;
    padding: 2rem 1rem;
    border-top: 1px solid rgba(255,255,255,0.05);
}

.formula-note {
    margin-top: 1rem;
    font-size: 0.75rem;
    line-height: 1.8;
    opacity: 0.7;
}

@media (max-width: 640px) {
    .input-section {
        grid-template-columns: 1fr;
    }
    header h1 {
        font-size: 1.5rem;
    }
    .result-card .amount {
        font-size: 1.8rem;
    }
}

.footer-cta {
    background: rgba(59, 130, 246, 0.08);
    border: 1px solid rgba(59, 130, 246, 0.2);
    border-radius: var(--radius);
    padding: 1.25rem 1.5rem;
    margin-bottom: 1.5rem;
    font-size: 0.92rem;
    line-height: 1.7;
}

.footer-cta p {
    margin: 0;
    color: var(--text);
}

.footer-cta p + p {
    margin-top: 0.5rem;
}

.footer-cta a {
    color: var(--accent);
    font-weight: 600;
    text-decoration: none;
    border-bottom: 1px solid rgba(34, 211, 238, 0.3);
    transition: border-color 0.2s;
}

.footer-cta a:hover {
    border-bottom-color: var(--accent);
}

.footer-contact {
    font-size: 0.85rem;
    opacity: 0.85;
}

/* SAVE RECORD */
.save-record {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 1.25rem;
    border: 1px solid rgba(255,255,255,0.05);
    box-shadow: var(--shadow);
}

.save-record .form-group {
    margin-bottom: 0.8rem;
}

.save-record input[type="text"] {
    width: 100%;
    padding: 0.6rem 0.8rem;
    background: var(--bg);
    border: 1px solid var(--surface-light);
    border-radius: 8px;
    color: var(--text);
    font-size: 0.95rem;
    transition: border-color 0.2s;
    outline: none;
    font-family: inherit;
}

.save-record input[type="text"]:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(59,130,246,0.15);
}

.save-record input::placeholder {
    color: var(--text-muted);
    opacity: 0.6;
}

.btn-wide {
    width: 100%;
    justify-content: center;
    padding: 0.75rem 1.2rem;
}

/* RECORDS SECTION */
.records-section {
    margin-top: 2.5rem;
    background: var(--surface);
    border-radius: var(--radius);
    padding: 1.25rem 1.5rem;
    border: 1px solid rgba(255,255,255,0.05);
    box-shadow: var(--shadow);
}

.records-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.75rem;
}

.records-count {
    font-size: 0.8rem;
    color: var(--text-muted);
    background: var(--bg);
    padding: 0.15rem 0.5rem;
    border-radius: 999px;
    margin-left: 0.4rem;
}

.records-content {
    margin-top: 1rem;
    padding-top: 1rem;
    border-top: 1px solid rgba(255,255,255,0.05);
}

.records-toolbar {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
    flex-wrap: wrap;
}

.records-title {
    display: flex;
    align-items: center;
    gap: 0.6rem;
}

.records-title .icon {
    font-size: 1.3rem;
}

.records-title h2 {
    font-size: 1.1rem;
    font-weight: 600;
}

.records-actions {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.btn-small {
    padding: 0.45rem 0.8rem;
    font-size: 0.8rem;
    border-radius: 6px;
}

.btn-danger {
    background: rgba(239, 68, 68, 0.1);
    color: var(--danger);
    border: 1px solid rgba(239, 68, 68, 0.2);
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 0.3rem;
}

.btn-danger:hover {
    background: rgba(239, 68, 68, 0.2);
}

.records-table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: 8px;
    border: 1px solid rgba(255,255,255,0.05);
}

.records-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
    min-width: 520px;
}

.records-table thead th {
    background: var(--bg);
    padding: 0.7rem 0.9rem;
    text-align: left;
    font-weight: 600;
    font-size: 0.82rem;
    color: var(--text-muted);
    border-bottom: 1px solid rgba(255,255,255,0.08);
    white-space: nowrap;
}

.records-table thead th.col-cost,
.records-table thead th.col-sale {
    text-align: right;
}

.records-table thead th.col-actions {
    text-align: center;
}

.records-table tbody td {
    padding: 0.75rem 0.9rem;
    border-bottom: 1px solid rgba(255,255,255,0.04);
    vertical-align: middle;
    color: var(--text);
}

.records-table tbody tr:hover {
    background: rgba(255,255,255,0.02);
}

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

.records-table .col-name {
    max-width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.records-table .col-date {
    white-space: nowrap;
    color: var(--text-muted);
    font-size: 0.85rem;
}

.records-table .col-cost,
.records-table .col-sale {
    text-align: right;
    font-weight: 600;
    white-space: nowrap;
}

.records-table .col-cost {
    color: var(--text);
}

.records-table .col-sale {
    color: var(--accent);
}

.records-table .col-actions {
    text-align: center;
    white-space: nowrap;
}

.record-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 1rem;
    padding: 0.3rem 0.4rem;
    border-radius: 4px;
    transition: all 0.15s;
    line-height: 1;
}

.record-btn:hover {
    background: rgba(255,255,255,0.08);
    color: var(--text);
}

.record-btn.btn-load:hover {
    color: var(--primary);
}

.record-btn.btn-print:hover {
    color: var(--success);
}

.record-btn.btn-delete:hover {
    color: var(--danger);
    background: rgba(239,68,68,0.1);
}

.records-empty {
    text-align: center;
    padding: 2.5rem 1rem;
    color: var(--text-muted);
}

.empty-icon {
    font-size: 2.5rem;
    display: block;
    margin-bottom: 0.75rem;
    opacity: 0.5;
}

.records-empty p {
    margin: 0;
    font-size: 0.95rem;
}

.empty-hint {
    font-size: 0.82rem;
    opacity: 0.7;
    margin-top: 0.35rem;
}

.print-only {
    display: none;
}

/* Print styles */
@media print {
    @page {
        margin: 2cm 1.5cm;
        size: A4;
    }

    body {
        background: #fff;
        color: #111;
        font-size: 11pt;
        line-height: 1.45;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    .calculator,
    header,
    footer,
    .actions,
    .card,
    .result-card,
    .breakdown-card,
    .result-details,
    .input-section,
    .results-section,
    .chart-container,
    .legend,
    #costChart,
    #costLegend {
        display: none !important;
    }

    .container {
        max-width: 100%;
        padding: 0;
        margin: 0;
    }

    .print-only {
        display: block !important;
    }

    /* Report layout */
    .report-header {
        text-align: center;
        margin-bottom: 1.5rem;
        padding-bottom: 1rem;
        border-bottom: 2px solid #111;
    }

    .report-header h1 {
        font-size: 18pt;
        font-weight: 700;
        margin: 0 0 0.3rem;
        color: #111;
        -webkit-text-fill-color: #111;
        background: none;
    }

    .report-meta {
        font-size: 10pt;
        color: #555;
        margin: 0;
    }

    .report-table {
        width: 100%;
        border-collapse: collapse;
        margin-bottom: 1.5rem;
        font-size: 10.5pt;
    }

    .report-table thead th {
        background: #f5f5f5;
        border-bottom: 2px solid #111;
        padding: 0.55rem 0.6rem;
        text-align: left;
        font-weight: 600;
        color: #111;
    }

    .report-table th.col-val,
    .report-table td.col-val {
        text-align: right;
    }

    .report-table tbody td {
        padding: 0.45rem 0.6rem;
        border-bottom: 1px solid #ddd;
        vertical-align: top;
    }

    .report-table tbody tr:last-child td {
        border-bottom: 1px solid #999;
    }

    .report-table tfoot td {
        padding: 0.5rem 0.6rem;
        font-weight: 600;
        border-bottom: 1px solid #ddd;
    }

    .report-table tfoot .row-total {
        border-top: 2px solid #111;
        border-bottom: 2px solid #111;
        background: #f9f9f9;
        font-size: 11pt;
    }

    .report-table tfoot .row-final {
        background: #eef;
        font-size: 12pt;
    }

    .report-notes {
        margin-top: 1.5rem;
        padding: 0.8rem 1rem;
        border: 1px solid #ddd;
        border-radius: 6px;
        background: #fafafa;
        font-size: 9.5pt;
        color: #444;
    }

    .report-notes h4 {
        margin: 0 0 0.3rem;
        font-size: 10pt;
        color: #111;
    }

    .report-notes p {
        margin: 0;
    }

    .report-footer {
        margin-top: 2rem;
        padding-top: 0.8rem;
        border-top: 1px solid #ccc;
        text-align: center;
        font-size: 9pt;
        color: #666;
    }

    .report-footer p {
        margin: 0.15rem 0;
    }
}
