:root{
  --bg-main:#f4f1ec;
  --bg-panel:#fbf9f6;
  --bg-header:#eee7de;
  --bg-time:#e3ddd4;
  --row-even:#ffffff;
  --row-odd:#f7f2ec;

  --border:#ddd6ce;
  --border-soft:#e8e2db;

  --text-main:#2f2b27;
  --text-soft:#8c857d;

  --accent:#c7a56a;
  --accent-dark:#b89458;
  --accent-soft:#efe3cc;

  --radius-lg:22px;
  --radius-md:14px;
  --radius-sm:10px;

  --shadow-soft:0 8px 24px rgba(0,0,0,0.05);
  --shadow-card:0 10px 30px rgba(0,0,0,0.07);

  --header-h:82px;
}

html,
body{
  height:100%;
  margin:0;
  overflow:hidden;
}

*{
  box-sizing:border-box;
  margin:0;
  padding:0;
  font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif;
}

body{
  background:
    linear-gradient(180deg, #f6f2ed 0%, #f2ede7 100%);
  color:var(--text-main);
}

/* ================= GENERALI ================= */

button{
  border:none;
  background:#f3eee7;
  color:var(--text-main);
  padding:10px 14px;
  border-radius:12px;
  font-size:14px;
  font-weight:600;
  cursor:pointer;
  transition:background .15s ease, transform .12s ease, box-shadow .15s ease;
  box-shadow:0 2px 8px rgba(0,0,0,0.03);
}

button:hover{
  background:#e8e1d8;
  box-shadow:0 4px 12px rgba(0,0,0,0.05);
}

button:active{
  transform:translateY(1px);
}

input,
select,
textarea{
  width:100%;
  border:1px solid var(--border);
  background:#fff;
  color:var(--text-main);
  border-radius:12px;
  padding:10px 12px;
  font-size:14px;
  outline:none;
}

input:focus,
select:focus,
textarea:focus{
  border-color:#cdbda7;
  box-shadow:0 0 0 3px rgba(199,165,106,.10);
}

textarea{
  resize:vertical;
  min-height:96px;
}

/* ================= HEADER ================= */

.main-header{
  position:sticky;
  top:0;
  z-index:100;
  height:var(--header-h);

  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:18px;

  padding:14px 26px;

  background:rgba(251,249,246,0.88);
  backdrop-filter:blur(12px);
  border-bottom:1px solid var(--border-soft);
  box-shadow:0 4px 18px rgba(0,0,0,0.04);
}

.header-left,
.header-center,
.header-right{
  display:flex;
  align-items:center;
  gap:10px;
}

.header-center{
  flex:1;
  justify-content:center;
}

.header-right{
  justify-content:flex-end;
}

.header-logo{
  height:42px;
  object-fit:contain;
  filter:drop-shadow(0 1px 1px rgba(0,0,0,0.03));
}

.arrow-btn{
  width:36px;
  min-width:36px;
  height:36px;
  padding:0;
  border-radius:12px;
  font-size:18px;
  line-height:1;
}

#day-selector{
  width:144px;
  text-align:center;
  background:#fffdfb;
}

.search-input-field{
  width:260px;
  background:#fffdfb;
}

/* ================= HOME / PLANNER ================= */

#planner{
  position:absolute;
  top:var(--header-h);
  left:0;
  right:0;
  bottom:0;
  padding:22px 20px 20px;
  background:transparent;
  overflow:hidden;
}

#planner-scroll{
  width:100%;
  height:100%;
  overflow:auto;
  background:linear-gradient(180deg,#f0ebe4,#ece4db);
  border:1px solid var(--border-soft);
  border-radius:24px;
  box-shadow:
    inset 0 1px 0 rgba(255,255,255,.55),
    0 10px 30px rgba(0,0,0,0.04);
  /* Necessario per l'overlay sticky */
  position:relative;
}

/* Overlay sticky: NON PIÙ USATO — sostituito da CSS Grid sticky nativo */
#sticky-overlay{
  display:none !important;
}

/* ================= CSS GRID STICKY CELLS ================= */

/* Corner: ORARIO — sticky sia in alto che a sinistra */
.sticky-corner-cell{
  position:sticky;
  top:0;
  left:0;
  z-index:90;
  display:flex;
  align-items:center;
  justify-content:center;
  font-size:13px;
  font-weight:700;
  color:#3c3834;
  background:var(--bg-time);
  border-right:1px solid var(--border);
  border-bottom:1px solid var(--border);
  white-space:nowrap;
}

