/* Reusable pieces: buttons, stat cards, form controls, modal, toast, empty state */

/* ===== Buttons ===== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    min-height: 44px;
    padding: 0.6rem 1.25rem;
    font-size: 0.85rem;
    font-weight: 600;
    border-radius: var(--radius-sm);
    transition: background-color var(--transition), color var(--transition),
        border-color var(--transition), box-shadow var(--transition);
}

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

.btn-primary:hover {
    background: var(--accent-hover);
    box-shadow: 0 4px 12px var(--accent-glow);
}

.btn-secondary {
    background: var(--bg-card);
    border: 1px solid var(--border);
    color: var(--text-secondary);
}

.btn-secondary:hover {
    background: var(--bg-card-hover);
    color: var(--text-primary);
}

.btn-danger {
    background: rgba(248, 113, 113, 0.15);
    color: var(--danger-text);
}

.btn-danger:hover {
    background: rgba(248, 113, 113, 0.25);
}

.btn-sm {
    padding: 0.4rem 0.75rem;
    font-size: 0.8rem;
}

/* 44x44 meets touch-target guidance */
.btn-icon {
    width: 44px;
    height: 44px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 0.9rem;
    cursor: pointer;
    transition: background-color var(--transition), border-color var(--transition);
}

.btn-icon:hover {
    background: var(--bg-card-hover);
    border-color: var(--accent-light);
}

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

.btn-icon.danger:hover {
    background: rgba(248, 113, 113, 0.15);
    border-color: var(--danger-text);
}

/* ===== Stat Cards ===== */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 1rem;
}

.stat-card {
    display: block;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem;
    transition: transform var(--transition), box-shadow var(--transition), border-color var(--transition);
}

/* Only cards that are real links advertise themselves as clickable */
a.stat-card:hover,
button.stat-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
    border-color: var(--border-strong);
}

.stat-card .stat-icon {
    font-size: 1.5rem;
    margin-bottom: 0.5rem;
}

.stat-card h3 {
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 0.25rem;
}

.stat-card .stat-value {
    font-size: 1.75rem;
    font-weight: 700;
}

/* ===== Forms ===== */
.form-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.form-label {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-primary);
}

.form-label .required {
    color: var(--danger-text);
    margin-left: 0.15rem;
}

/* The form-group's flex gap already spaces this from the input, so the
   browser's default <p> margin only pushes it out of alignment. */
.form-help {
    color: var(--text-muted);
    font-size: 0.75rem;
    line-height: 1.4;
    margin: 0;
}

.form-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: 1rem;
}

.form-actions {
    display: flex;
    gap: 0.75rem;
    margin-top: 0.5rem;
}

/* Shared add/edit dialogs: keep the form's page-specific layout, but give it
   one focused surface above the dimmed dashboard. */
html.form-dialog-open,
html.form-dialog-open body {
    overflow: hidden;
}

.form-dialog-backdrop {
    position: fixed;
    inset: 0;
    z-index: 900;
    background: rgb(8 10 18 / 68%);
    backdrop-filter: blur(3px);
    opacity: 0;
    transition: opacity 180ms ease;
}

.form-dialog-backdrop.is-visible {
    opacity: 1;
}

.form-dialog-panel:not([hidden]) {
    position: fixed;
    z-index: 950;
    top: 50%;
    left: 50%;
    width: min(42rem, calc(100vw - 2rem));
    max-height: min(88dvh, 54rem);
    overflow: auto;
    overscroll-behavior: contain;
    border-radius: var(--radius);
    opacity: 0;
    transform: translate(-50%, -47%) scale(0.985);
    transition: opacity 180ms ease, transform 180ms cubic-bezier(0.2, 0.75, 0.25, 1);
    scrollbar-gutter: stable;
}

.form-dialog-panel:not([hidden]).is-visible {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
}

.form-dialog-panel:not([hidden]) > .card {
    margin: 0;
}

.form-dialog-titlebar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 1rem;
    padding-bottom: 0.85rem;
    border-bottom: 1px solid var(--border);
}

.form-dialog-title {
    margin: 0;
    font-size: 1.2rem;
    line-height: 1.3;
}

.form-dialog-close {
    display: grid;
    flex: 0 0 2.5rem;
    width: 2.5rem;
    height: 2.5rem;
    place-items: center;
    border: 1px solid var(--border);
    border-radius: 50%;
    background: var(--bg-secondary);
    color: var(--text-secondary);
    font-size: 1.45rem;
    line-height: 1;
    cursor: pointer;
}

.form-dialog-close:hover {
    border-color: var(--border-strong);
    color: var(--text-primary);
}

.form-dialog-close:focus-visible {
    outline: 2px solid var(--accent-light);
    outline-offset: 2px;
}

@media (max-width: 600px) {
    .form-dialog-panel:not([hidden]) {
        top: 50%;
        left: 50%;
        width: calc(100vw - 2rem);
        max-height: calc(100dvh - 2rem - var(--app-safe-top) - var(--app-safe-bottom));
        border-radius: var(--radius);
        transform: translate(-50%, -47%) scale(0.985);
    }
}

