/* ============================================================
   SENTINEL — Management Hub Phase 1: Month Calendar
   Per-field task calendar. Loads AFTER style.css and reuses its
   design tokens (palette, spacing, radii, shadows, typography).
   Rendered by JS into <section id="calendar-view" class="cal-view">.
   Authoring rule: prefer CSS logical properties so RTL mostly
   "just works"; explicit [dir="rtl"] rules only where needed.
   ============================================================ */


/* ---------- Per-type chip accent tokens ----------
   Distinct, readable palette — one accent per task type. Each chip
   uses a tinted background + solid left accent border + bright text. */
:root {
    --cal-sow: #34d399;
    /* green   — sowing/planting */
    --cal-fertilise: #ffab00;
    /* amber   — fertilising */
    --cal-spray: #b388ff;
    /* purple  — spraying */
    --cal-irrigate: #2979ff;
    /* blue    — irrigation */
    --cal-mow: #1de9b6;
    /* teal    — mowing/cutting */
    --cal-move_stock: #ff7043;
    /* orange  — moving livestock */
    --cal-scout: #90a4ae;
    /* slate   — scouting/walking */
    --cal-harvest: #ffd54f;
    /* gold    — harvest */
    --cal-soil_test: #a1887f;
    /* earthy  — soil testing */
    --cal-other: #b0bec5;
    /* grey    — anything else */
    --cal-stage: #4dd0e1;
    /* cyan    — derived growth-stage milestones */
}


/* ============================================================
   SHELL
   ============================================================ */

.cal-view {
    padding-block: var(--space-md);
}


/* ---------- Header: two-row layout (nav row + controls row) ---------- */
.cal-header {
    display: flex;
    flex-direction: column;
    gap: var(--space-sm);
    margin-bottom: var(--space-md);
}

/* Row 1: prev / period label / next */
.cal-header-nav {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
}

/* Row 2: view toggle + overlays toggle + add-event */
.cal-header-controls {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    flex-wrap: wrap;
    padding-top: var(--space-xs);
    border-top: 1px solid rgba(255, 255, 255, 0.07);
}

.cal-month-label {
    flex: 1;
    text-align: center;
    font-size: var(--font-size-lg);
    font-weight: 700;
    letter-spacing: -0.02em;
}

/* Prev / next month buttons — small glassy icon buttons */
.cal-nav-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    flex-shrink: 0;
    background: rgba(255, 255, 255, 0.06);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: var(--radius-md);
    color: var(--color-text-secondary);
    font-family: var(--font-family);
    font-size: var(--font-size-lg);
    line-height: 1;
    cursor: pointer;
    transition: background var(--transition-fast), color var(--transition-fast), border-color var(--transition-fast);
}

.cal-nav-btn:hover {
    background: var(--color-bg-card-hover);
    color: var(--color-text-primary);
    border-color: rgba(255, 255, 255, 0.2);
}

.cal-nav-btn:active {
    transform: scale(0.95);
}

/* Add button — reuse the map-add-btn visual language (cyan capsule) */
.cal-header .cal-add-btn,
#cal-add {
    display: inline-flex;
    align-items: center;
    gap: var(--space-xs);
    padding: var(--space-sm) var(--space-md);
    background: rgba(41, 170, 223, 0.12);
    border: 1px solid rgba(41, 170, 223, 0.3);
    border-radius: var(--radius-full);
    color: var(--color-brand-cyan);
    font-family: var(--font-family);
    font-size: var(--font-size-xs);
    font-weight: 600;
    cursor: pointer;
    transition: background var(--transition-fast);
}

.cal-header .cal-add-btn:hover,
#cal-add:hover {
    background: rgba(41, 170, 223, 0.22);
}


/* ---------- Field context strip (shows selected field name in header) ---------- */
.cal-field-ctx {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 5px 10px;
    background: rgba(52, 211, 153, 0.07);
    border: 1px solid rgba(52, 211, 153, 0.18);
    border-radius: var(--radius-md);
    font-size: var(--font-size-sm);
    min-height: 30px;
    overflow: hidden;
}

.cal-field-ctx-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--color-emerald);
    flex-shrink: 0;
}

.cal-field-ctx-name {
    color: var(--color-emerald);
    font-weight: 700;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.cal-field-ctx-area {
    color: var(--color-emerald);
    font-weight: 700;
    font-size: var(--font-size-sm);
    white-space: nowrap;
    flex-shrink: 0;
}

.cal-field-ctx--empty {
    justify-content: center;
    color: var(--color-text-muted);
    font-size: var(--font-size-xs);
    font-style: italic;
    background: transparent;
    border-color: rgba(255, 255, 255, 0.06);
}

/* ---------- Empty state (no field selected) ---------- */
.cal-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 40vh;
    text-align: center;
    gap: var(--space-md);
    color: var(--color-text-muted);
    font-size: var(--font-size-sm);
    line-height: 1.5;
}


/* ============================================================
   MONTH GRID
   ============================================================ */

/* Weekday header row — 7 columns matching the grid below */
.cal-weekdays {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: var(--space-xs);
    margin-bottom: var(--space-xs);
}

