.habit-page {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    min-width: 0;
}

.habit-toolbar {
    display: grid;
    grid-template-columns: auto minmax(260px, 1fr) auto;
    align-items: center;
    gap: 1rem;
}

.habit-view-switch {
    display: inline-grid;
    grid-template-columns: repeat(3, 1fr);
    padding: 3px;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-md);
    background: var(--bg-surface-1);
}

.habit-view-btn,
.habit-period-label,
.habit-icon-btn {
    border: 0;
    background: transparent;
    color: var(--text-secondary);
}

.habit-view-btn {
    min-height: 34px;
    padding: 0.35rem 0.75rem;
    border-radius: var(--radius-sm);
    font-weight: 600;
}

.habit-view-btn:hover,
.habit-view-btn.is-active {
    background: var(--bg-surface-3);
    color: var(--text-primary);
}

.habit-view-btn.is-active {
    box-shadow: inset 0 0 0 1px var(--border-light);
}

.habit-period-nav {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 0;
    gap: 0.375rem;
}

.habit-period-label {
    min-width: 210px;
    max-width: 100%;
    padding: 0.35rem 0.625rem;
    color: var(--text-primary);
    font-weight: 600;
    text-align: center;
    text-transform: capitalize;
}

.habit-icon-btn {
    position: relative;
    display: inline-flex;
    width: 36px;
    height: 36px;
    flex: 0 0 36px;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-sm);
    background: var(--bg-surface-1);
}

.habit-icon-btn:hover,
.habit-icon-btn.is-active {
    border-color: var(--border-light);
    background: var(--bg-surface-3);
    color: var(--text-primary);
}

.habit-toolbar-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 0.5rem;
}

.habit-google-status {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    color: var(--color-success);
    font-size: 0.75rem;
    font-weight: 600;
    white-space: nowrap;
}

.habit-archive-count {
    position: absolute;
    top: -6px;
    right: -6px;
    min-width: 18px;
    height: 18px;
    padding: 0 4px;
    border-radius: 9px;
    background: var(--color-warning);
    color: var(--text-inverse);
    font-size: 0.6875rem;
    font-weight: 700;
    line-height: 18px;
}

.habit-archive-panel {
    padding: 1rem;
    border-top: 1px solid var(--border-default);
    border-bottom: 1px solid var(--border-default);
    background: var(--bg-surface-0);
}

.habit-archive-panel[hidden],
.habit-empty[hidden],
.habit-loading[hidden],
.habit-matrix-scroll[hidden] {
    display: none;
}

.habit-section-heading {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 0.75rem;
}

.habit-section-heading h2 {
    margin: 0;
    font-size: 1rem;
}

.habit-section-heading p {
    margin: 0.125rem 0 0;
    font-size: 0.8125rem;
}

.habit-archive-list {
    display: grid;
    gap: 0.5rem;
}

.habit-archive-item {
    display: grid;
    grid-template-columns: 10px minmax(0, 1fr) auto;
    align-items: center;
    gap: 0.75rem;
    min-height: 52px;
    padding: 0.5rem 0.625rem;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-sm);
    background: var(--bg-surface-1);
}

.habit-archive-marker {
    width: 8px;
    height: 28px;
    border-radius: 3px;
}

.habit-archive-item strong,
.habit-archive-item span {
    display: block;
}

.habit-archive-item span {
    color: var(--text-muted);
    font-size: 0.75rem;
}

.habit-archive-empty {
    margin: 0;
    color: var(--text-muted);
    font-size: 0.875rem;
}

.habit-matrix-shell {
    min-height: 300px;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-sm);
    background: var(--bg-surface-1);
    overflow: hidden;
}

.habit-loading,
.habit-empty {
    display: flex;
    min-height: 300px;
    padding: 2rem;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
}

.habit-loading {
    gap: 0.625rem;
}

.habit-empty {
    flex-direction: column;
    text-align: center;
}

.habit-empty > i {
    margin-bottom: 0.75rem;
    color: var(--accent-secondary);
    font-size: 2rem;
}

.habit-empty h2 {
    margin: 0;
    font-size: 1rem;
}

