:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --border: #e4e6ef;
  --text: #1c1f2a;
  --text-muted: #6b7086;
  --primary: #4f6df5;
  --primary-hover: #3d5ae0;
  --danger: #e5484d;
  --danger-bg: #fdeceb;
  --radius: 12px;
  --shadow: 0 2px 10px rgba(20, 20, 43, 0.06);
  --today-ring: #4f6df5;
  --cal-header-start: #4f6df5;
  --cal-header-end: #3453e0;
  --weekend-text: #e0567a;
  --weekend-bg: #fdeef2;
  --weekend-bg-hover: #fbe2e9;
  --selection-bg: #eef2ff;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #11131c;
    --surface: #1a1d29;
    --border: #2a2d3d;
    --text: #edeef5;
    --text-muted: #9397ab;
    --primary: #6c86ff;
    --primary-hover: #8197ff;
    --danger: #ff6b6f;
    --danger-bg: #3a1f22;
    --shadow: 0 2px 14px rgba(0, 0, 0, 0.4);
    --cal-header-start: #3d4f99;
    --cal-header-end: #2d3b78;
    --weekend-text: #ff8fac;
    --weekend-bg: #2c1f27;
    --weekend-bg-hover: #38232c;
    --selection-bg: #202a4a;
  }
}

:root[data-theme="dark"] {
  --bg: #11131c;
  --surface: #1a1d29;
  --border: #2a2d3d;
  --text: #edeef5;
  --text-muted: #9397ab;
  --primary: #6c86ff;
  --primary-hover: #8197ff;
  --danger: #ff6b6f;
  --danger-bg: #3a1f22;
  --shadow: 0 2px 14px rgba(0, 0, 0, 0.4);
  --cal-header-start: #3d4f99;
  --cal-header-end: #2d3b78;
  --weekend-text: #ff8fac;
  --weekend-bg: #2c1f27;
  --weekend-bg-hover: #38232c;
  --selection-bg: #202a4a;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.app {
  max-width: 1320px;
  margin: 0 auto;
  padding: 0 16px 40px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 24px 0;
  flex-wrap: wrap;
  gap: 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-icon { font-size: 28px; }

.brand h1 {
  font-size: 22px;
  margin: 0;
}

.btn {
  border: none;
  border-radius: 10px;
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s ease, transform .05s ease;
}
.btn:active { transform: scale(0.97); }

.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-hover); }

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
}
.btn-ghost:hover { background: var(--border); }

.btn-danger {
  background: var(--danger-bg);
  color: var(--danger);
}
.btn-danger:hover { opacity: 0.85; }

.icon-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  width: 34px;
  height: 34px;
  font-size: 18px;
  cursor: pointer;
  color: var(--text);
}
.icon-btn:hover { background: var(--border); }

.layout {
  display: grid;
  grid-template-columns: 1fr 300px;
  gap: 20px;
  align-items: start;
}

@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; }
}

.calendar-panel, .sidebar {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.calendar-panel { padding: 0 0 16px; }
.sidebar { padding: 18px; }

.calendar-nav {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 18px;
  background: linear-gradient(135deg, var(--cal-header-start), var(--cal-header-end));
  color: #fff;
}

.calendar-nav h2 {
  margin: 0;
  font-size: 19px;
  font-weight: 700;
  flex: 1;
  text-transform: capitalize;
}

.calendar-nav .icon-btn {
  background: rgba(255, 255, 255, 0.16);
  border: 1px solid rgba(255, 255, 255, 0.28);
  color: #fff;
}
.calendar-nav .icon-btn:hover { background: rgba(255, 255, 255, 0.28); }

.calendar-nav .btn-ghost {
  background: rgba(255, 255, 255, 0.12);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: #fff;
}
.calendar-nav .btn-ghost:hover { background: rgba(255, 255, 255, 0.24); }

.calendar-hint {
  font-size: 12px;
  color: var(--text-muted);
  padding: 10px 18px 0;
  margin: 0;
}

.weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  padding: 14px 10px 6px;
  gap: 4px;
}
.weekdays span {
  text-align: center;
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 700;
  padding: 4px 0;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  border-radius: 6px;
}
.weekdays span.weekend {
  color: var(--weekend-text);
  background: var(--weekend-bg);
}

