:root {
    --bg: #f4f6fb;
    --surface: #ffffff;
    --surface-2: #eef1f8;
    --text: #1a1d29;
    --text-muted: #6b7280;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --danger: #dc2626;
    --border: #e2e5ec;
    --shadow: 0 2px 10px rgba(20, 25, 40, 0.06);
    --radius: 14px;
}

[data-theme="dark"] {
    --bg: #12141c;
    --surface: #1b1e29;
    --surface-2: #242836;
    --text: #eef0f5;
    --text-muted: #9aa0b0;
    --border: #2c3040;
    --shadow: 0 2px 12px rgba(0, 0, 0, 0.4);
}

@media (prefers-color-scheme: dark) {
    html[data-theme-pref="system"] {
        --bg: #12141c;
        --surface: #1b1e29;
        --surface-2: #242836;
        --text: #eef0f5;
        --text-muted: #9aa0b0;
        --border: #2c3040;
        --shadow: 0 2px 12px rgba(0, 0, 0, 0.4);
    }
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    min-height: 100vh;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    -webkit-tap-highlight-color: transparent;
}

/* =========================================================
   TESTATA + RICERCA FLOTTANTE
   ========================================================= */
.topbar {
    position: sticky;
    top: 0;
    z-index: 20;

    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;

    min-height: 58px;
    padding: 10px 16px 14px;

    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

.app-logo {
    margin: 0;
    font-size: 1.15rem;
    white-space: nowrap;
}

.topbar-actions {
    display: flex;
    align-items: center;
    gap: 4px;
}

.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 36px;
    padding: 0;

    background: transparent;
    border: none;
    border-radius: 12px;
    color: var(--text);
    font-size: 1.15rem;
    text-decoration: none;
    cursor: pointer;
}

.icon-btn:hover {
    background: var(--surface-2);
}

.icon-btn.hidden,
.hidden {
    display: none !important;
}

.search-island-wrap {
    position: relative;
    z-index: 25;

    height: 0;

    display: flex;
    justify-content: center;
}

.search-island {
    position: relative;
    top: 5px;

    display: flex;
    align-items: center;
    gap: 9px;

    width: min(calc(100% - 32px), 540px);
    height: 44px;
    padding: 0 13px;

    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 999px;

    box-shadow:
        0 6px 18px rgba(0, 0, 0, 0.18),
        inset 0 1px 0 rgba(255, 255, 255, 0.04);

    transition:
        border-color 180ms ease,
        background 180ms ease,
        box-shadow 180ms ease,
        transform 180ms ease;
}

.search-island:focus-within {
    background: var(--surface);
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.18), 0 6px 18px rgba(0, 0, 0, 0.18);
    transform: translateY(1px);
}

.search-icon {
    flex: 0 0 auto;
    color: var(--text-muted);
    font-size: 1.2rem;
    line-height: 1;
}

#searchInput {
    width: 100%;
    min-width: 0;
    height: 100%;
    padding: 0;

    background: transparent;
    border: none;
    outline: none;
    color: var(--text);
    font-size: 0.92rem;
}

#searchInput::placeholder {
    color: var(--text-muted);
    opacity: 0.9;
}

#searchInput::-webkit-search-cancel-button {
    display: none;
}

.clear-search-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;

    width: 24px;
    height: 24px;
    padding: 0;

    background: var(--border);
    border: none;
    border-radius: 50%;
    color: var(--text-muted);
    font-size: 1.15rem;
    line-height: 1;
    cursor: pointer;
}

.clear-search-btn:hover {
    background: var(--text-muted);
    color: var(--surface);
}

/* =========================================================
   FILTRI E NOTE
   ========================================================= */
.filters-bar {
    display: flex;
    gap: 8px;
    overflow-x: auto;

    padding: 34px 16px 10px;

    background: var(--bg);
}

.filter-chip {
    flex: none;
    padding: 6px 14px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 999px;
    color: var(--text-muted);
    font-size: 0.85rem;
    white-space: nowrap;
    cursor: pointer;
}

.filter-chip.active {
    background: var(--primary);
    border-color: var(--primary);
    color: #fff;
}

.notes-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
    gap: 14px;
    padding: 16px;
}

.notes-grid-compact {
    margin-top: 10px;
    padding: 0;
}

.note-card {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 14px;

    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    cursor: pointer;
    transition: transform 0.12s ease;
}

.note-card:hover {
    transform: translateY(-2px);
}

.note-card h3 {
    margin: 0;
    font-size: 1rem;
}

