:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --text: #1c2430;
  --muted: #5d6878;
  --line: #d9dee6;
  --line-strong: #b9c1cd;
  --primary: #1f4e79;
  --primary-hover: #183d5f;
  --danger: #b42318;
  --focus: #2f7ad8;

  --st-bekliyor: #fff4cc;
  --st-yatti: #d6e9ff;
  --st-opere: #d9f2d0;
  --st-taburcu: #ececec;
  --st-iptal: #f8dcdc;
  --prep-y: #fff3bf;
  --prep-o: #ffd8a8;
  --prep-r: #ffc4c4;
  --struck-bg: #eceef1;
  --warn-bg: #ffd8b0;
  --warn-text: #a31d00;

  --radius: 8px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  font-size: 14px;
  color: var(--text);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); }
[hidden] { display: none !important; }

h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; margin: 20px 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.error { color: var(--danger); margin: 0; min-height: 1em; }
.error:empty { min-height: 0; }

button, .button, input, select, textarea {
  font: inherit; color: inherit;
}
button, .button {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 34px; padding: 0 14px; border: 1px solid var(--line-strong);
  border-radius: var(--radius); background: var(--surface); cursor: pointer;
  text-decoration: none; white-space: nowrap;
}
button:hover, .button:hover { background: #eef1f5; }
button.primary { background: var(--primary); border-color: var(--primary); color: #fff; font-weight: 600; }
button.primary:hover { background: var(--primary-hover); }
button.danger { color: var(--danger); border-color: #e6b3ae; margin-right: auto; }
button.danger:hover { background: #fdf0ef; }
button.icon { min-height: 32px; width: 32px; padding: 0; font-size: 1.4rem; border-color: transparent; background: transparent; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

input, select, textarea {
  width: 100%; min-height: 34px; padding: 6px 9px;
  border: 1px solid var(--line-strong); border-radius: 6px; background: #fff;
}
textarea { min-height: 64px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--focus); outline-offset: -1px; border-color: var(--focus); }
label { display: flex; flex-direction: column; gap: 4px; font-weight: 500; color: var(--muted); font-size: 0.9rem; }
label > input, label > select, label > textarea { color: var(--text); font-weight: 400; font-size: 14px; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; }

/* giriş */
.center-view { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(380px, 100%); padding: 28px; display: flex; flex-direction: column; gap: 14px; }
.login-card p { margin: -8px 0 4px; }

/* üst çubuk */
#main-view { display: flex; flex-direction: column; height: 100%; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 10px 16px; background: var(--primary); color: #fff;
}
.brand { font-weight: 700; font-size: 1.1rem; }
.topbar-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.topbar .button, .topbar button:not(.primary), .menu summary {
  background: rgba(255,255,255,0.12); border-color: rgba(255,255,255,0.35); color: #fff;
}
.topbar .button:hover, .topbar button:not(.primary):hover, .menu summary:hover { background: rgba(255,255,255,0.22); }
.topbar button.primary { background: #fff; color: var(--primary); border-color: #fff; }
.topbar button.primary:hover { background: #e8eef5; }

.menu { position: relative; }
.menu summary {
  list-style: none; min-height: 34px; padding: 0 14px; border: 1px solid; border-radius: var(--radius);
  display: flex; align-items: center; cursor: pointer;
}
.menu summary::-webkit-details-marker { display: none; }
.menu summary::after { content: "▾"; margin-left: 6px; }
.menu-list {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 20; min-width: 190px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0,0,0,0.15); padding: 6px; display: flex; flex-direction: column;
}
.menu-list button { justify-content: flex-start; border: 0; color: var(--text) !important; background: transparent !important; }
.menu-list button:hover { background: #eef1f5 !important; }

/* filtreler */
.filters {
  display: flex; gap: 8px; flex-wrap: wrap; align-items: center;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.filters input[type=search] { flex: 1 1 260px; width: auto; }
.filters select { flex: 0 1 190px; width: auto; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  min-height: 30px; padding: 0 12px; border-radius: 999px; font-size: 0.9rem;
  border: 1px solid var(--line-strong); background: var(--surface);
}
.chip .n { color: var(--muted); font-variant-numeric: tabular-nums; }
.chip[aria-pressed=true] { background: var(--primary); border-color: var(--primary); color: #fff; }
.chip[aria-pressed=true] .n { color: #d6e2ee; }
.dot.st-bekliyor { background: var(--st-bekliyor); }
.dot.st-yatti { background: var(--st-yatti); }
.dot.st-opere { background: var(--st-opere); }
.dot.st-taburcu { background: var(--st-taburcu); }
.dot.st-iptal { background: var(--st-iptal); }
.chip .dot { width: 10px; height: 10px; border-radius: 50%; border: 1px solid rgba(0,0,0,0.25); }

/* tablo */
.table-wrap { flex: 1; overflow: auto; }
#tbl { border-collapse: separate; border-spacing: 0; width: 100%; min-width: 1200px; background: var(--surface); }
#tbl th, #tbl td { padding: 7px 8px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
#tbl th {
  position: sticky; top: 0; z-index: 2; background: #e9edf3; font-weight: 600; font-size: 0.85rem;
  color: #384252; white-space: nowrap; cursor: pointer; user-select: none; border-bottom-color: var(--line-strong);
}
#tbl th:hover { background: #dfe5ed; }
#tbl th[aria-sort=ascending]::after { content: " ▲"; font-size: 0.7em; }
#tbl th[aria-sort=descending]::after { content: " ▼"; font-size: 0.7em; }
#tbl td.c-ad_soyad, #tbl th.c-ad_soyad { position: sticky; left: 0; z-index: 1; }
#tbl th.c-ad_soyad { z-index: 3; }
#tbl td.c-ad_soyad { background: inherit; font-weight: 600; min-width: 170px; white-space: normal; box-shadow: 1px 0 0 var(--line); }
#tbl tbody tr { cursor: pointer; background: var(--surface); }
#tbl tbody tr:hover { filter: brightness(0.96); }
#tbl td.num, #tbl th.num { text-align: right; font-variant-numeric: tabular-nums; }
#tbl td.date { white-space: nowrap; font-variant-numeric: tabular-nums; }
#tbl td { white-space: nowrap; }
#tbl td.wrap { white-space: normal; min-width: 180px; max-width: 280px; }
#tbl td.mono { font-variant-numeric: tabular-nums; white-space: nowrap; }
/* satır rengi: hazırlığın yaşı */
tr.prep-y { background: var(--prep-y) !important; }
tr.prep-o { background: var(--prep-o) !important; }
tr.prep-r { background: var(--prep-r) !important; }
tr.prep-expired, tr.row-done { background: var(--struck-bg) !important; color: #6b7280; }
tr.row-admitted { background: var(--st-yatti) !important; color: #3d5a80; }
tr.row-admitted td { text-decoration: line-through; text-decoration-color: rgba(40, 70, 120, 0.5); }
tr.row-admitted td.c-durum { text-decoration: none; }
tr.prep-expired td, tr.row-done td { text-decoration: line-through; text-decoration-color: rgba(60, 60, 60, 0.55); }
tr.prep-expired td.c-_hazirlik, tr.prep-expired td.c-durum, tr.row-done td.c-durum { text-decoration: none; }

.pill { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 0.85rem; border: 1px solid rgba(0,0,0,0.12); color: var(--text); }
.pill.st-bekliyor { background: var(--st-bekliyor); }
.pill.st-yatti { background: var(--st-yatti); }
.pill.st-opere { background: var(--st-opere); }
.pill.st-taburcu { background: var(--st-taburcu); }
.pill.st-iptal { background: var(--st-iptal); }

.prep-badge { font-variant-numeric: tabular-nums; }
.prep-b-y, .prep-b-o, .prep-b-r { font-weight: 600; }
.prep-b-r { color: #a61b1b; }
.prep-missing { color: #9a5b00; font-style: italic; }
.prep-b-expired { color: #fff; background: #b42318; border-radius: 4px; padding: 1px 6px; font-weight: 700; }

.legend { display: inline-flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.sw { display: inline-block; width: 12px; height: 12px; border-radius: 3px; border: 1px solid rgba(0,0,0,0.2); margin-left: 6px; }
.sw.prep-y { background: var(--prep-y); }
.sw.prep-o { background: var(--prep-o); }
.sw.prep-r { background: var(--prep-r); }
.sw.prep-expired { background: var(--struck-bg); }
.sw.unseen { background: #7048e8; }
.sw.admitted { background: var(--st-yatti); }
.warn { background: var(--warn-bg); color: var(--warn-text); font-weight: 700; border-radius: 4px; padding: 1px 6px; }
.empty { text-align: center; color: var(--muted); padding: 40px 16px; }
.statusbar {
  display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 6px 16px; padding: 6px 16px;
  background: var(--surface); border-top: 1px solid var(--line); color: var(--muted); font-size: 0.85rem;
}

/* diyaloglar */
dialog {
  border: 0; border-radius: 12px; padding: 0; width: min(440px, calc(100vw - 32px));
  max-height: calc(100vh - 32px); box-shadow: 0 20px 60px rgba(0,0,0,0.3); color: var(--text);
}
dialog.wide { width: min(980px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(15, 25, 40, 0.45); }
dialog > form, dialog > .dlg-wrap { display: flex; flex-direction: column; max-height: calc(100vh - 32px); }
.dlg-head { display: flex; align-items: center; gap: 16px; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.dlg-head .close { margin-left: auto; }
.dlg-body { padding: 16px 18px; overflow: auto; }
.dlg-foot {
  display: flex; align-items: center; justify-content: flex-end; gap: 8px; flex-wrap: wrap;
  padding: 12px 18px; border-top: 1px solid var(--line); background: #fafbfc;
}
.dlg-foot .error { flex-basis: 100%; text-align: right; order: -1; }
.dlg-foot #patient-meta { margin-right: auto; }
.stack { display: flex; flex-direction: column; gap: 12px; }

.tabs { display: flex; gap: 4px; }
.tab { min-height: 30px; border-color: transparent; background: transparent; color: var(--muted); }
.tab.active { background: #e9eef5; color: var(--primary); font-weight: 600; }

fieldset { border: 0; padding: 0; margin: 0 0 18px; }
legend { font-weight: 700; color: var(--primary); margin-bottom: 8px; font-size: 0.95rem; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px 14px; }
.grid .span2 { grid-column: span 2; }
.req::after { content: " *"; color: var(--danger); }
.hint { color: #9a5b00; font-size: 0.8rem; font-weight: 400; min-height: 0; }
.wait-info { font-size: 0.85rem; color: var(--muted); align-self: end; padding-bottom: 8px; }

.history { list-style: none; margin: 0; padding: 0; }
.history li { padding: 10px 0; border-bottom: 1px solid var(--line); }
.history .h-head { font-size: 0.85rem; color: var(--muted); margin-bottom: 4px; }
.history .h-head b { color: var(--text); }
.history .h-change { font-size: 0.9rem; }
.history del { color: #a33; }
.history ins { color: #1c6b2a; text-decoration: none; }

.lists-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px; }
.lists-grid textarea { min-height: 150px; }

table.plain { width: 100%; border-collapse: collapse; }
table.plain th, table.plain td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--line); }
table.plain td.actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }
table.plain button { min-height: 28px; padding: 0 10px; font-size: 0.85rem; }
.user-form { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 10px; align-items: end; }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px);
  background: #1c2430; color: #fff; padding: 10px 18px; border-radius: 8px; opacity: 0;
  transition: opacity 0.2s, transform 0.2s; pointer-events: none; z-index: 100; max-width: calc(100vw - 32px);
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---- mobil / masaüstü ayrımı (body.mobile JS ile 760px altında) */
.mobile-only { display: none !important; }
body.mobile .mobile-only { display: revert !important; }
body.mobile .desktop-only { display: none !important; }
body.mobile button.mobile-only { display: inline-flex !important; }
body.mobile .mobile-only[hidden] { display: none !important; }

/* hasta kartları */
.cards { display: flex; flex-direction: column; gap: 10px; padding: 12px 12px 24px; }
body.mobile .cards { display: flex !important; }
body.mobile #tbl { display: none; }
.pcard {
  background: var(--surface); border: 1px solid var(--line); border-left: 5px solid var(--line-strong);
  border-radius: 10px; padding: 12px 14px; display: flex; flex-direction: column; gap: 4px; cursor: pointer;
}
.pcard:active { filter: brightness(0.96); }
.pcard.prep-y { background: var(--prep-y); border-left-color: #e0b400; }
.pcard.prep-o { background: var(--prep-o); border-left-color: #e07b00; }
.pcard.prep-r { background: var(--prep-r); border-left-color: #c92a2a; }
.pcard.prep-expired, .pcard.row-done { background: var(--struck-bg); color: #6b7280; }
.pcard.row-admitted { background: var(--st-yatti); border-left-color: #4a7fc1; color: #3d5a80; }
.pcard.row-admitted .pc-name, .pcard.row-admitted .pc-tani, .pcard.row-admitted .pc-meta { text-decoration: line-through; }
.pcard.prep-expired .pc-name, .pcard.row-done .pc-name,
.pcard.prep-expired .pc-tani, .pcard.row-done .pc-tani,
.pcard.row-done .pc-meta { text-decoration: line-through; }
.pc-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.pc-name { font-weight: 700; font-size: 1.05rem; }
.pc-top .pill { flex-shrink: 0; }
.pc-tani { font-size: 0.95rem; }
.pc-meta { color: var(--muted); font-size: 0.9rem; }
.pc-badges { display: flex; flex-wrap: wrap; gap: 6px 14px; align-items: center; font-size: 0.9rem; margin-top: 4px; }
.pc-tel {
  margin-left: auto; padding: 4px 12px; border: 1px solid var(--line-strong); border-radius: 999px;
  background: var(--surface); color: var(--primary); text-decoration: none; font-weight: 600;
}

/* sayfa sayfa form */
.steps { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 10px; margin-bottom: 6px; scrollbar-width: none; }
body.mobile .steps { display: flex !important; }
.step-chip { min-height: 32px; padding: 0 10px; border-radius: 999px; font-size: 0.85rem; flex-shrink: 0; color: var(--muted); }
.step-chip .step-n {
  display: inline-grid; place-items: center; width: 20px; height: 20px; border-radius: 50%;
  background: var(--line); color: var(--text); font-size: 0.75rem; font-weight: 700;
}
.step-chip.done .step-n { background: #9fc5a8; }
.step-chip.active { border-color: var(--primary); color: var(--primary); font-weight: 600; }
.step-chip.active .step-n { background: var(--primary); color: #fff; }

/* uzman görmeden hazırlanan hasta: mor işaret */
.unseen-badge {
  display: inline-block; background: #7048e8; color: #fff; font-size: 0.75rem; font-weight: 700;
  padding: 1px 7px; border-radius: 4px; white-space: nowrap; text-decoration: none; vertical-align: middle;
}
tr.unseen td.c-_sira { background: #7048e8; color: #fff; font-weight: 700; }
tr.unseen td.c-ad_soyad { box-shadow: inset 4px 0 0 #7048e8, 1px 0 0 var(--line); }
.pcard.unseen { outline: 2px solid #7048e8; outline-offset: -1px; }
.chip-sort { margin-left: auto; }
.chip-sort[aria-pressed=true] { background: #1c2430; border-color: #1c2430; color: #fff; }
.chip-doluyor { border-color: #f0a3a3; }
.chip-doluyor[aria-pressed=true] { background: #c92a2a; border-color: #c92a2a; }
.chip-gecti { border-color: #e08f88; }
.chip-gecti[aria-pressed=true] { background: #8a1c12; border-color: #8a1c12; }
.chip-unseen { border-color: #b8a6f5; }
.chip-unseen[aria-pressed=true] { background: #7048e8; border-color: #7048e8; }
select.unseen-select { border-color: #7048e8; background: #f3efff; color: #5532c9; font-weight: 600; }
.btn-onay { border-color: #7048e8; color: #5532c9; font-weight: 600; }
.btn-onay:hover { background: #f3efff; }

/* geçmiş */
.h-title { margin: 0 0 8px; }
.history li.h-item { padding: 10px 0 10px 12px; border-bottom: 1px solid var(--line); border-left: 3px solid var(--line-strong); margin-bottom: 4px; }
.history li.h-new { border-left-color: #2f9e44; }
.history li.h-upd { border-left-color: var(--focus); }
.history li.h-del { border-left-color: var(--danger); }
.h-changes { margin: 4px 0 0; padding-left: 18px; }
.h-changes li { padding: 2px 0; }
.h-created { font-size: 0.9rem; }

body.mobile dialog, body.mobile dialog.wide {
  width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; border-radius: 0;
}
body.mobile dialog > form, body.mobile dialog > .dlg-wrap { height: 100dvh; max-height: 100dvh; }
body.mobile .dlg-body { flex: 1; }
body.mobile .dlg-foot { padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); }
body.mobile .dlg-foot button { flex: 1; min-height: 44px; }
body.mobile .dlg-foot .error { flex-basis: 100%; text-align: left; }
body.mobile input, body.mobile select, body.mobile textarea { font-size: 16px; min-height: 44px; }
body.mobile .grid { grid-template-columns: 1fr; }
body.mobile .topbar { padding: 8px 12px; }
body.mobile .filters { padding: 8px 12px; }
body.mobile .filters input[type=search] { flex-basis: 100%; }
body.mobile .filters select { flex: 1 1 30%; min-width: 0; }
body.mobile .statusbar .legend { display: none; }
#dlg-patient[data-tab=history] .dlg-foot { display: none; }

@media (max-width: 640px) {
  .topbar, .filters { padding-left: 16px; padding-right: 16px; }
  .filters select { flex: 1 1 45%; }
  .grid .span2 { grid-column: auto; }
  .dlg-head { flex-wrap: wrap; }
}

/* yazdırma: yalnızca tablo */
@media print {
  @page { size: A4 landscape; margin: 10mm; }
  body { background: #fff; font-size: 9pt; }
  .topbar, .filters, .statusbar, dialog, #toast { display: none !important; }
  #main-view { display: block; height: auto; }
  .table-wrap { overflow: visible; }
  #tbl { min-width: 0; }
  #tbl th { position: static; background: #e9edf3 !important; }
  #tbl td.c-ad_soyad { position: static; box-shadow: none; }
  #tbl th, #tbl td { padding: 3px 4px; }
  tr { break-inside: avoid; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .print-title { display: block !important; font-weight: 700; margin-bottom: 6px; }
}
.print-title { display: none; }
body.mobile .dlg-head { flex-wrap: nowrap; gap: 10px; }
body.mobile #patient-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
input.upper::placeholder, textarea.upper::placeholder { text-transform: none; }
.sel-note { font-size: 0.75rem; color: var(--muted); font-weight: 400; margin-top: -2px; }
.idle-dialog { width: min(380px, calc(100vw - 32px)) !important; height: auto !important; max-height: none !important; border-radius: 12px !important; margin: auto !important; text-align: center; }
.idle-dialog h2 { color: var(--danger); }
#idle-left { font-variant-numeric: tabular-nums; }
