* {
  box-sizing: border-box;
}

:root {
  color-scheme: dark;
  --bg: #14161b;
  --surface: #1e2128;
  --surface-2: #191c22;   /* other-month days, subtask rows */
  --elevated: #2a2f3a;    /* active segmented button */
  --chip: #262a33;        /* pills, view switch, progress track */
  --hover: #2c313c;
  --input: #0f1115;       /* form fields (inset look) */
  --border: #2c3038;
  --text: #e6e8ee;
  --muted: #9aa1b4;
  --faint: #5b6072;
  --primary: #4f8cff;
  --primary-dark: #3a6fd1;
  --danger: #e8553a;
  --success: #2fb56b;
  --drag: #22304a;
  --shadow: 0 12px 34px rgba(0, 0, 0, 0.55);
}

html, body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100%;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  gap: 12px;
  flex-wrap: wrap;
}

.topbar-left, .topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

#periodLabel {
  margin: 0 0 0 12px;
  font-size: 22px;
  font-weight: 600;
}

.btn {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 7px 14px;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  color: var(--text);
  transition: background .15s, border-color .15s;
}
.btn:hover { background: var(--hover); }

.btn.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}
.btn.primary:hover { background: var(--primary-dark); }

.btn.danger {
  background: var(--surface);
  border-color: var(--danger);
  color: var(--danger);
}
.btn.danger:hover { background: var(--danger); color: white; }

.icon-btn {
  background: transparent;
  border: none;
  width: 32px;
  height: 32px;
  border-radius: 6px;
  font-size: 16px;
  cursor: pointer;
  color: var(--text);
}
.icon-btn:hover { background: var(--hover); }

.sync-status {
  font-size: 12px;
  color: var(--muted);
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--chip);
  white-space: nowrap;
}

.view-switch {
  display: flex;
  background: var(--chip);
  border-radius: 8px;
  padding: 3px;
}
.view-btn {
  background: transparent;
  border: none;
  padding: 6px 14px;
  font-size: 13px;
  border-radius: 6px;
  cursor: pointer;
  color: var(--muted);
}
.view-btn.active {
  background: var(--elevated);
  color: var(--text);
  box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}

#calendar {
  flex: 1;
  padding: 16px 24px 24px;
  overflow: auto;
}

.month-grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  background: var(--border);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  gap: 1px;
  min-height: 100%;
}

.weekday-header {
  background: var(--surface);
  padding: 10px 8px;
  font-weight: 600;
  font-size: 12px;
  color: var(--muted);
  text-align: center;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.day-cell {
  background: var(--surface);
  padding: 6px;
  min-height: 110px;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  transition: background .12s;
}
.day-cell.other-month { background: var(--surface-2); }
.day-cell.other-month .day-number { color: var(--faint); }
.day-cell.today .day-number {
  background: var(--primary);
  color: white;
}
.day-cell.drag-over {
  background: var(--drag);
  outline: 2px dashed var(--primary);
  outline-offset: -3px;
}

.day-number {
  font-size: 13px;
  font-weight: 600;
  align-self: flex-start;
  padding: 2px 7px;
  border-radius: 999px;
  margin-bottom: 2px;
}

.task-card {
  background: var(--primary);
  color: white;
  font-size: 12px;
  min-width: 0;
  overflow: hidden;
  padding: 4px 7px;
  border-radius: 5px;
  cursor: grab;
  user-select: none;
  display: flex;
  flex-direction: column;
  gap: 2px;
  border-left: 3px solid rgba(0,0,0,0.25);
}
.task-card:active { cursor: grabbing; }
.task-card.dragging { opacity: 0.4; }
.task-card .title {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-weight: 600;
}
.task-card .meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 10px;
  opacity: 0.85;
}
.task-card .mini-progress {
  height: 3px;
  background: rgba(255,255,255,0.3);
  border-radius: 2px;
  overflow: hidden;
}
.task-card .mini-progress > div {
  height: 100%;
  background: white;
  transition: width .25s;
}
.task-card.complete { opacity: 0.7; }
.task-card.complete .title { text-decoration: line-through; }

.week-grid {
  display: grid;
  grid-template-columns: 60px repeat(7, minmax(0, 1fr));
  background: var(--border);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  gap: 1px;
}
.week-header-cell {
  background: var(--surface);
  padding: 10px 4px;
  text-align: center;
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
}
.week-header-cell .dow { text-transform: uppercase; letter-spacing: 0.5px; }
.week-header-cell .dom {
  display: inline-block;
  margin-top: 4px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 14px;
  color: var(--text);
}
.week-header-cell.today .dom { background: var(--primary); color: white; }

