/* ==========================================================================
   TaskFlow — คอมโพเนนต์
   ต้องโหลดหลัง theme.css เสมอ (ใช้โทเคนจากไฟล์นั้นทั้งหมด)
   ไม่มีค่าสีดิบอยู่ในไฟล์นี้ — สีทุกจุดมาจากตัวแปร จึงสลับโหมดได้ทันที
   ========================================================================== */


/* ==========================================================================
   0. กฎพื้นฐาน
   ========================================================================== */

/*
   แอตทริบิวต์ hidden ต้องซ่อนได้จริงเสมอ

   เบราว์เซอร์มีกฎ [hidden] { display: none } ให้อยู่แล้ว แต่มันอยู่ใน
   user-agent stylesheet ซึ่ง **แพ้กฎของเราทุกกฎ** พอเราเขียน
   .field { display: flex } หรือ .form-grid { display: grid } ไว้
   การสั่ง element.hidden = true จากฝั่ง JavaScript ก็ไม่มีผลอะไรเลย
   องค์ประกอบยังโชว์อยู่เหมือนเดิมโดยไม่มีอะไรฟ้อง

   ใช้ !important เพราะ "อย่าแสดง" ไม่ควรมีกฎไหนมาลบล้างได้ —
   ถ้าเขียนเป็นกฎธรรมดา มันจะแพ้ทันทีที่มีใครประกาศ display ทับทีหลัง
   ซึ่งคือกับดักที่เคยทำให้ช่องตั้งค่าการทำซ้ำโผล่มาหมดทุกช่องมาแล้ว
*/
[hidden] { display: none !important; }


/* ==========================================================================
   1. โครงหน้า
   ========================================================================== */

.app {
    display: grid;
    grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
    min-height: 100vh;
}

.main {
    min-width: 0;
    display: flex;
    flex-direction: column;
}

.container {
    width: 100%;
    max-width: var(--content-w);
    margin-inline: auto;
    padding-inline: var(--s6);
}

.page-body {
    padding-block: var(--s5) var(--s10);
    flex: 1;
}

.icon { flex: none; }

.sr-only {
    position: absolute;
    width: 1px; height: 1px;
    padding: 0; margin: -1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
}


/* ==========================================================================
   2. แถบข้าง
   ========================================================================== */

.sidebar {
    position: sticky;
    top: 0;
    align-self: start;
    height: 100vh;
    display: flex;
    flex-direction: column;
    gap: var(--s2);
    padding: var(--s5) var(--s3) var(--s3);
    background: var(--material);
    /* กระจกฝ้า — ลายเซ็นของ iOS */
    -webkit-backdrop-filter: saturate(180%) blur(20px);
    backdrop-filter: saturate(180%) blur(20px);
    border-right: 1px solid var(--separator);
    z-index: 30;
}

.sidebar__brand {
    display: flex;
    align-items: center;
    gap: var(--s3);
    padding: var(--s1) var(--s3) var(--s5);
    font: var(--text-title-2);
    letter-spacing: -.02em;
    color: var(--label);
}
.sidebar__brand:hover { text-decoration: none; }

.sidebar__mark {
    display: grid;
    place-items: center;
    width: 34px; height: 34px;
    border-radius: var(--r-md);
    background: var(--accent);
    color: var(--on-accent);
    flex: none;
}

.sidebar__nav {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: var(--s3);
    width: 100%;
    padding: var(--s2) var(--s3);
    /* ใช้กับทั้ง <a> และ <button> (ปุ่มออกจากระบบ) — ล้างสไตล์ปุ่มมาตรฐานของเบราว์เซอร์ทิ้ง */
    border: 0;
    background: none;
    text-align: start;
    cursor: pointer;
    border-radius: var(--r-md);
    font: var(--text-body);
    color: var(--label);
    transition: background var(--t-fast) var(--ease);
}
.nav-item:hover { background: var(--fill); text-decoration: none; }
.nav-item:active { background: var(--fill-strong); }
.nav-item .icon { color: var(--label-2); transition: color var(--t-fast) var(--ease); }

.nav-item.is-active {
    background: var(--accent-tint);
    color: var(--accent);
    font-weight: 600;
}
.nav-item.is-active .icon { color: var(--accent); }

.nav-item__count {
    margin-left: auto;
    font: var(--text-footnote);
    color: var(--label-3);
    font-variant-numeric: tabular-nums;
}
.nav-item.is-active .nav-item__count { color: var(--accent); }

.sidebar__spacer { flex: 1; }

.sidebar__footer {
    display: flex;
    flex-direction: column;
    gap: var(--s3);
    padding-top: var(--s3);
    border-top: 1px solid var(--separator);
}

.user-chip {
    display: flex;
    align-items: center;
    gap: var(--s3);
    padding: var(--s2) var(--s3);
    border-radius: var(--r-md);
    color: var(--label);
    min-width: 0;
}
.user-chip:hover { background: var(--fill); text-decoration: none; }