/* Header servizi: sticky solo in alto */
.sticky-header-cell{
  position:sticky;
  top:0;
  z-index:80;
  display:flex;
  align-items:center;
  justify-content:center;
  font-size:13px;
  font-weight:700;
  color:#3c3834;
  background:var(--bg-header);
  border-right:1px solid var(--border);
  border-bottom:1px solid var(--border);
  white-space:nowrap;
  cursor:pointer;
}

.sticky-header-cell:hover{
  background:#e5ddd2;
}

/* Colonna ore: sticky solo a sinistra — z-index sopra gli eventi (z-index:60) */
.sticky-time-cell{
  position:sticky;
  left:0;
  z-index:70;
  display:flex;
  align-items:center;
  justify-content:center;
  font-size:13px;
  font-weight:600;
  color:#6f6963;
  border-right:1px solid var(--border);
  border-bottom:1px solid var(--border);
  background:var(--bg-time);
}

/* Grid cell: ora è un figlio CSS Grid, non più position:absolute */
.grid-cell{
  border-right:1px solid var(--border-soft);
  border-bottom:1px solid var(--border-soft);
  transition:background .15s ease;
  /* position:absolute viene rimosso — è gestito da CSS Grid */
}

/* Events layer: assoluto sopra la griglia */
#events-layer{
  pointer-events:none;
}

#planner-grid{
  position:relative;
  min-width:max-content;
  background:#fff;
  border-radius:22px;
  /* Non usare overflow:hidden qui — rompe position:sticky dei figli */
}

/* ================= HEADER CELLS ================= */

/* Column headers: ora gestiti da .sticky-corner-cell e .sticky-header-cell */

/* ================= TIME CELLS ================= */

/* Time cells: ora gestiti da .sticky-time-cell */

/* ================= GRID CELLS ================= */

/* Le grid-cell ora usano gli stili da .grid-cell definiti nella sezione sticky */

.grid-cell:hover{
  background:#f2ebe3 !important;
}

.row-even{
  background:var(--row-even);
}

.row-odd{
  background:var(--row-odd);
}

/* ================= EVENTI ================= */


