:root {
  --bg: #f4f4f6;
  --card: #ffffff;
  --text: #2b2b2b;
  --muted: #8a8a8a;
  --ring-bg: #ececef;
  --field: #f5f5f7;
  --shadow: 0 20px 50px rgba(0,0,0,0.08);
  --border: rgba(0,0,0,0.06);
  /* default palette: coral */
  --accent: #e07a5f;
  --accent-short: #81b29a;
  --accent-long: #6d8dc7;
}

:root[data-theme="dark"] {
  --bg: #141519;
  --card: #1e2026;
  --text: #f2f2f2;
  --muted: #8b8d94;
  --ring-bg: #2b2d34;
  --field: #26282f;
  --shadow: 0 20px 50px rgba(0,0,0,0.45);
  --border: rgba(255,255,255,0.08);
}

/* Color palettes (accent trio only — independent of light/dark) */
:root[data-palette="coral"]    { --accent: #e07a5f; --accent-short: #81b29a; --accent-long: #6d8dc7; }
:root[data-palette="ocean"]    { --accent: #2f8fdf; --accent-short: #48b7b0; --accent-long: #4361ee; }
:root[data-palette="forest"]   { --accent: #4c956c; --accent-short: #90c290; --accent-long: #2c6e49; }
:root[data-palette="sunset"]   { --accent: #ef6f53; --accent-short: #f4b860; --accent-long: #e5546a; }
:root[data-palette="lavender"] { --accent: #8071d1; --accent-short: #a99be0; --accent-long: #6357a8; }
:root[data-palette="rose"]     { --accent: #d76a8f; --accent-short: #e79bb3; --accent-long: #b25c86; }

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  transition: background 0.4s ease, color 0.4s ease;
  padding: 36px 16px 60px;
}

.app {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  animation: rise 0.5s ease;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: translateY(0); }
}

.card, .tasks-card, .history-card {
  background: var(--card);
  border-radius: 26px;
  box-shadow: var(--shadow);
  width: 340px;
  max-width: 90vw;
  transition: background 0.4s ease;
}

.card {
  padding: 20px 36px 32px;
  text-align: center;
}

/* Icon buttons */

.icon-btn {
  border: none;
  background: transparent;
  cursor: pointer;
  color: var(--muted);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: inherit;
  transition: background 0.2s ease, color 0.2s ease;
}

.round-btn {
  width: 34px;
  height: 34px;
  border-radius: 50%;
}

.round-btn:hover {
  background: var(--ring-bg);
  color: var(--text);
}

.card-top {
  display: flex;
  justify-content: flex-end;
  gap: 4px;
  margin-bottom: 8px;
}

/* Modes */

.modes {
  display: flex;
  gap: 6px;
  background: var(--ring-bg);
  border-radius: 999px;
  padding: 4px;
  margin-bottom: 20px;
}

.mode-btn {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 600;
  padding: 9px 6px;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.25s ease, color 0.25s ease;
}

.mode-btn.active {
  background: var(--card);
  color: var(--text);
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}

/* Active task pill above ring */

.active-task {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  margin-bottom: 14px;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text);
}

.active-task-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  flex-shrink: 0;
}

body.mode-short .active-task-dot { background: var(--accent-short); }
body.mode-long .active-task-dot { background: var(--accent-long); }

#active-task-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 240px;
}

/* Timer ring */

.timer-ring {
  position: relative;
  width: 240px;
  height: 240px;
  margin: 0 auto 30px;
}

.timer-ring svg {
  width: 100%;
  height: 100%;
  transform: rotate(-90deg);
}

.ring-bg {
  fill: none;
  stroke: var(--ring-bg);
  stroke-width: 8;
}

.ring-progress {
  fill: none;
  stroke: var(--accent);
  stroke-width: 8;
  stroke-linecap: round;
  stroke-dasharray: 565.48;
  stroke-dashoffset: 0;
  transition: stroke-dashoffset 1s linear, stroke 0.4s ease;
}

.time-display {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

#time {
  font-size: 3rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: 1px;
}

#session-count {
  margin-top: 6px;
  font-size: 0.8rem;
  color: var(--muted);
  font-weight: 500;
}

/* Controls */

.controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 18px;
}

.controls button {
  border: none;
  cursor: pointer;
  font-family: inherit;
}

#reset-btn, #skip-btn {
  background: transparent;
  color: var(--muted);
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s ease, color 0.2s ease;
}

#reset-btn:hover, #skip-btn:hover {
  background: var(--ring-bg);
  color: var(--text);
}

#start-btn {
  background: var(--accent);
  color: white;
  font-size: 1rem;
  font-weight: 600;
  padding: 14px 40px;
  border-radius: 999px;
  transition: background 0.3s ease, transform 0.15s ease;
}