/* ชื่อกับ User ID ต้องซ้อนกันเสมอ ไม่ใช่บังเอิญขึ้นบรรทัดใหม่เพราะข้อความยาว */
.user-chip__text {
    min-width: 0;
    display: flex;
    flex-direction: column;
}
.user-chip__name {
    font: var(--text-subhead);
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.user-chip__mail {
    font: var(--text-caption);
    color: var(--label-2);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.avatar {
    display: grid;
    place-items: center;
    width: 32px; height: 32px;
    flex: none;
    border-radius: var(--r-full);
    background: var(--accent-tint);
    color: var(--accent);
    font: var(--text-footnote);
    font-weight: 700;
}
.avatar--lg { width: 44px; height: 44px; font: var(--text-headline); }
.avatar--sm { width: 22px; height: 22px; font-size: .6875rem; font-weight: 700; }


/* ==========================================================================
   3. หัวหน้าเพจ — หัวข้อใหญ่ที่ยุบเป็นแถบเล็กเมื่อเลื่อนลง
   ========================================================================== */

.page-head {
    position: sticky;
    top: 0;
    z-index: 20;
    background: var(--material);
    -webkit-backdrop-filter: saturate(180%) blur(20px);
    backdrop-filter: saturate(180%) blur(20px);
    border-bottom: 1px solid transparent;
    transition: border-color var(--t-base) var(--ease);
}
.page-head.is-scrolled { border-bottom-color: var(--separator); }

.page-head__bar {
    display: flex;
    align-items: center;
    gap: var(--s3);
    min-height: 52px;
    padding-block: var(--s2);
}

.page-head__compact {
    font: var(--text-headline);
    opacity: 0;
    transform: translateY(6px);
    transition: opacity var(--t-base) var(--ease), transform var(--t-base) var(--ease);
    pointer-events: none;
}
.page-head.is-scrolled .page-head__compact {
    opacity: 1;
    transform: none;
}

.page-head__actions {
    display: flex;
    align-items: center;
    gap: var(--s2);
    margin-left: auto;
}

.page-title {
    font: var(--text-large-title);
    letter-spacing: -.022em;
    padding-block: var(--s2) var(--s1);
}
.page-subtitle {
    font: var(--text-subhead);
    color: var(--label-2);
    padding-bottom: var(--s2);
}

/* ตัวตรวจจับว่าเลื่อนพ้นหัวข้อใหญ่แล้วหรือยัง (ไม่มีความสูง มองไม่เห็น) */
.scroll-sentinel { height: 1px; margin-top: -1px; }


/* ==========================================================================
   4. การ์ดและลิสต์
   ========================================================================== */

.card {
    background: var(--surface);
    border-radius: var(--r-lg);
    box-shadow: var(--shadow-1);
    padding: var(--s5);
}
.card--flush { padding: 0; overflow: hidden; }

.card__head {
    display: flex;
    align-items: center;
    gap: var(--s3);
    margin-bottom: var(--s4);
}
.card__title { font: var(--text-headline); }

.section-title {
    font: var(--text-footnote);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .04em;
    color: var(--label-2);
    padding: 0 var(--s1) var(--s2);
}

/* ลิสต์แบบ iOS: กล่องโค้ง เส้นคั่นเยื้องเข้ามาให้ตรงกับข้อความ */
.list {
    background: var(--surface);
    border-radius: var(--r-lg);
    box-shadow: var(--shadow-1);
    overflow: hidden;
}

.list__row {
    display: flex;
    align-items: center;
    gap: var(--s4);
    padding: var(--s3) var(--s5);
    min-height: 56px;
    position: relative;
    color: var(--label);
    width: 100%;
    text-align: start;
    background: none;
    border: 0;
}
a.list__row:hover, button.list__row:hover { background: var(--fill); text-decoration: none; }

.list__row + .list__row::before {
    content: "";
    position: absolute;
    inset-block-start: 0;
    inset-inline: var(--s5) 0;
    border-top: 1px solid var(--separator);
}
/* จอความละเอียดสูงวาดเส้นได้บางกว่า 1px จริง ๆ */
@media (min-resolution: 2dppx) {
    .list__row + .list__row::before { border-top-width: .5px; }
}

.list__main {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 3px;
}
.list__label { font: var(--text-body); }
.list__hint { font: var(--text-footnote); color: var(--label-2); }

/* ป้ายวันครบกำหนด — ใช้ทั้งบนการ์ด ในรายการ และหน้าแรก
   ต้องเป็น inline-flex เพราะไอคอน SVG เป็น display:block
   ถ้าปล่อยให้เป็น inline ธรรมดา ไอคอนจะดันข้อความตกไปบรรทัดใหม่ */
.due-tag {
    display: inline-flex;
    align-items: center;
    align-self: flex-start;
    gap: 5px;
    font: var(--text-caption);
    font-weight: 600;
    color: rgb(var(--tone-rgb, var(--gray-rgb)));
}
.due-tag.tone-muted { color: var(--label-2); }
.list__trailing {
    display: flex;
    align-items: center;
    gap: var(--s2);
    color: var(--label-3);
    flex: none;
}


/* ==========================================================================
   5. ปุ่ม
   ========================================================================== */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--s2);
    padding: var(--s2) var(--s4);
    min-height: 36px;
    border: 0;
    border-radius: var(--r-md);
    font: var(--text-subhead);
    font-weight: 600;
    color: var(--accent);
    background: none;
    cursor: pointer;
    white-space: nowrap;
    transition: background var(--t-fast) var(--ease),
                transform var(--t-fast) var(--ease),
                opacity var(--t-fast) var(--ease);
}
.btn:hover { text-decoration: none; }
/* กดแล้วยุบเล็กน้อย — ให้ความรู้สึกเดียวกับปุ่มบน iOS */
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .4; cursor: not-allowed; transform: none; }

.btn--filled { background: var(--accent); color: var(--on-accent); }
.btn--filled:hover { background: rgb(var(--accent-rgb) / .88); }

.btn--tinted { background: var(--accent-tint); }
.btn--tinted:hover { background: rgb(var(--accent-rgb) / .22); }

.btn--gray { background: var(--fill); color: var(--label); }
.btn--gray:hover { background: var(--fill-strong); }

.btn--plain:hover { background: var(--fill); }

