:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --ink: #1d2430;
  --muted: #667085;
  --line: #d9dde3;

  --work: #ffffff;
  --off: #f2b8b5;          /* почивни / празнични дни — червен фон */
  --off-ink: #8a1c17;
  --blocked: #a8c7f0;      /* блокиран ден — син */
  --blocked-ink: #123f78;
  --patient: #2f9a5d;      /* пациент — зелен */
  --patient-ink: #ffffff;

  --accent: #1f6f4a;
  --danger: #b42318;
  --blue: #1d5fb8;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .1);
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
  line-height: 1.4;
}
[hidden] { display: none !important; }

.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); min-height: 1.2em; margin: 6px 0 0; font-size: 14px; }
.warn { color: #93370d; background: #fef0c7; padding: 6px 10px; border-radius: 6px; font-size: 14px; margin: 8px 0 0; }

/* ---------- бутони и полета ---------- */
.btn {
  font: inherit;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  padding: 7px 12px;
  border-radius: 6px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: #eef0f3; }
.btn:focus-visible, input:focus-visible, select:focus-visible, .day:focus-visible {
  outline: 3px solid #84caff; outline-offset: 1px;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.primary:hover { background: #185a3c; }
.btn.blue { background: var(--blue); border-color: var(--blue); color: #fff; }
.btn.blue:hover { background: #174c94; }
.btn.danger { background: #fff; border-color: #f3b0aa; color: var(--danger); }
.btn.danger:hover { background: #fef3f2; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { background: #eef0f3; color: var(--ink); }
.btn.small { padding: 4px 9px; font-size: 13px; }
.btn.icon { padding: 6px 10px; min-width: 36px; font-size: 17px; line-height: 1; }

input, select {
  font: inherit;
  width: 100%;
  padding: 8px 10px;
  border: 1px solid #c4c9d1;
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
}
label { display: block; font-size: 13px; color: #344054; font-weight: 600; }
label > input, label > select { margin-top: 4px; font-weight: 400; }
label.inline { display: inline-flex; align-items: center; gap: 6px; font-weight: 500; }
label.inline select { width: auto; margin: 0; padding: 6px 8px; }

/* ---------- вход ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--surface); padding: 28px;
  border-radius: 12px; box-shadow: var(--shadow); display: grid; gap: 10px;
}
.login-card h1 { margin: 0; font-size: 24px; color: var(--accent); }
.login-card p { margin: 0 0 8px; }

/* ---------- горна лента ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 18px;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; font-size: 18px; color: var(--accent); }
.nav { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.range-label { font-weight: 700; font-size: 18px; margin-left: 8px; }
.tools { margin-left: auto; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

.legend { display: flex; flex-wrap: wrap; gap: 6px 18px; padding: 8px 16px 0; color: var(--muted); font-size: 13px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.sw { display: inline-block; width: 16px; height: 12px; border-radius: 3px; border: 1px solid rgba(0,0,0,.15); }
.sw-work { background: var(--work); }
.sw-off { background: var(--off); }
.sw-blocked { background: var(--blocked); }
.sw-patient { background: var(--patient); }

/* ---------- месеци ---------- */
.months { display: grid; gap: 16px; padding: 12px 16px 32px; }
.months[data-count="1"] { grid-template-columns: 1fr; }
.months[data-count="2"] { grid-template-columns: repeat(2, 1fr); }
.months[data-count="3"] { grid-template-columns: repeat(3, 1fr); }
.months[data-count="4"] { grid-template-columns: repeat(2, 1fr); }
.months[data-count="6"] { grid-template-columns: repeat(3, 1fr); }
.months[data-count="12"] { grid-template-columns: repeat(4, 1fr); }

.month { background: var(--surface); border-radius: 10px; box-shadow: var(--shadow); padding: 10px; min-width: 0; }
.month-title { margin: 2px 4px 8px; font-size: 17px; }
.months[data-count="1"] .month-title { display: none; }

.grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 4px; }
.wd { text-align: center; font-size: 12px; font-weight: 700; color: var(--muted); padding: 2px 0; }
.wd-we { color: var(--off-ink); }

.cell { min-height: 110px; border-radius: 6px; }
.months[data-count="2"] .cell { min-height: 92px; }
.months[data-count="3"] .cell, .months[data-count="4"] .cell { min-height: 76px; }
.months[data-count="6"] .cell { min-height: 58px; }
.months[data-count="12"] .cell { min-height: 40px; }
.cell.empty { background: transparent; }

.day {
  font: inherit; color: inherit; text-align: left;
  display: flex; flex-direction: column; gap: 3px;
  padding: 5px; border: 1px solid var(--line); background: var(--work);
  cursor: pointer; min-width: 0; overflow: hidden;
}
.day:hover { border-color: #98a2b3; box-shadow: 0 0 0 1px #98a2b3 inset; }
.day.off { background: var(--off); border-color: #e59a96; }
.day.off .num { color: var(--off-ink); }
.day.blocked { background: var(--blocked); border-color: #7fa9e0; }
.day.blocked .num { color: var(--blocked-ink); }
.day.past .num { opacity: .55; }
.day.today { box-shadow: 0 0 0 3px var(--ink) inset; }

.cell-head { display: flex; justify-content: space-between; align-items: center; gap: 4px; }
.num { font-weight: 700; font-size: 14px; }
.months[data-count="12"] .num { font-size: 12px; }
.badge { background: var(--patient); color: #fff; font-size: 11px; font-weight: 700; border-radius: 10px; padding: 0 6px; }
.hol { font-size: 11px; color: var(--off-ink); line-height: 1.2; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.blocked .hol { color: var(--blocked-ink); }

.pills { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.pill {
  background: var(--patient); color: var(--patient-ink);
  font-size: 12px; font-weight: 600; line-height: 1.35;
  padding: 1px 6px; border-radius: 4px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.months[data-count="6"] .pill, .months[data-count="12"] .pill { font-size: 10px; padding: 0 4px; }
.more { font-size: 11px; color: #1f5e3a; font-weight: 600; padding-left: 2px; }
.off .more { color: var(--off-ink); }

/* ---------- диалози ---------- */
.dialog {
  width: min(640px, calc(100vw - 24px)); max-height: calc(100vh - 32px);
  border: none; border-radius: 12px; padding: 18px 20px; box-shadow: 0 20px 50px rgba(0,0,0,.25);
}
.dialog.narrow { width: min(460px, calc(100vw - 24px)); }
.dialog::backdrop { background: rgba(16, 24, 40, .45); }
.dialog-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.dialog-head h2 { margin: 0; font-size: 20px; }
.tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.tag { font-size: 12px; padding: 2px 8px; border-radius: 10px; background: #eef0f3; }
.tag.red { background: var(--off); color: var(--off-ink); }
.tag.blue { background: var(--blocked); color: var(--blocked-ink); }
.tag.grey { color: var(--muted); }

.banner { margin-top: 14px; padding: 10px 12px; border-radius: 8px; display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: space-between; }
.banner.blocked { background: var(--blocked); color: var(--blocked-ink); font-weight: 600; }

.section-title { font-size: 14px; margin: 18px 0 8px; color: #344054; text-transform: uppercase; letter-spacing: .03em; }
.count { color: var(--muted); font-weight: 500; }

.patient-list, .zones-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.patient { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border: 1px solid var(--line); border-left: 5px solid var(--patient); border-radius: 6px; }
.idx { color: var(--muted); font-size: 13px; width: 18px; text-align: right; }
.p-main { flex: 1; min-width: 0; }
.p-name { font-weight: 600; overflow-wrap: anywhere; }
.p-meta { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 13px; color: var(--muted); }
.zone-chip { background: #e7f6ec; color: #1f5e3a; padding: 0 8px; border-radius: 10px; }
.row-actions { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
.confirm-q { font-size: 13px; color: var(--danger); font-weight: 600; }
.empty-note { color: var(--muted); font-size: 14px; padding: 4px 0; }

.appt-form { display: grid; gap: 10px; border-top: 1px solid var(--line); margin-top: 16px; }
.appt-form .section-title { margin-bottom: 0; }
.grid2 { display: grid; grid-template-columns: 2fr 1fr; gap: 10px; }
.new-zone { display: flex; gap: 6px; flex-wrap: wrap; }
.new-zone input { flex: 1 1 200px; width: auto; }
.form-actions { display: flex; justify-content: flex-end; }

.dialog-foot { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 8px; margin-top: 18px; padding-top: 12px; border-top: 1px solid var(--line); }

.zones-list { margin: 12px 0; }
.zones-list li { display: flex; align-items: center; gap: 10px; padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; }
.z-name { flex: 1; font-weight: 600; }

/* ---------- съобщения ---------- */
.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%) translateY(20px);
  background: #1d2430; color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 100; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.err { background: var(--danger); }

/* ---------- малки екрани ---------- */
@media (max-width: 1100px) {
  .months[data-count="3"], .months[data-count="6"] { grid-template-columns: repeat(2, 1fr); }
  .months[data-count="12"] { grid-template-columns: repeat(3, 1fr); }
}
@media (max-width: 760px) {
  .months[data-count] { grid-template-columns: 1fr; }
  .months .cell { min-height: 64px !important; }
  .tools { margin-left: 0; }
  .range-label { width: 100%; margin: 4px 0 0; }
  .grid2 { grid-template-columns: 1fr; }
  .pill { font-size: 10px; padding: 0 3px; }
  .hol { display: none; }
}

/* ---------- задължителни полета и предупреждение за повторен запис ---------- */
.req { color: var(--danger); }
input.invalid, select.invalid { border-color: var(--danger); box-shadow: 0 0 0 2px #fecdca; }
.dup-panel { background: #fffaeb; border: 1px solid #fec84b; border-left: 5px solid #f79009; border-radius: 8px; padding: 10px 12px; }
.dup-title { margin: 0 0 6px; font-weight: 700; color: #93370d; }
.dup-list { margin: 0 0 8px; padding-left: 18px; font-size: 14px; }
.dup-list li { margin: 2px 0; }
.dup-q { margin: 0 0 8px; font-weight: 600; }
.dup-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- търсене ---------- */
.search { display: flex; gap: 6px; }
.search input { width: 170px; padding: 6px 10px; }
.linkish { font: inherit; background: none; border: none; padding: 0; color: var(--blue); cursor: pointer; text-decoration: underline; text-underline-offset: 2px; }
.linkish:hover { color: #174c94; }
.next-card { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 16px; padding: 12px 14px; border: 1px solid #a6dfbd; border-left: 5px solid var(--patient); background: #f0faf4; border-radius: 8px; }
.next-date { font-size: 20px; font-weight: 700; }
.next-meta { display: flex; gap: 8px; align-items: center; flex: 1; }
.next-rel { font-weight: 600; color: #1f5e3a; white-space: nowrap; }
.next-meta .zone-chip, .search-list .zone-chip { white-space: nowrap; }
.search-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.search-list li { display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: center; padding: 6px 10px; border: 1px solid var(--line); border-radius: 6px; }
.search-list li.past-row { background: #f9fafb; }
.history-btn-row { margin-top: 18px; }
.history { margin-top: 10px; }
@media (max-width: 760px) { .search, .search input { width: 100%; } .search input { flex: 1; } }

/* ---------- печат на списък ---------- */
.print-form { display: grid; gap: 10px; margin-top: 12px; }
.print-area { display: none; }

@media print {
  @page { size: A4 portrait; margin: 14mm 12mm; }
  body.printing-list > *:not(#print-area) { display: none !important; }
  body.printing-list #print-area { display: block; color: #000; font-size: 11pt; }
  #print-area h1 { font-size: 15pt; margin: 0 0 10px; }
  #print-area table { width: 100%; border-collapse: collapse; }
  #print-area th, #print-area td { border: 1px solid #555; padding: 4px 6px; text-align: left; vertical-align: top; }
  #print-area th { background: #eee; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  #print-area thead { display: table-header-group; }
  #print-area tr { break-inside: avoid; }
  #print-area tr.new-day td { border-top: 2px solid #000; }
  #print-area .c-num { width: 2.2em; text-align: right; }
  #print-area .c-date { white-space: nowrap; width: 6.5em; }
}

@media print {
  .topbar .nav button, .tools, .legend { display: none; }
  body { background: #fff; }
  .month { box-shadow: none; }
}