.habit-empty p {
    margin: 0.25rem 0 1rem;
    font-size: 0.875rem;
}

.habit-matrix-scroll {
    max-width: 100%;
    overflow: auto;
    scrollbar-color: var(--border-light) var(--bg-surface-1);
}

.habit-matrix {
    width: max-content;
    min-width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    table-layout: fixed;
}

.habit-matrix th,
.habit-matrix td {
    height: 66px;
    padding: 0.5rem;
    border-right: 1px solid var(--border-default);
    border-bottom: 1px solid var(--border-default);
    background: var(--bg-surface-1);
}

.habit-matrix thead th {
    position: sticky;
    top: 0;
    z-index: 4;
    height: 62px;
    background: var(--bg-surface-2);
}

.habit-matrix tr:last-child th,
.habit-matrix tr:last-child td {
    border-bottom: 0;
}

.habit-name-column,
.habit-name-cell {
    position: sticky;
    left: 0;
    z-index: 3;
    width: 240px;
    min-width: 240px;
    max-width: 240px;
}

.habit-matrix thead .habit-name-column {
    z-index: 6;
    color: var(--text-secondary);
    font-size: 0.75rem;
    text-align: left;
    text-transform: uppercase;
}

.habit-name-cell {
    display: grid;
    grid-template-columns: 22px 5px minmax(0, 1fr) 32px;
    align-items: center;
    gap: 0.625rem;
    padding-left: 0.75rem !important;
    box-shadow: 4px 0 10px rgba(0, 0, 0, 0.12);
}

.habit-drag-handle {
    display: inline-flex;
    width: 22px;
    height: 34px;
    padding: 0;
    align-items: center;
    justify-content: center;
    border: 0;
    background: transparent;
    color: var(--text-muted);
    cursor: grab;
}

.habit-drag-handle:active {
    cursor: grabbing;
}

.habit-row.is-dragging {
    opacity: 0.45;
}

.habit-category-row th {
    height: 38px;
    padding: 0.45rem 0.75rem;
    border-bottom: 1px solid var(--border-default);
    background: var(--bg-surface-2);
    color: var(--text-secondary);
    font-size: 0.75rem;
    letter-spacing: 0.025em;
    text-align: left;
    text-transform: uppercase;
}

.habit-category-row th > span {
    display: inline-block;
    width: 8px;
    height: 8px;
    margin-right: 0.4rem;
    border-radius: 50%;
    background: var(--category-color);
}

.habit-category-row small {
    margin-left: 0.35rem;
    color: var(--text-muted);
}

.habit-row-marker {
    width: 5px;
    height: 34px;
    border-radius: 3px;
    background: var(--row-color);
}

.habit-name-button,
.habit-row-edit {
    min-width: 0;
    padding: 0;
    border: 0;
    background: transparent;
    color: inherit;
    text-align: left;
}

.habit-name-button strong,
.habit-name-button span {
    display: block;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.habit-name-button strong {
    color: var(--text-primary);
    font-size: 0.875rem;
}

.habit-name-button span {
    margin-top: 0.125rem;
    color: var(--text-muted);
    font-size: 0.6875rem;
    font-weight: 500;
}

.habit-row-edit {
    display: inline-flex;
    width: 32px;
    height: 32px;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    text-align: center;
}

.habit-row-edit:hover {
    background: var(--bg-surface-3);
    color: var(--text-primary);
}

.habit-date-heading,
.habit-day-cell {
    width: 54px;
    min-width: 54px;
    text-align: center;
}

.habit-date-heading span,
.habit-date-heading strong {
    display: block;
    letter-spacing: 0;
}

.habit-date-heading span {
    color: var(--text-muted);
    font-size: 0.6875rem;
    font-weight: 600;
    text-transform: capitalize;
}

.habit-date-heading strong {
    margin-top: 0.125rem;
    color: var(--text-secondary);
    font-size: 0.9375rem;
}

.habit-date-heading.is-today {
    box-shadow: inset 0 3px 0 var(--accent-secondary);
}

.habit-date-heading.is-today strong {
    color: var(--accent-secondary-light);
}

.habit-day-cell.is-off-plan,
.habit-day-cell.is-future {
    background: var(--bg-surface-0);
}

.habit-check {
    display: inline-flex;
    width: 30px;
    height: 30px;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--border-light);
    border-radius: var(--radius-sm);
    background: var(--bg-surface-2);
    color: transparent;
    transition: border-color var(--transition-fast), background var(--transition-fast), transform var(--transition-fast);
}