.appointment-item{
  position:absolute;
  background:linear-gradient(180deg,#ffffff,#fbfaf8);
  border-left:4px solid var(--accent);
  border-radius:12px;
  padding:10px;
  box-shadow:
    0 2px 6px rgba(0,0,0,0.04),
    0 8px 18px rgba(0,0,0,0.06);
  cursor:pointer;
  transition:transform .12s ease, box-shadow .12s ease, opacity .12s ease;
  z-index:60;
  overflow:hidden;
  /* Safari: impedisce allo scroll di intercettare i touch prima del JS */
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

/* Durante il drag disabilitiamo la transition per risposta immediata */
.appointment-item.dragging{
  transition: none !important;
}

.appointment-item:hover{
  transform:translateY(-1px);
  box-shadow:
    0 4px 10px rgba(0,0,0,0.06),
    0 10px 20px rgba(0,0,0,0.08);
}

.event-name{
  font-weight:700;
  font-size:14px;
  margin-bottom:3px;
  color:#36312d;
}

.event-phone{
  font-size:12px;
  color:#777068;
}

.event-count{
  font-size:11px;
  color:#9d968d;
  margin-top:4px;
}

/* ================= MODAL ================= */

.modal,
.overlay-modal{
  position:fixed;
  inset:0;
  background:rgba(0,0,0,0.25);
  display:none;
  align-items:center;
  justify-content:center;
  z-index:200;
}

.modal-box-content{
  background:rgba(255,255,255,0.96);
  backdrop-filter:blur(14px);
  border-radius:18px;
  padding:26px;
  width:360px;
  display:flex;
  flex-direction:column;
  gap:12px;
  box-shadow:0 20px 60px rgba(0,0,0,0.18);
  border:1px solid rgba(232,226,219,0.9);
}

.modal-footer{
  display:flex;
  gap:8px;
  margin-top:10px;
}

.modal-footer button{
  flex:1;
}

/* ================= TOAST ================= */

.toast{
  position:fixed;
  bottom:30px;
  left:50%;
  transform:translateX(-50%);
  background:#2f2b27;
  color:#fff;
  padding:12px 18px;
  border-radius:12px;
  font-size:13px;
  opacity:0;
  pointer-events:none;
  transition:.3s;
  z-index:300;
  box-shadow:0 10px 24px rgba(0,0,0,0.15);
}

.toast.show{
  opacity:1;
}

/* ================= LOCK SCREEN ================= */

.lock-screen{
  position:fixed;
  inset:0;
  background:rgba(244,241,236,0.92);
  backdrop-filter:blur(10px);
  display:flex;
  align-items:center;
  justify-content:center;
  z-index:9999;
}

.lock-box{
  width:360px;
  background:linear-gradient(180deg,#fbf9f6,#f3eee7);
  border:1px solid var(--border-soft);
  border-radius:22px;
  padding:30px 28px;
  box-shadow:0 10px 30px rgba(0,0,0,0.07);
  display:flex;
  flex-direction:column;
  gap:14px;
  align-items:center;
}

.lock-logo{
  width:150px;
  height:auto;
  margin-bottom:6px;
}

.lock-subtitle{
  font-size:13px;
  color:var(--text-soft);
  margin-top:-4px;
  margin-bottom:4px;
  letter-spacing:.4px;
}

.lock-box input{
  width:100%;
  border:1px solid var(--border);
  background:#fff;
  color:var(--text-main);
  border-radius:12px;
  padding:12px 14px;
  font-size:14px;
  text-align:center;
}

.lock-box button{
  width:100%;
  background:var(--accent);
  color:#fff;
  border:none;
  border-radius:12px;
  padding:12px 14px;
  font-size:14px;
  font-weight:700;
}

.lock-box button:hover{
  background:var(--accent-dark);
  transform:translateY(-1px);
}

/* ================= SCROLLBAR ================= */

::-webkit-scrollbar{
  width:10px;
  height:10px;
}

::-webkit-scrollbar-thumb{
  background:#d5cec6;
  border-radius:999px;
}

::-webkit-scrollbar-thumb:hover{
  background:#c7bfb6;
}

::-webkit-scrollbar-track{
  background:#eee8e1;
}

/* ================= RESPONSIVE ================= */

@media (max-width: 1100px){
  .main-header{
    padding:12px 18px;
    gap:12px;
  }

  .header-logo{
    height:36px;
  }

  .search-input-field{
    width:200px;
  }

  #planner{
    padding:16px;
  }

  .lock-box{
    width:min(360px, calc(100vw - 32px));
  }
}

@media (max-width: 820px){
  :root{
    --header-h:auto;
  }

  html,
  body{
    overflow:auto;
  }

  body{
    overflow-x:hidden;
  }

  .main-header{
    position:relative;
    height:auto;
    padding:14px 14px 12px;
    flex-direction:column;
    align-items:stretch;
    gap:12px;
  }

  .header-left,
  .header-center,
  .header-right{
    width:100%;
  }

  .header-left{
    justify-content:center;
  }

  .header-center{
    flex:none;
    justify-content:center;
    flex-wrap:wrap;
  }

  .header-right{
    justify-content:center;
    flex-wrap:wrap;
  }

  .header-logo{
    height:40px;
  }

  #day-selector{
    width:160px;
    max-width:100%;
  }

  .search-input-field{
    width:100%;
    max-width:420px;
  }

  #planner{
    position:relative;
    top:auto;
    left:auto;
    right:auto;
    bottom:auto;
    height:calc(100vh - 190px);
    min-height:420px;
    padding:14px;
  }

  #planner-scroll{
    border-radius:18px;
  }

  .modal-box-content{
    width:min(420px, calc(100vw - 24px));
    max-height:calc(100vh - 40px);
    overflow:auto;
    padding:22px;
  }

  .modal-footer{
    flex-wrap:wrap;
  }

  .modal-footer button{
    min-width:calc(50% - 4px);
    flex:1 1 calc(50% - 4px);
  }

  .lock-box{
    width:min(360px, calc(100vw - 24px));
    padding:24px 20px;
    border-radius:18px;
  }

  .lock-logo{
    width:130px;
  }
}