.note-card p {
    display: -webkit-box;
    margin: 0;
    overflow: hidden;
    color: var(--text-muted);
    font-size: 0.88rem;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 4;
}

.note-owner {
    color: var(--primary) !important;
    font-size: 0.78rem !important;
    -webkit-line-clamp: 1 !important;
}

.note-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.note-tag {
    padding: 3px 9px;
    background: var(--surface-2);
    border-radius: 999px;
    color: var(--primary);
    font-size: 0.72rem;
}

.empty-state {
    grid-column: 1 / -1;
    padding: 60px 20px;
    color: var(--text-muted);
    text-align: center;
}

.fab {
    position: fixed;
    right: 24px;
    bottom: 24px;
    z-index: 30;

    width: 58px;
    height: 58px;

    background: var(--primary);
    border: none;
    border-radius: 50%;
    box-shadow: 0 4px 14px rgba(37, 99, 235, 0.4);
    color: #fff;
    font-size: 1.8rem;
    cursor: pointer;
}

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

/* =========================================================
   MODAL / EDITOR
   ========================================================= */
.modal-overlay {
    position: fixed;
    inset: 0;
    z-index: 50;

    display: flex;
    align-items: flex-end;
    justify-content: center;

    background: rgba(10, 12, 20, 0.5);
}

@media (min-width: 640px) {
    .modal-overlay {
        align-items: center;
    }
}

.modal {
    display: flex;
    flex-direction: column;
    gap: 12px;

    width: 100%;
    max-width: 560px;
    max-height: 90vh;
    overflow-y: auto;
    padding: 18px;

    background: var(--surface);
    border-radius: 18px 18px 0 0;
}

@media (min-width: 640px) {
    .modal {
        border-radius: 18px;
    }
}

.modal-title {
    margin: 0;
    font-size: 1.15rem;
}

.modal-hint {
    margin: 0;
    color: var(--text-muted);
    font-size: 0.85rem;
}

.modal-header {
    display: flex;
    align-items: center;
    gap: 8px;
}

#noteTitle {
    flex: 1;
    padding: 10px 12px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 10px;
    color: var(--text);
    font-size: 1.1rem;
}

#noteBody {
    min-height: 160px;
    padding: 12px;
    resize: vertical;

    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 10px;
    color: var(--text);
    font-family: inherit;
    font-size: 0.95rem;
}

.tag-input-wrap {
    position: relative;
}

.tag-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 8px;
}

.tag-chip {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 10px;
    background: var(--surface-2);
    border-radius: 999px;
    color: var(--primary);
    font-size: 0.8rem;
}

.tag-chip button {
    padding: 0;
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 0.9rem;
    line-height: 1;
    cursor: pointer;
}

#tagInput {
    width: 100%;
    padding: 9px 12px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 10px;
    color: var(--text);
}

.tag-suggestions {
    position: absolute;
    top: 100%;
    right: 0;
    left: 0;
    z-index: 60;

    max-height: 180px;
    overflow-y: auto;
    margin-top: 4px;

    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: var(--shadow);
}

.tag-suggestions.hidden {
    display: none;
}

.tag-suggestion-item {
    padding: 9px 12px;
    font-size: 0.9rem;
    cursor: pointer;
}

.tag-suggestion-item:hover {
    background: var(--surface-2);
}

.modal-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.spacer {
    flex: 1;
}

.btn {
    padding: 10px 18px;
    border: none;
    border-radius: 10px;
    font-size: 0.92rem;
    font-weight: 600;
    cursor: pointer;
}

.btn-small {
    padding: 6px 12px;
    font-size: 0.8rem;
}

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

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

.btn-ghost {
    background: var(--surface-2);
    color: var(--text);
}

.btn-danger {
    background: transparent;
    border: 1px solid var(--danger);
    color: var(--danger);
}

/* =========================================================
   CONDIVISIONE
   ========================================================= */
.share-form-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
}

.share-form-row input[type="email"] {
    flex: 1;
    min-width: 180px;
    padding: 9px 12px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 10px;
    color: var(--text);
}

.checkbox-inline {
    display: flex;
    align-items: center;
    gap: 6px;
    color: var(--text-muted);
    font-size: 0.85rem;
    white-space: nowrap;
}

.share-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 10px;
}

.share-row {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    background: var(--surface-2);
    border-radius: 10px;
    font-size: 0.88rem;
}

.share-row span {
    flex: 1;
}

.exclusions-list {
    display: flex;
    flex-direction: column;
    gap: 4px;
    max-height: 300px;
    overflow-y: auto;
}

