:root {
  color-scheme: light dark;
  --bg: #f8fafc;
  --surface: rgba(255, 255, 255, 0.86);
  --panel: #ffffff;
  --text: #101827;
  --muted: #64748b;
  --line: #e2e8f0;
  --soft: #f1f5f9;
  --accent: #0f766e;
  --accent-2: #2563eb;
  --danger: #b91c1c;
  --shadow: 0 12px 36px rgba(15, 23, 42, 0.08);
  --radius: 18px;
  --sticky-top: 66px;
  --bs-body-bg: var(--bg);
  --bs-body-color: var(--text);
  --bs-border-color: var(--line);
  --bs-tertiary-bg: var(--soft);
  --bs-emphasis-color: var(--text);
  --bs-secondary-color: var(--muted);
  --bs-font-sans-serif: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme-mode="light"]) {
    --bg: #09090b;
    --surface: rgba(24, 24, 27, 0.86);
    --panel: #18181b;
    --text: #f8fafc;
    --muted: #a1a1aa;
    --line: #27272a;
    --soft: #111113;
    --accent: #2dd4bf;
    --accent-2: #60a5fa;
    --danger: #f87171;
    --shadow: 0 12px 36px rgba(0, 0, 0, 0.32);
  }
}

:root[data-theme-mode="dark"] {
  --bg: #09090b;
  --surface: rgba(24, 24, 27, 0.86);
  --panel: #18181b;
  --text: #f8fafc;
  --muted: #a1a1aa;
  --line: #27272a;
  --soft: #111113;
  --accent: #2dd4bf;
  --accent-2: #60a5fa;
  --danger: #f87171;
  --shadow: 0 12px 36px rgba(0, 0, 0, 0.32);
}

html,
body {
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
}

button,
input,
select,
textarea {
  font: inherit;
}

.btn {
  --bs-btn-border-radius: 12px;
  --bs-btn-padding-x: 0.8rem;
  --bs-btn-padding-y: 0.45rem;
}

.btn-dark {
  --bs-btn-bg: #111827;
  --bs-btn-border-color: #111827;
  --bs-btn-hover-bg: #0f172a;
  --bs-btn-hover-border-color: #0f172a;
}

:root[data-theme-mode="dark"] .btn-dark {
  --bs-btn-bg: #f8fafc;
  --bs-btn-border-color: #f8fafc;
  --bs-btn-color: #111827;
  --bs-btn-hover-bg: #e2e8f0;
  --bs-btn-hover-border-color: #e2e8f0;
  --bs-btn-hover-color: #111827;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme-mode="light"]) .btn-dark {
    --bs-btn-bg: #f8fafc;
    --bs-btn-border-color: #f8fafc;
    --bs-btn-color: #111827;
    --bs-btn-hover-bg: #e2e8f0;
    --bs-btn-hover-border-color: #e2e8f0;
    --bs-btn-hover-color: #111827;
  }
}

.app-shell {
  min-height: 100vh;
}

.form-control,
.form-select {
  border-color: var(--line);
  border-radius: 12px;
  background-color: var(--panel);
  color: var(--text);
}

.form-control:focus,
.form-select:focus {
  border-color: var(--accent-2);
  box-shadow: 0 0 0 0.2rem rgba(37, 99, 235, 0.14);
}

.form-label {
  width: 100%;
  color: var(--muted);
  font-size: 13px;
}

.form-label .form-control,
.form-label .form-select {
  margin-top: 6px;
  color: var(--text);
  font-size: 15px;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: grid;
  grid-template-columns: minmax(170px, 1fr) auto minmax(220px, 1fr);
  gap: 16px;
  align-items: center;
  padding: 14px 20px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  backdrop-filter: blur(18px);
}

.mobile-nav {
  display: none;
}

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

.brand-mark {
  width: 34px;
  height: 34px;
  border-radius: 12px;
  background: linear-gradient(135deg, #111827, #2563eb 56%, #0f766e);
  box-shadow: var(--shadow);
}

.brand strong {
  display: block;
  letter-spacing: 0;
}

.brand small,
.screen-head p,
.picker-item small,
.day-column header span {
  color: var(--muted);
}

.app-tabs {
  display: inline-flex;
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--soft);
}