.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  grid-auto-rows: minmax(92px, auto);
  gap: 4px;
  padding: 0 10px;
  user-select: none;
}

.day-cell {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-height: 92px;
  background: var(--surface);
  cursor: pointer;
  transition: background .1s ease, box-shadow .1s ease;
}
.day-cell:hover { background: var(--bg); }

.day-cell.weekend { background: var(--weekend-bg); }
.day-cell.weekend:hover { background: var(--weekend-bg-hover); }

.day-cell.outside {
  opacity: 0.4;
}

.day-cell.today {
  box-shadow: inset 0 0 0 2px var(--today-ring);
}

.day-cell.selecting {
  background: var(--selection-bg) !important;
  box-shadow: inset 0 0 0 2px var(--primary);
}

.day-num {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-muted);
}
.day-cell.today .day-num { color: var(--primary); }

.trip-chip {
  font-size: 11px;
  line-height: 1.3;
  padding: 3px 6px;
  border-radius: 5px;
  color: #fff;
  cursor: pointer;
  overflow: hidden;
}
.trip-chip:hover { filter: brightness(1.08); }
.chip-place {
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chip-note {
  font-weight: 400;
  opacity: 0.9;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 10px;
}

/* A day fully taken by one trip — the whole square is that trip's color. */
.day-cell.filled {
  border-color: transparent;
  color: #fff;
  cursor: pointer;
  justify-content: flex-start;
}
.day-cell.filled:hover { filter: brightness(1.06); }
.day-cell.filled .day-num { color: rgba(255, 255, 255, 0.85); }
.day-cell.filled.today { box-shadow: inset 0 0 0 2px #fff; }

.day-fill-label {
  margin-top: 2px;
  color: #fff;
  overflow: hidden;
}
.day-fill-label .chip-place {
  font-size: 12px;
  white-space: normal;
}
.day-fill-label .chip-note {
  font-size: 10px;
  white-space: normal;
}

/* Several trips overlap the same day — split the square into bands. */
.day-cell.multi {
  padding: 0;
  gap: 0;
}
.day-cell.multi .day-num {
  position: absolute;
  margin: 6px 0 0 6px;
  color: var(--text-muted);
  text-shadow: 0 0 3px rgba(255, 255, 255, 0.6);
  z-index: 1;
}
.day-cell.multi { position: relative; }
.day-bands {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
}
.day-band {
  flex: 1;
  display: flex;
  align-items: center;
  padding: 2px 6px;
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}
.day-band:first-child { padding-left: 24px; }
.day-band:hover { filter: brightness(1.08); }
.day-more {
  position: absolute;
  bottom: 2px;
  right: 6px;
  font-size: 10px;
  color: #fff;
  background: rgba(0, 0, 0, 0.35);
  padding: 1px 5px;
  border-radius: 4px;
  z-index: 1;
}

.sidebar h3 {
  font-size: 14px;
  margin: 4px 0 10px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.sidebar h3:not(:first-child) { margin-top: 20px; }

.trip-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.trip-card {
  border: 1px solid var(--border);
  border-left: 4px solid var(--primary);
  border-radius: 8px;
  padding: 8px 10px;
  cursor: pointer;
  background: var(--bg);
}
.trip-card:hover { background: var(--border); }

.trip-card .name {
  font-weight: 600;
  font-size: 13px;
}
.trip-card .meta {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}

.empty-hint {
  color: var(--text-muted);
  font-size: 13px;
  padding: 8px 0;
}

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 12, 20, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 50;
}
.modal-overlay.hidden { display: none; }

.modal {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px;
  width: 100%;
  max-width: 420px;
  box-shadow: var(--shadow);
  max-height: 90vh;
  overflow-y: auto;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}
.modal-header h3 { margin: 0; font-size: 17px; }

#tripForm label {
  display: block;
  font-size: 13px;
  color: var(--text-muted);
  font-weight: 600;
  margin-bottom: 12px;
}

#tripForm input[type="text"],
#tripForm input[type="date"],
#tripForm textarea {
  display: block;
  width: 100%;
  margin-top: 5px;
  padding: 9px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
}
#tripForm textarea { resize: vertical; }

