/* =============================================
   CSS Reset & Base Styles
   ============================================= */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary-color: #2c3e50;
    --primary-light: #34495e;
    --primary-lighter: #4a6785;
    --primary-lightest: #5d7d9a;
    --bg-color: #f5f6fa;
    --white: #ffffff;
    --text-color: #2c3e50;
    --text-muted: #7f8c8d;
    --border-color: #dcdde1;
    --shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 4px 16px rgba(0, 0, 0, 0.15);
    --radius: 10px;
    --radius-sm: 6px;
    --navbar-height: 56px;
    --transition: 0.25s ease;
}

html {
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    line-height: 1.5;
    min-height: 100vh;
    overflow-x: hidden;
}

button {
    cursor: pointer;
    border: none;
    background: none;
    font-family: inherit;
    font-size: inherit;
    color: inherit;
    -webkit-tap-highlight-color: transparent;
}

input, select {
    font-family: inherit;
    font-size: inherit;
    -webkit-tap-highlight-color: transparent;
}

.hidden {
    display: none !important;
}

/* =============================================
   Navigation Bar
   ============================================= */
.navbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--navbar-height);
    background: var(--primary-color);
    color: var(--white);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
    z-index: 1000;
    box-shadow: var(--shadow);
}

.navbar-title {
    font-size: 1.25rem;
    font-weight: 600;
    letter-spacing: 0.02em;
}

.navbar-btn {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    color: var(--white);
    transition: background var(--transition);
}

.navbar-btn:hover {
    background: rgba(255, 255, 255, 0.15);
}

.navbar-btn:active {
    background: rgba(255, 255, 255, 0.25);
}

/* =============================================
   Main Content
   ============================================= */
.main-content {
    padding-top: calc(var(--navbar-height) + 16px);
    padding-bottom: 48px;
    padding-left: 12px;
    padding-right: 12px;
    max-width: 500px;
    margin: 0 auto;
}

/* =============================================
   Calendar
   ============================================= */
.calendar-container {
    background: var(--white);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 16px;
    overflow: hidden;
}

.calendar-nav {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 16px;
}

.cal-nav-btn {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    color: var(--text-color);
    transition: background var(--transition);
}

.cal-nav-btn:hover {
    background: var(--bg-color);
}

.cal-nav-btn:active {
    background: var(--border-color);
}

.cal-month-title {
    font-size: 1.1rem;
    font-weight: 600;
    text-transform: capitalize;
}

.calendar-grid {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 2px;
}

.cal-header {
    text-align: center;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-muted);
    padding: 6px 0;
    text-transform: uppercase;
}

.cal-days {
    grid-column: 1 / -1;
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 2px;
}

.cal-day {
    aspect-ratio: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.875rem;
    font-weight: 500;
    border-radius: var(--radius-sm);
    position: relative;
}

.cal-day.empty {
    background: transparent;
}

.cal-day.today {
    outline: 2px solid var(--primary-color);
    outline-offset: -2px;
    font-weight: 700;
}

/* =============================================
   Calendar Legend
   ============================================= */
.calendar-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    margin-top: 16px;
    padding-top: 12px;
    border-top: 1px solid var(--border-color);
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.8rem;
    color: var(--text-muted);
}

.legend-swatch {
    width: 14px;
    height: 14px;
    border-radius: 3px;
    border: 1px solid rgba(0, 0, 0, 0.1);
    flex-shrink: 0;
}

/* =============================================
   Settings Overlay
   ============================================= */
.settings-overlay {
    position: fixed;
    top: var(--navbar-height);
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--bg-color);
    z-index: 900;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.settings-panel {
    max-width: 500px;
    margin: 0 auto;
    padding: 16px 12px 40px;
}

.settings-section {
    background: var(--white);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 16px;
    margin-bottom: 16px;
}

.settings-section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
}

.settings-label {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-color);
    display: block;
    margin-bottom: 8px;
}

.settings-section-header .settings-label {
    margin-bottom: 0;
}

.settings-input {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    font-size: 1rem;
    color: var(--text-color);
    background: var(--white);
    transition: border-color var(--transition);
    -webkit-appearance: none;
    appearance: none;
}

.settings-input:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(44, 62, 80, 0.1);
}

