/* ── Palette ── */
:root {
  --bg: #f8f8fa;
  --bg-card: #ffffff;
  --bg-hover: #f0f0f4;
  --bg-input: #f4f4f7;
  --border: #e4e4e9;
  --border-light: #eeeeef;
  --text: #1a1a2e;
  --text-2: #5a5a72;
  --text-3: #8e8ea0;
  --accent: #6c5ce7;
  --accent-soft: #e8e4fd;
  --accent-glow: rgba(108, 92, 231, 0.12);
  --danger: #e74c3c;
  --danger-soft: #fde8e6;
  --green: #00b894;
  --green-soft: #e6f9f3;
  --pill-monthly: #dfe6fd;
  --pill-yearly: #fef3cd;
  --pill-monthly-text: #4a4fc7;
  --pill-yearly-text: #856404;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.04);
  --shadow-md: 0 4px 20px rgba(0, 0, 0, 0.06);
  --shadow-lg: 0 12px 40px rgba(0, 0, 0, 0.1);
  --shadow-pop: 0 20px 60px rgba(0, 0, 0, 0.15), 0 0 0 1px rgba(0, 0, 0, 0.03);
  --radius: 10px;
  --radius-lg: 16px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e0e14;
    --bg-card: #1a1a26;
    --bg-hover: #22222e;
    --bg-input: #16161f;
    --border: #2a2a3a;
    --border-light: #222230;
    --text: #ededf0;
    --text-2: #9898aa;
    --text-3: #5e5e70;
    --accent: #a29bfe;
    --accent-soft: #262040;
    --accent-glow: rgba(162, 155, 254, 0.1);
    --danger: #ff6b6b;
    --danger-soft: #2e1515;
    --green: #55efc4;
    --green-soft: #112e24;
    --pill-monthly: #262040;
    --pill-yearly: #302a15;
    --pill-monthly-text: #a29bfe;
    --pill-yearly-text: #f0c040;
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.2);
    --shadow-md: 0 4px 20px rgba(0, 0, 0, 0.25);
    --shadow-lg: 0 12px 40px rgba(0, 0, 0, 0.4);
    --shadow-pop: 0 20px 60px rgba(0, 0, 0, 0.5), 0 0 0 1px rgba(255, 255, 255, 0.05);
  }
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100dvh;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* View visibility — class-based toggle instead of hidden attribute */
.is-hidden {
  display: none !important;
}

/* ── App Shell ── */
.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 520px;
  margin-inline: auto;
}

/* ── Header ── */
.header {
  flex-shrink: 0;
  padding: 0.375rem 0.5rem 0;
}

.header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.month-chrome {
  flex-shrink: 0;
  padding: 0.25rem 0.5rem 0;
}

.month-nav {
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.month-title {
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  text-align: center;
  user-select: none;
  cursor: pointer;
  padding: 0.25rem 0.5rem;
  border-radius: var(--radius);
  transition: background 0.15s, color 0.15s;

  &:hover {
    color: var(--accent);
    background: var(--bg-hover);
  }

  &:focus-visible {
    outline: none;
    color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-glow);
  }
}

.nav-btn, .action-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--text-2);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;

  &:hover {
    background: var(--bg-hover);
    color: var(--text);
  }

  &:active {
    scale: 0.92;
  }
}

.header-actions {
  display: flex;
  gap: 0.125rem;
}

#btn-quick-add {
  color: var(--accent);

  &:hover {
    background: var(--accent-soft);
  }
}

/* ── Filter Bar ── */
.filter-bar {
  margin-bottom: 0.25rem;
}

.toggle-filter {
  max-width: 100%;
}

/* ── Total Bar ── */
.total-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 0.625rem 0.75rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  cursor: pointer;
  font-family: inherit;
  text-align: left;
  color: var(--text);
  transition: box-shadow 0.2s, border-color 0.2s;
  box-shadow: var(--shadow-sm);
  margin-bottom: 0.25rem;

  &:hover {
    border-color: var(--accent);
    box-shadow: var(--shadow-md), 0 0 0 3px var(--accent-glow);
  }

  &:active {
    scale: 0.99;
  }
}

.total-content {
  display: flex;
  align-items: baseline;
  gap: 0.125rem;
}

.total-amount {
  font-size: 1.375rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
}

.total-suffix {
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--text-3);
}

.total-hint {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  font-size: 0.75rem;
  color: var(--text-3);
  transition: color 0.15s;

  .total-bar:hover & {
    color: var(--accent);
  }
}

/* ── Calendar ── */
.calendar {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 0 0.25rem 0.25rem;
  min-height: 0;
}

.weekday-header {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  text-align: center;
  font-size: 0.6875rem;
  font-weight: 600;
  color: var(--text-3);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding-bottom: 0.375rem;
  flex-shrink: 0;
}