.btn--danger { color: var(--red); }
.btn--danger:hover { background: rgb(var(--red-rgb) / .12); }
.btn--danger.btn--filled { background: var(--red); color: #fff; }

.btn--lg { min-height: 46px; padding-inline: var(--s6); font: var(--text-headline); }
.btn--block { width: 100%; }

.btn--icon {
    padding: 0;
    width: 36px;
    min-height: 36px;
    color: var(--label-2);
}
.btn--icon:hover { background: var(--fill); color: var(--label); }


/* ==========================================================================
   6. ป้ายและชิป
   ========================================================================== */

.badge {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 3px 9px;
    border-radius: var(--r-full);
    font: var(--text-caption);
    font-weight: 600;
    white-space: nowrap;
    /* --tone ถูกกำหนดจากคลาส .tone-* ด้านล่าง */
    color: rgb(var(--tone-rgb));
    background: rgb(var(--tone-rgb) / .14);
}
.badge .icon { width: 13px; height: 13px; }

.badge--solid { color: #fff; background: rgb(var(--tone-rgb)); }
.badge--dot { padding-inline: 8px; }
.badge--dot::before {
    content: "";
    width: 6px; height: 6px;
    border-radius: var(--r-full);
    background: rgb(var(--tone-rgb));
}

.tone-blue   { --tone-rgb: var(--blue-rgb); }
.tone-green  { --tone-rgb: var(--green-rgb); }
.tone-red    { --tone-rgb: var(--red-rgb); }
.tone-orange { --tone-rgb: var(--orange-rgb); }
.tone-yellow { --tone-rgb: var(--yellow-rgb); }
.tone-indigo { --tone-rgb: var(--indigo-rgb); }
.tone-teal   { --tone-rgb: var(--teal-rgb); }
.tone-pink   { --tone-rgb: var(--pink-rgb); }
.tone-purple { --tone-rgb: var(--purple-rgb); }
.tone-gray   { --tone-rgb: var(--gray-rgb); }
.tone-muted  { --tone-rgb: var(--gray-rgb); }
.tone-accent { --tone-rgb: var(--accent-rgb); }

/* ชิปกดเลือกได้ เช่น ตัวกรองแท็ก */
.chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 5px 12px;
    border: 0;
    border-radius: var(--r-full);
    font: var(--text-footnote);
    font-weight: 600;
    color: var(--label);
    background: var(--fill);
    cursor: pointer;
    transition: background var(--t-fast) var(--ease), transform var(--t-fast) var(--ease);
}
.chip:hover { background: var(--fill-strong); text-decoration: none; }
.chip:active { transform: scale(.96); }
.chip.is-on { background: var(--accent); color: var(--on-accent); }
.chip.is-on .chip__dot { background: currentColor; }

.chip__dot {
    width: 7px; height: 7px;
    border-radius: var(--r-full);
    background: rgb(var(--tone-rgb, var(--gray-rgb)));
}

/* ชิปที่ติ๊กเลือกได้ — ช่องติ๊กจริงซ่อนไว้ ตัวชิปคือ <label> ที่คู่กัน
   สถานะเลือก/ไม่เลือกจึงเปลี่ยนได้เองด้วย CSS ล้วน ไม่ต้องมี JavaScript */
.chip-input {
    position: absolute;
    opacity: 0;
    pointer-events: none;
}
.chip-input:checked + .chip { background: var(--accent); color: var(--on-accent); }
.chip-input:checked + .chip .chip__dot { background: currentColor; }
.chip-input:focus-visible + .chip { outline: 2px solid var(--accent); outline-offset: 2px; }


/* ==========================================================================
   7. ฟอร์ม
   ========================================================================== */

.field { display: flex; flex-direction: column; gap: 6px; }

.field__label {
    font: var(--text-subhead);
    font-weight: 600;
    color: var(--label-2);
}

/* คำอธิบายใต้ช่องกรอก — บอกว่าตัวเลือกนั้นจะทำให้เกิดอะไรขึ้น */
.field__hint {
    font: var(--text-footnote);
    color: var(--label-3);
    line-height: 1.4;
}

/* บรรทัดสรุปของงานประจำ — บอกวันที่จะได้จริงก่อนกดบันทึก */
.repeat-summary {
    display: flex;
    align-items: center;
    gap: var(--s2);
    padding: var(--s3) var(--s4);
    border-radius: var(--r-md);
    background: var(--accent-tint);
    color: var(--label);
    font: var(--text-footnote);
    line-height: 1.5;
}

.input, .textarea, .select {
    width: 100%;
    padding: 10px var(--s3);
    min-height: 42px;
    border: 1px solid var(--separator);
    border-radius: var(--r-md);
    background: var(--surface);
    color: var(--label);
    transition: border-color var(--t-fast) var(--ease), box-shadow var(--t-fast) var(--ease);
}
.input::placeholder, .textarea::placeholder { color: var(--label-3); }

.input:focus, .textarea:focus, .select:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 4px var(--accent-ring);
}

.textarea { min-height: 96px; resize: vertical; line-height: 1.55; }

/* ลูกศรของ <select> วาดเป็นเส้นบางแบบ iOS ไม่ใช่สามเหลี่ยมทึบของเบราว์เซอร์
   ฝังเป็น data URI จึงไม่ต้องโหลดไฟล์ภาพเพิ่ม แต่ระบายสีด้วย currentColor ไม่ได้
   จึงกำหนดสีเทาไว้สองชุดแล้วสลับตามโหมด */
.select {
    appearance: none;
    padding-inline-end: var(--s8);
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M2.8 4.6 6 7.8l3.2-3.2' fill='none' stroke='%238A8A8E' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    background-position: calc(100% - 11px) center;
    background-size: 12px 12px;
    background-repeat: no-repeat;
    cursor: pointer;
}

:root[data-theme="dark"] .select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M2.8 4.6 6 7.8l3.2-3.2' fill='none' stroke='%2398989E' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
}
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) .select {
        background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M2.8 4.6 6 7.8l3.2-3.2' fill='none' stroke='%2398989E' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    }
}

