:root {
    --bg: #f7f5f2;
    --card: #ffffff;
    --ink: #1e1b16;
    --ink-soft: #6b6459;
    --line: #e4dfd6;
    --accent: #7a5c2e;
    --accent-ink: #fff;
    --green: #2e7d32;
    --red: #b3261e;
    --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #17150f;
        --card: #221f18;
        --ink: #efe9df;
        --ink-soft: #a89f8d;
        --line: #332f25;
        --accent: #c79a5b;
        --accent-ink: #1a1710;
    }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--ink);
    font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    -webkit-text-size-adjust: 100%;
}

body {
    max-width: 640px;
    margin: 0 auto;
    padding-bottom: calc(72px + var(--safe-bottom));
}

button {
    font: inherit;
    cursor: pointer;
    border: none;
    border-radius: 10px;
    padding: 10px 14px;
    background: var(--accent);
    color: var(--accent-ink);
    -webkit-tap-highlight-color: transparent;
}
button.ghost {
    background: transparent;
    color: var(--ink-soft);
    border: 1px solid var(--line);
}

input, select, textarea {
    font: inherit;
    color: var(--ink);
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 10px;
    width: 100%;
}

/* ---------- gate ---------- */
.gate {
    position: fixed;
    inset: 0;
    background: var(--bg);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    padding: 24px;
}
.gate-box {
    width: 100%;
    max-width: 320px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    text-align: center;
}
.gate-box h1 { margin: 0; font-size: 26px; }
.gate-box p { margin: 0; color: var(--ink-soft); }
.gate-error { color: var(--red) !important; }

/* ---------- header ---------- */
.app-header {
    position: sticky;
    top: 0;
    z-index: 20;
    background: var(--bg);
    padding: calc(10px + var(--safe-top)) 12px 6px;
    border-bottom: 1px solid var(--line);
}
.search-row {
    display: flex;
    gap: 8px;
}
.search-row input { flex: 1; }
#filters-toggle { background: var(--card); color: var(--ink); border: 1px solid var(--line); white-space: nowrap; }
#filters-toggle.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

.tabs {
    display: flex;
    gap: 6px;
    margin-top: 8px;
}
.tabs button {
    flex: 1;
    background: transparent;
    color: var(--ink-soft);
    padding: 8px 4px;
    border-radius: 8px;
    font-size: 14px;
}
.tabs button.active {
    background: var(--card);
    color: var(--ink);
    box-shadow: var(--shadow);
    font-weight: 600;
}

/* ---------- filters ---------- */
.filters {
    background: var(--card);
    border-bottom: 1px solid var(--line);
    padding: 12px;
}
.filters-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}
.filters-grid label {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 12px;
    color: var(--ink-soft);
}
.filters-actions {
    display: flex;
    gap: 8px;
    margin-top: 12px;
}
.filters-actions button { flex: 1; }

/* ---------- views ---------- */
main { padding: 12px; }
.result-count, .hint {
    color: var(--ink-soft);
    font-size: 13px;
    margin: 4px 2px 10px;
}

.cards { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }

