:root {
    --bg: #f5f7fb;
    --card: #ffffff;
    --text: #172033;
    --muted: #65728a;
    --border: #dfe6f1;
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --danger: #dc2626;
    --success: #16a34a;
    --shadow: 0 14px 40px rgba(23, 32, 51, 0.08);
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    min-height: 100vh;
    color: var(--text);
    background: linear-gradient(135deg, #eef4ff 0%, var(--bg) 50%, #f8fafc 100%);
    font-family: Arial, Tahoma, sans-serif;
}

a {
    color: inherit;
    text-decoration: none;
}

.site-header {
    position: sticky;
    top: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    gap: 24px;
    justify-content: space-between;
    overflow-x: auto;
    padding: 16px clamp(20px, 4vw, 48px);
    background: rgba(255, 255, 255, 0.9);
    border-bottom: 1px solid var(--border);
    backdrop-filter: blur(12px);
    white-space: nowrap;
}

.brand {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    font-size: 20px;
    font-weight: 700;
}

.brand-mark {
    display: grid;
    width: 36px;
    height: 36px;
    color: #fff;
    background: var(--primary);
    border-radius: 12px;
    place-items: center;
}

.top-nav,
.user-menu,
.toolbar,
.form-actions,
.table-actions {
    display: flex;
    align-items: center;
    gap: 12px;
}

.top-nav {
    flex: 0 0 auto;
}

.top-nav a {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    color: var(--muted);
    font-weight: 600;
}

.icon {
    width: 1em;
    height: 1em;
    flex: 0 0 auto;
    fill: currentColor;
}

.header-search {
    display: flex !important;
    align-items: center;
    flex: 0 0 auto;
    flex-direction: row !important;
    flex-wrap: nowrap !important;
    gap: 8px;
}

.header-search input,
.header-search select {
    flex: 0 0 auto;
    width: auto !important;
    min-width: 120px;
    min-height: 36px;
    padding: 7px 10px;
    border-radius: 10px;
    font-size: 14px;
}

.header-search input[type="search"] {
    width: 210px !important;
    min-width: 210px;
}

.header-search select {
    width: 140px !important;
}

.header-search button,
.header-search .button {
    flex: 0 0 auto;
    min-height: 36px;
    padding: 7px 12px;
    border-radius: 10px;
    font-size: 14px;
    white-space: nowrap;
}

.page-shell {
    width: min(1180px, calc(100% - 32px));
    margin: 32px auto;
}

.auth-page .page-shell {
    display: grid;
    min-height: calc(100vh - 104px);
    place-items: center;
}

.card,
.auth-card,
.panel {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 20px;
    box-shadow: var(--shadow);
}

.auth-card {
    width: min(440px, 100%);
    padding: 32px;
}

.hero,
.panel {
    padding: 28px;
}

.hero {
    display: flex;
    gap: 18px;
    justify-content: space-between;
    margin-bottom: 24px;
}

.hero h1,
.auth-card h1,
.panel h1 {
    margin: 0 0 8px;
}

.muted {
    color: var(--muted);
}

.grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: 18px;
}

.app-card {
    display: flex;
    flex-direction: column;
    gap: 16px;
    min-height: 210px;
    padding: 22px;
    color: inherit;
    text-decoration: none;
    cursor: pointer;
    border: 2px solid transparent;
    outline: none;
    transition: transform 0.16s ease, box-shadow 0.16s ease, border-color 0.16s ease, background 0.16s ease;
}

.app-card:hover {
    background: linear-gradient(180deg, rgba(37, 99, 235, 0.04), rgba(37, 99, 235, 0.0));
    border-color: var(--primary);
    box-shadow: 0 22px 52px rgba(37, 99, 235, 0.18);
    transform: translateY(-3px);
}

.app-card:active {
    transform: translateY(-1px) scale(0.99);
    box-shadow: 0 10px 24px rgba(23, 32, 51, 0.12);
    transition-duration: 0.05s;
}

.app-card:focus-visible {
    border-color: var(--primary);
    box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.2);
}

.app-icon {
    display: grid;
    width: 48px;
    height: 48px;
    color: #fff;
    background: var(--primary);
    border-radius: 16px;
    font-size: 22px;
    font-weight: 700;
    place-items: center;
}

.app-card h2 {
    margin: 0;
}

.app-card p {
    flex: 1;
    margin: 0;
    color: var(--muted);
    line-height: 1.55;
}

.badge {
    display: inline-flex;
    align-items: center;
    width: fit-content;
    padding: 4px 10px;
    color: var(--muted);
    background: #eef2ff;
    border-radius: 999px;
    font-size: 13px;
    font-weight: 700;
}