.field__error {
    font: var(--text-footnote);
    color: var(--red);
}
.input.has-error, .textarea.has-error, .select.has-error { border-color: var(--red); }

.form-grid {
    display: grid;
    gap: var(--s4);
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}

/* ช่องค้นหาแบบ iOS: พื้นเทาจาง มุมมน มีไอคอนแว่นขยายข้างใน */
.search {
    display: flex;
    align-items: center;
    gap: var(--s2);
    padding: 0 var(--s3);
    min-height: 38px;
    border-radius: var(--r-md);
    background: var(--fill);
    color: var(--label-2);
    transition: box-shadow var(--t-fast) var(--ease);
}
.search:focus-within { box-shadow: 0 0 0 4px var(--accent-ring); }
.search input {
    flex: 1;
    min-width: 0;
    border: 0;
    background: none;
    outline: none;
    font: var(--text-subhead);
    color: var(--label);
}


/* ==========================================================================
   8. Segmented control (ทำงานได้แม้ปิด JavaScript — ใช้ input radio จริง)
   ========================================================================== */

.segmented {
    display: inline-flex;
    padding: 2px;
    border-radius: var(--r-md);
    background: var(--fill);
    gap: 2px;
}

.segmented input {
    position: absolute;
    opacity: 0;
    pointer-events: none;
}

.segmented label {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 5px var(--s3);
    min-height: 30px;
    border-radius: 8px;
    font: var(--text-footnote);
    font-weight: 600;
    color: var(--label-2);
    cursor: pointer;
    white-space: nowrap;
    transition: background var(--t-base) var(--ease), color var(--t-base) var(--ease);
}
.segmented label:hover { color: var(--label); }

.segmented input:checked + label {
    background: var(--surface-2);
    color: var(--label);
    box-shadow: var(--shadow-1);
}
.segmented input:focus-visible + label { outline: 2px solid var(--accent); outline-offset: 2px; }

.segmented--block { display: flex; }
.segmented--block label { flex: 1; }


/* ==========================================================================
   9. สวิตช์
   ========================================================================== */

.switch { display: inline-flex; align-items: center; gap: var(--s3); cursor: pointer; }
.switch input { position: absolute; opacity: 0; pointer-events: none; }

.switch__track {
    position: relative;
    width: 51px; height: 31px;
    flex: none;
    border-radius: var(--r-full);
    background: var(--fill-strong);
    transition: background var(--t-base) var(--ease);
}
.switch__track::after {
    content: "";
    position: absolute;
    top: 2px; left: 2px;
    width: 27px; height: 27px;
    border-radius: var(--r-full);
    background: #fff;
    box-shadow: 0 2px 4px rgb(0 0 0 / .2);
    transition: transform var(--t-base) var(--ease);
}
.switch input:checked + .switch__track { background: var(--green); }
.switch input:checked + .switch__track::after { transform: translateX(20px); }
.switch input:focus-visible + .switch__track { outline: 2px solid var(--accent); outline-offset: 2px; }


/* ==========================================================================
   10. วงกลมเลือกสี accent
   ========================================================================== */

.swatches { display: flex; flex-wrap: wrap; gap: var(--s3); }

.swatch { position: relative; }
.swatch input { position: absolute; opacity: 0; pointer-events: none; }

.swatch label {
    display: grid;
    place-items: center;
    width: 34px; height: 34px;
    border-radius: var(--r-full);
    background: rgb(var(--tone-rgb));
    color: #fff;
    cursor: pointer;
    transition: transform var(--t-base) var(--ease), box-shadow var(--t-base) var(--ease);
}
.swatch label:hover { transform: scale(1.08); }
.swatch label .icon { opacity: 0; transition: opacity var(--t-fast) var(--ease); }

.swatch input:checked + label {
    box-shadow: 0 0 0 3px var(--surface), 0 0 0 5px rgb(var(--tone-rgb));
}
.swatch input:checked + label .icon { opacity: 1; }
.swatch input:focus-visible + label { outline: 2px solid var(--label); outline-offset: 4px; }


/* ==========================================================================
   11. แผงลอย (sheet / modal)
   ========================================================================== */

.sheet {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: grid;
    place-items: center;
    padding: var(--s5);
}
.sheet[hidden] { display: none; }

.sheet__scrim {
    position: absolute;
    inset: 0;
    background: var(--scrim);
    -webkit-backdrop-filter: blur(3px);
    backdrop-filter: blur(3px);
    animation: fade-in var(--t-base) var(--ease);
}

.sheet__panel {
    position: relative;
    width: min(560px, 100%);
    max-height: min(88vh, 760px);
    display: flex;
    flex-direction: column;
    background: var(--surface-2);
    border-radius: var(--r-xl);
    box-shadow: var(--shadow-3);
    animation: sheet-in var(--t-slow) var(--ease);
    overflow: hidden;
}

.sheet__head {
    display: flex;
    align-items: center;
    gap: var(--s3);
    padding: var(--s4) var(--s5);
    border-bottom: 1px solid var(--separator);
}
.sheet__title { font: var(--text-headline); flex: 1; }

.sheet__body {
    padding: var(--s5);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: var(--s4);
}

.sheet__foot {
    display: flex;
    justify-content: flex-end;
    gap: var(--s2);
    padding: var(--s4) var(--s5);
    border-top: 1px solid var(--separator);
}

@keyframes fade-in { from { opacity: 0; } }
@keyframes sheet-in {
    from { opacity: 0; transform: translateY(16px) scale(.97); }
}


/* ==========================================================================
   12. ข้อความแจ้งเตือน
   ========================================================================== */

.toasts {
    position: fixed;
    top: var(--s5);
    right: var(--s5);
    z-index: 200;
    display: flex;
    flex-direction: column;
    gap: var(--s2);
    pointer-events: none;
}

