* { box-sizing: border-box; margin: 0; padding: 0; }
body { min-height: 100vh; background: #F7F3EE; font-family: 'Noto Serif TC', 'Georgia', serif; color: #2C1F0E; }

/* ── Login ── */
#login-screen { display: flex; align-items: center; justify-content: center; min-height: 100vh; background: #2C1F0E; }
.login-box { background: #fff; border-radius: 16px; padding: 48px 40px; width: 100%; max-width: 380px; box-shadow: 0 12px 48px rgba(0,0,0,0.3); text-align: center; }
.login-logo { font-size: 36px; margin-bottom: 16px; }
.login-title { font-size: 20px; font-weight: 700; color: #2C1F0E; letter-spacing: 2px; margin-bottom: 6px; }
.login-sub { font-size: 12px; color: #9a8c7a; letter-spacing: 3px; margin-bottom: 32px; }
.login-input { width: 100%; padding: 12px 16px; font-size: 15px; border: 1.5px solid #D8CEBD; border-radius: 8px; background: #FDFAF6; color: #2C1F0E; outline: none; font-family: inherit; text-align: center; letter-spacing: 4px; }
.login-input:focus { border-color: #C9A96E; }
.login-input.error { border-color: #c0392b; }
.login-btn { width: 100%; margin-top: 16px; background: #8B5E3C; color: #F7F3EE; border: none; border-radius: 8px; padding: 12px; font-size: 15px; cursor: pointer; font-weight: 600; letter-spacing: 2px; font-family: inherit; }
.login-btn:hover { background: #7a5232; }
.login-btn:disabled { background: #c4a882; cursor: not-allowed; }
.login-error { color: #c0392b; font-size: 12px; margin-top: 10px; min-height: 18px; }
#app-screen { display: none; }

/* ── Header ── */
header { background: #2C1F0E; padding: 20px 40px; display: flex; align-items: center; justify-content: space-between; }
.header-sub { color: #C9A96E; font-size: 11px; letter-spacing: 6px; text-transform: uppercase; margin-bottom: 4px; }
.header-title { color: #F7F3EE; font-size: 22px; font-weight: 700; letter-spacing: 2px; }
.header-right { display: flex; align-items: center; gap: 16px; }
.header-count { color: #C9A96E; font-size: 13px; }
.header-count strong { color: #F7F3EE; font-size: 16px; }
.logout-btn { background: none; border: 1px solid #C9A96E; color: #C9A96E; border-radius: 6px; padding: 6px 14px; font-size: 12px; cursor: pointer; font-family: inherit; letter-spacing: 1px; }
.logout-btn:hover { background: rgba(201,169,110,0.15); }

/* ── Tabs ── */
.tabs { background: #3a2a14; display: flex; padding: 0 40px; }
.tab-btn { background: none; border: none; color: #9a8c7a; padding: 14px 24px; font-size: 14px; cursor: pointer; font-family: inherit; letter-spacing: 1px; border-bottom: 3px solid transparent; transition: all 0.2s; }
.tab-btn:hover { color: #C9A96E; }
.tab-btn.active { color: #C9A96E; border-bottom-color: #C9A96E; }

/* ── Tab Panels ── */
.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* ── Main layout ── */
main { max-width: 1300px; margin: 0 auto; padding: 32px 24px; }

/* ── Card ── */
.card { background: #fff; border-radius: 12px; padding: 24px 28px; margin-bottom: 24px; box-shadow: 0 2px 16px rgba(44,31,14,0.07); border: 1px solid #E8DED0; }
.card-title { font-size: 15px; font-weight: 700; color: #8B5E3C; margin-bottom: 18px; letter-spacing: 1px; display: flex; align-items: center; gap: 8px; }
.card-title-bar { display: inline-block; width: 4px; height: 16px; background: #C9A96E; border-radius: 2px; flex-shrink: 0; }

/* ── Form ── */
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 16px; }
.form-group label { display: block; font-size: 12px; color: #7a6a5a; margin-bottom: 5px; letter-spacing: 0.5px; }
.form-group label span { color: #aaa; margin-left: 4px; font-size: 11px; }
.form-group input, .form-group textarea, .form-group select { width: 100%; padding: 10px 14px; font-size: 14px; border: 1.5px solid #D8CEBD; border-radius: 8px; background: #FDFAF6; color: #2C1F0E; outline: none; font-family: inherit; }
.form-group input:focus, .form-group textarea:focus, .form-group select:focus { border-color: #C9A96E; }
.form-group input.error, .form-group select.error { border-color: #c0392b; }
.form-group textarea { resize: vertical; min-height: 72px; }
.form-error { color: #c0392b; font-size: 11px; margin-top: 4px; }
.change-notice { background: #FFF8EE; border: 1px solid #C9A96E; border-radius: 8px; padding: 10px 16px; margin-bottom: 16px; font-size: 12px; color: #8B5E3C; display: flex; align-items: center; gap: 8px; }
.btn-row { display: flex; gap: 10px; margin-top: 18px; flex-wrap: wrap; }
.btn-primary { background: #8B5E3C; color: #F7F3EE; border: none; border-radius: 8px; padding: 10px 24px; font-size: 14px; cursor: pointer; font-weight: 600; letter-spacing: 1px; font-family: inherit; }
.btn-primary:hover { background: #7a5232; }
.btn-primary:disabled { background: #c4a882; cursor: not-allowed; }
.btn-secondary { background: #E8DED0; color: #5a4a3a; border: none; border-radius: 8px; padding: 10px 18px; font-size: 14px; cursor: pointer; font-family: inherit; }
.btn-secondary:hover { background: #d8cec0; }
.btn-danger { background: none; border: 1.5px solid #e0b0a0; color: #c0392b; border-radius: 8px; padding: 10px 18px; font-size: 14px; cursor: pointer; font-family: inherit; }
.btn-danger:hover { background: #fdecea; }

/* ── Search ── */
.search-row { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.search-wrap { position: relative; flex: 1; min-width: 200px; max-width: 360px; }
.search-icon { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: #9a8c7a; pointer-events: none; }
.search-wrap input { width: 100%; padding: 10px 14px 10px 36px; font-size: 14px; border: 1.5px solid #D8CEBD; border-radius: 8px; background: #fff; color: #2C1F0E; outline: none; font-family: inherit; }
.search-wrap input:focus { border-color: #C9A96E; }
.search-count { color: #9a8c7a; font-size: 13px; }
.search-type-group { display: flex; gap: 6px; }
.search-type-btn { background: #E8DED0; border: none; border-radius: 6px; padding: 6px 12px; font-size: 12px; cursor: pointer; font-family: inherit; color: #5a4a3a; }
.search-type-btn.active { background: #8B5E3C; color: #fff; }

/* ── Table ── */
.table-wrap { background: #fff; border-radius: 12px; overflow: auto; box-shadow: 0 2px 16px rgba(44,31,14,0.07); border: 1px solid #E8DED0; }
table { width: 100%; border-collapse: collapse; font-size: 14px; table-layout: fixed; }
thead tr { background: #2C1F0E; }
th { padding: 13px 14px; color: #C9A96E; font-weight: 600; text-align: left; font-size: 12px; letter-spacing: 0.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; position: relative; user-select: none; }
th.center { text-align: center; }
th.sortable { cursor: pointer; }
th.sortable:hover { color: #e8c87a; }
.sort-icon { margin-left: 4px; color: #9a8c7a; }
.sort-icon.active { color: #C9A96E; }
.resize-handle { position: absolute; right: 0; top: 0; bottom: 0; width: 6px; cursor: col-resize; background: transparent; z-index: 10; }
.resize-handle:hover, .resize-handle.dragging { background: rgba(201,169,110,0.5); }
tbody tr { transition: background 0.15s; }
tbody tr:nth-child(even) { background: #FDFAF6; }
tbody tr:nth-child(odd) { background: #fff; }
tbody tr.editing { background: #FFF8EE !important; border-left: 3px solid #C9A96E; }
tbody tr:hover { background: #FFF5E8 !important; }
td { padding: 12px 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.empty-row td { text-align: center; padding: 48px; color: #aaa; }
.btn-edit { background: none; border: 1.5px solid #C9A96E; color: #8B5E3C; border-radius: 6px; padding: 4px 12px; font-size: 12px; cursor: pointer; margin-right: 4px; font-family: inherit; }
.btn-edit:hover { background: #FFF0D6; }
.btn-del { background: none; border: 1.5px solid #e0b0a0; color: #c0392b; border-radius: 6px; padding: 4px 12px; font-size: 12px; cursor: pointer; font-family: inherit; }
.btn-del:hover { background: #fdecea; }
.btn-view { background: none; border: 1.5px solid #7a9a8a; color: #4a7c59; border-radius: 6px; padding: 4px 12px; font-size: 12px; cursor: pointer; margin-right: 4px; font-family: inherit; }
.btn-view:hover { background: #eaf5ed; }

/* ── Badge ── */
.badge { display: inline-block; padding: 3px 10px; border-radius: 20px; font-size: 12px; font-weight: 600; }
.badge.high { background: #eaf5ed; color: #27ae60; }
.badge.mid { background: #fef9e7; color: #c49a0f; }
.badge.low { background: #fdecea; color: #c0392b; }

/* ── Price cell ── */
.price-cell { display: flex; align-items: center; gap: 6px; }
.price-sym { color: #7a6a5a; font-size: 12px; }
.price-diff { font-size: 11px; font-weight: 600; }
.price-diff.up { color: #c0392b; }
.price-diff.down { color: #27ae60; }
.hist-btn { background: #F0F0F0; border: 1px solid #ddd; border-radius: 5px; padding: 2px 8px; font-size: 11px; cursor: pointer; color: #aaa; font-family: inherit; white-space: nowrap; }
.hist-btn.has-history { background: #FFF0D6; border-color: #C9A96E; color: #8B5E3C; }

/* ── Stats ── */
.stats-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; margin-top: 20px; }
.stat-card { background: #fff; border-radius: 10px; padding: 16px 20px; border: 1px solid #E8DED0; }
.stat-label { font-size: 11px; color: #9a8c7a; letter-spacing: 1px; margin-bottom: 4px; }
.stat-value { font-size: 18px; font-weight: 700; color: #8B5E3C; }
.footer-note { margin-top: 20px; font-size: 11px; color: #b0a090; text-align: center; }

/* ── Toast ── */
#toast { position: fixed; top: 24px; right: 24px; z-index: 9999; padding: 12px 24px; border-radius: 8px; font-size: 15px; box-shadow: 0 4px 20px rgba(0,0,0,0.2); color: #fff; display: none; font-family: inherit; }
#toast.show { display: block; }
#toast.success { background: #4a7c59; }
#toast.warn { background: #c0392b; }

/* ── Modal ── */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.45); z-index: 900; display: none; align-items: center; justify-content: center; }
.modal-overlay.show { display: flex; }
.modal-box { background: #fff; border-radius: 14px; padding: 28px 32px; min-width: 360px; max-width: 600px; width: 95%; box-shadow: 0 8px 40px rgba(44,31,14,0.2); max-height: 88vh; display: flex; flex-direction: column; }
.modal-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.modal-sub { font-size: 11px; color: #9a8c7a; letter-spacing: 2px; margin-bottom: 3px; }
.modal-name { font-size: 18px; font-weight: 700; color: #2C1F0E; }
.btn-close { background: #E8DED0; border: none; border-radius: 8px; width: 30px; height: 30px; cursor: pointer; font-size: 14px; color: #5a4a3a; }
.modal-body { border-top: 1px solid #E8DED0; padding-top: 14px; overflow-y: auto; flex: 1; }
.modal-footer-note { margin-top: 12px; font-size: 11px; color: #b0a090; text-align: center; }

/* History items */
.hist-item { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px; margin-bottom: 7px; border-radius: 8px; background: #FDFAF6; border: 1.5px solid #E8DED0; }
.hist-item.latest { background: #FFF8EE; border-color: #C9A96E; }
.hist-price { font-size: 15px; font-weight: 700; color: #2C1F0E; }
.hist-badge { font-size: 10px; background: #C9A96E; color: #fff; border-radius: 4px; padding: 2px 6px; margin-left: 6px; }
.hist-diff { font-size: 11px; font-weight: 600; margin-left: 8px; }
.hist-date { font-size: 11px; color: #9a8c7a; margin-top: 2px; }
.hist-seq { font-size: 11px; color: #b0a090; }

/* ── Prescription panel ── */
.rx-layout { display: grid; grid-template-columns: 320px 1fr; gap: 20px; align-items: start; }
@media(max-width:900px){ .rx-layout { grid-template-columns: 1fr; } }

/* Customer list panel */
.customer-list { background: #fff; border-radius: 12px; border: 1px solid #E8DED0; overflow: hidden; max-height: 600px; display: flex; flex-direction: column; }
.customer-list-header { padding: 14px 16px; background: #2C1F0E; color: #C9A96E; font-size: 12px; font-weight: 600; letter-spacing: 1px; }
.customer-list-search { padding: 10px 12px; border-bottom: 1px solid #E8DED0; }
.customer-list-search input { width: 100%; padding: 8px 12px; font-size: 13px; border: 1.5px solid #D8CEBD; border-radius: 6px; background: #FDFAF6; outline: none; font-family: inherit; }
.customer-list-search input:focus { border-color: #C9A96E; }
.customer-items { overflow-y: auto; flex: 1; }
.customer-item { padding: 12px 16px; border-bottom: 1px solid #F0E8DC; cursor: pointer; transition: background 0.15s; }
.customer-item:hover { background: #FFF5E8; }
.customer-item.selected { background: #FFF8EE; border-left: 3px solid #C9A96E; }
.customer-item-name { font-weight: 600; font-size: 14px; color: #2C1F0E; }
.customer-item-phone { font-size: 12px; color: #9a8c7a; margin-top: 2px; }
.customer-item-notes { font-size: 11px; color: #b0a090; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.customer-add-btn { padding: 12px 16px; border-top: 1px solid #E8DED0; }
.customer-add-btn button { width: 100%; background: #F7F3EE; border: 1.5px dashed #C9A96E; color: #8B5E3C; border-radius: 8px; padding: 8px; font-size: 13px; cursor: pointer; font-family: inherit; }
.customer-add-btn button:hover { background: #FFF0D6; }

/* Prescription right panel */
.rx-right { display: flex; flex-direction: column; gap: 16px; }
.rx-customer-info { background: #fff; border-radius: 12px; border: 1px solid #E8DED0; padding: 18px 20px; }
.rx-customer-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.rx-customer-title { font-size: 13px; font-weight: 700; color: #8B5E3C; letter-spacing: 1px; }
.rx-customer-data { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; font-size: 13px; }
.rx-customer-field { color: #9a8c7a; font-size: 11px; }
.rx-customer-val { color: #2C1F0E; font-size: 13px; font-weight: 500; }

/* Rx items editor */
.rx-editor { background: #fff; border-radius: 12px; border: 1px solid #E8DED0; padding: 18px 20px; }
.rx-items-list { margin-bottom: 14px; }
.rx-item-row { display: grid; grid-template-columns: 1fr 60px 60px 60px 60px 36px; gap: 8px; align-items: center; margin-bottom: 8px; }
.rx-item-row input, .rx-item-row select { padding: 7px 10px; font-size: 13px; border: 1.5px solid #D8CEBD; border-radius: 6px; background: #FDFAF6; color: #2C1F0E; outline: none; font-family: inherit; width: 100%; }
.rx-item-row input:focus, .rx-item-row select:focus { border-color: #C9A96E; }
.rx-item-row .item-price { font-size: 13px; color: #8B5E3C; font-weight: 600; text-align: right; }
.rx-item-del { background: none; border: none; color: #c0392b; cursor: pointer; font-size: 16px; padding: 0; line-height: 1; }
.rx-header-row { display: grid; grid-template-columns: 1fr 60px 60px 60px 60px 36px; gap: 8px; margin-bottom: 6px; }
.rx-header-row span { font-size: 11px; color: #9a8c7a; }
.rx-add-item { background: #F7F3EE; border: 1.5px dashed #C9A96E; color: #8B5E3C; border-radius: 8px; padding: 7px 16px; font-size: 13px; cursor: pointer; font-family: inherit; }
.rx-add-item:hover { background: #FFF0D6; }
.rx-total { display: flex; justify-content: flex-end; align-items: center; gap: 16px; padding-top: 12px; border-top: 1px solid #E8DED0; margin-top: 12px; }
.rx-total-label { font-size: 13px; color: #7a6a5a; }
.rx-total-price { font-size: 22px; font-weight: 700; color: #8B5E3C; }
.rx-save-row { display: flex; gap: 10px; justify-content: flex-end; margin-top: 12px; }

/* Prescription history */
.rx-history { background: #fff; border-radius: 12px; border: 1px solid #E8DED0; }
.rx-history-header { padding: 14px 20px; background: #2C1F0E; border-radius: 12px 12px 0 0; color: #C9A96E; font-size: 12px; font-weight: 600; letter-spacing: 1px; }
.rx-record { border-bottom: 1px solid #F0E8DC; padding: 14px 20px; transition: background 0.2s; }
.rx-record.editing { background: #FFF8EE; border-left: 3px solid #C9A96E; }
.rx-record:last-child { border-bottom: none; }
.rx-record-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.rx-record-date { font-size: 12px; color: #9a8c7a; }
.rx-record-total { font-size: 15px; font-weight: 700; color: #8B5E3C; }
.rx-record-items { display: flex; flex-wrap: wrap; gap: 6px; }
.rx-record-item { background: #FFF8EE; border: 1px solid #E8DED0; border-radius: 6px; padding: 3px 10px; font-size: 12px; color: #5a4a3a; }
.rx-empty { padding: 32px; text-align: center; color: #aaa; font-size: 13px; }

/* Autocomplete */
.autocomplete-wrap { position: relative; }
.autocomplete-list { position: absolute; top: 100%; left: 0; right: 0; background: #fff; border: 1.5px solid #C9A96E; border-radius: 8px; box-shadow: 0 4px 16px rgba(0,0,0,0.1); z-index: 100; max-height: 180px; overflow-y: auto; }
.autocomplete-item { padding: 8px 12px; cursor: pointer; font-size: 13px; }
.autocomplete-item:hover, .autocomplete-item.active { background: #FFF8EE; }
.autocomplete-item .ac-price { float: right; color: #8B5E3C; font-size: 12px; }

body.col-resizing { cursor: col-resize !important; user-select: none !important; }
body.col-resizing * { cursor: col-resize !important; }

@media(max-width:600px){
  header { padding: 16px 16px; }
  .rx-subtotal-inner { display: block; }
  .tabs { padding: 0 16px; }
  main { padding: 16px 12px; }
  .stats-grid { grid-template-columns: 1fr; }
  .rx-header-row { display: none; }
  .rx-item-row {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr 1fr 32px;
    grid-template-rows: auto auto;
    gap: 6px;
    background: #FDFAF6;
    border: 1px solid #E8DED0;
    border-radius: 8px;
    padding: 10px;
    margin-bottom: 10px;
  }
  .rx-item-row .rx-herb-wrap { grid-column: 1 / -1; }
  .rx-item-del { grid-column: 5; grid-row: 2; font-size: 18px; justify-self: center; }
  .rx-dose-wrap { display: flex; flex-direction: column; gap: 3px; }
  .rx-dose-label { font-size: 10px; color: #9a8c7a; text-align: center; }
  .rx-dose-wrap input { text-align: center; padding: 6px 4px; font-size: 13px; }
}

/* ── Rx Type Selector ── */
.rx-type-row { display: flex; align-items: center; gap: 16px; margin-bottom: 16px; }
.rx-type-label { font-size: 13px; color: #7a6a5a; font-weight: 600; white-space: nowrap; }
.rx-type-options { display: flex; gap: 10px; flex-wrap: wrap; }
.rx-type-opt { cursor: pointer; }
.rx-type-opt input[type="radio"] { display: none; }
.rx-type-tag {
  display: inline-block; padding: 5px 16px; border-radius: 20px;
  font-size: 14px; font-weight: 700; cursor: pointer;
  border: 2px solid transparent; transition: all 0.15s; letter-spacing: 2px;
  opacity: 0.45;
}
.rx-type-opt input[type="radio"]:checked + .rx-type-tag { opacity: 1; border-color: currentColor; transform: scale(1.08); }

/* Type colors */
.type-水 { background: #dbeeff; color: #1a6eb5; }
.type-粉 { background: #fdf0d5; color: #a0620a; }
.type-丸 { background: #e8f5e9; color: #2e7d32; }
.type-洗 { background: #ede7f6; color: #5e35b1; }
.type-膏 { background: #fce4ec; color: #c62828; }

/* History record type badge */
.rx-type-badge {
  display: inline-block; padding: 2px 10px; border-radius: 12px;
  font-size: 12px; font-weight: 700; letter-spacing: 1px; margin-right: 6px;
}
.badge-水 { background: #dbeeff; color: #1a6eb5; }
.badge-粉 { background: #fdf0d5; color: #a0620a; }
.badge-丸 { background: #e8f5e9; color: #2e7d32; }
.badge-洗 { background: #ede7f6; color: #5e35b1; }
.badge-膏 { background: #fce4ec; color: #c62828; }

/* History record tinted by type */
.rx-record.type-水 { border-left: 4px solid #1a6eb5; }
.rx-record.type-粉 { border-left: 4px solid #a0620a; }
.rx-record.type-丸 { border-left: 4px solid #2e7d32; }
.rx-record.type-洗 { border-left: 4px solid #5e35b1; }
.rx-record.type-膏 { border-left: 4px solid #c62828; }
