:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface2: #334155;
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --primary: #3b82f6;
  --primary-dark: #2563eb;
  --danger: #ef4444;
  --success: #22c55e;
  --border: #475569;
  --radius: 12px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

#app { height: 100%; }

.screen {
  display: none;
  height: 100%;
  flex-direction: column;
}
.screen.active { display: flex; }

/* Login */
#login-screen {
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: linear-gradient(160deg, #1e3a5f 0%, var(--bg) 60%);
}

.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border-radius: 20px;
  padding: 32px 24px;
  box-shadow: 0 20px 60px rgba(0,0,0,.4);
}

.logo {
  font-size: 28px;
  font-weight: 700;
  text-align: center;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.logo-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
}

.subtitle {
  text-align: center;
  color: var(--text-muted);
  margin-bottom: 28px;
  font-size: 14px;
}

.hint {
  text-align: center;
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 16px;
}
.hint code {
  background: var(--surface2);
  padding: 2px 6px;
  border-radius: 4px;
}

/* Forms */
input, select, textarea, button {
  font: inherit;
  border: none;
  outline: none;
}

#login-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

#login-form select,
#login-form input {
  width: 100%;
  padding: 14px 16px;
  background: var(--surface2);
  color: var(--text);
  border-radius: var(--radius);
  font-size: 16px;
}

.btn {
  padding: 14px 20px;
  border-radius: var(--radius);
  font-weight: 600;
  cursor: pointer;
  font-size: 15px;
}
.btn.primary { background: var(--primary); color: white; }
.btn.primary:active { background: var(--primary-dark); }
.btn.secondary { background: var(--surface2); color: var(--text); }
.btn.danger { background: var(--danger); color: white; }
.btn.success { background: var(--success); color: white; }

/* Top bar */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
}

.user-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  flex-shrink: 0;
}

.icon-btn {
  background: none;
  border: none;
  color: var(--text);
  font-size: 22px;
  cursor: pointer;
  padding: 4px 8px;
  line-height: 1;
}

/* Tabs */
.view-tabs {
  display: none;
}

.bottom-nav {
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
}

.nav-item {
  flex: 1;
  padding: 12px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 22px;
  cursor: pointer;
}
.nav-item.active { color: var(--primary); }

/* Overlay toggles */
.overlay-toggles {
  padding: 8px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.toggle {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
  color: var(--text-muted);
  cursor: pointer;
}
.toggle input { width: 18px; height: 18px; accent-color: var(--primary); }

/* Week nav */
.week-nav {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  background: var(--bg);
}

#week-label {
  flex: 1;
  text-align: center;
  font-weight: 600;
  font-size: 14px;
}

.today-btn {
  font-size: 13px !important;
  background: var(--surface2);
  border-radius: 8px;
  padding: 4px 10px !important;
}

/* Content */
#content {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding-bottom: 64px;
}

.view-panel { display: none; flex: 1; min-height: 0; overflow: hidden; }
.view-panel.active { display: flex; flex-direction: column; }

/* Week grid */
.week-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
  margin-bottom: 12px;
}

.day-header {
  text-align: center;
  font-size: 11px;
  color: var(--text-muted);
  padding: 4px 0;
}
.day-header.today { color: var(--primary); font-weight: 700; }
.day-header .day-num {
  display: block;
  font-size: 16px;
  color: var(--text);
  margin-top: 2px;
}
.day-header.today .day-num {
  background: var(--primary);
  color: white;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin: 2px auto 0;
}

/* Event cards */
.event-list { display: flex; flex-direction: column; gap: 8px; }

.event-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 12px 14px;
  border-left: 4px solid var(--primary);
  cursor: pointer;
  active: opacity: 0.8;
}

.event-card.shared { border-left-color: #f59e0b; }
.event-card.personal_overlay { border-left-style: dashed; opacity: 0.85; }
.event-card.birthday { border-left-color: #ec4899; }
.event-card.completed { opacity: 0.5; text-decoration: line-through; }

.event-card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}

.event-time {
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 600;
}

.event-title { font-weight: 600; font-size: 15px; }
.event-meta { font-size: 12px; color: var(--text-muted); margin-top: 4px; }

.badge {
  display: inline-block;
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--surface2);
  margin-right: 4px;
}

/* Day section in list views */
.day-section { margin-bottom: 16px; }
.day-section-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-muted);
  padding: 8px 4px 6px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.day-section-title.today-title { color: var(--primary); }

/* Notes */
.note-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 8px;
  cursor: pointer;
}
.note-card.shared { border-left: 4px solid #f59e0b; }
.note-title { font-weight: 600; margin-bottom: 6px; }
.note-content {
  font-size: 14px;
  color: var(--text-muted);
  white-space: pre-wrap;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.note-meta { font-size: 11px; color: var(--text-muted); margin-top: 8px; }

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-muted);
  font-size: 14px;
}

/* FAB */
.fab {
  position: fixed;
  bottom: calc(70px + var(--safe-bottom));
  right: 20px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--primary);
  color: white;
  font-size: 28px;
  border: none;
  box-shadow: 0 4px 20px rgba(59,130,246,.5);
  cursor: pointer;
  z-index: 50;
  line-height: 1;
}