.toast {
    display: flex;
    align-items: center;
    gap: var(--s3);
    padding: var(--s3) var(--s4);
    max-width: 380px;
    border-radius: var(--r-lg);
    background: var(--surface-2);
    box-shadow: var(--shadow-3);
    border: 1px solid var(--separator);
    font: var(--text-subhead);
    animation: toast-in var(--t-slow) var(--ease);
    pointer-events: auto;
}
.toast .icon { color: rgb(var(--tone-rgb, var(--gray-rgb))); }
.toast.is-leaving { animation: toast-out var(--t-base) var(--ease) forwards; }

@keyframes toast-in { from { opacity: 0; transform: translateX(24px); } }
@keyframes toast-out { to { opacity: 0; transform: translateX(24px); } }


/* ==========================================================================
   13. สถานะว่างเปล่า
   ========================================================================== */

.empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--s3);
    padding: var(--s10) var(--s5);
    text-align: center;
    color: var(--label-2);
}
.empty__icon { color: var(--label-3); }
.empty__title { font: var(--text-headline); color: var(--label); }
.empty__text { font: var(--text-subhead); max-width: 34ch; }


/* ==========================================================================
   11ก. หน้าแรก — ตัวเลขสรุปและกราฟแท่ง
   ========================================================================== */

.stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: var(--s3);
}

.stat {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 2px var(--s2);
    align-items: center;
    padding: var(--s4) var(--s5);
    background: var(--surface);
    border-radius: var(--r-lg);
    box-shadow: var(--shadow-1);
    color: var(--label);
    transition: transform var(--t-fast) var(--ease), box-shadow var(--t-fast) var(--ease);
}
.stat:hover { text-decoration: none; box-shadow: var(--shadow-2); }
.stat:active { transform: scale(.985); }

.stat__icon {
    display: grid;
    place-items: center;
    width: 28px; height: 28px;
    border-radius: var(--r-sm);
    color: rgb(var(--tone-rgb));
    background: rgb(var(--tone-rgb) / .14);
}

.stat__label {
    font: var(--text-footnote);
    color: var(--label-2);
}

.stat__value {
    grid-column: 1 / -1;
    /* ตัวเลขใหญ่ใช้ความกว้างตัวเลขปกติ ไม่ใช้ tabular-nums
       เพราะที่ขนาดนี้ตัวเลขความกว้างเท่ากันจะดูห่างผิดปกติ */
    font: 700 2rem/1.15 var(--font);
    letter-spacing: -.02em;
}


/* ── กราฟแท่งแนวนอน ────────────────────────────────────────────────────── */

.dash-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
    gap: var(--s4);
}

.bars { display: grid; gap: var(--s3); }

.bar-row {
    display: grid;
    grid-template-columns: minmax(88px, auto) minmax(0, 1fr) auto;
    align-items: center;
    gap: var(--s3);
    color: var(--label);
    border-radius: var(--r-sm);
    padding: 2px;
    margin: -2px;
    transition: background var(--t-fast) var(--ease);
}
a.bar-row:hover { background: var(--fill); text-decoration: none; }

.bar-row__label {
    display: flex;
    align-items: center;
    gap: var(--s2);
    font: var(--text-footnote);
    color: var(--label-2);
    white-space: nowrap;
}

.bar-row__dot {
    width: 8px; height: 8px;
    flex: none;
    border-radius: var(--r-full);
    background: rgb(var(--tone-rgb));
}

/* พื้นที่วาดแท่ง — ไม่มีรางพื้นหลัง แท่งงอกจากเส้นฐานซ้ายบนพื้นการ์ดโดยตรง */
.bar-row__plot { display: block; }

.bar-row__fill {
    display: block;
    height: 14px;                       /* แท่งบาง ไม่เกิน 24px ตามสเปก */
    min-width: 3px;                     /* ค่าเป็น 0 ยังเห็นเป็นขีดบาง ๆ ไม่หายไปเฉย ๆ */
    background: rgb(var(--tone-rgb));
    border-radius: 0 4px 4px 0;         /* มนที่ปลายข้อมูล ตัดตรงที่เส้นฐาน */
    transition: width var(--t-slow) var(--ease);
}

.bar-row__value {
    font: var(--text-subhead);
    font-weight: 600;
    min-width: 2.5ch;
    text-align: end;
}


/* ==========================================================================
   12ก. แถบตัวกรอง · การแบ่งหน้า · แถวรายการงาน
   ========================================================================== */

.filters {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--s2);
}
.filters .search { flex: 1 1 260px; min-height: 38px; }
.filters .select { width: auto; min-height: 38px; font: var(--text-footnote); font-weight: 600; }

.filters__tags {
    flex-basis: 100%;
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.pagination {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--s4);
}

.task-row__status { color: rgb(var(--tone-rgb, var(--gray-rgb))); flex: none; }

.task-row__meta {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px;
    margin-top: 3px;
}

.task-row.is-done .list__label {
    color: var(--label-2);
    text-decoration: line-through;
    text-decoration-color: var(--label-3);
}


/* ==========================================================================
   12ข. หน้าตั้งค่า
   ========================================================================== */

.settings {
    display: flex;
    flex-direction: column;
    gap: var(--s6);
    max-width: 760px;
}

