:root {
    --bg: #f3f3f3;
    --bg-soft: #ebebeb;
    --bg-card: #ffffff;
    --bg-hover: #e6e6e6;
    --text: #111111;
    --text-dim: #888888;
    --accent: #333333;
    --accent-glow: rgba(0, 0, 0, 0.06);
    --border: #dcdcdc;
    --border-light: #e8e8e8;
    --danger: #ef4444;
    --danger-bg: rgba(239, 68, 68, 0.1);
    --success: #4ade80;
    --success-bg: rgba(74, 222, 128, 0.1);
    --warning: #f59e0b;
    --warning-bg: rgba(245, 158, 11, 0.1);
}

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

body {
    font-family: 'Outfit', sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
}

h1, h2, h3, h4 { font-family: 'Syne', sans-serif; }

/* ========== AUTH ========== */
.auth-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 40px;
}
.auth-container { width: 100%; max-width: 420px; }
.auth-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 20px;
    padding: 40px;
}
.auth-header { text-align: center; margin-bottom: 32px; }
.auth-logo {
    font-family: 'Syne', sans-serif;
    font-size: 28px;
    font-weight: 800;
    color: var(--text);
    text-decoration: none;
    letter-spacing: -1px;
    display: block;
}
.auth-logo span { color: var(--accent); }
.auth-subtitle {
    display: block;
    font-size: 13px;
    color: var(--text-dim);
    margin-top: 4px;
    letter-spacing: 2px;
    text-transform: uppercase;
}

/* ========== ADMIN LAYOUT ========== */
.admin-body {
    min-height: 100vh;
    display: flex;
}
.admin-layout {
    display: flex;
    width: 100%;
    min-height: 100vh;
}

/* ========== SIDEBAR ========== */
.sidebar {
    width: 240px;
    min-width: 240px;
    background: var(--bg-soft);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    padding: 20px 16px;
    gap: 20px;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
}
.sidebar-logo {
    font-family: 'Syne', sans-serif;
    font-size: 22px;
    font-weight: 800;
    color: var(--text);
    text-decoration: none;
    letter-spacing: -1px;
    flex-shrink: 0;
    padding: 0 8px;
}
.sidebar-logo span { color: var(--accent); }

.sidebar-nav {
    display: flex;
    flex-direction: column;
    gap: 2px;
    flex: 1;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border-radius: 8px;
    color: var(--text-dim);
    text-decoration: none;
    font-size: 14px;
    font-weight: 500;
    transition: all 0.15s;
    flex-shrink: 0;
}
.nav-item:hover {
    background: var(--bg-hover);
    color: var(--text);
}
.nav-item.active {
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--border);
}
.nav-item svg { flex-shrink: 0; opacity: 0.6; }
.nav-item.active svg,
.nav-item:hover svg { opacity: 1; }

.sidebar-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-top: 14px;
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}
.user-info { display: flex; align-items: center; gap: 8px; }
.user-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--accent);
    color: var(--bg);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 700;
    font-family: 'Syne', sans-serif;
    flex-shrink: 0;
}
.user-name {
    font-size: 13px;
    font-weight: 600;
    display: block;
    line-height: 1.1;
    max-width: 110px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.user-role {
    font-size: 10px;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 1px;
}
.btn-logout {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    color: var(--text-dim);
    text-decoration: none;
    transition: all 0.15s;
    flex-shrink: 0;
}
.btn-logout:hover { background: var(--danger-bg); color: var(--danger); }

/* ========== MAIN ========== */
.main-content {
    flex: 1;
    min-width: 0;
    padding: 28px 32px;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.page-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
}
.page-header h1 { font-size: 22px; font-weight: 700; line-height: 1.2; }
.page-header .page-count { font-size: 13px; color: var(--text-dim); display: block; margin-top: 2px; }
.header-date {
    font-size: 13px;
    color: var(--text-dim);
    padding: 5px 12px;
    background: var(--bg-soft);
    border: 1px solid var(--border);
    border-radius: 100px;
    white-space: nowrap;
}
.back-link {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    color: var(--text-dim);
    text-decoration: none;
    font-size: 13px;
    margin-bottom: 3px;
    transition: color 0.15s;
}
.back-link:hover { color: var(--accent); }

/* ========== STATS ========== */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 14px;
}
.stat-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 20px;
    display: flex;
    align-items: center;
    gap: 14px;
}
.stat-icon {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    background: var(--bg);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--accent);
    flex-shrink: 0;
}
.stat-icon-warning { background: var(--warning-bg); color: var(--warning); }
.stat-icon-danger { background: var(--danger-bg); color: var(--danger); }
.stat-value { display: block; font-family: 'Syne', sans-serif; font-size: 26px; font-weight: 700; }
.stat-label { font-size: 11px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.5px; }