.row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.color-picker {
  display: flex;
  gap: 8px;
  margin-top: 6px;
}
.color-swatch {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid transparent;
}
.color-swatch.selected {
  border-color: var(--text);
}

.modal-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
}
.spacer { flex: 1; }
.hidden { display: none !important; }

/* ---------- Auth screen ---------- */
.auth-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.auth-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px 24px;
  width: 100%;
  max-width: 380px;
}
.auth-card .brand {
  justify-content: center;
  margin-bottom: 18px;
}
.auth-form label {
  display: block;
  font-size: 13px;
  color: var(--text-muted);
  font-weight: 600;
  margin-bottom: 14px;
}
.auth-form input {
  display: block;
  width: 100%;
  margin-top: 5px;
  padding: 10px 11px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
}
.btn-block { width: 100%; margin-top: 4px; }
.auth-form .btn-block + .btn-block { margin-top: 8px; }
.hint {
  font-size: 13px;
  color: var(--text-muted);
  margin: 0 0 14px;
  line-height: 1.5;
}
.auth-footnote {
  margin-top: 18px;
  font-size: 12px;
  color: var(--text-muted);
  text-align: center;
  line-height: 1.5;
}
.form-error {
  background: var(--danger-bg);
  color: var(--danger);
  font-size: 13px;
  padding: 8px 10px;
  border-radius: 8px;
  margin: -4px 0 12px;
}
.form-success {
  background: #e9f9ee;
  color: #17b26a;
  font-size: 13px;
  padding: 8px 10px;
  border-radius: 8px;
  margin: 8px 0;
}

/* ---------- Topbar extras ---------- */
.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}
.whoami {
  font-size: 13px;
  color: var(--text-muted);
}

