/* ==========================================================
   Yumat LINE Manager - Design System
   Font: Kanit | Primary: #0D6EFD | Radius: 12px
   ========================================================== */

:root {
    --yumat-primary: #0D6EFD;
    --yumat-primary-dark: #0B5ED7;
    --yumat-success: #16A34A;
    --yumat-warning: #F59E0B;
    --yumat-danger: #DC2626;
    --yumat-info: #0EA5E9;

    --yumat-bg: #F7F9FC;
    --yumat-card: #FFFFFF;
    --yumat-border: #E5EAF2;
    --yumat-text: #1F2937;
    --yumat-text-muted: #6B7280;

    --yumat-radius: 12px;
    --yumat-radius-sm: 8px;
    --yumat-sidebar-width: 264px;
    --yumat-sidebar-collapsed: 76px;
    --yumat-header-height: 64px;

    --yumat-shadow-sm: 0 1px 2px rgba(16, 24, 40, .05);
    --yumat-shadow: 0 4px 16px rgba(16, 24, 40, .06);
}

/* ---------------------------------------------------------
   พื้นฐาน
   --------------------------------------------------------- */

body {
    font-family: 'Kanit', 'Segoe UI', system-ui, -apple-system, sans-serif;
    background-color: var(--yumat-bg);
    color: var(--yumat-text);
    font-size: 15px;
    font-weight: 300;
    -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4, h5, h6, .fw-semibold {
    font-weight: 500;
}

a {
    text-decoration: none;
}

.text-muted-2 {
    color: var(--yumat-text-muted) !important;
}

/* ---------------------------------------------------------
   โครงหน้าจอ (Sidebar + Content)
   --------------------------------------------------------- */

.app-shell {
    display: flex;
    min-height: 100vh;
}

.app-sidebar {
    width: var(--yumat-sidebar-width);
    flex: 0 0 var(--yumat-sidebar-width);
    background: #0F172A;
    color: #CBD5E1;
    display: flex;
    flex-direction: column;
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 1040;
    transition: transform .2s ease, width .2s ease;
    overflow-y: auto;
}

.app-sidebar__brand {
    display: flex;
    align-items: center;
    gap: .65rem;
    height: var(--yumat-header-height);
    padding: 0 1.15rem;
    color: #fff;
    border-bottom: 1px solid rgba(255, 255, 255, .08);
    flex: 0 0 auto;
}

.app-sidebar__logo {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    background: linear-gradient(135deg, var(--yumat-primary), #6366F1);
    display: grid;
    place-items: center;
    font-weight: 600;
    color: #fff;
    flex: 0 0 auto;
}

.app-sidebar__brand-text {
    line-height: 1.15;
    overflow: hidden;
}

.app-sidebar__brand-text strong {
    display: block;
    font-size: .95rem;
    font-weight: 500;
    white-space: nowrap;
}

.app-sidebar__brand-text span {
    font-size: .72rem;
    color: #94A3B8;
    white-space: nowrap;
}

.app-sidebar__nav {
    padding: 1rem .75rem 2rem;
    flex: 1 1 auto;
}

.app-sidebar__heading {
    font-size: .7rem;
    letter-spacing: .06em;
    text-transform: uppercase;
    color: #64748B;
    padding: 1rem .75rem .4rem;
    white-space: nowrap;
}

.app-nav-link {
    display: flex;
    align-items: center;
    gap: .7rem;
    padding: .58rem .75rem;
    border-radius: var(--yumat-radius-sm);
    color: #CBD5E1;
    font-size: .9rem;
    margin-bottom: .15rem;
    transition: background-color .15s ease, color .15s ease;
    white-space: nowrap;
}

.app-nav-link:hover {
    background: rgba(255, 255, 255, .07);
    color: #fff;
}

.app-nav-link.is-active {
    background: var(--yumat-primary);
    color: #fff;
    box-shadow: 0 4px 12px rgba(13, 110, 253, .35);
}

.app-nav-link i {
    font-size: 1.05rem;
    width: 1.25rem;
    text-align: center;
    flex: 0 0 auto;
}

.app-nav-link.is-disabled {
    opacity: .45;
    cursor: not-allowed;
}

.app-nav-link .badge {
    font-size: .62rem;
    font-weight: 400;
}

.app-main {
    flex: 1 1 auto;
    min-width: 0;
    margin-left: var(--yumat-sidebar-width);
    display: flex;
    flex-direction: column;
    transition: margin-left .2s ease;
}

.app-header {
    height: var(--yumat-header-height);
    background: var(--yumat-card);
    border-bottom: 1px solid var(--yumat-border);
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0 1.25rem;
    position: sticky;
    top: 0;
    z-index: 1020;
}

.app-content {
    padding: 1.5rem 1.25rem 3rem;
    flex: 1 1 auto;
}

/* Sidebar แบบยุบ (จอใหญ่) */
body.sidebar-collapsed .app-sidebar {
    width: var(--yumat-sidebar-collapsed);
    flex-basis: var(--yumat-sidebar-collapsed);
}

body.sidebar-collapsed .app-main {
    margin-left: var(--yumat-sidebar-collapsed);
}

body.sidebar-collapsed .app-sidebar__brand-text,
body.sidebar-collapsed .app-sidebar__heading,
body.sidebar-collapsed .app-nav-link span {
    display: none;
}

body.sidebar-collapsed .app-nav-link {
    justify-content: center;
    padding-inline: 0;
}

/* ---------------------------------------------------------
   การ์ดและตาราง
   --------------------------------------------------------- */

.card {
    border: 1px solid var(--yumat-border);
    border-radius: var(--yumat-radius);
    background: var(--yumat-card);
    box-shadow: var(--yumat-shadow-sm);
}

.card-header {
    background: transparent;
    border-bottom: 1px solid var(--yumat-border);
    padding: 1rem 1.15rem;
    font-weight: 500;
}

.stat-card {
    border-radius: var(--yumat-radius);
    padding: 1.15rem;
    height: 100%;
}

.stat-card__icon {
    width: 46px;
    height: 46px;
    border-radius: var(--yumat-radius-sm);
    display: grid;
    place-items: center;
    font-size: 1.3rem;
}

.stat-card__value {
    font-size: 1.75rem;
    font-weight: 500;
    line-height: 1.2;
}

.table {
    --bs-table-bg: transparent;
    margin-bottom: 0;
}

.table > :not(caption) > * > * {
    padding: .8rem .9rem;
    vertical-align: middle;
}

.table thead th {
    font-weight: 500;
    font-size: .82rem;
    color: var(--yumat-text-muted);
    text-transform: uppercase;
    letter-spacing: .03em;
    border-bottom: 1px solid var(--yumat-border);
    white-space: nowrap;
}

.table-hover > tbody > tr:hover > * {
    background-color: #F8FAFF;
}

.table-responsive {
    border-radius: var(--yumat-radius);
}

/* ---------------------------------------------------------
   ฟอร์มและปุ่ม
   --------------------------------------------------------- */

.form-label {
    font-size: .88rem;
    font-weight: 400;
    margin-bottom: .35rem;
}

.form-label .required {
    color: var(--yumat-danger);
}

.form-control,
.form-select {
    border-radius: var(--yumat-radius-sm);
    border-color: var(--yumat-border);
    padding: .55rem .8rem;
    font-size: .92rem;
}

.form-control:focus,
.form-select:focus {
    border-color: var(--yumat-primary);
    box-shadow: 0 0 0 .2rem rgba(13, 110, 253, .12);
}

.form-text {
    font-size: .8rem;
}

.btn {
    border-radius: var(--yumat-radius-sm);
    font-size: .92rem;
    padding: .5rem 1rem;
    display: inline-flex;
    align-items: center;
    gap: .4rem;
}

.btn-primary {
    background-color: var(--yumat-primary);
    border-color: var(--yumat-primary);
}

.btn-primary:hover,
.btn-primary:focus {
    background-color: var(--yumat-primary-dark);
    border-color: var(--yumat-primary-dark);
}

.btn-icon {
    padding: .35rem .55rem;
    line-height: 1;
}

.badge {
    font-weight: 400;
    padding: .35em .6em;
    border-radius: 999px;
    display: inline-flex;
    align-items: center;
    gap: .3rem;
}

/* ---------------------------------------------------------
   สถานะว่าง / โหลด / ผิดพลาด
   --------------------------------------------------------- */

.empty-state {
    padding: 3rem 1rem;
    text-align: center;
    color: var(--yumat-text-muted);
}

.empty-state i {
    font-size: 2.5rem;
    opacity: .45;
    display: block;
    margin-bottom: .75rem;
}

.avatar-circle {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    background: #EEF2FF;
    color: var(--yumat-primary);
    display: grid;
    place-items: center;
    font-weight: 500;
    font-size: .85rem;
    flex: 0 0 auto;
}

.page-title {
    font-size: 1.35rem;
    font-weight: 500;
    margin-bottom: .15rem;
}

.page-subtitle {
    color: var(--yumat-text-muted);
    font-size: .88rem;
    margin-bottom: 0;
}

.permission-group {
    border: 1px solid var(--yumat-border);
    border-radius: var(--yumat-radius-sm);
    padding: .9rem 1rem;
    height: 100%;
}

.permission-group__title {
    font-size: .9rem;
    font-weight: 500;
    margin-bottom: .6rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: .5rem;
}

.timeline-item {
    display: flex;
    gap: .75rem;
    padding: .65rem 0;
    border-bottom: 1px dashed var(--yumat-border);
}

.timeline-item:last-child {
    border-bottom: 0;
}

/* ---------------------------------------------------------
   หน้าเข้าสู่ระบบ
   --------------------------------------------------------- */

.auth-page {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 2rem 1rem;
    background:
        radial-gradient(1200px 600px at 10% -10%, rgba(13, 110, 253, .14), transparent 60%),
        radial-gradient(900px 500px at 110% 110%, rgba(99, 102, 241, .16), transparent 55%),
        var(--yumat-bg);
}

.auth-card {
    width: 100%;
    max-width: 430px;
    background: var(--yumat-card);
    border-radius: 18px;
    box-shadow: var(--yumat-shadow);
    border: 1px solid var(--yumat-border);
    padding: 2rem 1.85rem;
}

.auth-logo {
    width: 56px;
    height: 56px;
    border-radius: 16px;
    background: linear-gradient(135deg, var(--yumat-primary), #6366F1);
    display: grid;
    place-items: center;
    color: #fff;
    font-size: 1.5rem;
    margin: 0 auto 1rem;
}

/* ---------------------------------------------------------
   Responsive
   --------------------------------------------------------- */

@media (max-width: 991.98px) {
    .app-sidebar {
        transform: translateX(-100%);
        box-shadow: 0 0 40px rgba(0, 0, 0, .25);
    }

    body.sidebar-open .app-sidebar {
        transform: translateX(0);
    }

    .app-main {
        margin-left: 0;
    }

    body.sidebar-collapsed .app-main {
        margin-left: 0;
    }

    .app-backdrop {
        position: fixed;
        inset: 0;
        background: rgba(15, 23, 42, .45);
        z-index: 1035;
        display: none;
    }

    body.sidebar-open .app-backdrop {
        display: block;
    }
}

@media (max-width: 575.98px) {
    .app-content {
        padding: 1rem .85rem 2.5rem;
    }

    .stat-card__value {
        font-size: 1.45rem;
    }
}

/* ---------------------------------------------------------
   โหมดมืด
   --------------------------------------------------------- */

[data-bs-theme="dark"] {
    --yumat-bg: #0B1220;
    --yumat-card: #131C2E;
    --yumat-border: #24304A;
    --yumat-text: #E5E7EB;
    --yumat-text-muted: #94A3B8;
}

[data-bs-theme="dark"] body {
    background-color: var(--yumat-bg);
    color: var(--yumat-text);
}

[data-bs-theme="dark"] .table-hover > tbody > tr:hover > * {
    background-color: rgba(255, 255, 255, .03);
}

/* ---------------------------------------------------------
   Rich Menu editor & preview
   --------------------------------------------------------- */

.richmenu-canvas {
    position: relative;
    width: 100%;
    background: #0b1220 repeating-conic-gradient(#1a2436 0% 25%, #141d2e 0% 50%) 50% / 24px 24px;
    border-radius: var(--yumat-radius-sm);
    overflow: hidden;
}

.richmenu-canvas img {
    display: block;
    width: 100%;
    height: auto;
}

.area-overlay {
    position: absolute;
    inset: 0;
    pointer-events: none;
}

.area-box {
    position: absolute;
    border: 2px solid var(--yumat-primary);
    box-sizing: border-box;
    display: flex;
    align-items: flex-start;
    justify-content: flex-start;
}

.area-box span {
    font-size: .68rem;
    line-height: 1.2;
    padding: 1px 4px;
    background: rgba(15, 23, 42, .72);
    color: #fff;
    border-radius: 0 0 4px 0;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/**
 * การ์ดพรีวิวในหน้าแก้ไข Rich Menu ตรึงติดจอแบบกะทัดรัด (sticky)
 * เพื่อให้เห็นกรอบพื้นที่กดขยับตามขณะเลื่อนแก้ไขพื้นที่ด้านล่าง
 * ย่อภาพให้พอดี ไม่บังช่องแก้ไข — เปิดเฉพาะจอกว้าง (>= 992px)
 */
@media (min-width: 992px) {
    .rm-sticky-preview {
        position: sticky;
        top: calc(var(--yumat-header-height) + 12px);
        z-index: 2;
    }

    /* ย่อภาพในพรีวิวให้สูงไม่เกินสัดส่วนหน้าจอ และจัดกึ่งกลาง
       ใช้ width:auto + max-height เพื่อให้กรอบ overlay (inset:0) ยังตรงกับภาพเสมอ */
    .rm-sticky-preview .richmenu-canvas {
        width: fit-content;
        max-width: 100%;
        margin-inline: auto;
    }

    .rm-sticky-preview .richmenu-canvas img {
        width: auto;
        max-width: 100%;
        max-height: 38vh;
        height: auto;
    }
}

/* Preview บนกรอบมือถือ */
.phone-frame {
    max-width: 360px;
    margin: 0 auto;
    border: 10px solid #111827;
    border-radius: 34px;
    overflow: hidden;
    background: #e9edf5;
    box-shadow: var(--yumat-shadow);
}

.phone-frame__screen {
    background: #7494c0;
    min-height: 360px;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
}

.phone-frame__chatbar {
    background: #fff;
    border-top: 1px solid var(--yumat-border);
    padding: .5rem .9rem;
    font-size: .85rem;
    color: var(--yumat-text);
    display: flex;
    align-items: center;
    gap: .5rem;
}

.phone-frame__menu {
    position: relative;
    line-height: 0;
}

.phone-frame__menu img {
    width: 100%;
    height: auto;
}

.phone-frame__menu.show-areas .area-box {
    border-width: 1px;
}

/* ---------------------------------------------------------
   Rich Menu Builder (Phase 4)
   --------------------------------------------------------- */

.builder-body {
    height: 100vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    background: var(--yumat-bg);
}

.builder-topbar {
    flex: 0 0 auto;
    height: 56px;
    background: var(--yumat-card);
    border-bottom: 1px solid var(--yumat-border);
    display: flex;
    align-items: center;
    gap: .75rem;
    padding: 0 1rem;
    z-index: 30;
}

.builder-topbar__title {
    font-weight: 500;
    line-height: 1.1;
    min-width: 0;
}

.builder-main {
    flex: 1 1 auto;
    display: flex;
    min-height: 0;
}

.builder-panel {
    flex: 0 0 250px;
    width: 250px;
    background: var(--yumat-card);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

.builder-panel--left {
    border-right: 1px solid var(--yumat-border);
}

.builder-panel--right {
    border-left: 1px solid var(--yumat-border);
    flex-basis: 300px;
    width: 300px;
}

.builder-panel__section {
    padding: .85rem 1rem;
    border-bottom: 1px solid var(--yumat-border);
}

.builder-panel__heading {
    font-size: .72rem;
    text-transform: uppercase;
    letter-spacing: .05em;
    color: var(--yumat-text-muted);
    margin-bottom: .6rem;
}

/* พื้นที่ Canvas ตรงกลาง */
.builder-stage {
    flex: 1 1 auto;
    min-width: 0;
    overflow: auto;
    background:
        repeating-conic-gradient(#e9edf5 0% 25%, #f7f9fc 0% 50%) 50% / 22px 22px;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: 40px;
}

[data-bs-theme="dark"] .builder-stage {
    background: repeating-conic-gradient(#131c2e 0% 25%, #0b1220 0% 50%) 50% / 22px 22px;
}

.builder-canvas-wrap {
    position: relative;
}

.builder-canvas {
    position: relative;
    line-height: 0;
    box-shadow: 0 8px 30px rgba(16, 24, 40, .18);
    background: #fff;
    user-select: none;
    touch-action: none;
}

.builder-canvas img {
    display: block;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.builder-canvas__grid {
    position: absolute;
    inset: 0;
    pointer-events: none;
    background-image:
        linear-gradient(rgba(13, 110, 253, .18) 1px, transparent 1px),
        linear-gradient(90deg, rgba(13, 110, 253, .18) 1px, transparent 1px);
    display: none;
}

.builder-canvas.show-grid .builder-canvas__grid {
    display: block;
}

/* กล่องพื้นที่กด */
.b-area {
    position: absolute;
    border: 2px solid var(--yumat-primary);
    background: rgba(13, 110, 253, .14);
    box-sizing: border-box;
    cursor: move;
}

.b-area__label {
    position: absolute;
    top: 0;
    left: 0;
    font-size: 11px;
    line-height: 1.3;
    padding: 1px 5px;
    background: var(--yumat-primary);
    color: #fff;
    white-space: nowrap;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    border-radius: 0 0 4px 0;
}

.b-area.is-selected {
    z-index: 5;
    box-shadow: 0 0 0 2px #fff, 0 0 0 4px var(--yumat-primary);
}

.b-area__handle {
    position: absolute;
    width: 12px;
    height: 12px;
    background: #fff;
    border: 2px solid var(--yumat-primary);
    border-radius: 2px;
    display: none;
}

.b-area.is-selected .b-area__handle {
    display: block;
}

.b-area__handle[data-dir="nw"] { top: -7px; left: -7px; cursor: nwse-resize; }
.b-area__handle[data-dir="ne"] { top: -7px; right: -7px; cursor: nesw-resize; }
.b-area__handle[data-dir="sw"] { bottom: -7px; left: -7px; cursor: nesw-resize; }
.b-area__handle[data-dir="se"] { bottom: -7px; right: -7px; cursor: nwse-resize; }
.b-area__handle[data-dir="n"]  { top: -7px; left: 50%; margin-left: -6px; cursor: ns-resize; }
.b-area__handle[data-dir="s"]  { bottom: -7px; left: 50%; margin-left: -6px; cursor: ns-resize; }
.b-area__handle[data-dir="w"]  { left: -7px; top: 50%; margin-top: -6px; cursor: ew-resize; }
.b-area__handle[data-dir="e"]  { right: -7px; top: 50%; margin-top: -6px; cursor: ew-resize; }

/* รายการ Layer ในแผงซ้าย */
.layer-item {
    display: flex;
    align-items: center;
    gap: .5rem;
    padding: .45rem .55rem;
    border: 1px solid var(--yumat-border);
    border-radius: var(--yumat-radius-sm);
    margin-bottom: .4rem;
    cursor: pointer;
    font-size: .85rem;
}

.layer-item.is-selected {
    border-color: var(--yumat-primary);
    background: rgba(13, 110, 253, .08);
}

.layer-item__swatch {
    width: 14px;
    height: 14px;
    border-radius: 3px;
    flex: 0 0 auto;
}

.layout-preset {
    border: 1px solid var(--yumat-border);
    border-radius: var(--yumat-radius-sm);
    padding: .5rem;
    cursor: pointer;
    background: none;
    width: 100%;
    text-align: center;
}

.layout-preset:hover {
    border-color: var(--yumat-primary);
    background: rgba(13, 110, 253, .06);
}

.layout-preset__grid {
    display: grid;
    gap: 2px;
    aspect-ratio: 3 / 2;
    margin-bottom: .35rem;
}

.layout-preset__cell {
    background: var(--yumat-primary);
    opacity: .55;
    border-radius: 2px;
}

.save-indicator {
    font-size: .8rem;
    display: inline-flex;
    align-items: center;
    gap: .35rem;
    white-space: nowrap;
}

/* Builder responsive: จอเล็กให้แผงกลายเป็น offcanvas ซ้อน ไม่ทับ Canvas */
@media (max-width: 1199.98px) {
    .builder-panel {
        position: absolute;
        top: 56px;
        bottom: 0;
        z-index: 25;
        transform: translateX(-100%);
        transition: transform .2s ease;
        box-shadow: var(--yumat-shadow);
    }

    .builder-panel--right {
        right: 0;
        transform: translateX(100%);
    }

    .builder-panel.is-open {
        transform: translateX(0);
    }

    .builder-main {
        position: relative;
    }
}

@media (min-width: 1200px) {
    .builder-panel-toggle {
        display: none !important;
    }
}

@media print {
    .app-sidebar,
    .app-header,
    .no-print {
        display: none !important;
    }

    .app-main {
        margin-left: 0;
    }
}
