:root {
  color-scheme: light;
  --bg: #f1f4fb;
  --card: #ffffff;
  --text: #1c2237;
  --muted: #566079;
  --border: #e1e6f2;
  --primary: #4f52e8;
  --primary-2: #7c4fe0;
  --danger: #d02f35;
  --ok: #0d7a5f;
  --warn: #a85d00;
  --on-primary: #ffffff;
  --on-solid: #ffffff;
  --shadow: 0 6px 24px rgba(40, 50, 100, .08);
  --radius: 16px;
}
[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0e1220;
  --card: #171c30;
  --text: #eef1fb;
  --muted: #9aa4c2;
  --border: #272e4a;
  --primary: #7c7fff;
  --primary-2: #a78bfa;
  --danger: #ff6b70;
  --ok: #34d3a8;
  --warn: #ffb020;
  --on-primary: #10142a;
  --on-solid: #1c0a0b;
  --shadow: 0 6px 24px rgba(0, 0, 0, .35);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  transition: background .3s, color .3s;
  background-image: radial-gradient(circle at 15% 0%, rgba(91, 94, 244, .18), transparent 40%),
                    radial-gradient(circle at 100% 10%, rgba(139, 92, 246, .14), transparent 35%);
  background-attachment: fixed;
}
.app { max-width: 1080px; margin: 0 auto; padding: 18px 14px 40px; }
:focus-visible { outline: 3px solid var(--primary); outline-offset: 2px; }

/* Header */
.header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.brand { display: flex; gap: 12px; align-items: center; }
.logo {
  width: 46px; height: 46px; border-radius: 14px; display: grid; place-items: center;
  color: var(--on-primary); font-size: 24px; font-weight: 700;
  background: linear-gradient(135deg, var(--primary), var(--primary-2));
  box-shadow: 0 8px 18px rgba(91, 94, 244, .4);
}
h1 { font-size: 1.5rem; letter-spacing: -.5px; }
.brand p { color: var(--muted); font-size: .85rem; }
.icon-btn {
  width: 46px; height: 46px; border-radius: 14px; border: 1px solid var(--border);
  background: var(--card); font-size: 20px; cursor: pointer; box-shadow: var(--shadow);
}

/* Cards */
.card, .progress-card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px; margin-bottom: 16px;
}
.progress-card {
  background: linear-gradient(135deg, var(--primary), var(--primary-2));
  color: var(--on-primary); border: none;
}
.progress-top { display: flex; justify-content: space-between; margin-bottom: 10px; font-size: .95rem; }
.progress-top strong { font-size: 1.3rem; }
.bar { height: 10px; background: rgba(255, 255, 255, .25); background: color-mix(in srgb, var(--on-primary) 28%, transparent); border-radius: 99px; overflow: hidden; }
.bar-fill { height: 100%; width: 0; background: var(--on-primary); border-radius: 99px; transition: width .5s ease; }
.stats { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 8px; margin-top: 14px; }
.stat { background: rgba(255, 255, 255, .16); background: color-mix(in srgb, var(--on-primary) 16%, transparent); border-radius: 12px; padding: 8px 4px; text-align: center; }
.stat b { display: block; font-size: 1.15rem; }
.stat span { font-size: .68rem; opacity: .9; }

/* Form */
.form h2 { font-size: 1.05rem; margin-bottom: 12px; }
.field { display: block; margin-bottom: 12px; }
.field > span, .inline { font-size: .8rem; font-weight: 600; color: var(--muted); }
input, select, textarea {
  width: 100%; min-width: 0; margin-top: 4px; padding: 12px; font: inherit; color: var(--text);
  background: var(--bg); border: 1.5px solid var(--border); border-radius: 12px;
  transition: border-color .2s;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); }
textarea { resize: vertical; }
.error { color: var(--danger); font-size: .8rem; }
.grid { display: grid; grid-template-columns: minmax(0, 1fr); gap: 0 12px; }
.form-actions { display: flex; gap: 10px; }

/* Buttons */
.btn {
  padding: 12px 18px; min-height: 44px; border-radius: 12px; border: 1.5px solid var(--border);
  background: var(--card); color: var(--text); font: inherit; font-weight: 600; cursor: pointer;
  transition: transform .15s, opacity .2s;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: linear-gradient(135deg, var(--primary), var(--primary-2)); color: var(--on-primary); border: none; flex: 1; }