.exclusion-row {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px;
    border-radius: 8px;
    font-size: 0.9rem;
    cursor: pointer;
}

.exclusion-row:hover {
    background: var(--surface-2);
}

/* =========================================================
   LOGIN / ACCOUNT
   ========================================================= */
.auth-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 20px;
}

.auth-card {
    width: 100%;
    max-width: 380px;
    padding: 32px 28px;
    background: var(--surface);
    border-radius: 18px;
    box-shadow: var(--shadow);
}

.auth-title {
    margin: 0 0 4px;
    font-size: 1.4rem;
}

.auth-subtitle {
    margin: 0 0 20px;
    color: var(--text-muted);
    font-size: 0.9rem;
}

.auth-card form {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 8px;
}

.auth-card label {
    color: var(--text-muted);
    font-size: 0.85rem;
}

.auth-card input {
    padding: 10px 12px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 10px;
    color: var(--text);
    font-size: 0.95rem;
}

.auth-card .btn {
    margin-top: 6px;
}

.btn-google {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 10px;
    margin-bottom: 4px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 10px;
    color: var(--text);
    font-weight: 600;
    text-decoration: none;
}

.auth-divider {
    position: relative;
    margin: 16px 0;
    color: var(--text-muted);
    font-size: 0.8rem;
    text-align: center;
}

.auth-tabs {
    display: flex;
    gap: 16px;
    margin-bottom: 16px;
    border-bottom: 1px solid var(--border);
}

.auth-tabs a {
    padding-bottom: 8px;
    color: var(--text-muted);
    font-size: 0.9rem;
    font-weight: 600;
    text-decoration: none;
}

.auth-tabs a.active {
    border-bottom: 2px solid var(--primary);
    color: var(--primary);
}

.auth-link {
    margin-top: 12px;
    font-size: 0.85rem;
    text-align: center;
}

.auth-link a,
.settings-card a {
    color: var(--primary);
}

.alert {
    padding: 10px 14px;
    margin-bottom: 12px;
    border-radius: 10px;
    font-size: 0.85rem;
}

.alert-error {
    background: #fee2e2;
    color: #991b1b;
}

.alert-success {
    background: #dcfce7;
    color: #166534;
}

/* =========================================================
   IMPOSTAZIONI / CONDIVISIONI
   ========================================================= */
.settings-page,
.shared-page {
    display: flex;
    flex-direction: column;
    gap: 16px;
    max-width: 560px;
    margin: 0 auto;
    padding: 20px;
}

.settings-card {
    padding: 18px;
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.settings-card h2 {
    margin: 0 0 12px;
    font-size: 1rem;
}

.theme-options {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.theme-options label {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 0.92rem;
}

.switch-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 0.92rem;
}

#settingsMsg {
    color: var(--text-muted);
    font-size: 0.85rem;
    text-align: center;
}

/* =========================================================
   MOBILE
   ========================================================= */
@media (max-width: 480px) {
    .topbar {
        min-height: 54px;
        padding: 9px 12px 13px;
    }

    .app-logo {
        font-size: 1rem;
    }

    .topbar-actions {
        gap: 1px;
    }

    .icon-btn {
        width: 34px;
        height: 34px;
        font-size: 1rem;
    }

    .search-island {
        top: 5px;

        width: calc(100% - 24px);
        height: 42px;
        padding: 0 12px;
    }

    .search-island:focus-within {
        transform: translateY(1px) scale(1.01);
    }

    #searchInput {
        font-size: 0.88rem;
    }

    .search-icon {
        font-size: 1.1rem;
    }

    .filters-bar {
        padding: 33px 12px 9px;
    }

    .notes-grid {
        grid-template-columns: 1fr;
        padding: 14px 12px;
    }

    .fab {
        right: 20px;
        bottom: 20px;
    }
}

.change-password-form {
    display: flex;
    flex-direction: column;
    gap: 9px;
    margin-top: 14px;
}

.change-password-form label {
    color: var(--text-muted);
    font-size: 0.85rem;
}

.change-password-form input {
    width: 100%;
    padding: 10px 12px;

    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 10px;

    color: var(--text);
    font-size: 0.95rem;
}

.change-password-form input:focus {
    border-color: var(--primary);
    outline: 2px solid rgba(37, 99, 235, 0.16);
}

.change-password-form .btn {
    align-self: flex-start;
    margin-top: 5px;
}

.form-message {
    min-height: 18px;
    margin-top: 10px;
    font-size: 0.85rem;
}

.form-message-success {
    color: #16a34a;
}

.form-message-error {
    color: var(--danger);
}