#start-btn:hover { transform: translateY(-1px); }
#start-btn:active { transform: translateY(0); }

body.mode-short #start-btn,
body.mode-short .ring-progress {
  background: var(--accent-short);
  stroke: var(--accent-short);
}

body.mode-long #start-btn,
body.mode-long .ring-progress {
  background: var(--accent-long);
  stroke: var(--accent-long);
}

/* Tasks & Projects */

.tasks-card { padding: 22px 24px 20px; }

.tasks-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 14px;
}

#project-select {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--border);
  background: var(--field);
  color: var(--text);
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  padding: 8px 10px;
  border-radius: 10px;
  cursor: pointer;
}

.project-actions { display: flex; gap: 2px; }

.project-actions .icon-btn {
  width: 30px;
  height: 30px;
  border-radius: 8px;
}

.project-actions .icon-btn:hover {
  background: var(--ring-bg);
  color: var(--text);
}

.inline-form {
  display: flex;
  gap: 6px;
  margin-bottom: 14px;
}

.inline-form input {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--border);
  background: var(--field);
  color: var(--text);
  font-family: inherit;
  font-size: 0.85rem;
  padding: 8px 10px;
  border-radius: 10px;
}

.btn-primary, .btn-secondary {
  border: none;
  font-family: inherit;
  font-weight: 600;
  cursor: pointer;
  border-radius: 10px;
  transition: filter 0.2s ease, background 0.2s ease;
}

.btn-primary { background: var(--accent); color: white; }
.btn-primary:hover { filter: brightness(1.06); }
.btn-secondary { background: var(--ring-bg); color: var(--text); }

.btn-small {
  font-size: 0.8rem;
  padding: 8px 12px;
  white-space: nowrap;
}

.task-form {
  display: flex;
  gap: 8px;
  margin-bottom: 14px;
}

.task-form input {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--border);
  background: var(--field);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
  padding: 10px 12px;
  border-radius: 12px;
}

.task-form input:focus,
.inline-form input:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

.add-btn {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  background: var(--ring-bg);
  flex-shrink: 0;
}

.add-btn:hover { background: var(--accent); color: white; }

.task-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
  max-height: 260px;
  overflow-y: auto;
}

.task-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 8px;
  border-radius: 12px;
  cursor: pointer;
  transition: background 0.2s ease;
}

.task-item:hover { background: var(--ring-bg); }

.task-item.active {
  background: color-mix(in srgb, var(--accent) 14%, transparent);
}

.task-check {
  width: 19px;
  height: 19px;
  border-radius: 50%;
  border: 2px solid var(--muted);
  background: transparent;
  flex-shrink: 0;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.task-item.completed .task-check {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.task-check svg { width: 11px; height: 11px; opacity: 0; }
.task-item.completed .task-check svg { opacity: 1; }

.task-name {
  flex: 1;
  min-width: 0;
  font-size: 0.88rem;
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.task-item.completed .task-name {
  color: var(--muted);
  text-decoration: line-through;
}

.task-pomodoros {
  font-size: 0.78rem;
  color: var(--muted);
  flex-shrink: 0;
  font-variant-numeric: tabular-nums;
}

/* Scheduling */

.task-schedule-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 8px 2px 0;
}

.task-schedule-row input[type="datetime-local"] {
  flex: 1;
  min-width: 0;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--ring-bg);
  color: var(--text);
  font-family: inherit;
  font-size: 0.82rem;
}

.task-schedule-row input:focus { outline: 2px solid var(--accent); outline-offset: 0; }

#task-schedule-btn.active { background: var(--accent); color: white; }

.task-due {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  flex-shrink: 0;
  border: none;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.72rem;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--ring-bg);
  color: var(--muted);
  white-space: nowrap;
  transition: filter 0.15s ease;
}

.task-due svg { flex-shrink: 0; opacity: 0.85; }
.task-due:hover { filter: brightness(0.95); }

