/* Mobile first: stili base per schermi stretti, media query min-width per ampliare. */
:root {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --text: #17202b;
  --muted: #5f6b7a;
  --line: #dbe1e8;
  --brand: #14365d;
  --brand-text: #ffffff;
  --accent: #1f6fd0;
  --danger-bg: #fdecec;
  --danger: #a62020;

  /* novita' */
  --fresh-bg: #fff2c2;
  --fresh-edge: #f0a400;
  --fresh-chip: #8a5a00;
  /* accumulate e non lette */
  --pending-bg: #e3edff;
  --pending-edge: #4b7bec;
  --pending-chip: #1d3f9c;

  --radius: 10px;
  --gap: 12px;
  --shadow: 0 1px 2px rgba(10, 25, 45, .08);
}

/* Temi: senza scelta dell'utente si segue il tema del dispositivo; data-theme (impostato da theme.js) lo forza. */
:root { color-scheme: light; }

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0f151c;
    --surface: #18212b;
    --text: #e7edf4;
    --muted: #93a1b1;
    --line: #2a3644;
    --brand: #0b2340;
    --accent: #6aa5ff;
    --danger-bg: #3a1b1b;
    --danger: #ff9d9d;
    --fresh-bg: #3d3210;
    --fresh-edge: #f0b429;
    --fresh-chip: #ffd770;
    --pending-bg: #172a4d;
    --pending-edge: #6f95f2;
    --pending-chip: #a9c2ff;
    --shadow: none;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f151c;
  --surface: #18212b;
  --text: #e7edf4;
  --muted: #93a1b1;
  --line: #2a3644;
  --brand: #0b2340;
  --accent: #6aa5ff;
  --danger-bg: #3a1b1b;
  --danger: #ff9d9d;
  --fresh-bg: #3d3210;
  --fresh-edge: #f0b429;
  --fresh-chip: #ffd770;
  --pending-bg: #172a4d;
  --pending-edge: #6f95f2;
  --pending-chip: #a9c2ff;
  --shadow: none;
}

/* ---------- pulsante cambio tema ---------- */
.theme-toggle {
  width: 40px; height: 40px; padding: 0;
  display: inline-flex; align-items: center; justify-content: center;
  background: none; border: 0; border-radius: 50%; color: inherit; cursor: pointer;
}
.theme-toggle:hover { background: rgba(127, 127, 127, .2); }
.theme-toggle .icon-sun { display: none; }
.theme-toggle .icon-moon { display: block; }
.theme-is-dark .theme-toggle .icon-sun { display: block; }
.theme-is-dark .theme-toggle .icon-moon { display: none; }
.theme-toggle-floating { position: fixed; top: calc(8px + env(safe-area-inset-top)); right: 10px; color: var(--text); z-index: 20; }

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

h1, h2, p { margin: 0; }
.inline { display: inline; margin: 0; }

/* ---------- barra superiore ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--gap);
  padding: 10px 14px;
  padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--brand);
  color: var(--brand-text);
}
.topbar h1 { font-size: 1.05rem; font-weight: 650; letter-spacing: .01em; }
.user { display: flex; align-items: center; gap: 10px; font-size: .85rem; min-width: 0; }
.user-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 38vw; opacity: .9; }
button.link {
  background: none; border: 0; padding: 4px 0; color: inherit; font: inherit;
  text-decoration: underline; cursor: pointer;
}

/* ---------- ricerca ---------- */
.searchbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  box-shadow: var(--shadow);
  padding: 8px 12px 0;
}
.row-main { display: flex; gap: 8px; }
.row-main input[type="search"] { flex: 1; min-width: 0; }

input[type="search"], input[type="text"], input[type="password"],
input[type="datetime-local"], select {
  width: 100%;
  min-height: 44px; /* bersaglio touch */
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}
input:focus-visible, select:focus-visible, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.btn-ghost {
  min-height: 44px; padding: 0 14px;
  border: 1px solid var(--line); border-radius: 8px;
  background: transparent; color: var(--text); font: inherit; cursor: pointer;
}
.btn-ghost[aria-expanded="true"] { background: var(--bg); border-color: var(--accent); }

.extra { display: none; grid-template-columns: 1fr 1fr; gap: 8px; padding-top: 8px; }
.searchbar.open .extra { display: grid; }
.field-s { display: flex; flex-direction: column; gap: 2px; font-size: .78rem; color: var(--muted); min-width: 0; }
.check { display: flex; align-items: center; gap: 8px; font-size: .9rem; grid-column: 1 / -1; min-height: 36px; }
.check input { width: 20px; height: 20px; }
.extra .btn-ghost { grid-column: 1 / -1; }