/*
   จอกว้างพอก็แบ่งเป็นสองคอลัมน์ ใช้พื้นที่คุ้มขึ้นโดยที่ช่องกรอกยังไม่ยืดจนอ่านยาก

   ใช้ columns แทน grid เพราะแต่ละส่วนสูงไม่เท่ากัน — grid จะบังคับให้แถวเดียวกัน
   สูงเท่ากันแล้วเหลือช่องว่างใต้ส่วนที่สั้นกว่า ส่วน columns ไหลต่อกันเองแบบ masonry

   "2 560px" = อย่างมากสองคอลัมน์ และแต่ละคอลัมน์ต้องกว้างอย่างน้อย 560px
   จอแคบกว่านั้นจึงยุบเหลือคอลัมน์เดียวเองโดยไม่ต้องเขียน media query
*/
@media (min-width: 1200px) {
    .settings {
        display: block;
        max-width: none;
        columns: 2 560px;
        column-gap: var(--s6);
    }
    /* ห้ามให้การ์ดใบเดียวถูกหั่นครึ่งข้ามคอลัมน์ */
    .settings > * {
        break-inside: avoid;
        margin-bottom: var(--s6);
    }
}

.setting-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--s4);
}

.setting-row__title { font: var(--text-headline); }
.setting-row__hint { font: var(--text-footnote); color: var(--label-2); }


/* ── หน้าผู้ดูแล ────────────────────────────────────────────────────────── */

.invite-fresh { border: 1px solid var(--accent); background: var(--accent-tint); }

/* รหัสเชิญถูกอ่านทางโทรศัพท์และพิมพ์ต่อด้วยมือ จึงต้องตัวใหญ่ ห่าง และเลือกคลุมง่าย */
.invite-code {
    display: inline-block;
    font-family: ui-monospace, "SF Mono", "Cascadia Mono", Consolas, monospace;
    font-size: 1.375rem;
    font-weight: 700;
    letter-spacing: .08em;
    user-select: all;
}
.invite-code--sm { font-size: 1rem; letter-spacing: .05em; }

.invite-link {
    font-family: ui-monospace, "SF Mono", "Cascadia Mono", Consolas, monospace;
    font-size: .8125rem;
    word-break: break-all;
    user-select: all;
}

/* บัญชีที่ถูกปิดยังต้องอ่านออก แค่ให้เห็นชัดว่าไม่ได้ใช้งานอยู่ */
.list__row.is-inactive { opacity: .55; }

.btn.small { min-height: 30px; padding-inline: var(--s3); font: var(--text-footnote); font-weight: 600; }


/* ── แถบเตือนเมื่อเปิดโหมดแก้จุดบกพร่อง ─────────────────────────────────── */

.debug-banner {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 300;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--s2);
    padding: var(--s2) var(--s4);
    background: var(--orange);
    color: #000;
    font: var(--text-footnote);
    font-weight: 600;
    text-align: center;
}
.debug-banner code {
    font-family: ui-monospace, "SF Mono", Consolas, monospace;
    background: rgb(0 0 0 / .15);
    padding: 1px 5px;
    border-radius: 4px;
}

/* เผื่อที่ว่างให้แถบเตือน ไม่ให้ไปทับปุ่มล่างสุดของหน้า */
:root[data-debug] body { padding-bottom: 40px; }
:root[data-debug] .sidebar { padding-bottom: calc(var(--s3) + 40px); }


/* ==========================================================================
   13ก. หน้าเข้าสู่ระบบ / สมัครสมาชิก
   ========================================================================== */

.auth {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--s5);
    padding: var(--s6) var(--s5);
}

.auth__card {
    width: min(400px, 100%);
    padding: var(--s8) var(--s6);
    background: var(--surface);
    border-radius: var(--r-xl);
    box-shadow: var(--shadow-2);
}
/* โหมดมืดไม่มีเงา จึงใช้เส้นขอบบาง ๆ แยกการ์ดออกจากพื้นหลังดำแทน */
:root[data-theme="dark"] .auth__card { border: 1px solid var(--separator); }
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) .auth__card { border: 1px solid var(--separator); }
}

.auth__brand {
    display: flex;
    align-items: center;
    gap: var(--s3);
    justify-content: center;
    margin-bottom: var(--s6);
    font: var(--text-title-2);
    letter-spacing: -.02em;
}

.auth__title { font: var(--text-title); letter-spacing: -.02em; }
.auth__lead { font: var(--text-subhead); color: var(--label-2); margin-bottom: var(--s5); }
.auth__foot { text-align: center; }


/* ==========================================================================
   14. ตัวช่วยจัดวาง
   ========================================================================== */

.stack   { display: flex; flex-direction: column; gap: var(--s4); }
.stack-2 { display: flex; flex-direction: column; gap: var(--s2); }
.stack-5 { display: flex; flex-direction: column; gap: var(--s5); }
.row     { display: flex; align-items: center; gap: var(--s3); flex-wrap: wrap; }
.row-2   { display: flex; align-items: center; gap: var(--s2); flex-wrap: wrap; }
.push    { margin-left: auto; }
.muted   { color: var(--label-2); }
.dim     { color: var(--label-3); }
.small   { font: var(--text-footnote); }
.nums    { font-variant-numeric: tabular-nums; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }

/* theme.js เติมคลาส .js ให้ <html> — ใช้แยกสิ่งที่ต้องมีเฉพาะตอนปิด JavaScript */
.js .no-js-only { display: none; }
html:not(.js) .js-only { display: none; }

/*
   ช่องตั้งจังหวะของงานประจำ

   เซิร์ฟเวอร์ซ่อนช่องที่ไม่เกี่ยวกับความถี่ที่บันทึกไว้มาให้ตั้งแต่เฟรมแรก
   หน้าจึงไม่กะพริบตอน JavaScript เข้ามาจัดการ

   แต่ถ้าปิด JavaScript ไว้ ต้องเห็นครบทุกช่อง ไม่งั้นเปลี่ยนความถี่แล้ว
   ตั้งจังหวะไม่ได้ในครั้งเดียว (ต้องบันทึกก่อนแล้วเปิดกลับมาแก้อีกรอบ)
   ค่า display ต้องตรงกับ .form-grid และ .field ข้างบน
*/
html:not(.js) [data-repeat-panel][hidden] { display: grid !important; }
html:not(.js) [data-repeat-field][hidden] { display: flex !important; }