/* =============================================
   Add Pattern Button
   ============================================= */
.btn-add {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    background: var(--primary-color);
    color: var(--white);
    transition: background var(--transition);
}

.btn-add:hover {
    background: var(--primary-lightest);
}

.btn-add:active {
    background: var(--primary-lightest);
}

/* =============================================
   Save Button
   ============================================= */
.btn-save {
    display: block;
    width: 100%;
    padding: 14px;
    background: var(--primary-color);
    color: var(--white);
    font-size: 1rem;
    font-weight: 600;
    border: none;
    border-radius: var(--radius);
    transition: background var(--transition);
    letter-spacing: 0.02em;
}

.btn-save:hover {
    background: var(--primary-lighter);
}

.btn-save:active {
    background: var(--primary-lighter);
}

/* =============================================
   Pattern List
   ============================================= */
.pattern-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.pattern-item {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--bg-color);
    border-radius: var(--radius-sm);
    padding: 10px;
    transition: box-shadow var(--transition);
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(-5px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Drag Handle */
.drag-handle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    min-height: 44px;
    cursor: grab;
    color: var(--text-muted);
    flex-shrink: 0;
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
    border-radius: var(--radius-sm);
    transition: background var(--transition), color var(--transition);
}

.drag-handle:hover {
    background: var(--border-color);
    color: var(--text-color);
}

.drag-handle:active {
    cursor: grabbing;
    background: var(--border-color);
    color: var(--text-color);
}

.drag-handle svg {
    pointer-events: none;
}

/* Reorder Controls */
.pattern-reorder {
    display: flex;
    flex-direction: column;
    gap: 2px;
    flex-shrink: 0;
}

.btn-reorder {
    width: 36px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    transition: background var(--transition), color var(--transition);
}

.btn-reorder:hover {
    background: var(--border-color);
    color: var(--text-color);
}

.btn-reorder:active {
    background: var(--primary-color);
    color: var(--white);
}

/* Drag & Drop States */
.pattern-item.dragging {
    opacity: 0.4;
    background: var(--border-color);
}

.pattern-item.drag-over-top {
    box-shadow: 0 -3px 0 0 var(--primary-color);
}

.pattern-item.drag-over-bottom {
    box-shadow: 0 3px 0 0 var(--primary-color);
}

.drag-ghost {
    position: fixed;
    pointer-events: none;
    z-index: 2000;
    opacity: 0.92;
    box-shadow: var(--shadow-lg);
    border-radius: var(--radius-sm);
    background: var(--bg-color);
    transition: none;
}

/* Pattern Fields */
.pattern-fields {
    flex: 1;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
    min-width: 0;
}

.pattern-name {
    flex: 1;
    min-width: 80px;
    padding: 8px 10px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    font-size: 0.875rem;
    color: var(--text-color);
    background: var(--white);
    transition: border-color var(--transition);
}

.pattern-name:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(44, 62, 80, 0.1);
}

/* Color Picker */
.pattern-color-wrapper {
    position: relative;
    flex-shrink: 0;
}

.pattern-color-btn {
    width: 36px;
    height: 36px;
    border-radius: var(--radius-sm);
    border: 2px solid var(--border-color);
    cursor: pointer;
    transition: border-color var(--transition);
}

.pattern-color-btn:hover {
    border-color: var(--primary-color);
}

/* Color Palette Popup */
.color-palette {
    position: absolute;
    top: 42px;
    right: 0;
    background: var(--white);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-lg);
    padding: 8px;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 6px;
    z-index: 1100;
    animation: fadeIn 0.15s ease;
}

.color-palette::before {
    content: '';
    position: absolute;
    top: -6px;
    right: 10px;
    width: 12px;
    height: 12px;
    background: var(--white);
    transform: rotate(45deg);
    box-shadow: -2px -2px 4px rgba(0, 0, 0, 0.05);
}

.color-swatch {
    width: 32px;
    height: 32px;
    border-radius: 4px;
    border: 2px solid transparent;
    cursor: pointer;
    transition: border-color 0.15s ease;
}

.color-swatch.selected {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 2px rgba(44, 62, 80, 0.3);
}

