:root {
  /* ธีม "อุ่นใจ" โทนส้มพีชอบอุ่น */
  --brand: #f0643c; --brand-d: #d0462a; --brand-l: #ffe3d6;
  --sun: #ffb648; --cream: #fff9f3;
  --nhso: #2563eb; --nhso-l: #e0ecff; --pho: #c2690a; --pho-l: #fff1cc;
  --bg: #fff6ee; --card: #fff; --line: #f6e2d3; --text: #3d2a20; --muted: #8b6f60; --danger: #dc2626;
  --head: #fff4ea; --hover: #fff0e5;
}
* { box-sizing: border-box; }
body { margin: 0; font-family: 'Sarabun', 'Noto Sans Thai', Tahoma, sans-serif; font-size: 15px; color: var(--text); background: var(--bg); }
::selection { background: var(--brand-l); }
a { color: var(--brand); }
h2 { margin: 0 0 14px; font-size: 23px; color: #4a2c1e; } h3 { margin: 16px 0 8px; font-size: 16px; color: var(--brand-d); }
.center-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px;
  background: radial-gradient(circle at 15% 20%, #ffd9c2 0, transparent 40%), radial-gradient(circle at 85% 80%, #ffe7b3 0, transparent 45%), var(--bg); }

/* layout */
.app { display: flex; min-height: 100vh; }
.side { width: 230px; background: linear-gradient(170deg, #ff7a4d 0%, #f6593a 45%, #e94a3f 100%); color: #fff5ee; flex-shrink: 0;
        display: flex; flex-direction: column; box-shadow: 4px 0 18px #f0643c22; }
.side .logo { padding: 20px 18px; font-weight: 700; color: #fff; font-size: 19px; line-height: 1.3; border-bottom: 1px solid #ffffff33; }
.side .logo::before { content: '🧡'; display: inline-block; margin-right: 8px; }
.side a { display: block; margin: 3px 10px; padding: 10px 14px; color: #fff5ee; text-decoration: none; border-radius: 10px; }
.side a:hover { background: #ffffff26; color: #fff; }
.side a.on { background: #fff; color: var(--brand-d); font-weight: 600; box-shadow: 0 4px 12px #b0301a33; }
.side .me { margin-top: auto; padding: 14px 18px; font-size: 13px; border-top: 1px solid #ffffff33; }
.side .me a { margin: 0; padding: 0; color: #fff3c4 !important; text-decoration: underline; }
.side .me a:hover { background: none; }
.main { flex: 1; padding: 22px; min-width: 0; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 20px; margin-bottom: 16px; box-shadow: 0 2px 10px #e4925a14; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.muted { color: var(--muted); font-size: 13px; }

/* forms */
input, select, textarea { font: inherit; padding: 8px 11px; border: 1px solid #ecd3c2; border-radius: 9px; background: #fffdfb; width: 100%; color: var(--text); }
textarea { min-height: 60px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 3px solid var(--brand-l); border-color: var(--brand); background: #fff; }
input[type=checkbox], input[type=radio] { accent-color: var(--brand); }
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
label.check input { width: auto; }
.form-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.form-grid.one { grid-template-columns: 1fr; }
.form-grid.two { grid-template-columns: repeat(2, 1fr); }
.form-grid .span2 { grid-column: span 2; } .form-grid .span4 { grid-column: 1 / -1; }
.vitals { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 10px; }

.btn { display: inline-flex; align-items: center; gap: 6px; padding: 9px 18px; border: 0; border-radius: 999px;
       background: linear-gradient(135deg, #ff8a4c, var(--brand)); color: #fff; font: inherit; font-weight: 600;
       cursor: pointer; text-decoration: none; white-space: nowrap; box-shadow: 0 3px 10px #f0643c40; transition: transform .1s, box-shadow .1s; }
.btn:hover { background: linear-gradient(135deg, #ff7a3c, var(--brand-d)); box-shadow: 0 5px 14px #f0643c55; transform: translateY(-1px); }
.btn.sec { background: #fff; color: var(--brand-d); border: 1.5px solid #f7b79c; box-shadow: none; }
.btn.sec:hover { background: var(--brand-l); }
.btn.danger { background: #ef4444; box-shadow: none; }
.btn.sm { padding: 4px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }

/* tables */
table.list { width: 100%; border-collapse: collapse; background: #fff; }
table.list th, table.list td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
table.list th { background: var(--head); font-weight: 600; font-size: 13px; color: var(--muted); }
table.list tr.click { cursor: pointer; } table.list tr.click:hover td { background: var(--hover); }
.num { text-align: right !important; }

/* badges */
.badge { display: inline-block; padding: 2px 9px; border-radius: 99px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.b-NHSO { background: var(--nhso-l); color: var(--nhso); } .b-PHO { background: var(--pho-l); color: var(--pho); }
.s-screen { background: #fee2e2; color: #991b1b; } .s-exam { background: #fef3c7; color: #92400e; }
.s-dispense { background: #dbeafe; color: #1e40af; } .s-done { background: #dcfce7; color: #166534; } .s-cancel { background: #e5e7eb; color: #4b5563; }

.tabs { display: flex; gap: 4px; border-bottom: 2px solid var(--line); margin-bottom: 14px; flex-wrap: wrap; }
.tabs button { background: none; border: 0; padding: 10px 16px; font: inherit; cursor: pointer; color: var(--muted); border-bottom: 3px solid transparent; margin-bottom: -2px; }
.tabs button.on { color: var(--brand-d); border-bottom-color: var(--brand); font-weight: 600; }
.tabs .cnt { background: var(--brand-l); color: var(--brand-d); border-radius: 99px; padding: 0 7px; font-size: 12px; margin-left: 4px; }

.pt-head { display: flex; gap: 18px; align-items: center; flex-wrap: wrap; }
.pt-head .name { font-size: 20px; font-weight: 700; }
.allergy { background: #fee2e2; color: #991b1b; border: 1px solid #fca5a5; padding: 8px 12px; border-radius: 8px; font-weight: 600; margin-top: 10px; }
.alert { background: #fef2f2; color: #991b1b; padding: 10px 12px; border-radius: 8px; margin-bottom: 12px; }

.visit-grid { display: grid; grid-template-columns: 1fr 260px; gap: 16px; align-items: start; }
.hist-item { padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 13px; }

/* drug dispensing */
.drug-search { position: relative; }
.drug-dd { position: absolute; z-index: 20; top: 100%; left: 0; right: 0; background: #fff; border: 1px solid #ecd3c2; border-radius: 10px;
           max-height: 320px; overflow: auto; box-shadow: 0 10px 25px #0002; }
.drug-dd div { padding: 8px 12px; cursor: pointer; display: flex; gap: 8px; align-items: center; }
.drug-dd div:hover, .drug-dd div.hl { background: var(--hover); }
.grp { border: 1px solid var(--line); border-radius: 14px; margin-top: 14px; overflow: hidden; }
.grp-h { padding: 10px 14px; font-weight: 700; display: flex; justify-content: space-between; }
.grp-NHSO .grp-h { background: var(--nhso-l); color: var(--nhso); } .grp-PHO .grp-h { background: var(--pho-l); color: var(--pho); }
.grp td input { padding: 5px 8px; }
.totals { display: flex; gap: 12px; margin-top: 14px; flex-wrap: wrap; }
.tot { flex: 1; min-width: 160px; padding: 14px 18px; border-radius: 14px; background: linear-gradient(135deg, #fff, var(--cream)); border: 1px solid var(--line); }
.tot b { display: block; font-size: 22px; }
.tot.NHSO { background: var(--nhso-l); color: var(--nhso); } .tot.PHO { background: var(--pho-l); color: var(--pho); }

/* modal & toast */
.modal-bg { position: fixed; inset: 0; background: #3d2a2066; display: flex; align-items: flex-start; justify-content: center; padding: 40px 16px; overflow: auto; z-index: 50; }
.modal { background: #fff; border-radius: 18px; box-shadow: 0 20px 50px #7a2e1033; padding: 22px; width: 100%; max-width: 820px; }
.toast { position: fixed; bottom: 20px; right: 20px; background: #3d2a20; color: #fff; padding: 12px 18px; border-radius: 8px; z-index: 99; box-shadow: 0 8px 20px #0003; }
.toast.err { background: var(--danger); }

@media (max-width: 900px) {
  .app { flex-direction: column; } .side { width: auto; flex-direction: row; flex-wrap: wrap; }
  .side .logo, .side .me { display: none; } .side a { padding: 10px 12px; }
  .form-grid { grid-template-columns: 1fr 1fr; } .visit-grid { grid-template-columns: 1fr; }
}