/* ==========================================================================
   14ข. พื้นหลังท้องฟ้า

   กลางวัน — ฟ้าใส แดดอ่อน ๆ มุมบน เมฆลอยช้ามาก
   กลางคืน — ฟ้าน้ำเงินเข้ม ดาวกะพริบเบา ๆ และดาวตกเป็นครั้งคราว

   ทั้งหมดวาดด้วย gradient ล้วน ไม่มีไฟล์รูปสักไฟล์ — โหลดเร็ว ไม่กินแบนด์วิดท์
   และไม่ติดข้อจำกัด CSP ที่อนุญาตเฉพาะทรัพยากรจากโดเมนตัวเอง

   ทุกอนิเมชันขยับด้วย transform กับ opacity เท่านั้น เบราว์เซอร์จึงส่งให้ GPU
   ทำได้โดยไม่ต้องคำนวณ layout ใหม่ ไม่แย่งแรงเครื่องไปจากการใช้งานจริง
   ========================================================================== */

.sky { display: none; }

:root[data-sky="on"] body { background: transparent; }
:root[data-sky="on"] .sky { display: block; }

/* เนื้อหาต้องลอยอยู่เหนือท้องฟ้าเสมอ (.auth คือหน้าล็อกอิน/สมัคร) */
.app,
.auth { position: relative; z-index: 1; }

.sky {
    position: fixed;
    inset: 0;
    z-index: 0;
    overflow: hidden;
    pointer-events: none;
    background: linear-gradient(
        180deg,
        rgb(var(--sky-top)) 0%,
        rgb(var(--sky-mid)) 46%,
        rgb(var(--sky-low)) 100%
    );
}

/*
   ม่านสีพื้นหน้าคลุมทับอีกชั้น — คือสิ่งที่ทำให้ท้องฟ้า "จาง ๆ อยู่ข้างหลัง"
   ไม่ใช่ภาพเด่นที่แย่งสายตาไปจากการ์ดงาน
   ใช้ ::before เพื่อให้ม่านอยู่ใต้เมฆกับดาว ดาวจึงยังเห็นชัดบนฟ้าที่ถูกหรี่แล้ว
*/
.sky::before {
    content: '';
    position: absolute;
    inset: 0;
    background: var(--bg);
    opacity: var(--sky-veil);
}

.sky__glow,
.sky__clouds,
.sky__stars,
.sky__shoot {
    position: absolute;
    display: block;
}

/* แสงอาทิตย์/แสงจันทร์มุมขวาบน */
.sky__glow {
    inset: -20% -20% auto auto;
    width: 70vw;
    height: 70vw;
    max-width: 900px;
    max-height: 900px;
    background: radial-gradient(circle, rgb(var(--sky-glow) / .55) 0%, transparent 62%);
}

/* ── กลางวัน: เมฆลอย ───────────────────────────────────────────────────── */

.sky__clouds {
    inset: 0 -60% 40% 0;
    background-repeat: no-repeat;
    background-image:
        radial-gradient(closest-side, rgb(255 255 255 / .85), transparent 100%),
        radial-gradient(closest-side, rgb(255 255 255 / .70), transparent 100%),
        radial-gradient(closest-side, rgb(255 255 255 / .55), transparent 100%),
        radial-gradient(closest-side, rgb(255 255 255 / .45), transparent 100%);
    background-size: 46% 34%, 34% 24%, 28% 22%, 40% 26%;
    background-position: 6% 18%, 34% 8%, 52% 30%, 74% 14%;
    animation: sky-drift 150s linear infinite;
}

/* ลอยไปทางขวาช้า ๆ แล้ววนกลับ — ระยะเท่ากับส่วนที่ยื่นออกไปนอกจอ จึงไม่เห็นรอยต่อ */
@keyframes sky-drift {
    from { transform: translate3d(0, 0, 0); }
    to   { transform: translate3d(37.5%, 0, 0); }
}

/* ── กลางคืน: ดาวและดาวตก ──────────────────────────────────────────────── */

.sky__stars,
.sky__shoot { display: none; }

.sky__stars {
    inset: 0;
    /*
       ดาวสี่ชั้น แต่ละชั้นปูซ้ำด้วยขนาดที่ไม่หารกันลงตัว
       ลายรวมจึงซ้ำที่ระยะไกลมากจนสายตาอ่านว่าเป็นการกระจายแบบสุ่ม
       ถูกกว่าการเขียนพิกัดดาวทีละดวงหลายสิบบรรทัดมาก
    */
    background-image:
        radial-gradient(circle 1.3px at 18px 26px,  rgb(var(--sky-star) / .90), transparent 100%),
        radial-gradient(circle 1px   at 74px 96px,  rgb(var(--sky-star) / .70), transparent 100%),
        radial-gradient(circle 1.1px at 131px 41px, rgb(var(--sky-star) / .80), transparent 100%),
        radial-gradient(circle .9px  at 47px 158px, rgb(var(--sky-star) / .55), transparent 100%);
    background-size: 197px 211px, 263px 241px, 149px 179px, 311px 293px;
    animation: sky-twinkle 7s ease-in-out infinite;
}

@keyframes sky-twinkle {
    0%, 100% { opacity: .75; }
    50%      { opacity: 1; }
}

.sky__shoot {
    width: 2px;
    height: 110px;
    border-radius: 2px;
    background: linear-gradient(to bottom, rgb(var(--sky-star) / 0), rgb(var(--sky-star) / .95));
    opacity: 0;
    animation: sky-shoot 17s ease-in infinite;
}