/* ========== CARDS ========== */
.card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 14px;
    overflow: hidden;
}
.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    gap: 12px;
    flex-wrap: wrap;
}
.card-header.no-border { border-bottom: none; padding-bottom: 8px; }
.card-header h2 { font-size: 15px; font-weight: 600; }

/* ========== TABLES ========== */
.table-wrapper { overflow-x: auto; }

.data-table {
    width: 100%;
    border-collapse: collapse;
}
.data-table th {
    padding: 10px 16px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-dim);
    text-align: left;
    border-bottom: 1px solid var(--border);
    background: var(--bg);
    white-space: nowrap;
}
.data-table td {
    padding: 12px 16px;
    font-size: 14px;
    border-bottom: 1px solid var(--border-light);
    vertical-align: middle;
}
.data-table tr:last-child td { border-bottom: none; }
.data-table tr:hover td { background: var(--bg-hover); }
.empty-row { text-align: center; color: var(--text-dim); padding: 30px 16px !important; }

.badge-tipo {
    display: inline-block;
    padding: 3px 10px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 100px;
    font-size: 12px;
    color: var(--accent);
    text-transform: capitalize;
    white-space: nowrap;
}
.badge-count {
    display: inline-block;
    padding: 2px 9px;
    border-radius: 100px;
    font-size: 12px;
    font-weight: 600;
    white-space: nowrap;
}
.badge-count.active { background: var(--success-bg); color: var(--success); }
.badge-count.expired { background: var(--danger-bg); color: var(--danger); }
.badge-count.warning { background: var(--warning-bg); color: var(--warning); }

.note-indicator {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border-radius: 6px;
    background: var(--warning-bg);
    color: var(--warning);
    cursor: help;
    transition: all 0.15s;
}
.note-indicator:hover {
    background: rgba(245, 158, 11, 0.2);
    color: #fbbf24;
}

.actions-cell { white-space: nowrap; text-align: right; }
.actions-cell > * + * { margin-left: 4px; }

.giorni { font-size: 13px; font-weight: 600; white-space: nowrap; }
.giorni-warning { color: var(--warning); }
.giorni-danger { color: var(--danger); }