.status {
  display: flex; align-items: center; gap: 10px;
  padding: 6px 0 7px;
  font-size: .8rem; color: var(--muted);
}
#statusText { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#statusText.err { color: var(--danger); }
.legend { display: none; white-space: nowrap; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin: 0 4px 0 10px; vertical-align: baseline; }
.dot-fresh { background: var(--fresh-edge); }
.dot-pending { background: var(--pending-edge); }
.icon-btn {
  width: 36px; height: 36px; border: 1px solid var(--line); border-radius: 50%;
  background: transparent; color: var(--text); font-size: 1.1rem; cursor: pointer;
}
.icon-btn.spin { animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- elenco ---------- */
.app { padding: 10px 10px 32px; padding-bottom: calc(32px + env(safe-area-inset-bottom)); }
.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-left-width: 5px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  transition: background-color .4s ease, border-color .4s ease;
}
.card { position: relative; }
.share {
  position: absolute; top: 4px; right: 4px;
  width: 40px; height: 40px; display: flex; align-items: center; justify-content: center;
  background: none; border: 0; border-radius: 50%; color: var(--muted); cursor: pointer;
}
.share:hover { background: var(--bg); color: var(--text); }
.card-head .time { margin-right: 36px; } /* spazio per il pulsante Condividi */
.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px);
  background: #222c38; color: #fff; padding: 10px 16px; border-radius: 8px; font-size: .9rem;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 30;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.card[data-mark="fresh"] { background: var(--fresh-bg); border-color: var(--fresh-edge); }
.card[data-mark="pending"] { background: var(--pending-bg); border-color: var(--pending-edge); }

.card-head {
  display: block; width: 100%; text-align: left;
  padding: 10px 12px; background: none; border: 0; color: inherit; font: inherit; cursor: pointer;
}
.meta { display: flex; align-items: center; gap: 6px; font-size: .75rem; color: var(--muted); margin-bottom: 4px; flex-wrap: wrap; }
.badge {
  background: var(--brand); color: var(--brand-text);
  border-radius: 4px; padding: 1px 6px; font-weight: 650; letter-spacing: .02em;
}
.cat { text-transform: uppercase; letter-spacing: .03em; }
.time { margin-left: auto; font-variant-numeric: tabular-nums; }
.flag { font-weight: 700; font-size: .68rem; letter-spacing: .05em; border-radius: 4px; padding: 1px 6px; }
.card[data-mark="fresh"] .flag { background: var(--fresh-edge); color: #231800; }
.card[data-mark="pending"] .flag { background: var(--pending-edge); color: #fff; }
.flag:empty { display: none; }

.title { font-size: 1rem; font-weight: 650; line-height: 1.3; overflow-wrap: anywhere; }
.card.read .title { font-weight: 500; color: var(--muted); }
.snippet {
  margin-top: 3px; font-size: .88rem; color: var(--muted);
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.card.open .snippet { display: none; }

.card-body { padding: 0 12px 12px; border-top: 1px dashed var(--line); }
.corpo {
  margin: 10px 0 0; white-space: pre-wrap; overflow-wrap: anywhere;
  font: inherit; font-size: .95rem; line-height: 1.55;
}
.loading-body { margin-top: 10px; color: var(--muted); font-size: .9rem; }

.btn-more {
  display: block; width: 100%; margin-top: 12px; min-height: 46px;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--surface); color: var(--text); font: inherit; cursor: pointer;
}
.empty { text-align: center; color: var(--muted); padding: 28px 8px; }
.empty.error { color: var(--danger); }

/* ---------- login ---------- */
.page-login { display: flex; min-height: 100vh; align-items: center; justify-content: center; padding: 16px; }
.login { width: 100%; max-width: 400px; }
.login-card {
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
  padding: 24px 20px; box-shadow: var(--shadow);
}
.login-card h1 { font-size: 1.35rem; margin-bottom: 4px; }
.muted { color: var(--muted); font-size: .92rem; margin-bottom: 16px; }
.alert { background: var(--danger-bg); color: var(--danger); border-radius: 8px; padding: 10px 12px; margin-bottom: 14px; font-size: .92rem; }
.btn {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  width: 100%; min-height: 46px; border-radius: 8px; font: inherit; font-weight: 600;
  text-decoration: none; cursor: pointer; border: 1px solid var(--line);
}
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); margin-top: 6px; }
.segmented { display: grid; grid-template-columns: 1fr 1fr; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; margin-bottom: 12px; }
.segmented label { position: relative; }
.segmented input { position: absolute; opacity: 0; inset: 0; }
.segmented span { display: block; text-align: center; padding: 10px 6px; font-size: .92rem; cursor: pointer; }
.segmented input:checked + span { background: var(--accent); color: #fff; font-weight: 600; }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: -2px; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; font-size: .85rem; color: var(--muted); }

/* ---------- schermi piu' ampi ---------- */
@media (min-width: 720px) {
  .topbar { padding-left: max(20px, calc((100vw - 960px) / 2)); padding-right: max(20px, calc((100vw - 960px) / 2)); }
  .user-name { max-width: 300px; }
  .searchbar { padding-left: max(16px, calc((100vw - 960px) / 2)); padding-right: max(16px, calc((100vw - 960px) / 2)); }
  .extra { display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; align-items: end; }
  #toggleFilters { display: none; }
  .check { grid-column: span 2; }
  .extra .btn-ghost { grid-column: span 2; justify-self: end; min-width: 160px; }
  .legend { display: inline; }
  .app { max-width: 960px; margin: 0 auto; padding: 14px 16px 40px; }
  .list { gap: 10px; }
  .snippet { -webkit-line-clamp: 3; }
}

@media (prefers-reduced-motion: reduce) {
  .card { transition: none; }
  .icon-btn.spin { animation: none; }
}