/* ---------- History modal ---------- */
.history-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: 60vh;
  overflow-y: auto;
}
.history-row {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  background: var(--bg);
}
.history-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}
.history-action {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 2px 7px;
  border-radius: 5px;
}
.history-insert { background: #e9f9ee; color: #17b26a; }
.history-update { background: #fff4e5; color: #f79009; }
.history-delete { background: var(--danger-bg); color: var(--danger); }
.history-when { font-size: 12px; color: var(--text-muted); }
.history-who { font-size: 12px; color: var(--text-muted); margin-bottom: 4px; }
.history-diff {
  margin: 4px 0 0;
  padding-left: 18px;
  font-size: 12px;
}
.history-diff li { margin-bottom: 2px; }

/* ---------- Users / admin panel ---------- */
.modal-wide { max-width: 560px; }
.mfa-gate { text-align: center; padding: 10px 0; }
.mfa-gate .auth-form { max-width: 260px; margin: 14px auto 0; text-align: left; }
.invite-form {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}
.invite-form input[type="email"] {
  flex: 1;
  min-width: 180px;
  padding: 9px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}
.invite-form select {
  padding: 9px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
}
.users-table {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.user-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
}
.user-email { font-size: 13px; font-weight: 600; }
.user-role { font-size: 12px; color: var(--text-muted); }
.user-actions { display: flex; gap: 6px; align-items: center; }
.user-actions select {
  padding: 5px 6px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 12px;
}
.btn-sm { padding: 5px 10px; font-size: 12px; }

/* ---------- PIN / links / users (v2) ---------- */
.pin-input {
  text-align: center;
  font-size: 22px !important;
  letter-spacing: 0.5em;
  padding-left: 0.5em !important;
}
.btn-link {
  background: transparent;
  color: var(--text-muted);
  font-weight: 500;
}
.btn-link:hover { color: var(--text); }
.user-row { grid-template-columns: 1fr; }
.user-meta { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.user-actions { flex-wrap: wrap; }
.invite-form input[type="text"] {
  flex: 1;
  min-width: 140px;
  padding: 9px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

/* ---------- Phones / PWA ---------- */
body { padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom); }
.calendar-grid { touch-action: manipulation; -webkit-user-select: none; }
input, select, textarea { font-size: 16px; } /* avoid iOS zoom on focus */

@media (max-width: 600px) {
  .app { padding: 0 8px 28px; }
  .topbar { padding: 14px 0; }
  .brand h1 { font-size: 18px; }
  .calendar-nav { padding: 12px 12px; gap: 6px; }
  .calendar-nav h2 { font-size: 16px; }
  .weekdays, .calendar-grid { padding-left: 4px; padding-right: 4px; gap: 2px; }
  .calendar-grid { grid-auto-rows: minmax(64px, auto); }
  .day-cell { min-height: 64px; padding: 3px; border-radius: 6px; }
  .day-num { font-size: 11px; }
  .day-fill-label .chip-place { font-size: 10px; }
  .day-fill-label .chip-note { display: none; }
  .day-band { font-size: 8px; padding: 1px 3px; }
  .day-band:first-child { padding-left: 16px; }
  .topbar-actions { width: 100%; }
  .whoami { width: 100%; }
}

/* ---------- Attachments ---------- */
.files-section { margin: 4px 0 12px; }
.files-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 6px; }
.files-title { font-size: 13px; color: var(--text-muted); font-weight: 600; }
.files-head select {
  padding: 6px 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
}
.drop-zone {
  border: 2px dashed var(--border);
  border-radius: 10px;
  padding: 14px 10px;
  text-align: center;
  cursor: pointer;
  background: var(--bg);
  transition: border-color .12s ease, background .12s ease;
}
.drop-zone:hover, .drop-zone:focus-visible { border-color: var(--primary); outline: none; }
.drop-zone.dragover { border-color: var(--primary); background: var(--selection-bg); }
.drop-title { font-weight: 600; font-size: 14px; }
.drop-sub { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.files-list { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.file-row {
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  font-size: 13px;
}
.file-icon { font-size: 18px; }
.file-info { min-width: 0; }
.file-name { display: block; color: var(--text); font-weight: 600; text-decoration: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
a.file-name:hover { color: var(--primary); text-decoration: underline; }
.file-meta { font-size: 11px; color: var(--text-muted); }
.file-row select {
  padding: 4px 6px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 12px;
  max-width: 150px;
}
.file-kind-badge { font-size: 11px; color: var(--text-muted); background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 2px 6px; white-space: nowrap; }
.file-remove { width: 28px; height: 28px; font-size: 14px; }
.file-row.pending { border-style: dashed; }
.files-status { font-size: 12px; color: var(--primary); margin: 6px 0 0; }
.files-hint { margin: 8px 0 0; }
.trip-card .clip { margin-left: 6px; }

@media (max-width: 600px) {
  .file-row { grid-template-columns: auto 1fr auto; }
  .file-row select, .file-row .file-kind-badge { grid-column: 2; justify-self: start; }
  .file-row .file-remove { grid-row: 1; grid-column: 3; }
}

/* ---------- fixes: narrow screens ---------- */
.modal-actions { flex-wrap: wrap; }
.file-row { grid-template-columns: auto minmax(0, 1fr) auto auto; }
@media (max-width: 600px) {
  .file-row { grid-template-columns: auto minmax(0, 1fr) auto; }
  .modal-actions .spacer { display: none; }
  .modal-actions .btn-primary { margin-left: auto; }
}