.time-label {
  background: var(--surface);
  font-size: 11px;
  color: var(--muted);
  padding: 4px 6px;
  text-align: right;
}
.time-slot {
  background: var(--surface);
  min-height: 42px;
  position: relative;
  min-width: 0;
  transition: background .12s;
}
.time-slot.drag-over {
  background: var(--drag);
  outline: 2px dashed var(--primary);
  outline-offset: -3px;
}
.time-slot .task-card {
  margin: 2px;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
}
.modal-backdrop.hidden { display: none; }
.modal {
  background: var(--surface);
  border-radius: 12px;
  width: 100%;
  max-width: 460px;
  box-shadow: var(--shadow);
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 { margin: 0; font-size: 17px; }
.modal-body {
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow: auto;
}
.modal-body label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  color: var(--muted);
}
.modal-body input[type=text],
.modal-body input[type=date],
.modal-body input[type=time],
.modal-body select,
.modal-body textarea {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  font-family: inherit;
  color: var(--text);
  background: var(--input);
}
.modal-body input[type=color] {
  width: 50px;
  height: 32px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
}
.modal-body textarea { resize: vertical; }
.row { display: flex; gap: 10px; }
.row .grow { flex: 1; }
.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 6px;
}

.muted { color: var(--muted); margin: 0; font-size: 13px; }

.progress {
  position: relative;
  height: 10px;
  background: var(--chip);
  border-radius: 999px;
  overflow: hidden;
  margin: 4px 0 6px;
}
.progress-bar {
  height: 100%;
  background: var(--success);
  width: 0%;
  transition: width .3s;
}
.progress-text {
  position: absolute;
  right: 8px;
  top: -18px;
  font-size: 11px;
  color: var(--muted);
}

.subtask-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 4px; }
.subtask-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 6px;
  background: var(--surface-2);
}
.subtask-item:hover { background: var(--hover); }
.subtask-item input[type=checkbox] { width: 16px; height: 16px; cursor: pointer; }
.subtask-item .text { flex: 1; font-size: 14px; }
.subtask-item.done .text { text-decoration: line-through; color: var(--muted); }
.subtask-item .remove {
  background: transparent;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 16px;
  padding: 0 4px;
  line-height: 1;
}
.subtask-item .remove:hover { color: var(--danger); }

.subtask-add {
  display: flex;
  gap: 8px;
  margin-top: 6px;
}
.subtask-add input {
  flex: 1;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
}

h3 { margin: 8px 0 4px; font-size: 14px; color: var(--muted); }

/* Priority indicator on task cards */
.task-card .meta .meta-left { display: inline-flex; align-items: center; gap: 4px; }
.prio-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  flex: 0 0 auto;
  box-shadow: 0 0 0 1px rgba(0,0,0,0.3);
}
.prio-high   { background: #ff5a4d; }
.prio-medium { background: #ffb020; }
.prio-low    { background: #cfd3dd; }

/* Priority badge in the editor's select label */
.priority-row { display: flex; gap: 10px; }

/* Task-level "complete" toggle in the detail modal */
.detail-complete {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 11px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  color: var(--text);
  cursor: pointer;
  user-select: none;
}
.detail-complete input { width: 18px; height: 18px; cursor: pointer; }
.detail-complete.is-done { color: var(--success); border-color: var(--success); }

@media (max-width: 640px) {
  .topbar { padding: 10px 12px; gap: 8px; }
  #periodLabel { font-size: 18px; margin-left: 4px; }
  .topbar-left, .topbar-right { gap: 6px; }
  #calendar { padding: 8px 10px 16px; }
  .day-cell { min-height: 72px; padding: 4px; }
  .week-grid { grid-template-columns: 42px repeat(7, minmax(0, 1fr)); }
  .time-label { font-size: 9px; padding: 2px 3px; }
  .time-slot { min-height: 38px; }
  .btn { padding: 6px 10px; font-size: 13px; }
  .sync-status { font-size: 10px; padding: 3px 7px; }
  .weekday-header { font-size: 10px; padding: 6px 2px; }
  .modal { max-width: 100%; }
}

@supports (padding: max(0px)) {
  .topbar {
    padding-left: max(24px, env(safe-area-inset-left));
    padding-right: max(24px, env(safe-area-inset-right));
    padding-top: max(14px, env(safe-area-inset-top));
  }
}
