/* Farmachat · design D2 (derivato Farmafiches) · accent testata #71DE7B
 * Regole: input >=16px (no zoom iOS) · tap target >=44px · safe-area ·
 * niente emoji colorate (solo testo/SVG currentColor) · temi light+dark. */

:root {
  --brand: #71DE7B;
  --brand-ink: #0B3D12;        /* testo scuro leggibile SUL verde */
  --brand-dark: #2FA53C;
  --bg: #F4F6F4;
  --surface-1: #FFFFFF;
  --surface-2: #EDF1ED;
  --text-1: #152016;
  --text-2: #4A574C;
  --text-3: #7A867B;
  --border: #DDE4DD;
  --bubble-mine: #D9F5DC;
  --bubble-other: #FFFFFF;
  --danger: #B3261E;
  --ok: #1B7C2C;
  --shadow: 0 1px 3px rgba(10, 30, 12, .08);
  --radius: 14px;
  --sp-1: 4px; --sp-2: 8px; --sp-3: 12px; --sp-4: 16px; --sp-5: 24px; --sp-6: 32px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101410;
    --surface-1: #1A211B;
    --surface-2: #232B24;
    --text-1: #E8EDE8;
    --text-2: #AEB9AF;
    --text-3: #7E8A7F;
    --border: #2E372F;
    --bubble-mine: #23472A;
    --bubble-other: #1F2620;
    --danger: #F2B8B5;
    --ok: #86D993;
    --shadow: 0 1px 3px rgba(0, 0, 0, .4);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text-1);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  min-height: 100dvh;
}
button, input, textarea, select { font: inherit; color: inherit; }
a { color: var(--brand-dark); text-decoration: none; }
img, video { max-width: 100%; }

/* ---------- Testata ---------- */
.topbar {
  background: var(--brand);
  color: var(--brand-ink);
  padding: calc(env(safe-area-inset-top) + 10px) var(--sp-4) 10px;
  display: flex; align-items: center; gap: var(--sp-3);
  position: sticky; top: 0; z-index: 20;
}
.topbar .logo { height: 26px; display: block; }
.topbar .spacer { flex: 1; }
.topbar .who { font-size: 14px; font-weight: 600; max-width: 40%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.iconbtn {
  min-width: 44px; min-height: 44px;
  display: inline-flex; align-items: center; justify-content: center;
  background: none; border: 0; border-radius: 12px; cursor: pointer;
  color: inherit; padding: 0;
}
.iconbtn:active { background: rgba(0,0,0,.12); }
.iconbtn svg { width: 22px; height: 22px; }

/* ---------- Layout ---------- */
.page { max-width: 640px; margin: 0 auto; padding: var(--sp-4) var(--sp-4) calc(env(safe-area-inset-bottom) + 32px); }
.page h1 { font-size: 22px; margin: var(--sp-2) 0 var(--sp-4); }
.page h2 { font-size: 15px; color: var(--text-2); text-transform: uppercase; letter-spacing: .04em; margin: var(--sp-5) 0 var(--sp-3); }
.muted { color: var(--text-3); font-size: 14px; }

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: var(--sp-4);
  margin-bottom: var(--sp-3);
}
.card h3 { margin: 0 0 var(--sp-1); font-size: 17px; }

.row { display: flex; align-items: center; gap: var(--sp-3); }
.row .grow { flex: 1; min-width: 0; }

/* item cliccabile in lista (>=44px) */
.item {
  display: flex; align-items: center; gap: var(--sp-3);
  width: 100%; text-align: left;
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: var(--sp-3) var(--sp-4); margin-bottom: var(--sp-2);
  min-height: 60px; cursor: pointer;
}
.item:active { background: var(--surface-2); }
.item .grow { flex: 1; min-width: 0; }
.item .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .sub { color: var(--text-3); font-size: 13px; }

.avatar {
  width: 42px; height: 42px; border-radius: 50%; flex: none;
  background: var(--surface-2); color: var(--text-2);
  display: inline-flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 15px;
}

.pill {
  display: inline-block; padding: 3px 10px; border-radius: 999px;
  font-size: 12px; font-weight: 700; white-space: nowrap;
}
.pill.aperto { background: var(--bubble-mine); color: var(--ok); }
.pill.chiuso { background: var(--surface-2); color: var(--text-3); }