.habit-check:hover:not(:disabled) {
    border-color: var(--row-color);
    transform: translateY(-1px);
}

.habit-check.is-checked {
    border-color: var(--row-color);
    background: var(--row-color);
    color: #fff;
}

.habit-check:disabled {
    cursor: not-allowed;
    opacity: 0.3;
}

.habit-check.is-checked:disabled {
    opacity: 0.7;
}

.habit-progress-column,
.habit-progress-cell {
    position: sticky;
    right: 0;
    z-index: 3;
    width: 112px;
    min-width: 112px;
    border-right: 0 !important;
    box-shadow: -4px 0 10px rgba(0, 0, 0, 0.12);
}

.habit-matrix thead .habit-progress-column {
    z-index: 6;
    color: var(--text-secondary);
    font-size: 0.75rem;
    text-align: center;
    text-transform: uppercase;
}

.habit-progress-values {
    display: flex;
    align-items: baseline;
    justify-content: center;
    gap: 0.2rem;
}

.habit-progress-values strong {
    color: var(--text-primary);
    font-size: 0.875rem;
}

.habit-progress-values span {
    color: var(--text-muted);
    font-size: 0.75rem;
}

.habit-progress-track {
    width: 72px;
    height: 4px;
    margin: 0.375rem auto 0;
    border-radius: 2px;
    background: var(--bg-surface-3);
    overflow: hidden;
}

.habit-progress-track span {
    display: block;
    height: 100%;
    border-radius: inherit;
    background: var(--row-color);
}

.habit-drawer-backdrop {
    position: fixed;
    inset: 0;
    z-index: 1090;
    visibility: hidden;
    background: rgba(0, 0, 0, 0.58);
    opacity: 0;
    transition: opacity var(--transition-normal), visibility var(--transition-normal);
}

.habit-drawer-backdrop.is-open {
    visibility: visible;
    opacity: 1;
}

.habit-drawer {
    position: fixed;
    top: 0;
    right: 0;
    z-index: 1100;
    width: min(430px, 100vw);
    height: 100vh;
    padding: 1.25rem;
    border-left: 1px solid var(--border-default);
    background: var(--bg-surface-1);
    box-shadow: var(--shadow-lg);
    overflow-y: auto;
    transform: translateX(100%);
    transition: transform var(--transition-normal);
}

.habit-drawer.is-open {
    transform: translateX(0);
}

body.habit-drawer-open {
    overflow: hidden;
}

.habit-drawer-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 1.25rem;
}

.habit-drawer-header h2 {
    margin: 0;
    font-size: 1.125rem;
}

.habit-kicker {
    margin: 0 0 0.125rem;
    color: var(--accent-secondary);
    font-size: 0.6875rem;
    font-weight: 700;
    text-transform: uppercase;
}

.habit-form {
    display: grid;
    gap: 1rem;
}

.habit-category-control {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 42px;
    gap: 0.5rem;
}

.habit-calendar-field {
    padding: 0.75rem;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-sm);
    background: var(--bg-surface-0);
}

.habit-calendar-toggle {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    color: var(--text-primary);
    font-size: 0.8125rem;
}

.habit-calendar-settings {
    display: grid;
    grid-template-columns: 1fr 1.4fr;
    gap: 0.625rem;
    margin-top: 0.625rem;
}

.habit-calendar-settings label {
    display: grid;
    gap: 0.25rem;
}

.habit-calendar-settings label > span {
    color: var(--text-muted);
    font-size: 0.6875rem;
}

.habit-calendar-help {
    margin: 0.5rem 0 0;
    color: var(--text-muted);
    font-size: 0.6875rem;
    line-height: 1.45;
}