/* Modals */
dialog {
  border: none;
  border-radius: 20px 20px 0 0;
  padding: 0;
  background: var(--surface);
  color: var(--text);
  width: 100%;
  max-width: 480px;
  max-height: 90vh;
  margin: auto auto 0;
  position: fixed;
  inset: auto 0 0;
}
dialog::backdrop { background: rgba(0,0,0,.6); }

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 { font-size: 18px; }

.modal-body {
  padding: 16px 20px;
  overflow-y: auto;
  max-height: 60vh;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal-body label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  color: var(--text-muted);
}
.modal-body label.row {
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
}
.modal-body input[type="text"],
.modal-body input[type="date"],
.modal-body input[type="time"],
.modal-body select,
.modal-body textarea {
  padding: 12px;
  background: var(--surface2);
  color: var(--text);
  border-radius: 8px;
  font-size: 16px;
  width: 100%;
}

.modal-actions {
  display: flex;
  gap: 10px;
  padding: 16px 20px;
  border-top: 1px solid var(--border);
}
.modal-actions .btn { flex: 1; }

fieldset {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
}
fieldset legend { font-size: 13px; color: var(--text-muted); padding: 0 4px; }

.participant-check {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 0;
  font-size: 14px;
  color: var(--text);
}

.code-block {
  background: var(--surface2);
  padding: 12px;
  border-radius: 8px;
  font-size: 13px;
  white-space: pre-wrap;
  word-break: break-all;
}

.birthdays-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.birthdays-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
  font-size: 14px;
}
.bday-dot { width: 10px; height: 10px; border-radius: 50%; }

.detail-time { font-size: 14px; color: var(--text-muted); margin-bottom: 8px; }
.detail-cat { margin-bottom: 8px; }
.detail-desc { font-size: 14px; line-height: 1.5; margin-bottom: 12px; white-space: pre-wrap; }
.detail-participants { font-size: 13px; color: var(--text-muted); }

/* View mode toggle */
.view-mode-bar {
  display: flex;
  gap: 6px;
  padding: 6px 16px;
  background: var(--bg);
}
.mode-btn {
  flex: 1;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.mode-btn.active {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}

/* ── Weekly planner grid ── */
.planner-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 10px;
  padding: 6px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.legend-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 10px;
  color: var(--text-muted);
  white-space: nowrap;
}
.legend-item i {
  width: 10px;
  height: 10px;
  border-radius: 2px;
  display: inline-block;
  flex-shrink: 0;
}

.planner-scroll {
  flex: 1;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  min-height: 0;
}

.planner {
  min-width: 640px;
}

.planner-head {
  display: grid;
  grid-template-columns: 44px repeat(7, 1fr);
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.planner-corner { }
.planner-day-head {
  text-align: center;
  padding: 6px 2px;
  border-left: 1px solid var(--border);
}
.planner-day-head.today { background: rgba(59,130,246,.15); }
.pd-name { display: block; font-size: 11px; color: var(--text-muted); font-weight: 600; }
.pd-date { display: block; font-size: 15px; font-weight: 700; }
.planner-day-head.today .pd-date { color: var(--primary); }

.planner-allday {
  display: grid;
  grid-template-columns: 44px repeat(7, 1fr);
  min-height: 28px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.planner-allday-label {
  font-size: 9px;
  color: var(--text-muted);
  writing-mode: vertical-lr;
  transform: rotate(180deg);
  text-align: center;
  padding: 4px 0;
}
.planner-allday-col {
  border-left: 1px solid var(--border);
  padding: 2px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.planner-allday-ev {
  font-size: 10px;
  padding: 2px 4px;
  border-radius: 3px;
  font-weight: 600;
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.planner-body {
  display: flex;
}

.planner-times {
  width: 44px;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  left: 0;
  z-index: 10;
}
.planner-time {
  font-size: 9px;
  color: var(--text-muted);
  text-align: right;
  padding-right: 4px;
  line-height: 26px;
  border-bottom: 1px solid rgba(71,85,105,.3);
}

.planner-days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  flex: 1;
}

.planner-day-col {
  position: relative;
  border-left: 1px solid var(--border);
  background: var(--bg);
}
.planner-day-col.today { background: rgba(59,130,246,.06); }

.planner-slot {
  border-bottom: 1px solid rgba(71,85,105,.25);
  cursor: pointer;
}
.planner-slot:active { background: rgba(59,130,246,.12); }

.planner-now {
  position: absolute;
  left: 0;
  right: 0;
  height: 2px;
  background: #ef4444;
  z-index: 5;
  pointer-events: none;
}
.planner-now::before {
  content: '';
  position: absolute;
  left: -4px;
  top: -3px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #ef4444;
}

.planner-event {
  position: absolute;
  z-index: 4;
  border-radius: 4px;
  padding: 2px 4px;
  overflow: hidden;
  cursor: pointer;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  line-height: 1.2;
}
.planner-event.overlay { opacity: .75; border-style: dashed !important; }
.planner-event.done { opacity: .45; text-decoration: line-through; }
.pe-title {
  font-size: 10px;
  font-weight: 700;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  word-break: break-word;
}
.pe-desc {
  font-size: 9px;
  opacity: .85;
  margin-top: 2px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

@media (min-width: 768px) {
  dialog {
    border-radius: 16px;
    margin: auto;
    inset: 0;
  }
  .bottom-nav { max-width: 480px; margin: 0 auto; }
  #main-screen { max-width: 480px; margin: 0 auto; border-left: 1px solid var(--border); border-right: 1px solid var(--border); }
}