/* ---------- Bottoni ---------- */
.btn {
  display: flex; width: 100%; align-items: center; justify-content: center; gap: var(--sp-2);
  min-height: 48px; padding: 12px var(--sp-4);
  border: 0; border-radius: 12px; cursor: pointer;
  background: var(--brand); color: var(--brand-ink);
  font-size: 16px; font-weight: 700;
}
.btn:active { filter: brightness(.93); }
.btn[disabled] { opacity: .55; pointer-events: none; }
.btn.secondary { background: var(--surface-2); color: var(--text-1); }
.btn.danger { background: transparent; color: var(--danger); border: 1px solid currentColor; }
/* sul verde della testata il danger non si legge: sempre inchiostro scuro (Fabio 14/07) */
.topbar .btn.danger { color: var(--brand-ink); }
.btn.small { min-height: 44px; font-size: 15px; }
.btn + .btn { margin-top: var(--sp-2); }
.btn .spin { display: none; }
.btn.busy .spin { display: inline-block; }
.btn.busy .label { opacity: .6; }

.spin {
  width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid currentColor; border-top-color: transparent;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Form ---------- */
.field { margin-bottom: var(--sp-3); }
.field label { display: block; font-size: 14px; font-weight: 600; color: var(--text-2); margin-bottom: var(--sp-1); }
.field input, .field textarea {
  width: 100%; font-size: 16px; /* >=16px: no auto-zoom iOS */
  padding: 12px var(--sp-3); border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface-1); color: var(--text-1);
  min-height: 48px;
}
.field input:focus, .field textarea:focus, .composer textarea:focus {
  outline: 2px solid var(--brand); outline-offset: 1px; border-color: transparent;
}
.formerror { color: var(--danger); font-size: 14px; min-height: 20px; margin: var(--sp-2) 0; }

/* ---------- Landing ---------- */
.hero {
  background: var(--brand); color: var(--brand-ink);
  text-align: center; padding: var(--sp-6) var(--sp-4);
  border-radius: 0 0 24px 24px;
}
.hero .logo { height: 40px; }
.hero p { margin: var(--sp-3) 0 0; font-size: 17px; font-weight: 600; }
.steps { counter-reset: step; }
.step { display: flex; gap: var(--sp-3); margin-bottom: var(--sp-3); align-items: flex-start; }
.step .n {
  counter-increment: step; flex: none;
  width: 30px; height: 30px; border-radius: 50%;
  background: var(--brand); color: var(--brand-ink);
  display: flex; align-items: center; justify-content: center; font-weight: 800;
}
.step .n::before { content: counter(step); }
.price { display: flex; align-items: baseline; gap: var(--sp-2); }
.price .amount { font-size: 26px; font-weight: 800; }
.notice {
  border: 1px solid var(--border); border-left: 4px solid var(--danger);
  border-radius: 10px; padding: var(--sp-3) var(--sp-4);
  font-size: 14px; color: var(--text-2); margin: var(--sp-4) 0;
}