.btn.danger { background: var(--danger); color: var(--on-solid); border: none; }
.btn.danger-outline { color: var(--danger); border-color: var(--danger); }

/* Controls */
.controls input[type="search"] { margin: 0 0 12px; }
.filters { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 6px; scrollbar-width: none; }
.filters::-webkit-scrollbar { display: none; }
.row { display: flex; gap: 10px; align-items: flex-end; justify-content: space-between; margin-top: 12px; flex-wrap: wrap; }
.inline { flex: 1; min-width: 150px; }

/* Task list */
.task-list { list-style: none; display: grid; grid-template-columns: minmax(0, 1fr); gap: 12px; }
.task {
  background: var(--card); border: 1px solid var(--border); border-left: 5px solid var(--border);
  border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow);
  display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 12px;
  animation: pop .3s ease;
}
@keyframes pop { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
.task.p-high { border-left-color: var(--danger); }
.task.p-medium { border-left-color: var(--warn); }
.task.p-low { border-left-color: var(--ok); }
.task.done .check { background: var(--ok); border-color: var(--ok); }
.task.done .task-title { text-decoration: line-through; color: var(--muted); }
.task.done { opacity: .75; }

/* Empty */
.empty { text-align: center; padding: 40px 20px; background: var(--card); border: 2px dashed var(--border); border-radius: var(--radius); }
.empty-icon { font-size: 52px; }
.empty h3 { margin: 8px 0 4px; }
.empty p { color: var(--muted); }
.footer { text-align: center; color: var(--muted); font-size: .8rem; margin-top: 26px; }

/* Toasts */
.toasts { position: fixed; left: 0; right: 0; bottom: 18px; display: grid; gap: 8px; justify-items: center; padding: 0 14px; pointer-events: none; z-index: 50; }
.toast {
  border: 1px solid var(--border);
  pointer-events: auto; display: flex; gap: 14px; align-items: center; max-width: 420px; width: 100%;
  background: #1c2237; color: #fff; padding: 12px 16px; border-radius: 14px; box-shadow: 0 10px 30px rgba(0, 0, 0, .3);
  animation: pop .3s ease; font-size: .9rem;
}
.toast span { flex: 1; }
.toast button { background: none; border: none; color: #a9abff; font: inherit; font-weight: 700; cursor: pointer; padding: 6px; }

/* Modal */
.modal { position: fixed; top: 0; right: 0; bottom: 0; left: 0; background: rgba(10, 14, 30, .6); display: grid; place-items: center; padding: 16px; z-index: 100; }
.modal[hidden] { display: none; }
.modal-box { background: var(--card); border-radius: 18px; padding: 22px; max-width: 380px; width: 100%; box-shadow: var(--shadow); animation: pop .25s ease; }
.modal-box p { color: var(--muted); margin: 8px 0 18px; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; }

.check::after { content: ""; position: absolute; top: -9px; right: -9px; bottom: -9px; left: -9px; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.sr-only {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}
input[type="date"] { min-height: 48px; -webkit-appearance: none; appearance: none; }
body.modal-open { overflow: hidden; }

/* Category manager */
.manage { margin-bottom: 14px; border: 1px solid var(--border); border-radius: 12px; background: var(--bg); }
.manage summary { padding: 12px; min-height: 44px; font-size: .85rem; font-weight: 600; color: var(--muted); cursor: pointer; }
.manage-body { padding: 0 12px 12px; }
.cat-add { display: flex; gap: 8px; }
.cat-add input { margin: 0; }
.cat-list { list-style: none; display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.cat-list li { display: flex; align-items: center; gap: 4px; padding: 2px 4px 2px 12px; border-radius: 99px; background: var(--card); border: 1px solid var(--border); font-size: .85rem; overflow-wrap: anywhere; }
.cat-list button { width: 36px; height: 36px; border: none; background: none; border-radius: 50%; color: var(--danger); font-size: 1.1rem; cursor: pointer; }
.hint { color: var(--muted); font-size: .82rem; margin-top: 8px; }

@media (max-width: 380px) {
  .stats { gap: 4px; }
  .stat span { font-size: .62rem; }
}
@media (min-width: 600px) {
  .app { padding-top: 30px; }
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .stat b { font-size: 1.4rem; }
  .stat span { font-size: .78rem; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}

[data-theme="dark"] .bar { background: rgba(0, 0, 0, .25); }
[data-theme="dark"] .stat { background: rgba(0, 0, 0, .16); }

/* ========== UI POLISH ========== */
.icon { width: 14px; height: 14px; flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* Filter chips */
.filters { gap: 6px; }
.chip {
  flex: none; display: inline-flex; align-items: center; gap: 6px; min-height: 36px; padding: 6px 12px;
  border-radius: 99px; border: 1px solid var(--border); background: transparent; color: var(--muted);
  font: inherit; font-size: .85rem; font-weight: 500; cursor: pointer;
  transition: background .15s, color .15s, border-color .15s;
}
.chip:hover { border-color: var(--muted); color: var(--text); }
.chip.active { background: var(--text); border-color: var(--text); color: var(--bg); }
.chip em { font-style: normal; font-size: .72rem; font-weight: 600; padding: 1px 7px; border-radius: 99px; background: var(--border); color: var(--muted); }
.chip.active em { background: color-mix(in srgb, var(--bg) 22%, transparent); color: var(--bg); }
@media (pointer: coarse) { .chip { min-height: 44px; } }

/* Select dropdowns */
select {
  appearance: none; -webkit-appearance: none; padding-right: 36px; cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%238a93ad' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center;
}
input, select, textarea { transition: border-color .15s, box-shadow .15s; }
input:focus, select:focus, textarea:focus { box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 18%, transparent); }

/* Task card */
.task { border-radius: 14px; border-left-width: 3px; box-shadow: none; transition: border-color .15s, box-shadow .15s; }
.task:hover { box-shadow: var(--shadow); }
.task-body { min-width: 0; }
.task-head { display: flex; align-items: flex-start; gap: 8px; }
.task-title { flex: 1; min-width: 0; font-size: 1rem; font-weight: 600; line-height: 1.4; letter-spacing: -.01em; color: var(--text); overflow-wrap: anywhere; }
.task-desc { margin-top: 2px; font-size: .875rem; font-weight: 400; line-height: 1.5; color: var(--muted); overflow-wrap: anywhere; }
.check {
  width: 24px; height: 24px; margin-top: 1px; border-radius: 50%; border: 2px solid var(--muted); position: relative;
  background: transparent; color: var(--on-solid); font-size: 13px; font-weight: 700; cursor: pointer;
  transition: border-color .15s, background .15s;
}
.check:hover { border-color: var(--ok); }

/* Meta badges and tags: tidy, wrapping rows */
.meta, .tags { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 10px; }
.tags { margin-top: 6px; }
.badge {
  display: inline-flex; align-items: center; gap: 5px; max-width: 100%; padding: 2px 9px;
  border-radius: 99px; border: 1px solid var(--border); background: transparent;
  font-size: .75rem; font-weight: 500; line-height: 1.6; color: var(--muted);
}
.badge span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge.high { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, transparent); }
.badge.medium { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 40%, transparent); }
.badge.low { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, transparent); }
.badge.overdue { background: var(--danger); border-color: var(--danger); color: var(--on-solid); }
.badge.today { background: var(--warn); border-color: var(--warn); color: var(--on-solid); }
.badge.upcoming { color: var(--primary); border-color: color-mix(in srgb, var(--primary) 40%, transparent); }
.tag {
  max-width: 100%; padding: 1px 8px; border-radius: 6px; background: color-mix(in srgb, var(--primary) 10%, transparent);
  color: var(--primary); font-size: .75rem; font-weight: 500; line-height: 1.6; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* Subtle icon-only actions */
.actions { display: flex; gap: 2px; margin: -6px -6px 0 0; opacity: .5; transition: opacity .15s; }
.task:hover .actions, .task:focus-within .actions { opacity: 1; }
.icon-act {
  width: 36px; height: 36px; display: grid; place-items: center; border: none; border-radius: 10px;
  background: transparent; color: var(--muted); cursor: pointer; transition: background .15s, color .15s;
}
.icon-act .icon { width: 16px; height: 16px; }
.icon-act:hover { background: var(--bg); color: var(--text); }
.icon-act.del:hover { background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); }
@media (hover: none) { .actions { opacity: 1; } }
@media (pointer: coarse) { .icon-act { width: 44px; height: 44px; } }

.empty-icon { display: grid; place-items: center; color: var(--muted); }
.empty-icon .icon { width: 48px; height: 48px; stroke-width: 1.5; }

/* ========== ADVANCED LAYOUT ========== */
.layout { display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px; }
.content { min-width: 0; }
.sidebar { min-width: 0; padding: 12px; margin-bottom: 0; }
.side-title { font-size: .72rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 0 4px 8px; }
.side-nav { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.side-nav::-webkit-scrollbar { display: none; }
.side-item {
  flex: none; display: flex; align-items: center; gap: 8px; min-height: 40px; padding: 6px 12px; border-radius: 10px;
  border: none; background: transparent; color: var(--muted); font: inherit; font-size: .9rem; font-weight: 500; cursor: pointer; text-align: left;
  transition: background .15s, color .15s;
}
.side-item span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-item em { font-style: normal; font-size: .72rem; opacity: .8; }
.side-item:hover { background: var(--bg); color: var(--text); }
.side-item.active { background: color-mix(in srgb, var(--primary) 12%, transparent); color: var(--primary); font-weight: 600; }
@media (min-width: 900px) {
  .layout { grid-template-columns: 230px minmax(0, 1fr); align-items: start; gap: 24px; }
  .sidebar { position: sticky; top: 16px; }
  .side-nav { flex-direction: column; overflow: visible; }
  .side-item { width: 100%; }
}

/* Quick add */
.quick { display: flex; gap: 8px; padding: 12px; }
.quick input { margin: 0; }
.quick .btn { flex: none; }

/* Subtasks */
.subtasks { margin-top: 10px; }
.sub-head { display: flex; align-items: center; gap: 8px; font-size: .75rem; font-weight: 600; color: var(--muted); }
.sub-bar { flex: 1; height: 5px; border-radius: 99px; background: var(--border); overflow: hidden; }
.sub-bar i { display: block; height: 100%; background: var(--ok); border-radius: 99px; transition: width .3s; }
.sub { display: flex; align-items: center; gap: 8px; min-height: 32px; font-size: .875rem; cursor: pointer; }
.sub input { width: 18px; height: 18px; min-width: 0; margin: 0; padding: 0; accent-color: var(--primary); flex: none; }
.sub span { min-width: 0; overflow-wrap: anywhere; }
.sub.done span { text-decoration: line-through; color: var(--muted); }

/* Drag & drop */
.drag { touch-action: none; cursor: grab; }
.task.dragging { opacity: .6; box-shadow: var(--shadow); border-color: var(--primary); }

/* Calendar */
.cal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.cal-head .icon-act { font-size: 1.4rem; width: 44px; height: 44px; }
.cal-grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 4px; }
.cal-dow { text-align: center; font-size: .72rem; font-weight: 600; color: var(--muted); padding: 4px 0; }
.cal-day {
  position: relative; min-height: 44px; border-radius: 10px; border: 1px solid transparent; background: var(--bg);
  color: var(--text); font: inherit; font-size: .9rem; cursor: pointer; transition: background .15s, border-color .15s;
}
.cal-day:hover { border-color: var(--primary); }
.cal-day.today { border-color: var(--primary); font-weight: 700; }
.cal-day.selected { background: var(--primary); color: var(--on-primary); }
.cal-day small { position: absolute; right: 4px; top: 2px; font-size: .62rem; font-weight: 700; color: var(--primary); }
.cal-day.selected small { color: var(--on-primary); }

/* Safety net against sideways scrolling */
html { max-width: 100%; }
body { max-width: 100%; overflow-x: clip; }
.app, .content, .card, .progress-card, .task, .task-body { min-width: 0; max-width: 100%; }
.field, .row, .quick, .controls > * { min-width: 0; }
img, svg { max-width: 100%; }