.app-tabs .nav-link {
  padding: 8px 12px;
  border-radius: 10px;
  color: var(--muted);
}

.app-tabs .nav-link.active,
.app-btn.active {
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 4px 16px rgba(15, 23, 42, 0.08);
}

.top-actions {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 8px;
}

.icon-button,
.small-button {
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 12px;
  white-space: nowrap;
}

.small-button {
  border: 1px solid var(--line);
  background: transparent;
  color: var(--text);
  box-shadow: none;
}

.app-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

#edit-mode.active {
  border-color: var(--accent-2);
  background: var(--panel);
  color: var(--accent-2);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  padding: 0;
}

.sync-status {
  display: inline-grid;
  place-items: center;
  width: 34px;
  height: 34px;
  min-width: 34px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--soft);
}

.sync-dot {
  width: 11px;
  height: 11px;
  border-radius: 999px;
  background: #94a3b8;
  box-shadow: 0 0 0 4px rgba(148, 163, 184, 0.16);
}

.sync-status.is-saved .sync-dot {
  background: #16a34a;
  box-shadow: 0 0 0 4px rgba(22, 163, 74, 0.16);
}

.sync-status.is-syncing .sync-dot,
.sync-status.is-server .sync-dot {
  background: #2563eb;
  box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.16);
}

.sync-status.is-dirty .sync-dot {
  background: #d97706;
  box-shadow: 0 0 0 4px rgba(217, 119, 6, 0.16);
}

.sync-status.is-offline .sync-dot {
  background: #64748b;
  box-shadow: 0 0 0 4px rgba(100, 116, 139, 0.16);
}

.sync-status.is-error .sync-dot {
  background: #dc2626;
  box-shadow: 0 0 0 4px rgba(220, 38, 38, 0.16);
}

.main {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 0;
  padding: 18px;
}

.activity-picker {
  display: none;
  position: sticky;
  top: 84px;
  align-self: start;
  max-height: calc(100vh - 104px);
  overflow: auto;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
}

.activity-picker.is-picker-visible {
  display: block;
}

.app-shell.has-picker .main {
  grid-template-columns: 250px minmax(0, 1fr);
  gap: 18px;
}

.app-shell.has-picker .view {
  grid-column: 2;
}

.activity-picker.is-picker-disabled {
  box-shadow: none;
}

.selected-purchase {
  display: grid;
  grid-template-columns: 12px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  width: 100%;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--soft);
  color: var(--text);
  text-align: left;
}

.selected-purchase:hover,
.selected-purchase:focus {
  border-color: var(--accent-2);
}

.selected-purchase.is-muted {
  pointer-events: none;
  opacity: 0.7;
}

.selected-purchase strong,
.selected-purchase small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.selected-purchase small,
.picker-hint {
  color: var(--muted);
  font-size: 12px;
}

.picker-actions {
  display: grid;
  margin-top: 10px;
}

.picker-hint {
  margin-top: 10px;
  line-height: 1.35;
}

.picker-head,
.screen-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 14px;
}

.screen-head h1 {
  margin: 0;
  font-size: clamp(24px, 3vw, 34px);
}

.screen-head p {
  margin: 3px 0 0;
}

.picker-list,
.picker-tree {
  display: grid;
  gap: 6px;
}

.picker-item,
.picker-tree-item {
  display: grid;
  grid-template-columns: 12px minmax(0, 1fr) auto;
  gap: 9px;
  align-items: center;
  width: 100%;
  padding: 9px 10px;
  border: 1px solid transparent;
  border-radius: 12px;
  background: transparent;
  color: var(--text);
  text-align: left;
}

.picker-item:hover,
.picker-item.is-selected,
.picker-tree-item:hover,
.picker-tree-item.is-selected {
  border-color: var(--line);
  background: var(--soft);
}

.picker-tree-node {
  display: grid;
  gap: 6px;
}

.picker-tree-item {
  padding-left: calc(10px + (var(--level, 1) - 1) * 18px);
}

.picker-tree-item.is-group {
  background: rgba(100, 116, 139, 0.06);
}