/*
   พาดลงมาเฉียง ๆ แล้วจางหาย จากนั้นรออีกนานกว่าจะมาอีกรอบ
   ช่วงที่เห็นจริงกินเวลาแค่ราว 8% ของรอบ — นาน ๆ ทีจึงจะเห็น ซึ่งคือความตั้งใจ
*/
@keyframes sky-shoot {
    0%       { opacity: 0;  transform: rotate(34deg) translate3d(0, -180px, 0); }
    2%       { opacity: .95; }
    8%       { opacity: 0;  transform: rotate(34deg) translate3d(0, 660px, 0); }
    100%     { opacity: 0;  transform: rotate(34deg) translate3d(0, 660px, 0); }
}

.sky__shoot--1 { top: 4%;  left: 18%; animation-duration: 17s; animation-delay: 3s; }
.sky__shoot--2 { top: 0;   left: 58%; animation-duration: 26s; animation-delay: 11s; }
.sky__shoot--3 { top: 10%; left: 82%; animation-duration: 34s; animation-delay: 21s; }

/* สลับชุดตามโหมดสี — เขียนสองที่เหมือนโทเคนในธีม
   เพราะโหมด "อัตโนมัติ" ไม่มี data-theme ออกมาเลย ต้องอาศัย media query */
:root[data-theme="dark"] .sky__clouds { display: none; }
:root[data-theme="dark"] .sky__stars,
:root[data-theme="dark"] .sky__shoot { display: block; }

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) .sky__clouds { display: none; }
    :root:not([data-theme="light"]) .sky__stars,
    :root:not([data-theme="light"]) .sky__shoot { display: block; }
}

/*
   คนที่ตั้งเครื่องว่าไม่ชอบภาพเคลื่อนไหว ต้องได้ท้องฟ้าที่นิ่งสนิท
   ดาวตกซ่อนไปเลยเพราะตัวมันเองคือการเคลื่อนไหวล้วน ๆ ไม่มีสถานะหยุดนิ่งที่มีความหมาย
*/
@media (prefers-reduced-motion: reduce) {
    .sky__clouds, .sky__stars { animation: none; }
    .sky__shoot { display: none !important; }
}


/* ==========================================================================
   15. จอเล็กลง
   ========================================================================== */

/* แถบบนสำหรับจอแคบ — ซ่อนไว้บนจอใหญ่ */
.topbar { display: none; }

/*
   ต่ำกว่า 1280px พื้นที่แนวนอนมีค่ามาก ย่อแถบข้างเหลือไอคอน

   เดิมตัดที่ 1023px ซึ่งทำให้ iPad แนวนอน (1024px) โดนแถบข้างเต็ม 260px กิน
   เหลือที่ให้กระดานแค่ 716px — สามคอลัมน์ใส่ไม่ลง ต้องเลื่อนเอา
   ย่อแถบข้างแล้วเหลือ 900px ซึ่งพอดีสามคอลัมน์ ไม่ต้องเลื่อน
*/
@media (max-width: 1279px) {
    :root { --sidebar-w: 76px; }

    .sidebar { padding-inline: var(--s2); align-items: center; }
    .sidebar__brand { padding: var(--s1) 0 var(--s5); }
    .sidebar__brand span, .nav-item span, .nav-item__count,
    .user-chip__text { display: none; }
    .nav-item { justify-content: center; padding-inline: 0; }
    .user-chip { justify-content: center; }
    .sidebar__footer .segmented { display: none; }
}

@media (max-width: 767px) {
    .app { grid-template-columns: minmax(0, 1fr); }

    .topbar {
        position: sticky;
        top: 0;
        z-index: 40;
        display: flex;
        align-items: center;
        gap: var(--s3);
        padding: var(--s2) var(--s4);
        background: var(--material);
        -webkit-backdrop-filter: saturate(180%) blur(20px);
        backdrop-filter: saturate(180%) blur(20px);
        border-bottom: 1px solid var(--separator);
    }
    .topbar__title { font: var(--text-headline); }

    .sidebar {
        position: fixed;
        inset: 0 auto 0 0;
        width: 268px;
        max-width: 84vw;
        padding-inline: var(--s3);
        align-items: stretch;
        transform: translateX(-100%);
        transition: transform var(--t-slow) var(--ease);
        z-index: 60;
    }
    .sidebar.is-open { transform: none; }
    .sidebar__brand span, .nav-item span, .nav-item__count,
    .user-chip__text { display: revert; }
    .nav-item { justify-content: flex-start; padding-inline: var(--s3); }
    .user-chip { justify-content: flex-start; }
    .sidebar__footer .segmented { display: inline-flex; }

    .sidebar-scrim {
        position: fixed;
        inset: 0;
        z-index: 50;
        background: var(--scrim);
        animation: fade-in var(--t-base) var(--ease);
    }
    .sidebar-scrim[hidden] { display: none; }

    .container { padding-inline: var(--s4); }
    .page-title { font: var(--text-title); }
    .page-head__bar { min-height: 44px; }
    .sheet { padding: 0; place-items: end stretch; }
    .sheet__panel {
        width: 100%;
        max-height: 92vh;
        border-radius: var(--r-xl) var(--r-xl) 0 0;
        /* จอเล็กให้แผงเลื่อนขึ้นมาจากด้านล่างเหมือน sheet บน iOS */
        animation: sheet-up var(--t-slow) var(--ease);
    }
    .toasts { top: auto; bottom: var(--s4); left: var(--s4); right: var(--s4); }
    .toast { max-width: none; }
}

@keyframes sheet-up { from { transform: translateY(100%); } }

@media (min-width: 768px) {
    .sidebar-scrim { display: none; }
}


/* ==========================================================================
   16. พิมพ์ออกกระดาษ
   ========================================================================== */

@media print {
    .sidebar, .topbar, .page-head__actions, .btn, .sheet, .toasts { display: none !important; }
    .app { display: block; }
    body { background: #fff; }
}