/* ---------- Chat ---------- */
.chatwrap { display: flex; flex-direction: column; height: 100dvh; }
.chatlog {
  flex: 1; overflow-y: auto;
  padding: var(--sp-4) var(--sp-4) var(--sp-2);
  display: flex; flex-direction: column; gap: var(--sp-2);
  max-width: 640px; margin: 0 auto; width: 100%;
}
.bubble {
  max-width: 82%; padding: 10px 14px; border-radius: 16px;
  box-shadow: var(--shadow); font-size: 16px;
  overflow-wrap: anywhere; white-space: pre-wrap;
}
.bubble.mine { align-self: flex-end; background: var(--bubble-mine); border-bottom-right-radius: 4px; }
.bubble.other { align-self: flex-start; background: var(--bubble-other); border-bottom-left-radius: 4px; }
.bubble .time { display: block; font-size: 11px; color: var(--text-3); margin-top: 3px; text-align: right; }
.bubble .media { display: block; border-radius: 10px; margin: 4px 0; max-height: 320px; object-fit: contain; background: #000; }
.chatinfo { align-self: center; font-size: 13px; color: var(--text-3); background: var(--surface-2); border-radius: 999px; padding: 4px 14px; margin: var(--sp-2) 0; }

.composer {
  border-top: 1px solid var(--border); background: var(--surface-1);
  padding: var(--sp-2) var(--sp-3) calc(env(safe-area-inset-bottom) + 8px);
}
.composer .inner { max-width: 640px; margin: 0 auto; display: flex; gap: var(--sp-2); align-items: flex-end; }
.composer textarea {
  flex: 1; font-size: 16px; resize: none;
  border: 1px solid var(--border); border-radius: 20px;
  background: var(--bg); padding: 11px 14px;
  max-height: 120px; min-height: 46px;
}
.composer .iconbtn { background: var(--surface-2); border-radius: 50%; flex: none; }
.composer .iconbtn.send { background: var(--brand); color: var(--brand-ink); }
.previews { max-width: 640px; margin: 0 auto var(--sp-2); display: flex; gap: var(--sp-2); flex-wrap: wrap; }
.previews .thumb {
  position: relative; width: 76px; height: 76px;
  border-radius: 12px; overflow: visible; flex: none;
}
.previews .thumb img, .previews .thumb video {
  width: 100%; height: 100%; object-fit: cover;
  border-radius: 12px; border: 1px solid var(--border); background: var(--surface-2);
  display: block;
}
.previews .thumb .tag {
  position: absolute; left: 4px; bottom: 4px;
  background: rgba(0,0,0,.65); color: #fff;
  font-size: 10px; font-weight: 700; padding: 1px 6px; border-radius: 999px;
}
/* rimozione: area tocco 44px, pallino visibile più piccolo in alto a destra */
.previews .thumb button {
  position: absolute; top: -14px; right: -14px;
  width: 44px; height: 44px; border: 0; background: none; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.previews .thumb button::before {
  content: "×";
  width: 24px; height: 24px; border-radius: 50%;
  background: var(--text-1); color: var(--bg);
  font-size: 16px; line-height: 24px; text-align: center; display: block;
}

/* ---------- Documento in chat (ricetta PDF) ---------- */
.docchip {
  display: flex; align-items: center; gap: 10px;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 12px; padding: 10px 14px; margin: 4px 0;
  color: var(--text-1); font-weight: 600; min-height: 44px;
}
.docchip svg { width: 22px; height: 22px; flex: none; color: var(--brand-dark); }
.docchip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- Foglio ricetta (bottom sheet) ---------- */
.sheetwrap {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(0,0,0,.45);
  display: flex; align-items: flex-end; justify-content: center;
}
.sheetwrap[hidden] { display: none; } /* hidden deve vincere sul display:flex */
.sheet {
  background: var(--bg); width: 100%; max-width: 640px;
  border-radius: 20px 20px 0 0; max-height: 88dvh;
  display: flex; flex-direction: column;
}
.sheethead {
  display: flex; align-items: center; justify-content: space-between;
  padding: var(--sp-3) var(--sp-4) var(--sp-2);
}
.sheethead h3 { margin: 0; font-size: 18px; }
.sheethead .iconbtn { font-size: 24px; color: var(--text-2); }
.sheetbody { overflow-y: auto; padding: 0 var(--sp-4); flex: 1; }
.sheetfoot { padding: var(--sp-3) var(--sp-4) calc(env(safe-area-inset-bottom) + 12px); }

.rxresult {
  display: flex; align-items: center; gap: var(--sp-3); width: 100%; text-align: left;
  background: var(--surface-1); border: 1px solid var(--border); border-radius: 12px;
  padding: 10px var(--sp-3); margin-bottom: var(--sp-2); min-height: 56px; cursor: pointer;
}
.rxresult:active { background: var(--surface-2); }
.rxresult .grow { flex: 1; min-width: 0; display: block; }
.rxresult .title { display: block; font-weight: 600; }
.rxresult .sub, .rxitem .sub { display: block; color: var(--text-3); font-size: 13px; }
.rxresult .addmark {
  flex: none; width: 32px; height: 32px; border-radius: 50%;
  background: var(--brand); color: var(--brand-ink);
  display: flex; align-items: center; justify-content: center;
  font-size: 20px; font-weight: 700;
}
.rxlabel { font-size: 13px; font-weight: 700; color: var(--text-2); margin: var(--sp-3) 0 var(--sp-2); }
.rxitem {
  background: var(--surface-1); border: 1px solid var(--border); border-radius: 12px;
  padding: var(--sp-3); margin-bottom: var(--sp-2);
}
.rxitem .title { font-weight: 600; }
.rxitem .iconbtn { color: var(--text-2); font-size: 22px; }
.rxitem textarea {
  width: 100%; font-size: 16px; margin-top: var(--sp-2);
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--bg); color: var(--text-1); padding: 10px var(--sp-3); resize: none;
}

/* ---------- Toast ---------- */
#toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(env(safe-area-inset-bottom) + 20px);
  background: var(--text-1); color: var(--bg);
  padding: 10px 18px; border-radius: 999px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 50;
  max-width: 90vw; text-align: center;
}
#toast.show { opacity: 1; }

.linkbox {
  display: flex; gap: var(--sp-2); align-items: center;
  background: var(--surface-2); border-radius: 12px;
  padding: var(--sp-2) var(--sp-2) var(--sp-2) var(--sp-3);
}
.linkbox code { flex: 1; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.center { text-align: center; }
.mt { margin-top: var(--sp-4); }