@media (max-width: 560px){
  .main-header{
    padding:12px 10px;
  }

  .header-center{
    gap:8px;
  }

  .header-right{
    gap:8px;
  }

  .arrow-btn{
    width:34px;
    min-width:34px;
    height:34px;
  }

  #day-selector{
    width:100%;
    max-width:none;
  }

  .search-input-field{
    width:100%;
    max-width:none;
  }

  .header-center input,
  .header-center button,
  .header-right button{
    font-size:14px;
  }

  #planner{
    padding:10px;
    height:calc(100vh - 210px);
  }

  #planner-scroll{
    border-radius:16px;
  }

  .modal-box-content{
    width:calc(100vw - 16px);
    padding:18px;
    border-radius:16px;
    gap:10px;
  }

  .modal-footer{
    flex-direction:column;
  }

  .modal-footer button{
    width:100%;
    min-width:100%;
    flex:none;
  }

  .lock-box{
    width:calc(100vw - 20px);
    padding:20px 16px;
    border-radius:16px;
    gap:12px;
  }

  .lock-logo{
    width:115px;
  }

  .lock-subtitle{
    font-size:12px;
    text-align:center;
  }

  .toast{
    width:calc(100vw - 24px);
    left:12px;
    right:12px;
    transform:none;
    text-align:center;
  }
}

@media (max-width: 420px){
  .header-right{
    flex-direction:column;
    align-items:stretch;
  }

  .header-right button{
    width:100%;
  }

  .header-center{
    flex-direction:column;
    align-items:stretch;
  }

  .header-center .arrow-btn{
    align-self:center;
  }

  #planner{
    height:calc(100vh - 260px);
  }

  .event-name{
    font-size:13px;
  }

  .event-phone,
  .event-count{
    font-size:11px;
  }
}


/* ================= SCRITTURA CON PENNA ================= */

.event-tools{
  display:flex;
  justify-content:flex-end;
  margin-top:-4px;
  margin-bottom:4px;
}

.pen-btn{
  display:inline-flex;
  align-items:center;
  justify-content:center;
  gap:6px;
  background:var(--accent-soft, #efe3cc);
  color:var(--text-main);
  border:1px solid var(--border);
  font-weight:700;
}

.pen-btn:hover{
  background:#e8dcc3;
}

.handwriting-box{
  width:min(92vw, 560px);
}

.handwriting-input{
  min-height:220px;
  font-size:22px;
  line-height:1.45;
  padding:18px;
  border-radius:14px;
  background:#fffdfb;
}

#handwriting-target{
  background:#fffdfb;
}

@media (max-width: 768px){
  .handwriting-box{
    width:92vw;
    padding:20px;
  }

  .handwriting-input{
    min-height:180px;
    font-size:20px;
  }
}

/* ================= SCROLL PERFORMANCE ================= */
#planner-scroll{
  -webkit-overflow-scrolling: touch;
}

.event-modal-box {
  width: min(92vw, 760px);
  max-height: 90vh;
  overflow-y: auto;
  padding: 24px;
  border-radius: 22px;
  background: #f7f3ed;
}

.event-modal-title {
  margin: 0 0 18px;
  text-align: center;
  font-size: 24px;
  font-weight: 600;
  color: #3f3d46;
}

.event-form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.pencil-input,
.pencil-textarea {
  width: 100%;
  border: 1.5px solid #d8cfc2;
  border-radius: 16px;
  background: #fffdf9;
  color: #3f3d46;
  font-size: 18px;
  padding: 16px 18px;
  box-sizing: border-box;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}

.pencil-input {
  min-height: 58px;
}

.pencil-textarea {
  grid-column: 1 / -1;
  min-height: 180px;
  resize: vertical;
  line-height: 1.45;
}

.pencil-input:focus,
.pencil-textarea:focus {
  border-color: #c7a56a;
  box-shadow: 0 0 0 4px rgba(199, 165, 106, 0.14);
}

.event-modal-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: center;
  margin-top: 18px;
}

.event-modal-actions button {
  min-width: 120px;
  min-height: 52px;
  padding: 12px 18px;
  border: none;
  border-radius: 14px;
  background: #c7a56a;
  color: white;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}

.event-modal-actions button:hover {
  opacity: 0.92;
}

@media (max-width: 768px) {
  .event-form-grid {
    grid-template-columns: 1fr;
  }

  .pencil-textarea {
    grid-column: auto;
  }

  .event-modal-box {
    width: 94vw;
    padding: 18px;
  }

  .pencil-input,
  .pencil-textarea {
    font-size: 17px;
  }
}
/* ================= HANDWRITING PAD ================= */

#hw-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 1000;
  align-items: flex-end;
  justify-content: center;
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