.cal-weekday {
    text-align: center;
    font-size: var(--font-size-xs);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--color-text-muted);
    padding-block: var(--space-xs);
}

/* Day grid — 7 equal columns */
.cal-grid {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: var(--space-xs);
}

/* A single day cell — clickable, glassy card */
.cal-day {
    display: flex;
    flex-direction: column;
    gap: 3px;
    min-height: 84px;
    padding: var(--space-xs);
    background: var(--color-bg-card);
    border: 1px solid rgba(255, 255, 255, 0.06);
    border-radius: var(--radius-sm);
    cursor: pointer;
    overflow: hidden;
    transition: background var(--transition-fast), border-color var(--transition-fast);
}

.cal-day:hover {
    background: var(--color-bg-card-hover);
    border-color: rgba(255, 255, 255, 0.14);
}

.cal-day:active {
    transform: scale(0.99);
}

/* Today — cyan ring + subtle tint */
.cal-day--today {
    border-color: var(--color-brand-cyan);
    box-shadow: 0 0 0 1px var(--color-brand-cyan), 0 0 12px rgba(41, 170, 223, 0.25);
    background: rgba(41, 170, 223, 0.08);
}

/* Leading/trailing days from adjacent months — dimmed */
.cal-day--other-month {
    opacity: 0.4;
}

.cal-day--other-month:hover {
    opacity: 0.6;
}

/* The date number, sits in the start-top corner */
.cal-day-num {
    align-self: flex-start;
    font-size: var(--font-size-xs);
    font-weight: 600;
    color: var(--color-text-secondary);
    text-align: start;
    line-height: 1;
}

.cal-day--today .cal-day-num {
    color: var(--color-brand-cyan);
    font-weight: 700;
}


/* ---------- Daily weather strip (Phase 4) ----------
   Ambient per-day forecast (hi/lo temp + rain) on each day inside the
   14-day Open-Meteo window. Read-only context; kept subtle so it never
   competes with the event chips below it. */
.cal-day-wx {
    display: flex;
    align-items: baseline;
    gap: 5px;
    margin-bottom: 1px;
    font-size: 10px;
    line-height: 1;
    color: var(--color-text-muted);
}

.cal-wx-hi {
    color: var(--color-text-secondary);
    font-weight: 600;
}

.cal-wx-lo {
    color: var(--color-text-muted);
}

/* Rain amount, pushed to the far (end) edge of the row */
.cal-wx-p {
    margin-inline-start: auto;
    color: #5c9eff;
    font-weight: 600;
}

.cal-wx-pu {
    margin-inline-start: 1px;
    font-size: 8px;
    opacity: 0.8;
}