.picker-tree-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.picker-item > span:first-child,
.activity-marker {
  width: 10px;
  height: 10px;
  border-radius: 999px;
  background: var(--marker, var(--accent));
}

.picker-item.system > span:first-child,
.picker-tree-item.system .activity-marker {
  height: 2px;
  border-radius: 0;
  background: var(--muted);
}

.view {
  min-width: 0;
}

.warnings {
  display: grid;
  gap: 6px;
  margin-bottom: 14px;
  padding: 12px;
  border: 1px solid rgba(185, 28, 28, 0.24);
  border-radius: 14px;
  background: rgba(185, 28, 28, 0.08);
  color: var(--danger);
}

.week-scroll,
.stats-scroll {
  overflow-x: auto;
  padding-bottom: 10px;
}

.week-grid {
  display: grid;
  grid-template-columns: repeat(var(--day-count), 224px);
  gap: 12px;
  min-width: calc(var(--day-count) * 224px + (var(--day-count) - 1) * 12px);
}

.day-column {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--panel);
  box-shadow: var(--shadow);
  overflow: visible;
}

.day-column.is-extra {
  outline: 2px solid rgba(185, 28, 28, 0.22);
}

.day-column header {
  position: sticky;
  top: var(--sticky-top);
  z-index: 3;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
  border-bottom: 1px solid var(--line);
  background: var(--soft);
}

.day-body {
  display: grid;
  gap: 6px;
  padding: 8px;
}

.week-block,
.gap,
.day-end {
  width: 100%;
  border: 0;
  border-radius: 12px;
  text-align: left;
  transition: transform 160ms ease, box-shadow 160ms ease, opacity 160ms ease;
}

.week-block {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 6px;
  align-items: center;
  min-width: 0;
  min-height: 26px;
  padding: 4px 7px;
  box-shadow: 0 8px 18px rgba(15, 23, 42, 0.12);
}

.week-block:hover {
  transform: translateY(-1px);
  box-shadow: 0 10px 24px rgba(15, 23, 42, 0.16);
}

.week-block strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: left;
}

.week-block-times,
.week-block-times span,
.week-block em {
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  opacity: 0.82;
  font-size: 11px;
}

.week-block-times {
  display: grid;
  gap: 0;
  line-height: 0.95;
}

.week-block-times span {
  font-size: 10px;
}

.week-block em {
  font-style: normal;
  opacity: 0.9;
}

.week-block small {
  grid-column: 1 / -1;
  opacity: 0.75;
}

.gap {
  display: grid;
  place-items: center;
  border: 1px dashed var(--line);
  background: rgba(100, 116, 139, 0.08);
  color: var(--muted);
  font-size: 12px;
  cursor: pointer;
  opacity: 0.42;
}

.gap.is-editable,
.gap:hover {
  opacity: 1;
}

.day-end {
  display: flex;
  justify-content: space-between;
  align-items: center;
  min-height: 26px;
  padding: 4px 8px;
  border-radius: 7px;
  background: repeating-linear-gradient(90deg, transparent, transparent 6px, rgba(100, 116, 139, 0.22) 6px, rgba(100, 116, 139, 0.22) 10px);
  color: var(--muted);
  font-size: 12px;
}

.activity-tree {
  display: grid;
  gap: 6px;
}

.activity-row {
  display: grid;
  grid-template-columns: 12px minmax(0, 1fr) auto auto;
  gap: 10px;
  align-items: center;
  min-height: 54px;
  padding: 10px 12px;
  padding-left: calc(12px + (var(--level) - 1) * 12px);
  border: 1px solid var(--line);
  border-left: 4px solid var(--marker);
  border-radius: 12px;
  background:
    linear-gradient(90deg, rgba(100, 116, 139, 0.08) 0 1px, transparent 1px 25%, rgba(100, 116, 139, 0.08) 25% calc(25% + 1px), transparent calc(25% + 1px) 50%, rgba(100, 116, 139, 0.08) 50% calc(50% + 1px), transparent calc(50% + 1px) 75%, rgba(100, 116, 139, 0.08) 75% calc(75% + 1px), transparent calc(75% + 1px)),
    var(--panel);
}