#hw-overlay.hw-visible {
  display: flex;
}

#hw-panel {
  width: min(98vw, 720px);
  background: linear-gradient(180deg, #fbf9f6, #f3eee7);
  border-radius: 22px 22px 0 0;
  border: 1px solid var(--border-soft);
  border-bottom: none;
  box-shadow: 0 -8px 32px rgba(0,0,0,0.15);
  display: flex;
  flex-direction: column;
  gap: 0;
  overflow: hidden;
  max-height: 70vh;
}

#hw-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px 12px;
  background: var(--bg-header);
  border-bottom: 1px solid var(--border-soft);
  flex-shrink: 0;
}

#hw-label {
  flex: 1;
  font-size: 15px;
  font-weight: 700;
  color: var(--text-main);
}

#hw-toolbar button {
  padding: 8px 14px;
  font-size: 14px;
  font-weight: 600;
  border-radius: 10px;
  min-height: 38px;
}

#hw-recognize-btn {
  background: var(--accent);
  color: #fff;
}

#hw-recognize-btn:hover {
  background: var(--accent-dark);
}

#hw-clear-btn {
  background: #f0e8de;
}

#hw-done-btn {
  background: #e8e1d8;
}

#hw-canvas {
  flex: 1;
  width: 100%;
  min-height: 220px;
  max-height: 340px;
  background: #fffdf9;
  cursor: crosshair;
  touch-action: none;
  display: block;
}

#hw-preview {
  min-height: 36px;
  padding: 8px 16px;
  font-size: 14px;
  color: var(--text-soft);
  text-align: center;
  background: var(--bg-panel);
  border-top: 1px solid var(--border-soft);
  white-space: pre-wrap;
  flex-shrink: 0;
}

/* ---- bottone scrittura accanto ad ogni campo ---- */

.hw-input-wrap {
  position: relative;
  display: flex;
  align-items: stretch;
  gap: 8px;
}

.hw-input-wrap .pencil-input,
.hw-input-wrap .pencil-textarea {
  flex: 1;
  min-width: 0;
}

.hw-trigger-btn {
  flex-shrink: 0;
  width: 48px;
  min-height: 48px;
  padding: 0;
  font-size: 14px;
  border-radius: 12px;
  background: var(--accent-soft);
  border: 1.5px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: none;
  align-self: flex-start;
}

.hw-trigger-btn:hover {
  background: #e8dcc3;
}

@media (max-width: 768px) {
  #hw-canvas {
    min-height: 200px;
  }

  #hw-panel {
    border-radius: 18px 18px 0 0;
  }
}
/* ================= DIALOG CUSTOM ================= */

.dialog-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.35);
  backdrop-filter: blur(4px);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 5000;
  padding: 16px;
}

.dialog-overlay.open {
  display: flex;
}

.dialog-box {
  background: linear-gradient(180deg, #fbf9f6, #f3eee7);
  border: 1px solid var(--border-soft);
  border-radius: 20px;
  padding: 28px 26px 22px;
  width: min(400px, calc(100vw - 32px));
  box-shadow: 0 20px 60px rgba(0,0,0,0.18);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.dialog-message {
  font-size: 16px;
  font-weight: 500;
  color: var(--text-main);
  text-align: center;
  line-height: 1.5;
  white-space: pre-wrap;
  margin: 0;
}

.dialog-input-wrap {
  width: 100%;
}

.dialog-input-wrap .pencil-input {
  font-size: 16px;
  text-align: center;
}

.dialog-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
  margin-top: 4px;
}

.dialog-buttons button {
  min-width: 110px;
  min-height: 48px;
  font-size: 15px;
  font-weight: 600;
  border-radius: 14px;
  flex: 1;
}

.dialog-btn-primary {
  background: var(--accent) !important;
  color: #fff !important;
}

.dialog-btn-primary:hover {
  background: var(--accent-dark) !important;
}

.dialog-btn-danger {
  background: #d94f3d !important;
  color: #fff !important;
}

.dialog-btn-danger:hover {
  background: #c0402f !important;
}

@media (max-width: 560px) {
  .dialog-box {
    padding: 22px 18px 18px;
    border-radius: 16px;
  }

  .dialog-buttons button {
    min-width: calc(50% - 5px);
  }
}

/* ================= LINEA ORA CORRENTE ================= */

#current-time-line {
  pointer-events: none;
}