.button,
button {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    justify-content: center;
    min-height: 40px;
    padding: 10px 16px;
    color: #fff;
    background: var(--primary);
    border: 0;
    border-radius: 12px;
    cursor: pointer;
    font-weight: 700;
}

.button:hover,
button:hover {
    background: var(--primary-dark);
}

.button-ghost {
    color: var(--primary);
    background: #eef4ff;
}

.button-ghost:hover {
    color: #fff;
}

.button-danger {
    background: var(--danger);
}

.form-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 18px;
}

.field {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 16px;
}

.field-full {
    grid-column: 1 / -1;
}

label {
    color: var(--muted);
    font-weight: 700;
}

input,
textarea,
select {
    width: 100%;
    min-height: 44px;
    padding: 10px 12px;
    color: var(--text);
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 12px;
    font: inherit;
}

input[type="checkbox"] {
    width: auto;
    min-height: auto;
}

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

.flash {
    margin-bottom: 18px;
    padding: 14px 16px;
    border-radius: 14px;
    font-weight: 700;
}

.flash-success {
    color: #166534;
    background: #dcfce7;
}

.flash-error {
    color: #991b1b;
    background: #fee2e2;
}

.data-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 18px;
}

.data-table th,
.data-table td {
    padding: 14px;
    border-bottom: 1px solid var(--border);
    text-align: left;
    vertical-align: top;
}

.data-table th {
    color: var(--muted);
    font-size: 13px;
    text-transform: uppercase;
}

.empty-state {
    padding: 34px;
    color: var(--muted);
    text-align: center;
}

/* ===== Iframe view (เปิดเว็บแอปฝังในหน้าเดียวกัน) ===== */
.frame-page .page-shell {
    width: 100%;
    margin: 0;
    padding: 0;
}

.frame-toolbar {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px clamp(16px, 3vw, 32px);
    background: var(--card);
    border-bottom: 1px solid var(--border);
    flex-wrap: wrap;
}

.frame-toolbar .frame-title {
    display: flex;
    align-items: center;
    gap: 10px;
    flex: 1 1 auto;
    min-width: 0;
    font-size: 16px;
}

.frame-toolbar .frame-title strong {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.frame-wrap {
    position: relative;
    height: calc(100vh - 68px - 65px);
    min-height: 480px;
    background: #fff;
}

.frame-iframe {
    width: 100%;
    height: 100%;
    border: 0;
    display: block;
}

@media (max-width: 760px) {
    .hero,
    .toolbar,
    .form-actions {
        align-items: stretch;
        flex-direction: column;
    }

    .top-nav,
    .user-menu,
    .table-actions {
        flex-wrap: wrap;
    }

    .form-grid {
        grid-template-columns: 1fr;
    }

    .frame-wrap {
        height: calc(100vh - 64px - 80px);
    }
}

/* ===== Icon image support ===== */
.app-icon img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: inherit;
}

.icon-current {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 12px;
}