/* Days Selector */
.pattern-days {
    width: 60px;
    padding: 8px 6px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    font-size: 0.875rem;
    color: var(--text-color);
    background: var(--white);
    text-align: center;
    -webkit-appearance: none;
    appearance: none;
    cursor: pointer;
}

.pattern-days:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(44, 62, 80, 0.1);
}

/* Delete Button */
.btn-delete {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    color: #e74c3c;
    flex-shrink: 0;
    transition: background var(--transition);
}

.btn-delete:hover {
    background: rgba(231, 76, 60, 0.1);
}

.btn-delete:active {
    background: rgba(231, 76, 60, 0.2);
}

/* =============================================
   Color Palette Backdrop
   ============================================= */
.palette-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1050;
}

/* =============================================
   Touch Device Fix – prevent sticky :hover
   ============================================= */
@media (hover: none) {
    .navbar-btn:hover {
        background: none;
    }

    .cal-nav-btn:hover {
        background: none;
    }

    .cal-day:hover {
        background: inherit;
    }

    .btn-add:hover {
        background: var(--primary-color);
    }

    .btn-save:hover {
        background: var(--primary-color);
    }

    .btn-reorder:hover {
        background: none;
        color: var(--text-muted);
    }

    .drag-handle:hover {
        background: none;
        color: var(--text-muted);
    }

    .btn-delete:hover {
        background: none;
    }

    .pattern-color-btn:hover {
        border-color: var(--border-color);
    }

    /* Enhanced :active feedback for touch devices */
    .navbar-btn:active {
        background: rgba(255, 255, 255, 0.25);
    }

    .cal-nav-btn:active {
        background: var(--border-color);
    }

    .btn-add:active {
        background: var(--primary-lightest);
    }

    .btn-save:active {
        background: var(--primary-lightest);
    }

    .btn-reorder:active {
        background: var(--primary-color);
        color: var(--white);
    }

    .drag-handle:active {
        background: var(--border-color);
        color: var(--text-color);
    }

    .btn-delete:active {
        background: rgba(231, 76, 60, 0.2);
    }

    .pattern-color-btn:active {
        border-color: var(--primary-color);
    }
}

/* =============================================
   Responsive Adjustments
   ============================================= */
@media (max-width: 380px) {
    .cal-day {
        font-size: 0.8rem;
    }

    .pattern-fields {
        gap: 6px;
    }

    .pattern-name {
        min-width: 60px;
        padding: 7px 8px;
        font-size: 0.8rem;
    }

    .pattern-days {
        width: 52px;
        padding: 7px 4px;
        font-size: 0.8rem;
    }

    .pattern-color-btn {
        width: 32px;
        height: 32px;
    }

    .btn-reorder {
        width: 32px;
        height: 28px;
    }

    .drag-handle {
        width: 28px;
    }
}

@media (min-width: 501px) {
    .main-content {
        padding-left: 24px;
        padding-right: 24px;
    }

    .settings-panel {
        padding-left: 24px;
        padding-right: 24px;
    }

    .calendar-container {
        padding: 20px;
    }

    .cal-day {
        font-size: 0.95rem;
    }
}

/* =============================================
   Footer
   ============================================= */
.site-footer {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    text-align: center;
    padding: 10px 16px;
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--primary-color);
    background: transparent;
    pointer-events: none;
    z-index: 999;
}

@keyframes rainbowFlash {
    0%   { color: #e74c3c; }
    14%  { color: #e67e22; }
    28%  { color: #f1c40f; }
    42%  { color: #2ecc71; }
    57%  { color: #3498db; }
    71%  { color: #9b59b6; }
    85%  { color: #e91e63; }
    100% { color: #e74c3c; }
}

.rainbow-text {
    animation: rainbowFlash 4s ease-in-out infinite;
    font-weight: 700;
}

/* =============================================
   Safe Area (notch) Support
   ============================================= */
@supports (padding-top: env(safe-area-inset-top)) {
    .navbar {
        padding-top: env(safe-area-inset-top);
        height: calc(var(--navbar-height) + env(safe-area-inset-top));
    }

    .main-content {
        padding-top: calc(var(--navbar-height) + env(safe-area-inset-top) + 16px);
    }

    .settings-overlay {
        top: calc(var(--navbar-height) + env(safe-area-inset-top));
    }
}