/* ---------- Event chips ---------- */
.cal-event-chip {
    display: block;
    max-width: 100%;
    padding: 2px 6px;
    border-radius: var(--radius-sm);
    border-inline-start: 3px solid var(--color-text-muted);
    background: rgba(255, 255, 255, 0.08);
    color: var(--color-text-primary);
    font-size: 10px;
    font-weight: 500;
    line-height: 1.4;
    text-align: start;
    /* truncate overflow text to one line */
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Per-type accents — tinted bg + accent border + readable text.
   color-mix keeps each background a low-opacity tint of its accent so
   the bright accent colour carries contrast against the chip text. */
.cal-event-chip[data-type="sow"] {
    border-inline-start-color: var(--cal-sow);
    background: color-mix(in srgb, var(--cal-sow) 22%, transparent);
}

.cal-event-chip[data-type="fertilise"] {
    border-inline-start-color: var(--cal-fertilise);
    background: color-mix(in srgb, var(--cal-fertilise) 22%, transparent);
}

.cal-event-chip[data-type="spray"] {
    border-inline-start-color: var(--cal-spray);
    background: color-mix(in srgb, var(--cal-spray) 22%, transparent);
}

.cal-event-chip[data-type="irrigate"] {
    border-inline-start-color: var(--cal-irrigate);
    background: color-mix(in srgb, var(--cal-irrigate) 24%, transparent);
}

.cal-event-chip[data-type="mow"] {
    border-inline-start-color: var(--cal-mow);
    background: color-mix(in srgb, var(--cal-mow) 22%, transparent);
}

.cal-event-chip[data-type="move_stock"] {
    border-inline-start-color: var(--cal-move_stock);
    background: color-mix(in srgb, var(--cal-move_stock) 22%, transparent);
}

.cal-event-chip[data-type="scout"] {
    border-inline-start-color: var(--cal-scout);
    background: color-mix(in srgb, var(--cal-scout) 24%, transparent);
}

.cal-event-chip[data-type="harvest"] {
    border-inline-start-color: var(--cal-harvest);
    background: color-mix(in srgb, var(--cal-harvest) 22%, transparent);
}

.cal-event-chip[data-type="soil_test"] {
    border-inline-start-color: var(--cal-soil_test);
    background: color-mix(in srgb, var(--cal-soil_test) 26%, transparent);
}

.cal-event-chip[data-type="other"] {
    border-inline-start-color: var(--cal-other);
    background: color-mix(in srgb, var(--cal-other) 20%, transparent);
}

.cal-event-chip[data-type="stage"] {
    border-inline-start-color: var(--cal-stage);
    background: color-mix(in srgb, var(--cal-stage) 20%, transparent);
}

/* Derived overlays (Phase 2) — computed from crop + sowing date, read-only.
   Dashed border + faint italic text sets them apart from the manager's own
   logged events while keeping each type's accent colour. */
.cal-event-chip--derived {
    border-inline-start-style: dashed;
    background: transparent;
    font-style: italic;
    opacity: 0.85;
    cursor: default;
}

.cal-event-chip--derived:hover {
    opacity: 1;
}

/* GDD-reconciled overlay (Phase 5) whose date is anchored to measured
   accumulated heat — firm rather than forecast, so it reads as solid. */
.cal-event-chip--derived.cal-event-chip--measured {
    border-inline-start-style: solid;
    font-style: normal;
    opacity: 1;
}

/* Weather-hazard overlays (Phase 3) — forecast frost/heat/rain/wind.
   Solid-tinted (more prominent than crop projections, since hazards
   are actionable), read-only, with a dotted accent to read as "auto". */
.cal-event-chip--weather {
    border-inline-start-style: dotted;
    border-inline-start-width: 3px;
    font-weight: 600;
    cursor: default;
}

.cal-event-chip--weather[data-hazard="frost"] {
    border-inline-start-color: #4fc3f7;
    background: color-mix(in srgb, #4fc3f7 26%, transparent);
}

.cal-event-chip--weather[data-hazard="heat"] {
    border-inline-start-color: #ff7043;
    background: color-mix(in srgb, #ff7043 26%, transparent);
}

.cal-event-chip--weather[data-hazard="rain"] {
    border-inline-start-color: #5c9eff;
    background: color-mix(in srgb, #5c9eff 26%, transparent);
}

.cal-event-chip--weather[data-hazard="wind"] {
    border-inline-start-color: #80cbc4;
    background: color-mix(in srgb, #80cbc4 26%, transparent);
}


/* ============================================================
   MODAL — add / edit event
   ============================================================ */

/* Full-screen dimmed backdrop. JS toggles the `hidden` attribute to
   close; an optional .is-open class is also supported. */
/* ---- Day picker (click → Events / Notes choice) ---- */
/* Full-screen transparent backdrop — click outside the card closes it */
.cal-day-picker-overlay {
    position: fixed;
    inset: 0;
    z-index: 10000;
    background: transparent;
}
.cal-day-picker-overlay.hidden { display: none; }

/* The visible card, absolutely positioned near the click point */
.cal-day-picker-card {
    position: absolute;
    background: var(--color-bg-secondary);
    border: 1px solid rgba(255,255,255,0.15);
    border-radius: var(--radius-md);
    box-shadow: 0 8px 24px rgba(0,0,0,0.55);
    display: flex;
    flex-direction: column;
    min-width: 160px;
    overflow: hidden;
    animation: scale-in 150ms ease both;
}

.cal-day-picker-btn {
    background: none;
    border: none;
    color: var(--color-text-primary);
    font-size: var(--font-size-md);
    font-family: inherit;
    text-align: start;
    padding: 11px 18px;
    cursor: pointer;
    transition: background 0.12s;
}
.cal-day-picker-btn:hover { background: rgba(255,255,255,0.08); }
.cal-day-picker-btn + .cal-day-picker-btn { border-top: 1px solid rgba(255,255,255,0.06); }

/* ---- Back-to-month button in calendar header ---- */
.cal-back-btn {
    font-size: var(--font-size-sm) !important;
    opacity: 0.75;
    margin-inline-end: var(--space-xs);
}
.cal-back-btn:hover { opacity: 1; }

/* ---- Note sheet (month-view long-press diary popup) ---- */
.cal-note-overlay {
    position: fixed;
    inset: 0;
    z-index: 9998;
    display: flex;
    align-items: flex-end;        /* bottom-sheet on mobile */
    justify-content: center;
    padding: var(--space-md);
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
}
.cal-note-overlay.hidden { display: none; }

.cal-note-sheet {
    width: 100%;
    max-width: 480px;
    padding: var(--space-lg);
    background: var(--color-bg-card);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.4);
    animation: slide-up 220ms ease both;
}
@keyframes slide-up {
    from { transform: translateY(60px); opacity: 0; }
    to   { transform: translateY(0);    opacity: 1; }
}
@media (min-width: 600px) {
    .cal-note-overlay { align-items: center; }
    .cal-note-sheet { border-radius: var(--radius-lg); }
}

.cal-note-sheet-hdr {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: var(--space-md);
}
.cal-note-sheet-date {
    font-size: var(--font-size-md);
    font-weight: 600;
    color: var(--color-text-primary);
}
.cal-note-close {
    background: none;
    border: none;
    color: var(--color-text-muted);
    font-size: 1.1rem;
    cursor: pointer;
    padding: var(--space-xs);
    line-height: 1;
}
.cal-note-textarea {
    width: 100%;
    min-height: 110px;
    resize: vertical;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: var(--radius-sm);
    color: var(--color-text-primary);
    font-size: var(--font-size-md);
    padding: var(--space-sm) var(--space-md);
    font-family: inherit;
    box-sizing: border-box;
}
.cal-note-textarea:focus { outline: none; border-color: var(--color-accent); }
.cal-note-actions { margin-top: var(--space-md); display: flex; justify-content: flex-end; }

.cal-modal-overlay {
    position: fixed;
    inset: 0;
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-md);
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
}

.cal-modal-overlay[hidden] {
    display: none;
}

/* The dialog card — matches .modal-container in style.css */
.cal-modal-card {
    width: 100%;
    max-width: 440px;
    max-height: 88vh;
    overflow-y: auto;
    padding: var(--space-lg);
    background: var(--color-bg-card);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: var(--radius-lg);
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.5);
    animation: scale-in 250ms var(--transition-base) both;
}

.cal-modal-title {
    font-size: var(--font-size-lg);
    font-weight: 600;
    color: var(--color-text-primary);
    margin-bottom: var(--space-md);
    padding-bottom: var(--space-md);
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}


/* ---------- Form controls inside the card ---------- */
.cal-modal-card label {
    display: block;
    margin-bottom: var(--space-sm);
    font-size: var(--font-size-sm);
    font-weight: 500;
    color: var(--color-text-secondary);
    text-align: start;
}

.cal-modal-card input,
.cal-modal-card select,
.cal-modal-card textarea {
    width: 100%;
    margin-bottom: var(--space-md);
    padding: 10px 12px;
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: var(--radius-sm);
    color: var(--color-text-primary);
    font-family: var(--font-family);
    font-size: 0.95rem;
}

.cal-modal-card textarea {
    min-height: 72px;
    resize: vertical;
}

.cal-modal-card input::placeholder,
.cal-modal-card textarea::placeholder {
    color: var(--color-text-muted);
}

.cal-modal-card input:focus,
.cal-modal-card select:focus,
.cal-modal-card textarea:focus {
    border-color: var(--color-brand-cyan);
    box-shadow: 0 0 0 3px rgba(41, 170, 223, 0.15);
    outline: none;
}

/* Schedule section — end date + repeat rule, visually grouped under the date field. */
.cal-modal-section {
    margin-bottom: var(--space-xs);
    padding: var(--space-sm) var(--space-sm) 0;
    border-inline-start: 2px solid rgba(255, 255, 255, 0.08);
    margin-inline-start: var(--space-xs);
}

/* Hidden utility class used to collapse repeat rows. */
.cal-modal-row--hidden {
    display: none !important;
}

/* Inline group for "Every [N] [days]" and "After [N] [occurrences] / On [date]". */
.cal-repeat-inline {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    margin-bottom: var(--space-md);
}

.cal-repeat-inline input[type="number"],
.cal-repeat-inline input[type="date"] {
    width: auto;
    flex: 0 0 auto;
    margin-bottom: 0;
}

.cal-repeat-inline input[type="number"] {
    width: 5em;
}

.cal-repeat-inline select {
    width: auto;
    flex: 0 0 auto;
    margin-bottom: 0;
}

.cal-repeat-inline .cal-repeat-occ {
    font-size: var(--font-size-sm);
    color: var(--color-text-muted);
    white-space: nowrap;
}

/* Phase 5 — type-specific detail fields, separated from the core fields. */
.cal-details:not(:empty) {
    margin-top: var(--space-xs);
    padding-top: var(--space-md);
    border-top: 1px dashed rgba(255, 255, 255, 0.12);
}

/* Number field + its language-neutral unit (kg/ha, mm…) on one line. */
.cal-detail-input {
    display: flex;
    align-items: center;
    gap: 8px;
}

.cal-detail-input input {
    flex: 1;
}

.cal-detail-unit {
    flex: 0 0 auto;
    margin-bottom: var(--space-md);
    font-size: var(--font-size-sm);
    color: var(--color-text-muted);
    white-space: nowrap;
}

/* ---- Irrigate: checkbox row ---- */
.cal-detail-checkbox-row {
    margin-bottom: var(--space-md);
}

.cal-detail-checkbox-label {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    font-size: 0.95rem;
    color: var(--color-text-primary);
    font-weight: 400;
}

.cal-detail-checkbox-label input[type="checkbox"] {
    width: auto;
    margin: 0;
    padding: 0;
    flex: 0 0 auto;
    accent-color: var(--color-brand-cyan);
    cursor: pointer;
}

/* ---- Irrigate: fertigation sub-fields ---- */
.cal-irrig-fertigation {
    padding-inline-start: var(--space-md);
    border-inline-start: 2px solid rgba(41, 170, 223, 0.25);
    margin-bottom: var(--space-sm);
}

/* Compact text input used for the unit alongside the rate number input. */
.cal-detail-unit-input {
    flex: 0 0 auto;
    width: 6em !important;
    margin-bottom: 0 !important;
    text-align: center;
}

/* ---- Irrigate: advanced calculation section ---- */
.cal-irrig-advanced-toggle-row {
    margin-bottom: var(--space-sm);
}

.cal-irrig-advanced-toggle {
    background: none;
    border: none;
    padding: 0;
    font-size: var(--font-size-sm);
    color: var(--color-brand-cyan);
    cursor: pointer;
    text-decoration: underline;
    text-underline-offset: 2px;
}

.cal-irrig-advanced-toggle:hover {
    opacity: 0.8;
}

.cal-irrig-advanced {
    padding: var(--space-sm) var(--space-md);
    background: rgba(0, 0, 0, 0.15);
    border-radius: var(--radius-sm);
    border: 1px dashed rgba(255, 255, 255, 0.1);
    margin-bottom: var(--space-md);
}

.cal-detail-hint {
    font-size: var(--font-size-sm);
    color: var(--color-text-muted);
    margin: 0 0 var(--space-sm);
    line-height: 1.4;
}

/* Multi-day span continuation chip (days after the first) — slightly dimmed
   so the start day reads as the anchor. */
.cal-event-chip--span-cont {
    opacity: 0.72;
    border-inline-start-style: dashed;
}

/* Subtle dot marking an event that carries logged detail fields (hover for the summary). */
.cal-event-chip--detailed::after {
    content: '';
    display: inline-block;
    width: 4px;
    height: 4px;
    margin-inline-start: 4px;
    border-radius: 50%;
    background: currentColor;
    opacity: 0.6;
    vertical-align: middle;
}


/* ============================================================
   BUTTONS
   ============================================================ */

.cal-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-xs);
    padding: 8px 16px;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    font-family: var(--font-family);
    font-size: var(--font-size-sm);
    font-weight: 600;
    cursor: pointer;
    transition: background var(--transition-fast), color var(--transition-fast), border-color var(--transition-fast), box-shadow var(--transition-fast);
}