.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  grid-template-rows: repeat(6, 1fr);
  gap: 2px;
  flex: 1;
  min-height: 0;
}

.day-cell {
  background: var(--bg-card);
  border-radius: 8px;
  padding: 0.25rem;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  cursor: pointer;
  transition: background 0.12s, box-shadow 0.12s;
  position: relative;

  &:hover {
    background: var(--bg-hover);
    box-shadow: inset 0 0 0 1px var(--border);
  }

  &.outside {
    background: transparent;
    opacity: 0.25;
    pointer-events: none;
  }

  &.today {
    background: var(--accent-soft);
    box-shadow: inset 0 0 0 1.5px var(--accent);
  }

  &.today:hover {
    background: var(--accent-soft);
  }

  &.has-subs {
    background: var(--bg-card);
    box-shadow: var(--shadow-sm);
  }
}

.day-number {
  font-size: 0.6875rem;
  font-weight: 500;
  color: var(--text-3);
  line-height: 1;
  margin-bottom: 0.125rem;
  font-variant-numeric: tabular-nums;
}

.today .day-number {
  color: var(--accent);
  font-weight: 700;
}

.day-subs {
  display: flex;
  flex-direction: column;
  gap: 1px;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;

  &::-webkit-scrollbar {
    width: 4px;
  }

  &::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 2px;
  }

  &::-webkit-scrollbar-track {
    background: transparent;
  }
}

.day-sub-item {
  display: flex;
  align-items: center;
  gap: 3px;
  padding: 1px 3px;
  border-radius: 4px;
  font-size: 0.5625rem;
  font-weight: 500;
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
  color: var(--text-2);
  transition: background 0.1s;

  &:hover {
    background: var(--accent-glow);
    color: var(--text);
  }

  & img {
    width: 12px;
    height: 12px;
    flex-shrink: 0;
    border-radius: 3px;
  }
}

/* ── Empty State ── */
.empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  text-align: center;
  padding: 2rem;
  pointer-events: none;
}

.empty-state-icon {
  font-size: 2.5rem;
  opacity: 0.4;
}

.empty-state-text {
  font-size: 0.875rem;
  color: var(--text-3);
  line-height: 1.5;
}

/* ── Popovers ── */
[popover] {
  position: fixed;
  inset: 0;
  margin: auto;
  width: fit-content;
  height: fit-content;
  border: none;
  border-radius: var(--radius-lg);
  background: var(--bg-card);
  color: var(--text);
  box-shadow: var(--shadow-pop);
  padding: 0;
  max-width: min(26rem, calc(100vw - 2rem));
  width: 100%;
  max-height: 90dvh;
  overflow-y: auto;
  font-family: inherit;
  opacity: 0;
  scale: 0.97;
  transition:
    opacity 0.2s ease,
    scale 0.2s ease,
    display 0.2s allow-discrete;

  &:popover-open {
    opacity: 1;
    scale: 1;

    @starting-style {
      opacity: 0;
      scale: 0.97;
    }
  }

  &::backdrop {
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    opacity: 0;
    transition: opacity 0.2s ease, display 0.2s allow-discrete;
  }

  &:popover-open::backdrop {
    opacity: 1;

    @starting-style {
      opacity: 0;
    }
  }

  /* Overscroll containment */
  overscroll-behavior: contain;
}

@media (max-width: 639px) {
  [popover] {
    margin: auto 0.25rem 0.25rem;
    max-width: calc(100vw - 0.5rem);
    max-height: 88dvh;
    border-radius: var(--radius-lg);
  }
}

@media (max-width: 359px) {
  .field-row {
    grid-template-columns: 1fr;
    gap: 0;
  }

  .pop-body {
    padding: 0.875rem;
  }

  .pop-header {
    padding: 0.75rem 0.875rem;
  }
}

.pop-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border-light);
  position: sticky;
  top: 0;
  background: var(--bg-card);
  z-index: 1;

  & h2 {
    font-size: 1.0625rem;
    font-weight: 700;
    letter-spacing: -0.01em;
  }
}

.pop-close {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  border: none;
  border-radius: 50%;
  background: var(--bg-hover);
  color: var(--text-3);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;

  &:hover {
    background: var(--danger-soft);
    color: var(--danger);
  }
}

.pop-body {
  padding: 1.25rem;
}

.pop-actions {
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
  padding-top: 0.75rem;
  margin-top: 0.25rem;
}

.spacer { flex: 1; }

/* ── Forms ── */
.field {
  display: block;
  margin-bottom: 1rem;
}

.field-label {
  display: block;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-3);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 0.375rem;
}