.icon-current label {
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.icon-preview {
    width: 56px;
    height: 56px;
    object-fit: cover;
    border-radius: 12px;
    background: #f1f5f9;
    border: 1px solid var(--border);
}

.icon-preview-small {
    width: 36px;
    height: 36px;
    object-fit: cover;
    border-radius: 8px;
    background: #f1f5f9;
    border: 1px solid var(--border);
}

.icon-inputs {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

@media (max-width: 760px) {
    .icon-inputs {
        grid-template-columns: 1fr;
    }
}

/* ===== Icon image refinements (avoid clipping wide logos) ===== */
.app-icon {
    flex-shrink: 0;
    align-self: flex-start;
    overflow: hidden;
}

.app-icon img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    background: rgba(255, 255, 255, 0.92);
    border-radius: inherit;
}

/* When card has an image icon, give the icon a touch more breathing room */
.app-card .app-icon:has(img) {
    width: 64px;
    height: 64px;
    background: #ffffff !important;
    border: 1px solid var(--border);
}

/* ===== Custom card background — preserve hover effects ===== */
/* When card has inline background, hover keeps it but adds the border + lift */
.app-card[style*="background"]:hover {
    background: inherit;
    filter: brightness(0.96);
}

/* ===== Tinted card (with custom card_color gradient) ===== */
.app-card-tinted {
    border-color: rgba(0, 0, 0, 0.06);
    box-shadow: 0 16px 36px rgba(23, 32, 51, 0.10);
}

/* Override the earlier `background: inherit` rule so the gradient survives :hover */
.app-card[style*="background"]:hover {
    background: inherit;
}
.app-card-tinted:hover {
    filter: brightness(0.96);
    border-color: rgba(0, 0, 0, 0.12);
    box-shadow: 0 22px 52px rgba(0, 0, 0, 0.18);
    transform: translateY(-3px);
}

/* Dark backgrounds: switch text to light colors for contrast */
.app-card-dark {
    color: #fff;
}
.app-card-dark h2 {
    color: #fff;
}
.app-card-dark p {
    color: rgba(255, 255, 255, 0.86);
}
.app-card-dark .badge {
    color: #fff;
    background: rgba(255, 255, 255, 0.20);
}

/* ===== Iframe fallback (when target site blocks embedding) ===== */
.frame-fallback {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    padding: 24px;
    background: linear-gradient(180deg, #f8fafc, #eef2ff);
    z-index: 2;
}

.frame-fallback[hidden] {
    display: none !important;
}

.frame-fallback-card {
    max-width: 560px;
    padding: 28px;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 18px;
    text-align: center;
    box-shadow: var(--shadow);
}

.frame-fallback-card h2 {
    margin: 0 0 12px;
    color: var(--text);
}

.frame-fallback-card p {
    margin: 8px 0 16px;
    line-height: 1.6;
}

.frame-fallback-card code {
    padding: 2px 6px;
    background: #f1f5f9;
    border-radius: 6px;
    font-size: 13px;
    word-break: break-all;
}

.frame-fallback-card .button {
    margin-top: 8px;
}

/* ===== Site footer (powered by Mplus) ===== */
.site-footer {
    margin-top: 48px;
    padding: 24px clamp(20px, 4vw, 48px);
    background: rgba(255, 255, 255, 0.65);
    border-top: 1px solid var(--border);
    backdrop-filter: blur(8px);
}

.site-footer-inner {
    max-width: 1180px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 14px;
    flex-wrap: wrap;
}

.footer-logo {
    height: 44px;
    width: auto;
    display: block;
}

.footer-text {
    margin: 0;
    color: var(--muted);
    font-size: 14px;
}

.footer-text strong {
    color: var(--text);
}

/* Compact footer on iframe page */
.frame-page .site-footer {
    margin-top: 0;
    padding: 8px 16px;
    background: rgba(255, 255, 255, 0.85);
    border-top: 1px solid var(--border);
}

.frame-page .footer-logo {
    height: 26px;
}

.frame-page .footer-text {
    font-size: 12px;
}

/* When iframe view is used, shrink the iframe wrap to make room for footer */
.frame-page .frame-wrap {
    height: calc(100vh - 68px - 65px - 50px);
}

/* ===== Sticky footer — push footer to viewport bottom when page is short ===== */
body {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

.site-header {
    flex: 0 0 auto;
}

main.page-shell {
    flex: 1 0 auto;
}

.site-footer {
    flex: 0 0 auto;
}

/* Reset auth-page so its min-height doesn't fight the flex container */
.auth-page .page-shell {
    min-height: auto;
    flex: 1 0 auto;
}


/* ===== Brand-mark uses logo image (overrides the M letter version) ===== */
img.brand-mark {
    background: transparent;
    border-radius: 8px;
    object-fit: contain;
    width: 40px;
    height: 40px;
}

/* ===== Icon source sections in admin form ===== */
details.icon-source {
    margin: 10px 0;
    padding: 12px 14px;
    background: #f8fafc;
    border: 1px solid var(--border);
    border-radius: 12px;
}

details.icon-source[open] {
    background: #ffffff;
}

details.icon-source summary {
    cursor: pointer;
    list-style: none;
    user-select: none;
    padding: 4px 0;
}

details.icon-source summary::-webkit-details-marker {
    display: none;
}

details.icon-source summary::before {
    content: '▸ ';
    display: inline-block;
    margin-right: 4px;
    transition: transform 0.15s ease;
}

details.icon-source[open] summary::before {
    transform: rotate(90deg);
}

/* Iconify picker */
.icon-picker {
    margin-top: 12px;
}

.icon-picker input[type="search"] {
    width: 100%;
}

.icon-picker-hint {
    font-size: 12px;
    margin: 6px 0 10px;
}

.icon-picker-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(56px, 1fr));
    gap: 8px;
    max-height: 360px;
    overflow-y: auto;
    padding: 4px;
}

.icon-picker-item {
    display: grid;
    place-items: center;
    width: 100%;
    height: 56px;
    padding: 8px;
    background: #ffffff;
    border: 1px solid var(--border);
    border-radius: 10px;
    cursor: pointer;
    transition: all 0.12s ease;
}

.icon-picker-item:hover {
    border-color: var(--primary);
    background: #eef4ff;
    transform: translateY(-1px);
}

.icon-picker-item.is-selected {
    border-color: var(--primary);
    background: #eef4ff;
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.18);
}

.icon-picker-item img {
    width: 24px;
    height: 24px;
    display: block;
    pointer-events: none;
}