.task-due.overdue { background: color-mix(in srgb, #d64545 16%, transparent); color: #d64545; }
.task-due.soon    { background: color-mix(in srgb, var(--accent) 18%, transparent); color: var(--accent); }
.task-due.today   { background: color-mix(in srgb, var(--accent) 12%, transparent); color: var(--accent); }
.task-due.done    { opacity: 0.5; text-decoration: line-through; }

.task-item.completed .task-due { opacity: 0.5; }

.task-due-edit {
  flex-shrink: 0;
  padding: 2px 6px;
  border-radius: 8px;
  border: 1px solid var(--accent);
  background: var(--card);
  color: var(--text);
  font-family: inherit;
  font-size: 0.74rem;
}

.task-delete {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  flex-shrink: 0;
  font-size: 1.1rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.15s ease, background 0.15s ease, color 0.15s ease;
}

.task-item:hover .task-delete { opacity: 1; }
.task-delete:hover { background: var(--card); color: #d64545; }

.task-empty {
  text-align: center;
  color: var(--muted);
  font-size: 0.85rem;
  margin: 12px 0 4px;
}

.task-empty.hidden { display: none; }

/* History / Activity */

.history-card { padding: 22px 24px 20px; }

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.section-head h3 {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 700;
}

.text-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 6px;
  transition: color 0.2s ease, background 0.2s ease;
}

.text-btn:hover { color: var(--text); background: var(--ring-bg); }

.stats-row {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
}

.stat {
  flex: 1;
  background: var(--field);
  border-radius: 14px;
  padding: 14px 8px;
  text-align: center;
}

.stat-num {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}

.stat-label {
  font-size: 0.72rem;
  color: var(--muted);
  margin-top: 4px;
}

.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 260px;
  overflow-y: auto;
}

.history-day {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  padding: 10px 2px 6px;
}

.history-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 2px;
  border-bottom: 1px solid var(--border);
}

.history-item:last-child { border-bottom: none; }

.history-time {
  font-size: 0.78rem;
  color: var(--muted);
  flex-shrink: 0;
  width: 62px;
  font-variant-numeric: tabular-nums;
}

.history-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.history-name {
  font-size: 0.86rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-project {
  font-size: 0.72rem;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-badge {
  flex-shrink: 0;
  font-size: 0.74rem;
  font-weight: 600;
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 14%, transparent);
  padding: 3px 8px;
  border-radius: 999px;
}

.history-empty {
  text-align: center;
  color: var(--muted);
  font-size: 0.85rem;
  margin: 8px 0 4px;
}

.history-empty.hidden { display: none; }

/* Thin scrollbars */
.task-list::-webkit-scrollbar,
.history-list::-webkit-scrollbar { width: 6px; }
.task-list::-webkit-scrollbar-thumb,
.history-list::-webkit-scrollbar-thumb {
  background: var(--ring-bg);
  border-radius: 3px;
}

/* Settings modal */

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  padding: 20px;
  animation: fade 0.2s ease;
}

@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.modal {
  background: var(--card);
  border-radius: 22px;
  padding: 26px 28px;
  width: 320px;
  max-width: 100%;
  box-shadow: var(--shadow);
  animation: pop 0.22s ease;
}

@keyframes pop {
  from { opacity: 0; transform: scale(0.96); }
  to   { opacity: 1; transform: scale(1); }
}

.modal h2 { margin: 0 0 18px; font-size: 1.15rem; }

.modal-section {
  padding-bottom: 16px;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--border);
}

.modal-subtitle {
  margin: 0 0 12px;
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.modal-field {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
  font-size: 0.88rem;
  font-weight: 600;
}

.modal-field span { flex: 1; }

.modal-field input {
  width: 68px;
  border: 1px solid var(--border);
  background: var(--field);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
  padding: 7px 8px;
  border-radius: 8px;
  text-align: center;
}

.modal-field input:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

.swatches {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.swatch {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  padding: 0;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.swatch:hover { transform: scale(1.12); }

.swatch.selected {
  box-shadow: 0 0 0 2px var(--card), 0 0 0 4px var(--text);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 4px;
}

.modal-actions button {
  padding: 9px 18px;
  font-size: 0.85rem;
}

/* ---- Mobile / PWA ---- */

/* Respect notches and rounded corners when installed / on modern phones. */
body {
  padding-top: max(36px, env(safe-area-inset-top));
  padding-bottom: max(60px, env(safe-area-inset-bottom));
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
}

@media (max-width: 430px) {
  body { padding-top: max(20px, env(safe-area-inset-top)); }

  .app {
    gap: 14px;
    width: 100%;
  }

  .card, .tasks-card, .history-card {
    width: 100%;
    max-width: 460px;
  }

  .card { padding: 16px 22px 26px; }

  .timer-ring {
    width: 214px;
    height: 214px;
    margin-bottom: 26px;
  }

  #time { font-size: 2.7rem; }

  /* 16px font on focusable inputs prevents iOS Safari from auto-zooming. */
  .task-form input,
  .inline-form input,
  .modal-field input,
  #task-due-input,
  .task-due-edit,
  #project-select {
    font-size: 16px;
  }
}

/* Slightly larger touch targets on touch devices. */
@media (hover: none) and (pointer: coarse) {
  #reset-btn, #skip-btn { width: 44px; height: 44px; }
  .task-delete { opacity: 1; }
  .swatch { width: 34px; height: 34px; }
}