.cal-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Primary — outlined gold, matches outline aesthetic */
.cal-btn--primary {
    background: transparent;
    border-color: var(--color-brand-cyan);
    color: var(--color-brand-cyan);
    box-shadow: 0 0 10px rgba(255, 171, 0, 0.1);
}

.cal-btn--primary:hover {
    background: rgba(255, 171, 0, 0.08);
    border-color: var(--color-brand-cyan);
    color: var(--color-brand-cyan);
    box-shadow: 0 0 15px rgba(255, 171, 0, 0.2);
}

/* Ghost — subtle outline, matches .btn-secondary */
.cal-btn--ghost {
    background: transparent;
    border-color: rgba(255, 255, 255, 0.1);
    color: var(--color-text-secondary);
}

.cal-btn--ghost:hover {
    background: rgba(255, 255, 255, 0.05);
    color: var(--color-text-primary);
}

/* Toggle — outlined when off, cyan-tinted when the crop overlays are shown */
.cal-btn--toggle {
    background: transparent;
    border-color: rgba(255, 255, 255, 0.1);
    color: var(--color-text-secondary);
    font-weight: 500;
}

.cal-btn--toggle:hover {
    color: var(--color-text-primary);
    border-color: rgba(255, 255, 255, 0.2);
}

.cal-btn--toggle.is-active {
    background: color-mix(in srgb, var(--color-brand-cyan) 18%, transparent);
    border-color: color-mix(in srgb, var(--color-brand-cyan) 45%, transparent);
    color: var(--color-text-primary);
}