.activity-row.is-group {
  background:
    linear-gradient(90deg, rgba(100, 116, 139, 0.08) 0 1px, transparent 1px 25%, rgba(100, 116, 139, 0.08) 25% calc(25% + 1px), transparent calc(25% + 1px) 50%, rgba(100, 116, 139, 0.08) 50% calc(50% + 1px), transparent calc(50% + 1px) 75%, rgba(100, 116, 139, 0.08) 75% calc(75% + 1px), transparent calc(75% + 1px)),
    linear-gradient(180deg, var(--panel), var(--soft));
}

.activity-row {
  margin-left: calc((var(--level) - 1) * 18px);
}

.activity-row.is-archived {
  opacity: 0.56;
}

.activity-main {
  min-width: 0;
}

.activity-main strong,
.activity-main small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.activity-main small {
  color: var(--muted);
}

.stats-table {
  min-width: max(1120px, calc(280px + var(--day-count, 7) * 132px));
  border-collapse: separate;
  border-spacing: 0;
  background:
    linear-gradient(90deg, rgba(37, 99, 235, 0.035), transparent 34%, rgba(15, 118, 110, 0.04)),
    var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  overflow: hidden;
}

.stats-table thead th {
  position: sticky;
  top: var(--sticky-top);
  z-index: 4;
  background: color-mix(in srgb, var(--panel) 88%, var(--soft));
}

.stats-table th,
.stats-table td {
  min-width: 120px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  border-right: 1px solid rgba(100, 116, 139, 0.12);
  white-space: nowrap;
  text-align: center;
}

.stats-table tbody tr:nth-child(odd) {
  background: linear-gradient(90deg, rgba(37, 99, 235, 0.055), rgba(15, 118, 110, 0.035));
}

.stats-table tbody tr:nth-child(even) {
  background: linear-gradient(90deg, rgba(100, 116, 139, 0.035), transparent);
}

.stats-table th:first-child {
  min-width: 280px;
  text-align: left;
}

.stats-table th:first-child,
.stats-table td:first-child {
  position: sticky;
  left: 0;
  z-index: 5;
  background: color-mix(in srgb, var(--panel) 88%, var(--soft));
}

.stats-table thead th:first-child {
  z-index: 7;
}

.stats-table tbody th {
  padding-left: calc(10px + (var(--level, 1) - 1) * 20px);
}

.stats-table .activity-marker {
  display: inline-block;
  margin-right: 8px;
  vertical-align: middle;
}

.warn-col {
  color: var(--danger);
}

.stats-table .service-row {
  background: linear-gradient(90deg, rgba(100, 116, 139, 0.12), rgba(37, 99, 235, 0.06));
}

.activity-select-button {
  display: grid;
  grid-template-columns: 12px minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--soft);
  color: var(--text);
  text-align: left;
}

.activity-select-button strong,
.activity-select-button small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.activity-select-button small {
  color: var(--muted);
}

.modal-backdrop {
  backdrop-filter: blur(5px);
}

.app-modal {
  border: 1px solid var(--line);
  border-radius: 22px;
  background: var(--panel);
  color: var(--text);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.modal-header,
.modal-body {
  border-color: var(--line);
}

.modal-title {
  font-size: 20px;
  line-height: 1.2;
}

.btn-close {
  filter: none;
}

:root[data-theme-mode="dark"] .btn-close {
  filter: invert(1) grayscale(100%) brightness(200%);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme-mode="light"]) .btn-close {
    filter: invert(1) grayscale(100%) brightness(200%);
  }
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

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

.dialog-actions.wrap {
  flex-wrap: wrap;
}

.color-grid {
  display: grid;
  gap: 10px;
  margin: 10px 0;
}

.color-family {
  display: grid;
  grid-template-columns: 92px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(100, 116, 139, 0.05);
}

.color-family-title {
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

.color-family-swatches {
  display: grid;
  grid-template-columns: repeat(8, minmax(22px, 1fr));
  gap: 6px;
}

.color-swatch {
  aspect-ratio: 1;
  min-height: 22px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--swatch);
}

.color-swatch.is-selected {
  outline: 2px solid var(--accent-2);
  outline-offset: 2px;
}