/* ========== BUTTONS ========== */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border-radius: 8px;
    font-family: inherit;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    border: 1px solid transparent;
    transition: all 0.15s;
    white-space: nowrap;
    line-height: 1.3;
}
.btn-primary { background: var(--accent); color: var(--bg); border-color: var(--accent); }
.btn-primary:hover { background: #e0e0e0; }
.btn-outline { background: transparent; color: var(--text); border-color: var(--border); }
.btn-outline:hover { background: var(--bg-hover); border-color: var(--accent); }
.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-danger:hover { background: #dc2626; }
.btn-danger-icon {
    background: transparent;
    color: var(--danger);
    border: 1px solid transparent;
    width: 28px;
    height: 28px;
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    line-height: 1;
    flex-shrink: 0;
    border-radius: 6px;
}
.btn-danger-icon:hover { background: var(--danger-bg); }
.btn-sm { padding: 5px 12px; font-size: 12px; border-radius: 6px; }
.btn-full { width: 100%; justify-content: center; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ========== FORMS ========== */
.auth-form { display: flex; flex-direction: column; gap: 16px; }

.edit-form { padding: 20px; display: flex; flex-direction: column; gap: 14px; }

.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 14px;
}

.form-group { display: flex; flex-direction: column; gap: 5px; }
.form-group label {
    font-size: 11px;
    font-weight: 600;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.form-group input,
.form-group select,
.form-group textarea {
    padding: 9px 12px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text);
    font-family: inherit;
    font-size: 14px;
    outline: none;
    transition: border-color 0.15s;
    width: 100%;
}
.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus { border-color: var(--accent); }
.form-group textarea { resize: vertical; min-height: 80px; }
.form-group select {
    cursor: pointer;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23666' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 10px center;
    padding-right: 32px;
}
.form-group select option { background: var(--bg-card); color: var(--text); }

.form-btn { justify-content: flex-end; }
.form-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.form-inline { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; }
.form-inline .form-group { flex: 1; min-width: 140px; }

/* ========== SEARCH ========== */
.search-form {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 14px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    max-width: 380px;
    width: 100%;
}
.search-form svg { color: var(--text-dim); flex-shrink: 0; }
.search-form input {
    background: none;
    border: none;
    outline: none;
    color: var(--text);
    font-family: inherit;
    font-size: 14px;
    width: 100%;
    min-width: 0;
}
.search-form input::placeholder { color: var(--text-dim); }
.search-clear { color: var(--text-dim); text-decoration: none; font-size: 18px; line-height: 1; flex-shrink: 0; }
.search-clear:hover { color: var(--text); }

/* ========== ALERTS ========== */
.alert {
    padding: 12px 16px;
    border-radius: 8px;
    font-size: 13px;
    line-height: 1.5;
}
.alert-error { background: var(--danger-bg); border: 1px solid rgba(239,68,68,0.3); color: #f87171; }
.alert-success { background: var(--success-bg); border: 1px solid rgba(74,222,128,0.3); color: #55d88a; }

/* ========== MODAL ========== */
.modal {
    position: fixed;
    inset: 0;
    z-index: 300;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}
.modal.hidden { display: none; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,0.8); backdrop-filter: blur(4px); }
.modal-content {
    position: relative;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 24px;
    width: 100%;
    max-width: 460px;
    z-index: 1;
    max-height: 90vh;
    overflow-y: auto;
}
.modal-lg { max-width: 680px; }
.modal-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.modal-header h3 { font-size: 17px; }
.modal-close {
    background: none;
    border: none;
    color: var(--text-dim);
    font-size: 22px;
    cursor: pointer;
    padding: 2px 6px;
    border-radius: 4px;
    transition: all 0.15s;
    line-height: 1;
}
.modal-close:hover { background: var(--bg); color: var(--text); }

/* ========== UTILITY ========== */
.scadenza-form { padding: 16px 20px; border-bottom: 1px solid var(--border); background: var(--bg); }
.scadenza-form.hidden { display: none; }
.history-content { max-height: 400px; overflow-y: auto; }
.code-block {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 12px 16px;
    overflow-x: auto;
}
.code-block code {
    font-family: 'SF Mono', 'Consolas', 'Monaco', monospace;
    font-size: 13px;
    color: var(--accent);
}

/* ========== EMAIL CHIPS ========== */
.cards-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
}

.email-list {
    padding: 16px 20px;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.email-chip {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 7px 10px 7px 12px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 13px;
    transition: border-color 0.15s;
}
.email-chip:hover { border-color: var(--accent); }

.email-chip-icon {
    color: var(--accent);
    display: flex;
    align-items: center;
    flex-shrink: 0;
}

.email-chip-text {
    color: var(--text);
}

.email-chip-remove {
    background: none;
    border: none;
    color: var(--text-dim);
    cursor: pointer;
    font-size: 16px;
    line-height: 1;
    padding: 0 2px;
    transition: color 0.15s;
    font-family: inherit;
}
.email-chip-remove:hover { color: var(--danger); }

/* ========== RESPONSIVE ========== */
@media (max-width: 900px) {
    .sidebar { width: 200px; min-width: 200px; padding: 16px 12px; gap: 14px; }
    .main-content { padding: 20px; }
    .data-table th, .data-table td { padding: 10px 12px; font-size: 13px; }
}

@media (max-width: 700px) {
    .admin-body { flex-direction: column; }
    .admin-layout { flex-direction: column; }
    .sidebar {
        width: 100%;
        min-width: 100%;
        height: auto;
        max-height: none;
        flex-direction: row;
        align-items: center;
        padding: 10px 14px;
        gap: 10px;
        flex-wrap: wrap;
        position: sticky;
        top: 0;
        z-index: 100;
        border-right: none;
        border-bottom: 1px solid var(--border);
        overflow-y: visible;
    }
    .sidebar-logo { font-size: 18px; padding: 0; }
    .sidebar-nav {
        flex-direction: row;
        flex: 1;
        overflow-x: auto;
        gap: 1px;
    }
    .nav-item { padding: 8px 10px; font-size: 13px; }
    .nav-item span { display: none; }
    .sidebar-footer { padding-top: 0; border-top: none; }
    .sidebar-footer .user-name,
    .sidebar-footer .user-role { display: none; }
    .btn-logout { width: 28px; height: 28px; }

    .main-content { padding: 16px; gap: 14px; }

    .stats-grid { grid-template-columns: 1fr; }
    .form-grid { grid-template-columns: 1fr; }
    .form-inline { flex-direction: column; }
    .form-inline .form-group { min-width: 100%; }

    .page-header { flex-direction: column; gap: 6px; }
    .page-header h1 { font-size: 20px; }

    .modal-content { padding: 20px; max-width: calc(100vw - 32px); margin: 16px; }
    .auth-card { padding: 24px 18px; }
    .cards-row { grid-template-columns: 1fr; }
}