.field input[type="text"],
.field input[type="url"],
.field input[type="number"],
.field select {
  width: 100%;
  padding: 0.625rem 0.75rem;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-input);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9375rem;
  transition: border-color 0.15s, box-shadow 0.15s;

  &::placeholder {
    color: var(--text-3);
  }

  &:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-glow);
  }
}

.field select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg width='12' height='8' viewBox='0 0 12 8' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1.5L6 6.5L11 1.5' stroke='%238e8ea0' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.75rem center;
  padding-right: 2.5rem;
}

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
}

.url-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;

  & input { flex: 1; }

  & .favicon-preview {
    flex-shrink: 0;
    border-radius: 5px;
    border: 1px solid var(--border);
    padding: 2px;
    background: var(--bg);
  }
}

/* ── Renewal date ── */
.renewal-row {
  display: flex;
  gap: 0.5rem;

  & input { flex: 1; }
  & select { flex: 1.2; }
}

.renewal-month {
  transition: opacity 0.15s, max-width 0.2s;
}

/* ── Toggle Group (segmented control) ── */
.toggle-group {
  display: flex;
  background: var(--bg-input);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  padding: 3px;
  gap: 2px;

  & input[type="radio"] {
    position: absolute;
    opacity: 0;
    pointer-events: none;
    width: 0;
    height: 0;
  }

  & label {
    flex: 1;
    text-align: center;
    padding: 0.3rem 0.25rem;
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--text-3);
    border-radius: 7px;
    cursor: pointer;
    transition: all 0.15s;
    user-select: none;
  }

  & input:checked + label {
    background: var(--bg-card);
    color: var(--text);
    box-shadow: var(--shadow-sm);
    font-weight: 600;
  }
}

.toggle-wide {
  max-width: 16rem;
}

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.375rem;
  padding: 0.5625rem 1.125rem;
  border-radius: var(--radius);
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  border: 1.5px solid transparent;
  font-family: inherit;
  transition: all 0.15s;
  white-space: nowrap;

  &:active {
    scale: 0.97;
  }
}

.btn-primary {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 2px 8px rgba(108, 92, 231, 0.3);

  &:hover {
    filter: brightness(1.1);
    box-shadow: 0 4px 14px rgba(108, 92, 231, 0.35);
  }
}

.btn-ghost {
  background: transparent;
  color: var(--text-2);
  border-color: var(--border);

  &:hover {
    background: var(--bg-hover);
    border-color: var(--accent);
    color: var(--accent);
  }
}

.btn-danger {
  background: var(--danger-soft);
  color: var(--danger);
  border-color: transparent;

  &:hover {
    background: var(--danger);
    color: #fff;
  }
}

.import-label {
  cursor: pointer;
}

/* ── Settings ── */
.settings-section {
  margin-bottom: 1rem;
}

.settings-heading {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-3);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 0.5rem;
}

.data-row {
  display: flex;
  gap: 0.5rem;
}

/* ── Breakdown ── */
.breakdown-hero {
  text-align: center;
  font-size: 2rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  padding: 0.5rem 0 1rem;
  font-variant-numeric: tabular-nums;

  & span {
    font-size: 1rem;
    font-weight: 500;
    color: var(--text-3);
  }
}

.breakdown-list {
  list-style: none;
}

.breakdown-item {
  display: grid;
  grid-template-columns: 32px 1fr auto auto;
  grid-template-rows: auto auto;
  gap: 0 0.625rem;
  padding: 0.625rem 0.25rem;
  border-top: 1px solid var(--border-light);
  align-items: center;
  transition: background 0.12s;

  &:first-child {
    border-top: none;
  }

  &:hover {
    background: var(--bg-hover);
    border-radius: var(--radius);
  }
}

.breakdown-favicon {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-input);
  border: 1px solid var(--border-light);
  overflow: hidden;
  grid-row: 1 / 3;

  & img {
    width: 20px;
    height: 20px;
  }
}

.breakdown-info {
  min-width: 0;
}

.breakdown-name {
  display: block;
  font-weight: 600;
  font-size: 0.875rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: inherit;
  text-decoration: none;
}

a.breakdown-name:hover {
  text-decoration: underline;
}

.breakdown-tags {
  grid-column: 2 / -1;
  display: flex;
  gap: 0.25rem;
  padding-top: 0.25rem;
}

.breakdown-cycle {
  display: inline-flex;
  font-size: 0.625rem;
  font-weight: 600;
  padding: 0.125rem 0.375rem;
  border-radius: 4px;
}

.cycle-monthly {
  background: var(--pill-monthly);
  color: var(--pill-monthly-text);
}

.cycle-yearly {
  background: var(--pill-yearly);
  color: var(--pill-yearly-text);
}

.cat-badge {
  display: inline-flex;
  font-size: 0.625rem;
  font-weight: 600;
  padding: 0.125rem 0.375rem;
  border-radius: 4px;
}

