:root {
  --bg: var(--tg-theme-bg-color, #f5f5f5);
  --bg2: var(--tg-theme-secondary-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #1a1a1a);
  --hint: var(--tg-theme-hint-color, #6e6e6e);
  --accent: var(--tg-theme-button-color, #333333);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: #c0392b;
  --warn: #b9770e;
  --ok: #1e8449;
  --border: rgba(0,0,0,0.08);
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0,0,0,0.06);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
}

#app { max-width: 900px; margin: 0 auto; min-height: 100%; display: flex; flex-direction: column; }
.top-bar {
  position: sticky; top: 0; z-index: 10;
  background: var(--bg2); border-bottom: 1px solid var(--border);
  padding: 12px 16px; display: flex; justify-content: space-between; align-items: center;
}
.brand { font-weight: 700; font-size: 17px; }
.badges { display: flex; gap: 6px; }
.badge {
  font-size: 11px; padding: 3px 7px; border-radius: 20px; background: var(--accent);
  color: var(--accent-text); min-width: 20px; text-align: center;
}
.badge.warn { background: var(--warn); }
.badge.danger { background: var(--danger); }

main { flex: 1; padding: 12px 14px 90px; overflow-y: auto; }
.bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0; max-width: 900px; margin: 0 auto;
  background: var(--bg2); border-top: 1px solid var(--border);
  display: grid; grid-template-columns: repeat(5, 1fr); padding: 6px 0 14px;
}
.bottom-nav button {
  background: none; border: none; color: var(--hint); font-size: 12px; padding: 6px 2px;
  display: flex; flex-direction: column; align-items: center; gap: 4px; cursor: pointer;
}
.bottom-nav button.active { color: var(--accent); font-weight: 600; }

.section { display: none; animation: fade .15s ease; }
.section.active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; } }

.card {
  background: var(--bg2); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px; margin-bottom: 12px;
}
.card h3 { margin: 0 0 10px; font-size: 15px; font-weight: 700; }
.card h4 { margin: 12px 0 8px; font-size: 14px; color: var(--hint); font-weight: 600; }

.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 10px; }
.row:last-child { margin-bottom: 0; }
.grow { flex: 1; }

input, select, textarea, button.btn {
  font: inherit; padding: 10px 12px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
textarea { width: 100%; min-height: 80px; resize: vertical; }
button.btn {
  background: var(--accent); color: var(--accent-text); border-color: transparent;
  cursor: pointer; font-weight: 500;
}
button.btn:disabled { opacity: .5; cursor: default; }
button.btn.secondary { background: transparent; color: var(--accent); border: 1px solid var(--border); }
button.btn.danger { background: var(--danger); }
button.btn.warn { background: var(--warn); }
button.chip {
  font: inherit; font-size: 13px; padding: 6px 10px; border-radius: 20px;
  border: 1px solid var(--border); background: var(--bg2); color: var(--text); cursor: pointer;
}
button.chip.active { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }

.kpi-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.kpi { background: var(--bg); border-radius: 8px; padding: 12px; }
.kpi .label { font-size: 12px; color: var(--hint); margin-bottom: 4px; }
.kpi .value { font-size: 20px; font-weight: 700; }

.bar-list { display: flex; flex-direction: column; gap: 6px; }
.bar-row { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.bar-row .name { width: 110px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-row .bar-wrap { flex: 1; background: var(--bg); border-radius: 4px; height: 8px; }
.bar-row .bar { background: var(--accent); height: 100%; border-radius: 4px; min-width: 2px; }
.bar-row .count { width: 30px; text-align: right; }

.booking-card {
  border-left: 3px solid var(--accent); padding-left: 12px; margin-bottom: 14px;
}
.booking-card.unsynced { border-color: var(--warn); }
.booking-card.cancelled { border-color: var(--danger); opacity: .75; }
.booking-card .line { display: flex; justify-content: space-between; gap: 8px; margin-bottom: 4px; }
.booking-card .meta { color: var(--hint); font-size: 13px; }
.booking-card .actions { margin-top: 8px; display: flex; gap: 8px; flex-wrap: wrap; }

.case-card, .customer-card, .task-row, .op-row {
  border-bottom: 1px solid var(--border); padding: 12px 0;
}
.case-card:last-child, .customer-card:last-child, .task-row:last-child, .op-row:last-child { border-bottom: none; }
.case-card pre { white-space: pre-wrap; font: inherit; margin: 6px 0; }
.case-card .reply-box { margin-top: 8px; display: flex; gap: 8px; }

.chat-thread { max-height: 55vh; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; margin: 10px 0; }
.msg { padding: 10px 12px; border-radius: 14px; max-width: 85%; font-size: 14px; }
.msg.user { align-self: flex-end; background: var(--accent); color: var(--accent-text); }
.msg.bot, .msg.manager, .msg.event { align-self: flex-start; background: var(--bg); border: 1px solid var(--border); }
.msg .meta { font-size: 11px; color: var(--hint); margin-top: 4px; }

.timeline { display: flex; flex-direction: column; gap: 10px; }
.timeline .entry { padding: 10px; background: var(--bg); border-radius: 8px; font-size: 13px; }
.timeline .entry .kind { font-weight: 600; font-size: 11px; color: var(--hint); text-transform: uppercase; }

.rule-item {
  display: flex; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--border);
}
.rule-item:last-child { border-bottom: none; }
.rule-item .num { color: var(--hint); font-size: 13px; width: 24px; }
.rule-item .text { flex: 1; }

.json-editor { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; }

.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 100;
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.modal {
  background: var(--bg2); border-radius: var(--radius); width: 100%; max-width: 480px;
  max-height: 90vh; overflow-y: auto; box-shadow: 0 10px 40px rgba(0,0,0,.2);
}
.modal header { padding: 14px 16px; border-bottom: 1px solid var(--border); font-weight: 700; }
.modal .body { padding: 14px 16px; }
.modal footer { padding: 12px 16px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 10px; }

.toast {
  position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%) translateY(120px);
  background: var(--text); color: var(--bg2); padding: 10px 18px; border-radius: 20px;
  font-size: 14px; opacity: 0; transition: all .2s ease; z-index: 200; pointer-events: none;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.empty { color: var(--hint); text-align: center; padding: 30px 10px; font-size: 14px; }
.status-pill {
  display: inline-block; font-size: 11px; padding: 2px 8px; border-radius: 12px;
  background: var(--bg); color: var(--hint); border: 1px solid var(--border);
}
.status-pill.active { background: #d4edda; color: #155724; border-color: #c3e6cb; }
.status-pill.cancelled { background: #f8d7da; color: #721c24; border-color: #f5c6cb; }
.status-pill.pending { background: #fff3cd; color: #856404; border-color: #ffeeba; }

.login-overlay {
  position: fixed; inset: 0; background: var(--bg); z-index: 300;
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.login-box {
  width: 100%; max-width: 340px; background: var(--bg2); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 20px; display: flex; flex-direction: column; gap: 12px;
}
.login-box h3 { margin: 0; }
.login-box input { width: 100%; }

@media (min-width: 600px) {
  .kpi-grid { grid-template-columns: repeat(4, 1fr); }
}
