:root {
  --bg: #f6f7fb; --card: #fff; --text: #1f2937; --muted: #6b7280; --line: #e5e7eb;
  --primary: #6366f1; --primary-d: #4f46e5; --danger: #ef4444; --ok: #10b981; --warn: #f59e0b;
  --radius: 12px; --shadow: 0 1px 3px rgba(0,0,0,.06), 0 4px 14px rgba(0,0,0,.05);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-family: 'Sarabun', system-ui, sans-serif; background: var(--bg); color: var(--text); font-size: 15px; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--primary); }

.app { display: flex; min-height: 100vh; }
.sidebar { width: 210px; background: #1e1b4b; color: #e0e7ff; padding: 18px 12px; position: sticky; top: 0; height: 100vh; flex-shrink: 0; }
.brand { font-size: 22px; font-weight: 700; display: flex; gap: 10px; align-items: center; margin: 0 8px 24px; line-height: 1.1; }
.brand small { font-weight: 500; opacity: .7; font-size: 14px; }
#nav a { display: flex; gap: 10px; align-items: center; padding: 10px 12px; border-radius: 10px; color: inherit; text-decoration: none; margin-bottom: 4px; }
#nav a:hover { background: rgba(255,255,255,.08); }
#nav a.active { background: var(--primary); color: #fff; }
#main { flex: 1; padding: 20px 24px 100px; min-width: 0; }

h1 { font-size: 24px; margin: 0 0 16px; }
h2 { font-size: 18px; margin: 18px 0 10px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.btn { border: 1px solid var(--line); background: #fff; padding: 8px 14px; border-radius: 10px; cursor: pointer; white-space: nowrap; }
.btn:hover { background: #f3f4f6; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-d); }
.btn.danger { color: var(--danger); border-color: #fecaca; }
.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.sm { padding: 4px 10px; font-size: 13px; }
.btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-group { display: inline-flex; }
.btn-group .btn { border-radius: 0; margin-left: -1px; }
.btn-group .btn:first-child { border-radius: 10px 0 0 10px; }
.btn-group .btn:last-child { border-radius: 0 10px 10px 0; }

.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 12px; }
.toolbar .title { font-size: 18px; font-weight: 600; min-width: 180px; }
.spacer { flex: 1; }
.filters { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.filters select { padding: 6px 10px; border: 1px solid var(--line); border-radius: 10px; background: #fff; max-width: 170px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 4px 12px 4px 4px; border-radius: 99px; background: #fff; border: 2px solid var(--line); cursor: pointer; }
.chip.all { padding-left: 12px; }
.chip.active { border-color: var(--c, var(--primary)); background: color-mix(in srgb, var(--c, var(--primary)) 12%, #fff); }

.avatar { width: 28px; height: 28px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; color: #fff; font-weight: 700; font-size: 13px; background-size: cover; background-position: center; flex-shrink: 0; }
.avatar.lg { width: 64px; height: 64px; font-size: 24px; }

/* Event card */
.ev { --c: var(--primary); border-left: 4px solid var(--c); background: color-mix(in srgb, var(--c) 10%, #fff); border-radius: 8px; padding: 6px 8px; cursor: pointer; font-size: 13px; line-height: 1.35; margin-bottom: 6px; position: relative; overflow: hidden; }
.ev:hover { filter: brightness(.97); }
.ev .t { font-weight: 700; }
.ev .s { font-weight: 600; }
.ev .m { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ev.cancelled { opacity: .5; text-decoration: line-through; }
.ev.conflict { outline: 2px solid var(--danger); }
.ev .flag { position: absolute; top: 4px; right: 6px; font-size: 12px; }
.trip { font-size: 11.5px; color: #374151; margin-top: 2px; }

.badge { display: inline-block; padding: 1px 8px; border-radius: 99px; font-size: 12px; font-weight: 600; }
.b-wait { background: #fef3c7; color: #92400e; }
.b-done { background: #d1fae5; color: #065f46; }
.b-red { background: #fee2e2; color: #991b1b; }
.b-gray { background: #f3f4f6; color: #4b5563; }

/* Week view */
.week { display: grid; grid-template-columns: repeat(7, minmax(130px, 1fr)); gap: 8px; overflow-x: auto; }
.week .col { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); padding: 8px; min-height: 260px; }
.week .col h4 { margin: 0 0 8px; font-size: 13px; text-align: center; color: var(--muted); cursor: pointer; }
.week .col h4 b { display: block; font-size: 20px; color: var(--text); }
.week .col.today h4 b { color: #fff; background: var(--primary); border-radius: 50%; width: 34px; height: 34px; line-height: 34px; margin: 0 auto; }

/* Day view */
.day { position: relative; background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.day .hour { height: 56px; border-top: 1px solid var(--line); position: relative; }
.day .hour span { position: absolute; left: 8px; top: 2px; font-size: 12px; color: var(--muted); }
.day .events { position: absolute; left: 60px; right: 8px; top: 0; bottom: 0; }
.day .ev { position: absolute; margin: 0; }
.now-line { position: absolute; left: 50px; right: 0; border-top: 2px solid var(--danger); z-index: 2; }

/* Month view */
.month { display: grid; grid-template-columns: repeat(7, 1fr); background: var(--line); gap: 1px; border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.month .dow { background: #f9fafb; text-align: center; padding: 6px; font-size: 13px; font-weight: 600; color: var(--muted); }
.month .cell { background: #fff; min-height: 104px; padding: 4px; cursor: pointer; min-width: 0; }
.month .cell.other { background: #fafafa; color: #c0c4cc; }
.month .cell .n { font-size: 13px; font-weight: 600; margin-bottom: 2px; }
.month .cell.today .n { color: #fff; background: var(--primary); border-radius: 99px; display: inline-block; padding: 0 7px; }
.mini { --c: var(--primary); font-size: 11.5px; background: color-mix(in srgb, var(--c) 15%, #fff); border-left: 3px solid var(--c); border-radius: 4px; padding: 1px 4px; margin-bottom: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mini.cancelled { text-decoration: line-through; opacity: .5; }
.mini.conflict { background: #fee2e2; }
.more { font-size: 11px; color: var(--muted); }

/* List view */
.list-day { margin-bottom: 14px; }
.list-day h3 { font-size: 15px; margin: 0 0 6px; color: var(--muted); }
.list-day .ev { display: flex; gap: 12px; align-items: flex-start; padding: 10px 12px; font-size: 14px; }
.list-day .ev .time { font-weight: 700; min-width: 92px; }

/* Entity lists */
.grid-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.entity { display: flex; gap: 12px; align-items: flex-start; }
.entity .info { flex: 1; min-width: 0; }
.entity .info b { font-size: 16px; }
.entity .acts { display: flex; gap: 6px; margin-top: 10px; }
.swatch { width: 18px; height: 18px; border-radius: 6px; display: inline-block; vertical-align: middle; }

/* Pickup page */
.leg { display: flex; gap: 12px; align-items: center; padding: 12px; border-radius: var(--radius); background: #fff; box-shadow: var(--shadow); margin-bottom: 8px; border-left: 5px solid var(--c, var(--primary)); }
.leg .time { font-size: 18px; font-weight: 700; min-width: 60px; }
.leg .body { flex: 1; min-width: 0; }
.leg .who { font-weight: 600; }
.summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; margin-bottom: 14px; }
.summary .card { text-align: center; padding: 12px; }
.summary .card b { font-size: 26px; display: block; }

/* Forms / modal */
.modal-bg { position: fixed; inset: 0; background: rgba(15,23,42,.45); display: flex; align-items: flex-start; justify-content: center; z-index: 50; overflow-y: auto; padding: 30px 12px; }
.modal { background: #fff; border-radius: 16px; width: 100%; max-width: 680px; box-shadow: 0 20px 50px rgba(0,0,0,.25); }
.modal.sm { max-width: 440px; }
.modal header { display: flex; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.modal header h3 { margin: 0; flex: 1; font-size: 18px; }
.modal header .x { border: 0; background: none; font-size: 22px; cursor: pointer; color: var(--muted); }
.modal .body { padding: 16px 18px; }
.modal footer { display: flex; gap: 8px; justify-content: flex-end; padding: 12px 18px; border-top: 1px solid var(--line); flex-wrap: wrap; }
.form { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 14px; }
.form .full { grid-column: 1 / -1; }
.form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 600; color: #374151; }
.form input, .form select, .form textarea { font-weight: 400; padding: 8px 10px; border: 1px solid #d1d5db; border-radius: 10px; background: #fff; width: 100%; font-size: 15px; }
.form input[type=color] { padding: 2px; height: 40px; }
.form textarea { min-height: 64px; resize: vertical; }
.form fieldset { grid-column: 1 / -1; border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; margin: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 10px 14px; }
.form legend { font-weight: 700; padding: 0 6px; }
.days { display: flex; gap: 6px; flex-wrap: wrap; }
.days label { flex-direction: row; align-items: center; gap: 4px; border: 1px solid var(--line); border-radius: 8px; padding: 4px 10px; cursor: pointer; font-weight: 500; }
.days input { width: auto; }
.tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.tag { background: #eef2ff; color: #3730a3; border-radius: 99px; padding: 2px 10px; font-size: 13px; }
.tag button { border: 0; background: none; cursor: pointer; color: inherit; padding: 0 0 0 4px; }
.warnbox { background: #fef2f2; border: 1px solid #fecaca; color: #991b1b; border-radius: 10px; padding: 10px 12px; font-size: 14px; }
.infobox { background: #eef2ff; border-radius: 10px; padding: 10px 12px; font-size: 14px; }
.kv { display: grid; grid-template-columns: 110px 1fr; gap: 6px 10px; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-word; }
.scope-opt { display: block; width: 100%; text-align: left; margin-bottom: 8px; padding: 12px 14px; }
.scope-opt small { display: block; color: var(--muted); }

.fab { position: fixed; right: 24px; bottom: 24px; width: 58px; height: 58px; border-radius: 50%; border: 0; background: var(--primary); color: #fff; font-size: 30px; box-shadow: 0 8px 20px rgba(79,70,229,.4); cursor: pointer; z-index: 40; }
.toast { position: fixed; left: 50%; bottom: 30px; transform: translateX(-50%) translateY(120px); background: #111827; color: #fff; padding: 10px 18px; border-radius: 10px; transition: transform .25s; z-index: 60; max-width: 90vw; }
.toast.show { transform: translateX(-50%) translateY(0); }
.empty { text-align: center; padding: 40px 16px; color: var(--muted); }

/* Tablet */
@media (max-width: 1024px) {
  .sidebar { width: 72px; padding: 14px 8px; }
  .brand span, #nav a span:not(.ic) { display: none; }
  .brand { justify-content: center; margin: 0 0 20px; }
  #nav a { justify-content: center; font-size: 20px; }
}
/* Phone */
@media (max-width: 680px) {
  .app { flex-direction: column; }
  .sidebar { position: fixed; bottom: 0; top: auto; left: 0; right: 0; width: 100%; height: auto; padding: 4px 4px calc(4px + env(safe-area-inset-bottom)); z-index: 45; }
  .brand { display: none; }
  #nav { display: flex; justify-content: space-around; }
  #nav a { flex-direction: column; gap: 0; padding: 6px 4px; font-size: 20px; margin: 0; flex: 1; }
  #nav a span:not(.ic) { display: block; font-size: 11px; }
  #main { padding: 14px 12px 120px; }
  .fab { bottom: 84px; right: 16px; }
  .week { grid-template-columns: 1fr; }
  .week .col { min-height: 0; }
  .week .col h4 { text-align: left; display: flex; gap: 8px; align-items: center; }
  .week .col h4 b { display: inline-block; font-size: 16px; }
  .week .col.today h4 b { width: 28px; height: 28px; line-height: 28px; text-align: center; }
  .month .cell { min-height: 64px; }
  .month .mini { font-size: 0; height: 6px; padding: 0; border-left-width: 0; background: var(--c); }
  .form, .form fieldset { grid-template-columns: 1fr; }
  .filters select { flex: 1 1 45%; max-width: none; }
  .list-day .ev { flex-direction: column; gap: 2px; }
  .modal-bg { padding: 0; }
  .modal { border-radius: 0; min-height: 100%; }
}

/* Family sync badge */
.sync-badge { position: fixed; top: 10px; right: 12px; z-index: 44; border: 1px solid var(--line); background: #fff; color: var(--muted); border-radius: 99px; padding: 3px 10px; font-size: 12px; box-shadow: var(--shadow); cursor: pointer; }
.sync-badge.bad { background: #fef3c7; color: #92400e; border-color: #fde68a; }
@media (max-width: 680px) { .sync-badge { top: 6px; right: 8px; } }