.cat-personal {
  background: var(--green-soft);
  color: var(--green);
}

.cat-business {
  background: var(--accent-soft);
  color: var(--accent);
}

.tag-renews {
  display: inline-flex;
  font-size: 0.625rem;
  font-weight: 600;
  padding: 0.125rem 0.375rem;
  border-radius: 4px;
  background: var(--danger-soft);
  color: var(--danger);
}

.breakdown-price {
  text-align: right;
  flex-shrink: 0;
}

.breakdown-converted {
  font-weight: 700;
  font-size: 0.9375rem;
  font-variant-numeric: tabular-nums;
}

.breakdown-original {
  font-size: 0.6875rem;
  color: var(--text-3);
}

.breakdown-actions {
  display: flex;
  gap: 0.125rem;
  flex-shrink: 0;

  & button {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 1.75rem;
    height: 1.75rem;
    border: none;
    background: none;
    border-radius: 6px;
    color: var(--text-3);
    cursor: pointer;
    font-size: 0.875rem;
    transition: all 0.12s;

    &:hover {
      background: var(--bg-hover);
      color: var(--text);
    }

    &[data-delete-id]:hover {
      background: var(--danger-soft);
      color: var(--danger);
    }
  }
}

.breakdown-empty {
  text-align: center;
  color: var(--text-3);
  font-size: 0.875rem;
  padding: 2rem 1rem;
}

/* ── Year View ── */
.year-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 0 0.5rem 0.5rem;
  min-height: 0;
  overflow-y: auto;
}

.year-filter {
  padding-bottom: 0.5rem;
}

.year-total {
  text-align: center;
  font-size: 0.75rem;
  color: var(--text-3);
  font-weight: 600;
  padding-bottom: 0.25rem;
}

.year-grid {
  display: flex;
  flex-direction: column;
  gap: 3px;
  flex: 1;
}

.year-month {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.375rem 0.5rem;
  background: var(--bg-card);
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.12s, box-shadow 0.12s;
  flex: 1;

  &:hover {
    box-shadow: inset 0 0 0 1px var(--border);
  }

  &.current-month {
    box-shadow: inset 0 0 0 1.5px var(--accent);
  }
}

.year-month-heat {
  width: 6px;
  height: 28px;
  border-radius: 3px;
  flex-shrink: 0;
  transition: background 0.2s;
}

.year-month-name {
  font-size: 0.8125rem;
  font-weight: 600;
  width: 2.5rem;
  flex-shrink: 0;
}

.year-month-bar {
  flex: 1;
  height: 6px;
  background: var(--bg-input);
  border-radius: 3px;
  overflow: hidden;
  min-width: 0;
}

.year-month-bar-fill {
  height: 100%;
  border-radius: 3px;
  transition: width 0.3s ease;
}

.year-month-amount {
  font-size: 0.8125rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  flex-shrink: 0;
  min-width: 4rem;
  text-align: right;
}

.year-month-count {
  font-size: 0.6875rem;
  color: var(--text-3);
  flex-shrink: 0;
  min-width: 1.5rem;
  text-align: right;
}

/* Active year view button */
#btn-year-view.active {
  color: var(--accent);
  background: var(--accent-soft);
}

/* ── Desktop ── */
@media (min-width: 640px) {
  .header {
    padding: 1rem 0.75rem 0;
  }

  .month-title {
    font-size: 1.375rem;
  }

  .total-amount {
    font-size: 1.75rem;
  }

  .total-suffix {
    font-size: 1rem;
  }

  .total-bar {
    padding: 0.75rem 1rem;
  }

  .nav-btn, .action-btn {
    width: 2.25rem;
    height: 2.25rem;
  }

  .toggle-group label {
    padding: 0.4rem 0.5rem;
    font-size: 0.8125rem;
  }

  .calendar {
    padding: 0 0.5rem 0.75rem;
  }

  .weekday-header {
    font-size: 0.75rem;
    padding-bottom: 0.5rem;
  }

  .calendar-grid {
    gap: 3px;
  }

  .day-cell {
    padding: 0.375rem;
    border-radius: 10px;
  }

  .day-number {
    font-size: 0.75rem;
  }

  .day-sub-item {
    font-size: 0.625rem;
    padding: 2px 4px;

    & img {
      width: 14px;
      height: 14px;
    }
  }

  .total-amount {
    font-size: 2rem;
  }
}

/* ── Wide screens: expand just the calendar ── */
@media (min-width: 900px) {
  .app {
    max-width: min(1100px, 92vw);
  }

  .header,
  .month-chrome,
  .year-view {
    width: 100%;
    max-width: 520px;
    margin-inline: auto;
  }
}