/* Danger — red, for delete */
.cal-btn--danger {
    background: rgba(255, 23, 68, 0.1);
    border-color: rgba(255, 23, 68, 0.3);
    color: var(--color-red);
}

.cal-btn--danger:hover {
    background: rgba(255, 23, 68, 0.22);
}

/* Footer action row inside the modal card (buttons grouped at the end) */
.cal-modal-card .cal-modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: var(--space-sm);
    margin-top: var(--space-md);
    flex-wrap: wrap;
    align-items: center;
}

/* Inline delete confirmation — replaces native window.confirm() */
.cal-delete-confirm {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    margin-inline-end: auto;
}

.cal-confirm-label {
    font-size: var(--font-size-sm);
    color: var(--color-text-secondary);
    white-space: nowrap;
}


/* ============================================================
   VIEW TOGGLE — segmented Month | Week | Day control
   Matches the glassy pill language used elsewhere in the app.
   ============================================================ */

.cal-view-toggle {
    display: flex;
    align-items: center;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: var(--radius-full);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    overflow: hidden;
    flex-shrink: 0;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
}

.cal-view-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 7px 14px;
    background: transparent;
    border: none;
    color: var(--color-text-muted);
    font-family: var(--font-family);
    font-size: var(--font-size-xs);
    font-weight: 500;
    cursor: pointer;
    transition: background var(--transition-fast), color var(--transition-fast), box-shadow var(--transition-fast);
    white-space: nowrap;
}

.cal-view-btn:hover {
    background: rgba(255, 255, 255, 0.06);
    color: var(--color-text-secondary);
}

.cal-view-btn.is-active {
    background: rgba(41, 170, 223, 0.2);
    color: var(--color-brand-cyan);
    font-weight: 700;
    box-shadow: 0 0 10px rgba(41, 170, 223, 0.18) inset;
}

/* Keep the period label centred in the nav row */
.cal-header-nav .cal-month-label {
    flex: 1;
    /* Day view shows a long full-date string — allow it to shrink gracefully */
    font-size: clamp(0.85rem, 3.5vw, 1.25rem);
}