.card {
    background: var(--card);
    border-radius: 12px;
    box-shadow: var(--shadow);
    overflow: hidden;
}
.card-head {
    padding: 12px 14px;
    display: block;
}
.card-title { font-weight: 600; margin: 0 0 2px; }
.card-authors { color: var(--ink-soft); font-size: 14px; }
.card-meta {
    margin-top: 6px;
    font-size: 12px;
    color: var(--ink-soft);
    display: flex;
    flex-wrap: wrap;
    gap: 4px 10px;
}
.card-flags-mini {
    display: flex;
    gap: 5px;
    margin-top: 8px;
    flex-wrap: wrap;
}
.pill {
    font-size: 11px;
    padding: 2px 7px;
    border-radius: 999px;
    background: var(--bg);
    border: 1px solid var(--line);
    color: var(--ink-soft);
}
.pill.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.pill.research { background: #1d4e6b; color: #fff; border-color: #1d4e6b; }
.pill.done { background: var(--green); color: #fff; border-color: var(--green); }

.loading { color: var(--ink-soft); font-size: 13px; padding: 8px 0; }

/* ---------- ricerca Claude ---------- */
.research {
    margin-top: 14px;
    border-top: 1px solid var(--line);
    padding-top: 10px;
}
.research > summary {
    cursor: pointer;
    font-weight: 600;
    font-size: 14px;
    padding: 6px 0;
    list-style: none;
}
.research > summary::-webkit-details-marker { display: none; }
.research > summary::before { content: "▸ "; color: var(--ink-soft); }
.research[open] > summary::before { content: "▾ "; }

.r-field { display: flex; flex-direction: column; gap: 4px; margin-top: 10px; }
.r-field > span { font-size: 12px; color: var(--ink-soft); }
.research textarea {
    width: 100%;
    resize: vertical;
    font: 13px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    color: var(--ink);
    background: var(--bg);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 10px;
}
.r-row { display: flex; gap: 10px; }
.r-eur { flex: 1; }
.r-check {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 12px;
    font-size: 14px;
}
.r-check input { width: 20px; height: 20px; flex: none; }
.r-actions {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
    margin-top: 12px;
}
.r-status { font-size: 12px; color: var(--ink-soft); }

.card-body {
    border-top: 1px solid var(--line);
    padding: 12px 14px;
    display: none;
}
.card.open .card-body { display: block; }

.detail-grid {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 4px 12px;
    font-size: 13px;
    margin-bottom: 14px;
}
.detail-grid dt { color: var(--ink-soft); }
.detail-grid dd { margin: 0; }

.toggles { display: flex; flex-direction: column; gap: 2px; }
.toggle-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 0;
    border-top: 1px solid var(--line);
}
.toggle-row:first-child { border-top: none; }
.toggle-row span { font-size: 15px; }

.switch { position: relative; width: 50px; height: 30px; flex: none; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; }
.switch .slider {
    position: absolute;
    inset: 0;
    background: var(--line);
    border-radius: 999px;
    transition: background .18s;
}
.switch .slider::before {
    content: "";
    position: absolute;
    width: 26px; height: 26px;
    left: 2px; top: 2px;
    background: #fff;
    border-radius: 50%;
    box-shadow: 0 1px 3px rgba(0,0,0,.3);
    transition: transform .18s;
}
.switch input:checked + .slider { background: var(--green); }
.switch input:checked + .slider::before { transform: translateX(20px); }
.switch.danger input:checked + .slider { background: var(--red); }
.switch input:disabled + .slider { opacity: .5; }

.link-out { display: inline-block; color: var(--accent); font-size: 13px; }

.card-actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 10px 14px;
    margin-bottom: 12px;
}
.prompt-btn {
    background: var(--card);
    color: var(--accent);
    border: 1px solid var(--accent);
    font-size: 13px;
    padding: 8px 12px;
}

.prompt-box { max-height: 88vh; }
.prompt-hint { margin: 0 0 4px; font-size: 13px; color: var(--ink-soft); }
#prompt-text {
    width: 100%;
    resize: none;
    font: 13px/1.45 ui-monospace, "SF Mono", Menlo, monospace;
    color: var(--ink);
    background: var(--bg);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 10px;
    min-height: 200px;
    max-height: 52vh;
}
#prompt-sheet .sheet-box h2 { display: flex; gap: 6px; flex-wrap: wrap; }
#prompt-copy { background: var(--accent); color: var(--accent-ink); border: none; }

.load-more { width: 100%; margin-top: 14px; background: var(--card); color: var(--ink); border: 1px solid var(--line); }

/* ---------- rows (contenitori / preset) ---------- */
.rows { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.rows li button {
    width: 100%;
    background: var(--card);
    color: var(--ink);
    box-shadow: var(--shadow);
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px;
    text-align: left;
}
.rows .count {
    background: var(--bg);
    border-radius: 999px;
    padding: 2px 10px;
    font-size: 13px;
    color: var(--ink-soft);
    min-width: 34px;
    text-align: center;
}

/* ---------- footer + sheet ---------- */
.app-footer {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 20;
    background: var(--bg);
    border-top: 1px solid var(--line);
    padding: 10px 12px calc(10px + var(--safe-bottom));
    max-width: 640px;
    margin: 0 auto;
}
.app-footer button { width: 100%; }

.sheet {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.4);
    display: flex;
    align-items: flex-end;
    justify-content: center;
    z-index: 40;
}
.sheet-box {
    background: var(--card);
    width: 100%;
    max-width: 640px;
    border-radius: 16px 16px 0 0;
    padding: 16px 16px calc(16px + var(--safe-bottom));
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.sheet-box h2 { margin: 0 0 6px; font-size: 17px; }
.sheet-box button { width: 100%; background: var(--card); color: var(--ink); border: 1px solid var(--line); }
.sheet-box button.ghost { color: var(--ink-soft); }

/* ---------- toast ---------- */
.toast {
    position: fixed;
    left: 50%;
    bottom: calc(84px + var(--safe-bottom));
    transform: translateX(-50%);
    background: var(--ink);
    color: var(--bg);
    padding: 10px 16px;
    border-radius: 999px;
    font-size: 13px;
    z-index: 50;
    max-width: 90vw;
    text-align: center;
}
.toast.err { background: var(--red); color: #fff; }

.empty { text-align: center; color: var(--ink-soft); padding: 40px 0; }