.check-line {
  display: flex;
  align-items: center;
  gap: 8px;
}

.danger-text {
  color: var(--danger);
}

.auth-screen {
  display: grid;
  min-height: 100vh;
  place-items: center;
  padding: 20px;
}

.auth-box {
  width: min(420px, 100%);
  padding: 24px;
  border: 1px solid var(--line);
  border-radius: 22px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  z-index: 80;
  max-width: min(420px, calc(100vw - 24px));
  padding: 10px 14px;
  border-radius: 999px;
  background: var(--text);
  color: var(--bg);
  opacity: 0;
  transform: translate(-50%, 8px);
  transition: opacity 160ms ease, transform 160ms ease;
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

@media (min-width: 861px) {
  :root {
    --sticky-top: 0px;
  }
}

@media (max-width: 860px) {
  :root {
    --sticky-top: 47px;
  }

  body {
    padding-bottom: 62px;
  }

  .topbar {
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
    padding: 6px 8px;
  }

  .brand-mark {
    width: 28px;
    height: 28px;
    border-radius: 9px;
  }

  .brand strong {
    line-height: 1.05;
  }

  .brand small {
    display: none;
  }

  .desktop-tabs {
    display: none;
  }

  .mobile-nav {
    position: fixed;
    left: 50%;
    bottom: 14px;
    z-index: 50;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 4px;
    width: min(360px, calc(100vw - 28px));
    padding: 4px;
    border: 1px solid var(--line);
    border-radius: 20px;
    background: var(--surface);
    box-shadow: var(--shadow);
    backdrop-filter: blur(18px);
    transform: translateX(-50%);
  }

  .mobile-nav .nav-link {
    display: grid;
    gap: 2px;
    place-items: center;
    padding: 5px 4px;
    border-radius: 15px;
    color: var(--muted);
    font-size: 10px;
  }

  .mobile-nav .nav-link i {
    font-size: 15px;
  }

  .mobile-nav .nav-link.active {
    background: var(--panel);
    color: var(--text);
    box-shadow: 0 6px 18px rgba(15, 23, 42, 0.12);
  }

  .top-actions {
    justify-content: flex-end;
    overflow-x: visible;
    gap: 6px;
  }

  #edit-mode {
    padding: 0;
  }

  .sync-status,
  .icon-button {
    width: 32px;
    height: 32px;
    min-width: 32px;
  }

  .main {
    grid-template-columns: 1fr;
    padding: 10px;
  }

  .app-shell.has-picker .main {
    grid-template-columns: 1fr;
  }

  .activity-picker {
    display: none;
    position: static;
    max-height: none;
  }

  .activity-picker.is-picker-visible {
    display: block;
  }

  .week-grid {
    min-width: calc(var(--day-count) * 214px + (var(--day-count) - 1) * 10px);
    grid-template-columns: repeat(var(--day-count), 214px);
    gap: 10px;
  }

  .week-scroll,
  .stats-scroll {
    max-height: calc(100vh - 152px);
    overflow: auto;
    overscroll-behavior: contain;
  }

  .day-column header,
  .stats-table thead th {
    top: 0;
  }

  .screen-head {
    margin-bottom: 8px;
  }

  .screen-head h1 {
    font-size: 24px;
  }

  .stats-table {
    min-width: max(760px, calc(168px + var(--day-count, 7) * 86px));
    font-size: 12px;
  }

  .stats-table th,
  .stats-table td {
    min-width: 82px;
    padding: 8px 9px;
  }

  .stats-table th:first-child {
    min-width: 168px;
    max-width: 168px;
    white-space: normal;
    line-height: 1.15;
  }

  .stats-table tbody th {
    padding-left: calc(8px + (var(--level, 1) - 1) * 10px);
  }

  .gap {
    display: none;
  }

  .gap.is-editable {
    display: grid;
  }

  .form-grid {
    grid-template-columns: 1fr;
  }

  .color-grid {
    gap: 8px;
  }

  .color-family {
    grid-template-columns: 1fr;
  }

  .color-family-swatches {
    grid-template-columns: repeat(8, minmax(24px, 1fr));
  }
}