/* ============================================================
   WEEK VIEW — 7 day columns
   ============================================================ */

.cal-week-grid {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: var(--space-xs);
    overflow-x: auto;
}

.cal-week-col {
    display: flex;
    flex-direction: column;
    gap: 3px;
    min-height: 180px;
    min-width: 70px;
    padding: var(--space-xs);
    background: var(--color-bg-card);
    border: 1px solid rgba(255, 255, 255, 0.07);
    border-radius: var(--radius-md);
    cursor: pointer;
    overflow: hidden;
    box-shadow: var(--shadow-card);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    transition: background var(--transition-fast), border-color var(--transition-fast), box-shadow var(--transition-fast);
}

.cal-week-col:hover {
    background: var(--color-bg-card-hover);
    border-color: rgba(255, 255, 255, 0.14);
    box-shadow: var(--shadow-card-hover);
}

.cal-week-col--today {
    border-color: rgba(41, 170, 223, 0.5);
    box-shadow: 0 0 0 1px rgba(41, 170, 223, 0.4), 0 4px 24px rgba(0, 0, 0, 0.3), 0 0 20px rgba(41, 170, 223, 0.12);
    background: rgba(41, 170, 223, 0.06);
}

.cal-week-day-hdr {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    padding-bottom: var(--space-sm);
    margin-bottom: 3px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.07);
    flex-shrink: 0;
    cursor: pointer;
}

.cal-week-day-hdr:hover .cal-week-dnum:not(.is-today) {
    color: var(--color-brand-cyan);
}

.cal-week-wday {
    font-size: 9px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--color-text-muted);
}

.cal-week-dnum {
    font-size: var(--font-size-base);
    font-weight: 700;
    color: var(--color-text-secondary);
    line-height: 1;
}

/* Today's date number — filled cyan circle badge */
.cal-week-dnum.is-today {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    background: var(--color-brand-cyan);
    color: #fff;
    border-radius: var(--radius-full);
    font-size: var(--font-size-sm);
    box-shadow: 0 0 10px rgba(41, 170, 223, 0.4);
}


/* ============================================================
   DIARY DOT — pen indicator on days with a diary entry
   ============================================================ */

.cal-diary-dot {
    display: inline-block;
    align-self: flex-start;
    font-size: 9px;
    line-height: 1;
    opacity: 0.5;
    user-select: none;
    pointer-events: none;
}


/* ============================================================
   DAY VIEW — single-day detail
   ============================================================ */

.cal-day-view {
    display: flex;
    flex-direction: column;
    gap: var(--space-md);
    padding-bottom: var(--space-lg);
}

/* ---- Diary section — glassy card with a cyan top accent ---- */
.cal-diary-section {
    background: rgba(41, 170, 223, 0.04);
    border: 1px solid rgba(41, 170, 223, 0.18);
    border-top: 2px solid rgba(41, 170, 223, 0.45);
    border-radius: var(--radius-lg);
    padding: var(--space-md);
    display: flex;
    flex-direction: column;
    gap: var(--space-sm);
    box-shadow: var(--shadow-card);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
}

.cal-diary-label {
    font-size: var(--font-size-xs);
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--color-brand-cyan);
    opacity: 0.8;
}

.cal-diary-textarea {
    width: 100%;
    min-height: 110px;
    padding: 10px 12px;
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: var(--radius-md);
    color: var(--color-text-primary);
    font-family: var(--font-family);
    font-size: 0.95rem;
    line-height: 1.7;
    resize: vertical;
    box-sizing: border-box;
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}

.cal-diary-textarea::placeholder {
    color: var(--color-text-muted);
    font-style: italic;
}

.cal-diary-textarea:focus {
    border-color: var(--color-brand-cyan);
    box-shadow: 0 0 0 3px rgba(41, 170, 223, 0.15);
    outline: none;
}

.cal-diary-actions {
    display: flex;
    gap: var(--space-sm);
    justify-content: flex-end;
}

/* ---- Events list in day view ---- */
.cal-day-events {
    display: flex;
    flex-direction: column;
    gap: var(--space-sm);
}

.cal-day-section-label {
    font-size: var(--font-size-xs);
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--color-text-muted);
    padding-inline-start: var(--space-xs);
    border-inline-start: 2px solid rgba(255, 255, 255, 0.15);
}

.cal-day-event-card {
    background: var(--color-bg-card);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-inline-start-width: 3px;
    border-inline-start-color: var(--cal-other);
    border-radius: var(--radius-md);
    padding: var(--space-sm) var(--space-md);
    cursor: pointer;
    display: flex;
    flex-direction: column;
    gap: 5px;
    box-shadow: var(--shadow-card);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    transition: background var(--transition-fast), border-color var(--transition-fast),
                box-shadow var(--transition-fast), transform var(--transition-fast);
}

.cal-day-event-card:hover {
    background: var(--color-bg-card-hover);
    box-shadow: var(--shadow-card-hover);
    transform: translateY(-1px);
}