.habit-field {
    display: grid;
    min-width: 0;
    margin: 0;
    padding: 0;
    border: 0;
    gap: 0.375rem;
}

.habit-field > span,
.habit-field legend {
    width: auto;
    margin: 0;
    color: var(--text-secondary);
    font-size: 0.75rem;
    font-weight: 600;
}

.habit-day-picker {
    display: grid;
    grid-template-columns: repeat(7, minmax(0, 1fr));
    gap: 0.375rem;
}

.habit-day-picker input {
    position: absolute;
    opacity: 0;
    pointer-events: none;
}

.habit-day-picker span {
    display: flex;
    min-height: 36px;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-sm);
    background: var(--bg-surface-2);
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 0.6875rem;
    font-weight: 600;
}

.habit-day-picker input:checked + span {
    border-color: var(--accent-secondary);
    background: var(--color-info-subtle);
    color: var(--accent-secondary-light);
}

.habit-color-picker {
    display: grid;
    grid-template-columns: repeat(8, 32px);
    gap: 0.625rem;
}

.habit-color {
    width: 30px;
    height: 30px;
    padding: 0;
    border: 2px solid transparent;
    border-radius: 50%;
    background: var(--habit-color);
}

.habit-color.is-selected {
    border-color: var(--text-primary);
    box-shadow: 0 0 0 2px var(--bg-surface-1), 0 0 0 4px var(--habit-color);
}

.habit-form-error {
    padding: 0.75rem;
    border: 1px solid var(--color-error);
    border-radius: var(--radius-sm);
    background: var(--color-error-subtle);
    color: var(--text-primary);
    font-size: 0.8125rem;
}

.habit-form-error[hidden] {
    display: none;
}

.habit-drawer-actions {
    position: sticky;
    bottom: -1.25rem;
    display: flex;
    justify-content: flex-end;
    gap: 0.625rem;
    margin: 0.25rem -1.25rem -1.25rem;
    padding: 1rem 1.25rem;
    border-top: 1px solid var(--border-default);
    background: var(--bg-surface-1);
}

.habit-drawer-actions .btn-danger {
    margin-right: auto;
}

.habit-toast-stack {
    position: fixed;
    right: 1rem;
    bottom: 1rem;
    z-index: 1200;
    display: grid;
    gap: 0.5rem;
}

.habit-toast {
    width: min(340px, calc(100vw - 2rem));
    padding: 0.75rem 0.875rem;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-sm);
    background: var(--bg-surface-2);
    color: var(--text-primary);
    box-shadow: var(--shadow-md);
    font-size: 0.8125rem;
}

.habit-toast.success {
    border-color: var(--color-success);
}

.habit-toast.error {
    border-color: var(--color-error);
}

@media (max-width: 900px) {
    .habit-toolbar {
        grid-template-columns: 1fr auto;
    }

    .habit-period-nav {
        grid-column: 1 / -1;
        grid-row: 2;
        justify-content: flex-start;
    }
}

@media (max-width: 600px) {
    .habit-toolbar {
        grid-template-columns: 1fr;
    }

    .habit-toolbar-actions,
    .habit-toolbar-actions .btn {
        width: 100%;
    }

    .habit-toolbar-actions .btn {
        flex: 1;
    }

    .habit-period-nav {
        grid-column: auto;
        grid-row: auto;
    }

    .habit-period-label {
        min-width: 0;
        flex: 1;
        font-size: 0.8125rem;
    }

    .habit-name-column,
    .habit-name-cell {
        width: 178px;
        min-width: 178px;
        max-width: 178px;
    }

    .habit-progress-column,
    .habit-progress-cell {
        position: static;
        width: 92px;
        min-width: 92px;
        box-shadow: none;
    }

    .habit-day-picker {
        grid-template-columns: repeat(4, minmax(0, 1fr));
    }

    .habit-color-picker {
        grid-template-columns: repeat(4, 32px);
    }

    .habit-drawer-actions {
        flex-wrap: wrap;
    }

    .habit-drawer-actions .btn {
        flex: 1 1 auto;
    }
}