@media (prefers-reduced-motion: reduce) {
    .form-dialog-backdrop,
    .form-dialog-panel:not([hidden]) {
        transition-duration: 0.01ms;
    }
}

/* ===== Modal ===== */
.modal {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.modal-content {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 2rem;
    max-width: 480px;
    width: 100%;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

.modal-content h3 {
    font-size: 1.25rem;
    font-weight: 600;
    margin-bottom: 1rem;
    color: var(--text-primary);
}

.modal-content p {
    font-size: 0.9rem;
    color: var(--text-secondary);
    line-height: 1.6;
    margin-bottom: 1.5rem;
}

.modal-actions {
    display: flex;
    gap: 0.75rem;
    justify-content: flex-end;
}

/* ===== Toast Notification ===== */
.toast {
    position: fixed;
    bottom: 2rem;
    right: 2rem;
    background: var(--bg-card);
    color: var(--text-primary);
    padding: 1rem 1.5rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
    font-size: 0.9rem;
    font-weight: 500;
    z-index: 2000;
    opacity: 0;
    transform: translateY(100px);
    transition: opacity 0.3s ease, transform 0.3s ease;
    pointer-events: none;
}

.toast.show {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
}

.toast-success {
    border-left: 4px solid var(--success);
}

.toast-error {
    border-left: 4px solid var(--danger);
}

.toast-info {
    border-left: 4px solid var(--accent-light);
}

/* ===== Empty State ===== */
.empty-state {
    text-align: center;
    padding: 3rem;
    color: var(--text-muted);
}

.empty-state .empty-icon {
    font-size: 2.5rem;
    margin-bottom: 0.75rem;
}

.empty-state p {
    font-size: 0.9rem;
}

.task-list .empty-state {
    padding: 2rem;
    text-align: center;
    color: var(--text-muted);
    font-size: 0.9rem;
    font-style: italic;
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
    .stats-grid {
        grid-template-columns: 1fr 1fr;
    }

    .form-row {
        grid-template-columns: 1fr;
    }

    /* Every text field, select and textarea measured 38-41px tall, under the
       44px target. Checkboxes and radios are excluded: the task checkbox is
       deliberately 24px, and the note colour radios are visually hidden
       0x0 inputs whose tap target is the swatch label beside them. */
    input:not([type="checkbox"]):not([type="radio"]),
    select,
    textarea {
        min-height: 44px;
    }

    .toast {
        right: 1rem;
        left: 1rem;
        /* Clears the tab bar so a confirmation never hides behind it. */
        bottom: calc(var(--tab-bar-total) + 0.75rem);
    }

    .modal-content {
        padding: 1.5rem;
    }
}

/* A centred dialog on a phone floats in the middle of the screen with dead
   space above and below, and its buttons sit right where a thumb covers
   them. The sheet drops to the bottom edge, fills the width, and puts the
   actions under the thumb instead. Anchored with align-items:flex-end on the
   overlay rather than auto margins, so the sheet is fully open on arrival. */
@media (max-width: 768px) {
    .modal {
        align-items: flex-end;
        padding: 0;
    }

    .modal-content {
        width: 100%;
        max-width: 100%;
        padding: 1.5rem 1.25rem calc(1.5rem + var(--app-safe-bottom));
        border-radius: var(--radius) var(--radius) 0 0;
        border-bottom: none;
        box-shadow: 0 -20px 60px rgba(0, 0, 0, 0.5);
    }

    /* Actions go full width and stack, so each is a reliable thumb target
       rather than two short buttons side by side. */
    .modal-actions {
        flex-direction: column-reverse;
    }

    .modal-actions .btn {
        width: 100%;
    }
}

/* Below 480px a full-width stat card is mostly padding, and four of them
   pushed the first real content (Today's Tasks) off the screen entirely.
   The compact treatment keeps the 2-up grid from the rule above and trims
   the icon, padding, and value scale so the overview fits in roughly half
   the vertical space. */
@media (max-width: 480px) {
    .stats-grid {
        gap: 0.6rem;
    }

    .stat-card {
        padding: 0.85rem 0.9rem;
    }

    .stat-card .stat-icon {
        font-size: 1.15rem;
        margin-bottom: 0.3rem;
    }

    .stat-card h3 {
        font-size: 0.68rem;
        margin-bottom: 0.15rem;
    }

    .stat-card .stat-value {
        font-size: 1.3rem;
    }

    /* 3rem of padding on each side plus a nowrap link ("Create your first
       goal ->") overflowed the card at 320px. The link is allowed to wrap and
       the padding gives way, which also lifts the message closer to the
       content it refers to. */
    .empty-state {
        padding: 1.5rem 1rem;
    }

    .empty-state .section-link {
        white-space: normal;
    }

    .form-actions {
        flex-direction: column;
    }

    .form-actions .btn {
        width: 100%;
    }
}