.cal-day-event-card[data-type="sow"]        { border-inline-start-color: var(--cal-sow); }
.cal-day-event-card[data-type="fertilise"]  { border-inline-start-color: var(--cal-fertilise); }
.cal-day-event-card[data-type="spray"]      { border-inline-start-color: var(--cal-spray); }
.cal-day-event-card[data-type="irrigate"]   { border-inline-start-color: var(--cal-irrigate); }
.cal-day-event-card[data-type="mow"]        { border-inline-start-color: var(--cal-mow); }
.cal-day-event-card[data-type="move_stock"] { border-inline-start-color: var(--cal-move_stock); }
.cal-day-event-card[data-type="scout"]      { border-inline-start-color: var(--cal-scout); }
.cal-day-event-card[data-type="harvest"]    { border-inline-start-color: var(--cal-harvest); }
.cal-day-event-card[data-type="soil_test"]  { border-inline-start-color: var(--cal-soil_test); }

.cal-day-event-type {
    font-size: var(--font-size-xs);
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--color-text-muted);
}

.cal-day-event-title {
    font-size: var(--font-size-sm);
    font-weight: 600;
    color: var(--color-text-primary);
}

.cal-day-event-detail {
    font-size: var(--font-size-xs);
    color: var(--color-text-secondary);
    line-height: 1.4;
}

.cal-day-event-note {
    font-size: var(--font-size-xs);
    color: var(--color-text-muted);
    font-style: italic;
    border-top: 1px solid rgba(255, 255, 255, 0.06);
    padding-top: 4px;
    margin-top: 1px;
}

.cal-day-overlays {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-xs);
    margin-top: var(--space-xs);
}

.cal-day-empty {
    color: var(--color-text-muted);
    font-size: var(--font-size-sm);
    text-align: center;
    padding-block: var(--space-lg);
    font-style: italic;
}

.cal-day-footer {
    display: flex;
    justify-content: flex-start;
    padding-top: var(--space-sm);
    border-top: 1px solid rgba(255, 255, 255, 0.07);
    margin-top: var(--space-xs);
}


/* ============================================================
   RTL — explicit overrides where direction matters.
   Grid + logical properties handle most of it automatically; we
   only flip the directional nav icons here.
   ============================================================ */

/* Prev/next chevron glyphs (if SVG/arrow content) point the right way */
[dir="rtl"] .cal-nav-btn svg {
    transform: scaleX(-1);
}

/* Modal action buttons sit at the start edge in RTL */
[dir="rtl"] .cal-modal-card .cal-modal-actions {
    flex-direction: row-reverse;
}

[dir="rtl"] .cal-diary-actions {
    flex-direction: row-reverse;
}

[dir="rtl"] .cal-day-footer {
    justify-content: flex-end;
}


/* ============================================================
   RESPONSIVE — narrow phones
   ============================================================ */

@media (max-width: 480px) {
    .cal-day {
        min-height: 64px;
        padding: 3px;
        gap: 2px;
    }

    .cal-event-chip {
        padding: 1px 4px;
        font-size: 9px;
    }

    .cal-day-wx {
        gap: 3px;
        font-size: 9px;
    }

    .cal-header-controls {
        gap: var(--space-xs);
    }

    .cal-view-btn {
        padding: 5px 9px;
    }

    .cal-week-col {
        min-height: 120px;
        min-width: 52px;
        padding: 3px;
        gap: 2px;
    }

    .cal-week-wday {
        font-size: 8px;
    }
}

