:root {
  --forest: #17201a; --moss: #4a6b4d; --cream: #f6f2e9; --paper: #fbf9f4; --ink: #1c231e;
  --muted: #5d685f; --line: #e3ddd0; --gold: #e3a83f; --red: #b3412e;
  --serif: "Fraunces", Georgia, serif; --sans: "Inter", system-ui, "Segoe UI", sans-serif;
}
* { box-sizing: border-box; }
body { margin: 0; font-family: var(--sans); color: var(--ink); background: var(--paper); font-size: 17px; line-height: 1.5; }
[hidden] { display: none !important; }
h1, h2, h3 { font-family: var(--serif); font-weight: 600; margin: 0; }
.brand { font-family: var(--serif); font-weight: 600; font-size: 1.25rem; margin: 0; }
.brand span { font-family: var(--sans); font-weight: 400; font-size: .95rem; color: var(--muted); }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; border: 1px solid transparent; border-radius: 10px; padding: 11px 18px; font: inherit; font-weight: 600; cursor: pointer; text-decoration: none; transition: .15s; }
.btn.primary { background: var(--moss); color: #fff; }
.btn.primary:hover { background: #3c5a3f; }
.btn.primary:disabled { opacity: .55; cursor: wait; }
.btn.ghost { background: #fff; border-color: var(--line); color: var(--ink); }
.btn.ghost:hover { border-color: var(--moss); }
.btn.danger { background: #fff; border-color: #e7c3bb; color: var(--red); }
.btn.danger:hover { background: #fbefec; }
.btn.wide { width: 100%; }
.btn.small { padding: 6px 12px; font-size: .9rem; }

/* Accesso */
.login { min-height: 100vh; display: grid; place-items: center; padding: 20px; background: radial-gradient(circle at 20% 10%, #dfe8d9, transparent 50%), var(--cream); }
.login-box { width: min(400px, 100%); background: #fff; border: 1px solid var(--line); border-radius: 18px; padding: 34px; display: flex; flex-direction: column; gap: 16px; box-shadow: 0 10px 40px rgba(23,32,26,.08); }
.login-box h1 { font-size: 2rem; margin-bottom: 6px; }
.login-box .brand { color: var(--moss); font-size: 1rem; }
label { display: flex; flex-direction: column; gap: 6px; font-weight: 500; font-size: .95rem; }
input, textarea, select { font: inherit; padding: 11px 13px; border: 1px solid #d5cfbf; border-radius: 10px; background: #fff; color: var(--ink); }
input:focus, textarea:focus, select:focus { outline: 2px solid rgba(74,107,77,.35); border-color: var(--moss); }
.err { color: var(--red); margin: 0; font-size: .95rem; }
.back { color: var(--muted); font-size: .9rem; text-align: center; }

/* Pannello */
.app { display: grid; grid-template-columns: 300px 1fr; grid-template-rows: auto 1fr; height: 100vh; }
.top { position: sticky; top: 0; z-index: 30; grid-column: 1 / -1; display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 22px; background: #fff; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.top-actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.status { font-size: .88rem; color: var(--muted); }
.status.dirty { color: #9a6a12; font-weight: 600; }
.side { border-right: 1px solid var(--line); background: var(--cream); padding: 16px; display: flex; flex-direction: column; gap: 10px; min-height: 0; }
.side-search { width: 100%; }
.side-list { overflow: auto; flex: 1; display: flex; flex-direction: column; gap: 2px; margin: 0 -6px; padding: 0 6px; }
.side-item { display: flex; align-items: center; gap: 10px; text-align: left; border: 0; background: none; padding: 8px 10px; border-radius: 10px; font: inherit; cursor: pointer; color: var(--ink); }
.side-item:hover { background: rgba(74,107,77,.1); }
.side-item.on { background: var(--moss); color: #fff; }
.side-item img, .side-item .noimg { width: 44px; height: 34px; object-fit: cover; border-radius: 6px; flex: none; background: #e6dfcf; }
.side-item small { display: block; font-size: .78rem; opacity: .7; }
.side-item.general { border: 1px dashed #c9c1ad; justify-content: center; }
.side-group { font-size: .75rem; letter-spacing: .12em; text-transform: uppercase; color: var(--muted); margin: 12px 10px 4px; font-weight: 600; }

.editor { overflow: auto; padding: 28px clamp(18px, 4vw, 48px) 120px; }
.editor-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; flex-wrap: wrap; margin-bottom: 22px; }
.editor-head h2 { font-size: 2rem; }
.editor-head p { margin: 4px 0 0; color: var(--muted); }
.fields { display: grid; grid-template-columns: 1fr 1fr 200px; gap: 16px; margin-bottom: 26px; }
.check { flex-direction: row; align-items: center; gap: 10px; font-weight: 500; }
.check input { width: 20px; height: 20px; accent-color: var(--moss); }
.section { background: #fff; border: 1px solid var(--line); border-radius: 16px; padding: 20px; margin-bottom: 22px; }
.section h3 { font-size: 1.3rem; margin-bottom: 4px; }
.section .hint { color: var(--muted); font-size: .9rem; margin: 0 0 14px; }

.photos { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 14px; }
.photo { position: relative; border-radius: 12px; overflow: hidden; border: 1px solid var(--line); background: var(--cream); }
.photo img { width: 100%; aspect-ratio: 4/3; object-fit: contain; background: #1f2a22; display: block; }
.photo .bar { display: flex; justify-content: space-between; gap: 4px; padding: 6px; }
.photo .bar button { flex: 1; border: 1px solid var(--line); background: #fff; border-radius: 8px; padding: 5px 0; cursor: pointer; font: inherit; font-size: .95rem; }
.photo .bar button:hover { border-color: var(--moss); }
.photo .bar button.del { color: var(--red); }
.photo .bar { flex-wrap: wrap; }
.photo .bar button[data-a="crop"] { flex-basis: 100%; order: -1; font-weight: 600; color: var(--moss); }
.photo .badge { position: absolute; top: 8px; left: 8px; background: var(--gold); color: var(--forest); font-size: .75rem; font-weight: 600; padding: 2px 8px; border-radius: 999px; }
.photo .pending { position: absolute; inset: 0 0 auto 0; aspect-ratio: 4/3; display: grid; place-items: center; background: rgba(23,32,26,.55); color: #fff; font-size: .9rem; text-align: center; padding: 10px; }
.drop { border: 2px dashed #c9c1ad; border-radius: 12px; display: grid; place-items: center; text-align: center; padding: 20px; color: var(--muted); cursor: pointer; min-height: 170px; transition: .15s; }
.drop:hover, .drop.over { border-color: var(--moss); background: rgba(74,107,77,.06); color: var(--moss); }
.drop b { display: block; font-size: 2rem; line-height: 1; margin-bottom: 6px; }

.toolbar { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.toolbar button { border: 1px solid var(--line); background: #fff; border-radius: 8px; padding: 6px 12px; font: inherit; font-size: .92rem; cursor: pointer; }
.toolbar button:hover { border-color: var(--moss); color: var(--moss); }
.text-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
textarea { width: 100%; min-height: 420px; resize: vertical; line-height: 1.6; }
.preview { border: 1px solid var(--line); border-radius: 10px; padding: 14px 18px; background: var(--paper); max-height: 600px; overflow: auto; font-size: .98rem; }
.preview h4 { font-family: var(--serif); font-size: 1.2rem; margin: 1rem 0 .4rem; }
.preview p, .preview ul { margin: 0 0 .9rem; }
.preview-label { font-size: .8rem; text-transform: uppercase; letter-spacing: .1em; color: var(--muted); margin-bottom: 6px; font-weight: 600; }

#saveBtn.pulse { box-shadow: 0 0 0 0 rgba(227,168,63,.7); animation: pulse 1.8s infinite; }
@keyframes pulse { 70% { box-shadow: 0 0 0 10px rgba(227,168,63,0); } 100% { box-shadow: 0 0 0 0 rgba(227,168,63,0); } }
.danger-zone { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin-top: 10px; padding: 16px 18px; border: 1px dashed #e7c3bb; border-radius: 14px; }
.danger-zone p { margin: 0; color: var(--muted); font-size: .9rem; }
.savebar { position: sticky; bottom: 0; display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 14px 18px; margin: 10px -6px 0; background: rgba(251,249,244,.95); border: 1px solid var(--line); border-radius: 14px; backdrop-filter: blur(6px); box-shadow: 0 -4px 20px rgba(23,32,26,.06); flex-wrap: wrap; }
.savebar p { margin: 0; color: var(--muted); font-size: .9rem; }

.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: var(--forest); color: #fff; padding: 12px 20px; border-radius: 12px; box-shadow: 0 10px 30px rgba(0,0,0,.25); z-index: 99; max-width: 90vw; }
.toast.error { background: var(--red); }

/* Pagina iniziale */
.hello { background: linear-gradient(135deg, #eef3ea, #f8f3e6); border: 1px solid var(--line); border-radius: 18px; padding: 26px 28px; margin-bottom: 22px; }
.hello h2 { font-size: 2.3rem; }
.hello > p { margin: 8px 0 18px; font-size: 1.05rem; color: #2d362f; max-width: 720px; }
.hello .hint { margin: 14px 0 0; }
.stats { display: flex; gap: 30px; flex-wrap: wrap; margin-bottom: 20px; }
.stats div { display: flex; flex-direction: column; }
.stats b { font-family: var(--serif); font-size: 2rem; color: var(--moss); line-height: 1; }
.stats span { font-size: .85rem; color: var(--muted); margin-top: 4px; }
.quick { display: flex; flex-wrap: wrap; gap: 10px; }
.covers { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 14px; }
.covers .wide { grid-column: 1 / -1; margin: 0; }
.cover { position: relative; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: var(--cream); }
.cover img { width: 100%; aspect-ratio: 16 / 10; object-fit: contain; background: #1f2a22; display: block; }
.cover-name { font-family: var(--serif); font-weight: 600; padding: 8px 10px 0; }
.cover .bar { display: flex; gap: 4px; padding: 8px; }
.cover .bar button { flex: 1; border: 1px solid var(--line); background: #fff; border-radius: 8px; padding: 5px 0; cursor: pointer; font: inherit; font-size: .95rem; }
.cover .bar button:hover { border-color: var(--moss); }
.cover .bar button.del { color: var(--red); }
.cover .badge { position: absolute; top: 8px; left: 8px; background: var(--gold); color: var(--forest); font-size: .75rem; font-weight: 600; padding: 2px 8px; border-radius: 999px; }
.cover .badge.num { background: rgba(255,255,255,.9); }
.add-cover { font: inherit; background: none; }

/* Scelta copertine */
.picker { position: fixed; inset: 0; z-index: 90; background: rgba(12,17,13,.75); display: grid; place-items: center; padding: 16px; }
.picker-box { background: #fff; border-radius: 16px; width: min(1100px, 100%); height: min(90vh, 900px); display: flex; flex-direction: column; overflow: hidden; }
.picker-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 14px; flex-wrap: wrap; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.picker-head h3 { font-size: 1.4rem; }
.picker-head p { margin: 4px 0 0; color: var(--muted); font-size: .92rem; }
.picker-scroll { flex: 1 1 auto; min-height: 0; overflow: auto; }
.picker-grid { padding: 16px 20px; display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 10px; align-content: start; }
.pick { position: relative; padding: 0; border: 3px solid transparent; border-radius: 10px; overflow: hidden; cursor: pointer; background: #1f2a22; text-align: left; font: inherit; }
.pick .im { display: block; position: relative; height: 130px; }
.pick .im img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; display: block; }
.pick .pname { display: block; background: #fff; color: var(--ink); font-size: .85rem; padding: 5px 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pick:hover { border-color: var(--sage, #a9bba3); }
.pick.on { border-color: var(--gold); }
.pick .tick { position: absolute; top: 6px; right: 6px; background: var(--gold); color: var(--forest); font-weight: 700; font-size: .85rem; padding: 2px 8px; border-radius: 999px; }
.picker-foot { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 12px 20px 16px; border-top: 1px solid var(--line); }
.picker-foot span { color: var(--muted); }

/* Ritaglio */
.crop { position: fixed; inset: 0; z-index: 90; background: rgba(12,17,13,.75); display: grid; place-items: center; padding: 16px; }
.crop-box { background: #fff; border-radius: 16px; width: min(1000px, 100%); max-height: 100%; display: flex; flex-direction: column; overflow: auto; }
.crop-head { padding: 16px 20px 8px; }
.crop-head h3 { font-size: 1.4rem; }
.crop-head p { margin: 4px 0 0; color: var(--muted); font-size: .92rem; }
.crop-area { background: #1c231e; height: min(58vh, 560px); flex: none; }
.crop-area img { display: block; max-width: 100%; }
.crop-tools { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 10px; padding: 12px 20px; border-bottom: 1px solid var(--line); }
.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg button { border: 1px solid var(--line); background: #fff; border-radius: 8px; padding: 7px 12px; font: inherit; font-size: .92rem; cursor: pointer; }
.seg button:hover { border-color: var(--moss); }
.seg button.on { background: var(--moss); border-color: var(--moss); color: #fff; }
.crop-foot { display: flex; justify-content: flex-end; gap: 10px; padding: 12px 20px 16px; }

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; grid-template-rows: auto auto 1fr; height: auto; }
  .side { border-right: 0; border-bottom: 1px solid var(--line); }
  .side-list { max-height: 260px; }
  .fields, .text-grid { grid-template-columns: 1fr; }
  .editor { overflow: visible; }
  .top { padding: 10px 14px; }
  .top .brand span { display: none; }
  .top-actions { gap: 6px; width: 100%; }
  .top-actions .status { flex-basis: 100%; }
  .top-actions .btn { padding: 9px 12px; font-size: .92rem; }
  #saveBtn { flex: 1; white-space: nowrap; }
  textarea { min-height: 300px; }
  .hello { padding: 20px; }
  .hello h2 { font-size: 1.9rem; }
  .picker-grid { grid-template-columns: repeat(2, 1fr); padding: 12px; }
  .pick .im { height: 110px; }
}