/* Very narrow: collapse chips to coloured dots to keep cells legible */
@media (max-width: 360px) {
    .cal-day {
        min-height: 54px;
    }

    .cal-day-wx {
        font-size: 8px;
        gap: 2px;
    }

    /* drop the "mm" unit to keep the cramped row legible */
    .cal-wx-pu {
        display: none;
    }

    .cal-event-chip {
        /* hide the label, keep a coloured dot driven by the accent border */
        height: 6px;
        padding: 0;
        border-radius: var(--radius-full);
        border-inline-start-width: 0;
        text-indent: -9999px;
    }

    .cal-event-chip[data-type="sow"] {
        background: var(--cal-sow);
    }

    .cal-event-chip[data-type="fertilise"] {
        background: var(--cal-fertilise);
    }

    .cal-event-chip[data-type="spray"] {
        background: var(--cal-spray);
    }

    .cal-event-chip[data-type="irrigate"] {
        background: var(--cal-irrigate);
    }

    .cal-event-chip[data-type="mow"] {
        background: var(--cal-mow);
    }

    .cal-event-chip[data-type="move_stock"] {
        background: var(--cal-move_stock);
    }

    .cal-event-chip[data-type="scout"] {
        background: var(--cal-scout);
    }

    .cal-event-chip[data-type="harvest"] {
        background: var(--cal-harvest);
    }

    .cal-event-chip[data-type="soil_test"] {
        background: var(--cal-soil_test);
    }

    .cal-event-chip[data-type="other"] {
        background: var(--cal-other);
    }

    .cal-event-chip[data-type="stage"] {
        background: var(--cal-stage);
    }

    .cal-event-chip--weather[data-hazard="frost"] { background: #4fc3f7; }
    .cal-event-chip--weather[data-hazard="heat"]  { background: #ff7043; }
    .cal-event-chip--weather[data-hazard="rain"]  { background: #5c9eff; }
    .cal-event-chip--weather[data-hazard="wind"]  { background: #80cbc4; }
}

/* ── Hazard alert ticker (desktop only) ─────────────────────────────────── */
.cal-hazard-ticker {
    display: flex;
    align-items: center;
    background: rgba(255, 255, 255, 0.04);
    border-top: 1px solid rgba(255, 255, 255, 0.09);
    color: rgba(255, 255, 255, 0.72);
    font-size: var(--font-size-xs);
    font-weight: 500;
    height: 2rem;
    overflow: hidden;
    flex-shrink: 0;
}
.cal-hazard-label {
    flex-shrink: 0;
    font-weight: 700;
    color: var(--color-amber, #ffab00);
    padding: 0 0.65rem;
    border-right: 1px solid rgba(255, 255, 255, 0.09);
    white-space: nowrap;
    letter-spacing: 0.03em;
    text-transform: uppercase;
    font-size: 0.65rem;
}
.cal-hazard-scroll {
    flex: 1;
    overflow: hidden;
    position: relative;
}
.cal-hazard-scroll > span {
    display: inline-block;
    white-space: nowrap;
    padding-left: 100%;
    animation: cal-hazard-scroll var(--ticker-duration, 45s) linear infinite;
}
.cal-hazard-scroll:hover > span {
    animation-play-state: paused;
}
.cal-hazard-scroll a {
    color: inherit;
    text-decoration: none;
}
.cal-hazard-scroll a:hover {
    color: var(--color-emerald, #34d399);
    text-decoration: underline dotted;
}
@keyframes cal-hazard-scroll {
    from { transform: translateX(0); }
    to   { transform: translateX(-200%); }
}
@media (max-width: 767px) {
    .cal-hazard-ticker { display: none !important; }
}

/* ============================================================
   JOURNAL VIEW — field diary database with search + filters
   ============================================================ */

.cal-journal-view {
    display: flex;
    flex-direction: column;
    gap: var(--space-sm);
    padding: var(--space-sm) var(--space-md) var(--space-xl);
    max-width: 680px;
    margin: 0 auto;
}

.cal-journal-search {
    position: relative;
}

.cal-journal-search-input {
    width: 100%;
    box-sizing: border-box;
    padding: 10px 14px;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: var(--radius-sm);
    color: var(--color-text-primary);
    font-family: var(--font-family);
    font-size: var(--font-size-sm);
    outline: none;
    transition: border-color var(--transition-fast);
}

.cal-journal-search-input::placeholder {
    color: var(--color-text-muted);
}

.cal-journal-search-input:focus {
    border-color: rgba(41, 170, 223, 0.5);
    background: rgba(255, 255, 255, 0.07);
}

.cal-journal-filters {
    display: flex;
    gap: var(--space-sm);
}

.cal-journal-select {
    flex: 1;
    padding: 8px 10px;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: var(--radius-sm);
    color: var(--color-text-primary);
    font-family: var(--font-family);
    font-size: var(--font-size-sm);
    outline: none;
    cursor: pointer;
    transition: border-color var(--transition-fast);
}

.cal-journal-select:focus {
    border-color: rgba(41, 170, 223, 0.5);
}

.cal-journal-select option {
    background: var(--color-surface-2, #1e2130);
    color: var(--color-text-primary);
}

.cal-journal-entries {
    display: flex;
    flex-direction: column;
    gap: 0;
    margin-top: var(--space-xs);
}

.cal-journal-month-header {
    font-size: var(--font-size-xs);
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--color-brand-cyan);
    padding: var(--space-sm) 0 var(--space-xs);
    border-bottom: 1px solid rgba(41, 170, 223, 0.18);
    margin-bottom: var(--space-xs);
}

.cal-journal-month-header:not(:first-child) {
    margin-top: var(--space-md);
}

.cal-journal-entry {
    padding: var(--space-sm) 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.cal-journal-entry:last-child {
    border-bottom: none;
}

.cal-journal-entry-hdr {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-sm);
    margin-bottom: 4px;
}

.cal-journal-entry-date {
    font-size: var(--font-size-xs);
    font-weight: 600;
    color: var(--color-text-secondary);
}

.cal-journal-entry-edit {
    background: transparent;
    border: none;
    color: var(--color-text-muted);
    font-size: 13px;
    cursor: pointer;
    padding: 2px 4px;
    border-radius: var(--radius-xs, 4px);
    line-height: 1;
    transition: color var(--transition-fast), background var(--transition-fast);
    flex-shrink: 0;
}

.cal-journal-entry-edit:hover {
    color: var(--color-brand-cyan);
    background: rgba(41, 170, 223, 0.1);
}

.cal-journal-entry-note {
    font-size: var(--font-size-sm);
    color: var(--color-text-primary);
    line-height: 1.55;
    white-space: pre-wrap;
    word-break: break-word;
}

.cal-journal-empty {
    padding: var(--space-xl) 0;
    text-align: center;
    color: var(--color-text-muted);
    font-size: var(--font-size-sm);
}
