/* ==========================================================================
   yotathai.link — ระบบย่อลิงก์และ QR Code ภายในองค์กรโยธาไทย
   ออกแบบใหม่ 10 ก.ย. 2569

   หลักการ:
   - ปุ่มทำลายข้อมูลต้องไม่อยู่ใกล้ปุ่มใช้งานปกติ และต้องดูต่างจนสังเกตได้
   - ทุกสีตัวอักษรผูกกับพื้นหลังของมันผ่านตัวแปรชุดเดียวกัน กันตัวหนังสือ
     กลืนพื้น (บั๊กปุ่มน้ำเงินอักษรดำที่เจอมาก่อน)
   ========================================================================== */

:root {
    /* พื้นและตัวอักษร — เทาอมน้ำเงินเล็กน้อยให้เข้ากับสีแบรนด์ */
    --bg: #f4f6fb;
    --surface: #ffffff;
    --surface-2: #eef1f8;
    --line: #dde3ef;
    --line-soft: #e9edf5;

    --text: #0f1729;
    --text-soft: #4c576e;
    --text-faint: #7d8699;

    /* แบรนด์โยธาไทย */
    --brand: #1d4ed8;
    --brand-strong: #1a3fb0;
    --brand-soft: #e8eeff;
    --on-brand: #ffffff;        /* ตัวอักษรบนพื้นแบรนด์ — ต้องใช้ตัวนี้เสมอ */

    --accent: #b45309;
    --accent-soft: #fdf3e3;

    /* สีบอกสถานะ แยกจากสีแบรนด์ */
    --ok: #15803d;
    --ok-soft: #e7f6ec;
    --warn: #b45309;
    --warn-soft: #fdf3e3;
    --danger: #b91c1c;
    --danger-soft: #fdeaea;
    --on-danger: #ffffff;

    --radius: 10px;
    --radius-sm: 7px;
    --shadow-sm: 0 1px 2px rgba(15, 23, 41, .06);
    --shadow: 0 1px 3px rgba(15, 23, 41, .07), 0 12px 28px -16px rgba(15, 23, 41, .22);
    --nav-h: 62px;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #0a0f1c;
        --surface: #121a2b;
        --surface-2: #1a2438;
        --line: #253148;
        --line-soft: #1c2537;

        --text: #e8ecf7;
        --text-soft: #a7b1c6;
        --text-faint: #7b8598;

        --brand: #4d7cf3;
        --brand-strong: #6b93f7;
        --brand-soft: #16233f;
        --on-brand: #ffffff;

        --accent: #e0a458;
        --accent-soft: #2a1f0f;

        --ok: #4ade80;
        --ok-soft: #10281c;
        --warn: #e0a458;
        --warn-soft: #2a1f0f;
        --danger: #f87171;
        --danger-soft: #2c1414;
        --on-danger: #1a0b0b;

        --shadow-sm: 0 1px 2px rgba(0, 0, 0, .4);
        --shadow: 0 1px 3px rgba(0, 0, 0, .5), 0 12px 28px -16px rgba(0, 0, 0, .8);
    }
}

:root[data-theme="dark"] {
    --bg: #0a0f1c;
    --surface: #121a2b;
    --surface-2: #1a2438;
    --line: #253148;
    --line-soft: #1c2537;

    --text: #e8ecf7;
    --text-soft: #a7b1c6;
    --text-faint: #7b8598;

    --brand: #4d7cf3;
    --brand-strong: #6b93f7;
    --brand-soft: #16233f;
    --on-brand: #ffffff;

    --accent: #e0a458;
    --accent-soft: #2a1f0f;

    --ok: #4ade80;
    --ok-soft: #10281c;
    --warn: #e0a458;
    --warn-soft: #2a1f0f;
    --danger: #f87171;
    --danger-soft: #2c1414;
    --on-danger: #1a0b0b;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, .4);
    --shadow: 0 1px 3px rgba(0, 0, 0, .5), 0 12px 28px -16px rgba(0, 0, 0, .8);
}

* { box-sizing: border-box; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: 'Prompt', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-size: 15px;
    line-height: 1.65;
    -webkit-font-smoothing: antialiased;
}

.mono {
    font-family: 'IBM Plex Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
    font-variant-numeric: tabular-nums;
}

.hidden { display: none !important; }

/* ---------- แถบบนสุด ---------- */
.navbar {
    height: var(--nav-h);
    background: var(--surface);
    border-bottom: 1px solid var(--line);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    gap: 16px;
    position: sticky;
    top: 0;
    z-index: 50;
}

.logo { display: flex; align-items: center; gap: 9px; min-width: 0; }
.logo img { width: 26px; height: 26px; object-fit: contain; }
.logo-text {
    font-weight: 600;
    font-size: 1.05rem;
    letter-spacing: -.01em;
    color: var(--brand);
    white-space: nowrap;
}
.logo-badge {
    font-size: .66rem;
    font-weight: 500;
    padding: 2px 7px;
    border-radius: 20px;
    background: var(--surface-2);
    color: var(--text-faint);
    white-space: nowrap;
}

.nav-right { display: flex; align-items: center; gap: 10px; }

.theme-toggle {
    background: transparent;
    border: 1px solid var(--line);
    color: var(--text-soft);
    border-radius: var(--radius-sm);
    width: 34px; height: 34px;
    display: flex; align-items: center; justify-content: center;
    cursor: pointer;
    transition: background .15s, color .15s;
}
.theme-toggle:hover { background: var(--surface-2); color: var(--text); }

.nav-loader { color: var(--text-faint); font-size: .85rem; }

/* บัญชีผู้ใช้ */
.user-chip {
    display: flex; align-items: center; gap: 9px;
    padding: 4px 4px 4px 10px;
    border: 1px solid var(--line);
    border-radius: 30px;
    background: var(--surface);
    cursor: pointer;
}
.user-chip:hover { background: var(--surface-2); }
.user-chip .name {
    font-size: .85rem; font-weight: 500; max-width: 150px;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.avatar { width: 28px; height: 28px; border-radius: 50%; object-fit: cover; flex: none; }

.user-menu {
    position: absolute;
    top: calc(var(--nav-h) - 6px);
    right: 20px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    min-width: 230px;
    padding: 6px;
    z-index: 60;
}
.user-menu .who {
    padding: 10px 12px 12px;
    border-bottom: 1px solid var(--line-soft);
    margin-bottom: 6px;
}
.user-menu .who b { display: block; font-size: .9rem; font-weight: 600; }
.user-menu .who span { font-size: .78rem; color: var(--text-faint); overflow-wrap: anywhere; }
.user-menu .role {
    display: inline-block; margin-top: 6px;
    font-size: .7rem; font-weight: 600; padding: 2px 8px; border-radius: 20px;
    background: var(--brand-soft); color: var(--brand);
}
.menu-item {
    display: flex; align-items: center; gap: 9px;
    width: 100%; text-align: left;
    background: transparent; border: none;
    color: var(--text); font-family: inherit; font-size: .88rem;
    padding: 9px 12px; border-radius: var(--radius-sm); cursor: pointer;
}
.menu-item:hover { background: var(--surface-2); }

/* ---------- โครงหน้า ---------- */
.page { max-width: 1080px; margin: 0 auto; padding: 26px 20px 80px; }

.page-head { margin-bottom: 20px; }
.page-head h1 {
    margin: 0 0 4px;
    font-size: 1.45rem; font-weight: 600; letter-spacing: -.01em;
    text-wrap: balance;
}
.page-head p { margin: 0; color: var(--text-soft); font-size: .92rem; }

/* แถบสลับหน้า */
.viewtabs {
    display: inline-flex;
    background: var(--surface-2);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 4px;
    gap: 4px;
    margin-bottom: 22px;
}
.viewtab {
    border: none; background: transparent;
    color: var(--text-soft); font-family: inherit; font-size: .9rem; font-weight: 500;
    padding: 7px 18px; border-radius: var(--radius-sm); cursor: pointer;
    display: flex; align-items: center; gap: 7px;
    transition: background .15s, color .15s;
}
.viewtab:hover { color: var(--text); }
.viewtab.active {
    background: var(--surface);
    color: var(--brand);
    box-shadow: var(--shadow-sm);
}
.viewtab .count {
    font-size: .74rem; padding: 1px 7px; border-radius: 20px;
    background: var(--brand-soft); color: var(--brand); font-weight: 600;
}

/* ---------- การ์ด ---------- */
.card {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
}
.card-pad { padding: 22px; }

.notice {
    display: flex; gap: 11px; align-items: flex-start;
    padding: 13px 16px;
    border-radius: var(--radius);
    font-size: .9rem;
    margin-bottom: 18px;
}
.notice.lock { background: var(--warn-soft); color: var(--warn); border: 1px solid var(--line); }
.notice.info { background: var(--brand-soft); color: var(--brand); }

/* ---------- ฟอร์ม ---------- */
label.field-label {
    display: block;
    font-size: .85rem; font-weight: 500; color: var(--text-soft);
    margin-bottom: 6px;
}
input[type="text"], input[type="url"], input[type="email"], input[type="password"], select, textarea {
    width: 100%;
    background: var(--surface);
    border: 1px solid var(--line);
    color: var(--text);
    font-family: inherit; font-size: .95rem;
    padding: 11px 14px;
    border-radius: var(--radius-sm);
    transition: border-color .15s, box-shadow .15s;
}
input:focus-visible, select:focus-visible, textarea:focus-visible {
    outline: none;
    border-color: var(--brand);
    box-shadow: 0 0 0 3px var(--brand-soft);
}
input:disabled { background: var(--surface-2); color: var(--text-faint); cursor: not-allowed; }
input::placeholder { color: var(--text-faint); }

.alias-row { display: flex; align-items: stretch; }
.alias-prefix {
    display: flex; align-items: center;
    padding: 0 12px;
    background: var(--surface-2);
    border: 1px solid var(--line);
    border-right: none;
    border-radius: var(--radius-sm) 0 0 var(--radius-sm);
    color: var(--text-faint); font-size: .88rem; white-space: nowrap;
}
.alias-row input { border-radius: 0 var(--radius-sm) var(--radius-sm) 0; }

.check {
    display: flex; align-items: center; gap: 9px;
    font-size: .9rem; color: var(--text-soft); cursor: pointer;
}
.check input[type="checkbox"] { width: 17px; height: 17px; accent-color: var(--brand); cursor: pointer; }

/* ---------- ปุ่ม ---------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    font-family: inherit; font-size: .92rem; font-weight: 500;
    padding: 11px 20px;
    border-radius: var(--radius-sm);
    border: 1px solid transparent;
    cursor: pointer;
    transition: background .15s, border-color .15s, color .15s, opacity .15s;
    white-space: nowrap;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }

/* ตัวอักษรบนปุ่มหลักต้องเป็น --on-brand เสมอ ห้ามปล่อยให้สืบทอดสีตัวหนังสือทั่วไป */
.btn-primary { background: var(--brand); color: var(--on-brand); }
.btn-primary:hover:not(:disabled) { background: var(--brand-strong); }

.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--line); }
.btn-secondary:hover:not(:disabled) { background: var(--surface-2); }

.btn-ghost { background: transparent; color: var(--text-soft); }
.btn-ghost:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }

.btn-block { width: 100%; }
.btn-lg { padding: 14px 24px; font-size: 1rem; }
.btn-sm { padding: 6px 12px; font-size: .82rem; }

/* ปุ่มที่ทำให้ลิงก์หยุดทำงาน — จงใจให้หน้าตาไม่เหมือนปุ่มอื่นเลย */
.btn-caution {
    background: transparent;
    color: var(--warn);
    border: 1px dashed var(--warn);
}
.btn-caution:hover:not(:disabled) { background: var(--warn-soft); }

/* ---------- เลือกโหมดสร้าง ---------- */
.modes { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 20px; }
.mode-btn {
    border: 1px solid var(--line);
    background: var(--surface);
    color: var(--text-soft);
    font-family: inherit; font-size: .88rem; font-weight: 500;
    padding: 13px 10px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    display: flex; flex-direction: column; align-items: center; gap: 4px;
    transition: border-color .15s, background .15s, color .15s;
}
.mode-btn:hover { border-color: var(--brand); color: var(--text); }
.mode-btn.active {
    border-color: var(--brand);
    background: var(--brand-soft);
    color: var(--brand);
    font-weight: 600;
}
.mode-btn small { font-size: .72rem; font-weight: 400; color: var(--text-faint); }
.mode-btn.active small { color: var(--brand); opacity: .8; }

/* ---------- ตัวเลือก QR ---------- */
.qr-panel {
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 18px;
    background: var(--surface-2);
    margin-top: 18px;
}
.qr-panel h3 { margin: 0 0 14px; font-size: .95rem; font-weight: 600; }
.qr-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 18px; }
.qr-control { display: flex; flex-direction: column; gap: 7px; }

.color-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
input[type="color"] {
    width: 38px; height: 34px; padding: 2px;
    border: 1px solid var(--line); border-radius: var(--radius-sm);
    background: var(--surface); cursor: pointer;
}
.swatch {
    width: 22px; height: 22px; border-radius: 50%;
    border: 1px solid var(--line); cursor: pointer;
    transition: transform .12s;
}
.swatch:hover { transform: scale(1.14); }

.file-input { font-size: .82rem; color: var(--text-soft); }
.file-input::file-selector-button {
    font-family: inherit; font-size: .82rem;
    background: var(--surface); color: var(--text);
    border: 1px solid var(--line); border-radius: var(--radius-sm);
    padding: 6px 12px; margin-right: 10px; cursor: pointer;
}
.file-input::file-selector-button:hover { background: var(--surface-2); }

.logo-gallery { display: flex; gap: 8px; flex-wrap: wrap; }
.logo-thumb {
    width: 42px; height: 42px; object-fit: contain;
    border: 2px solid var(--line); border-radius: var(--radius-sm);
    background: #fff; cursor: pointer; padding: 3px;
}
.logo-thumb.selected { border-color: var(--brand); }

.logo-preview-wrap { position: relative; display: inline-block; margin-top: 4px; }
.logo-preview-wrap img {
    width: 58px; height: 58px; object-fit: contain;
    border: 1px solid var(--line); border-radius: var(--radius-sm);
    background: #fff; padding: 3px;
}
.logo-remove {
    position: absolute; top: -7px; right: -7px;
    width: 21px; height: 21px; border-radius: 50%;
    background: var(--danger); color: var(--on-danger);
    border: none; cursor: pointer;
    font-size: 11px; line-height: 1;
    display: flex; align-items: center; justify-content: center;
}

/* ---------- ผลลัพธ์ ---------- */
.result { margin-top: 22px; display: flex; flex-direction: column; gap: 18px; }

.result-link {
    display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
    padding: 16px 18px;
    background: var(--ok-soft);
    border: 1px solid var(--line);
    border-radius: var(--radius);
}
.result-link .url {
    font-family: 'IBM Plex Mono', monospace;
    font-size: 1.02rem; font-weight: 500; color: var(--ok);
    overflow-wrap: anywhere; flex: 1; min-width: 200px;
}

.qr-result { display: flex; gap: 22px; align-items: flex-start; flex-wrap: wrap; }
.qr-canvas-wrap {
    background: #fff; padding: 14px;
    border: 1px solid var(--line); border-radius: var(--radius);
}
.qr-canvas-wrap canvas { display: block; max-width: 240px; height: auto; }

/* ---------- หน้าจัดการลิงก์ ---------- */
.toolbar {
    display: flex; gap: 10px; flex-wrap: wrap; align-items: center;
    margin-bottom: 16px;
}
.search-wrap { position: relative; flex: 1; min-width: 220px; }
.search-wrap svg { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--text-faint); }
.search-wrap input { padding-left: 38px; }

.stat-strip {
    display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
    gap: 1px; background: var(--line);
    border: 1px solid var(--line); border-radius: var(--radius);
    overflow: hidden; margin-bottom: 18px;
}
.stat-strip div { background: var(--surface); padding: 13px 16px; }
.stat-strip .k { font-size: .75rem; color: var(--text-faint); display: block; }
.stat-strip .v {
    font-size: 1.25rem; font-weight: 600;
    font-family: 'IBM Plex Mono', monospace; font-variant-numeric: tabular-nums;
}

.link-list { display: flex; flex-direction: column; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); overflow: hidden; }

.link-row {
    display: grid;
    grid-template-columns: 1fr auto auto;
    gap: 10px 16px;
    align-items: center;
    padding: 13px 16px;
    border-bottom: 1px solid var(--line-soft);
}
.link-row:last-child { border-bottom: none; }
.link-row:hover { background: var(--surface-2); }
.link-row.is-off { opacity: .62; }

.link-main { min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.link-title {
    font-size: .93rem; font-weight: 500;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.link-alias {
    font-family: 'IBM Plex Mono', monospace;
    font-size: .82rem; color: var(--brand); text-decoration: none;
}
.link-alias:hover { text-decoration: underline; }
.link-dest {
    font-size: .76rem; color: var(--text-faint);
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.link-clicks { text-align: right; white-space: nowrap; }
.link-clicks .n {
    font-family: 'IBM Plex Mono', monospace; font-variant-numeric: tabular-nums;
    font-size: 1rem; font-weight: 600;
}
.link-clicks .l { font-size: .7rem; color: var(--text-faint); display: block; margin-top: -3px; }

.link-actions { display: flex; gap: 4px; }
.icon-btn {
    width: 32px; height: 32px;
    display: flex; align-items: center; justify-content: center;
    background: transparent; border: 1px solid transparent;
    color: var(--text-faint); border-radius: var(--radius-sm); cursor: pointer;
    transition: background .12s, color .12s, border-color .12s;
}
.icon-btn:hover { background: var(--surface); border-color: var(--line); color: var(--brand); }
.icon-btn.caution:hover { color: var(--warn); border-color: var(--warn); }

.badge-off {
    font-size: .68rem; font-weight: 600;
    padding: 1px 7px; border-radius: 20px;
    background: var(--warn-soft); color: var(--warn);
}

.empty {
    padding: 46px 20px; text-align: center; color: var(--text-faint);
    display: flex; flex-direction: column; align-items: center; gap: 8px;
}

.load-more { display: flex; justify-content: center; padding: 14px; }

/* ---------- กล่องข้อความซ้อน ---------- */
.overlay {
    position: fixed; inset: 0;
    background: rgba(9, 14, 26, .55);
    display: flex; align-items: center; justify-content: center;
    padding: 20px; z-index: 100;
    backdrop-filter: blur(2px);
}
.modal {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    width: 100%; max-width: 420px;
    padding: 24px;
    display: flex; flex-direction: column; gap: 14px;
}
.modal h3 { margin: 0; font-size: 1.1rem; font-weight: 600; }
.modal p { margin: 0; color: var(--text-soft); font-size: .9rem; }
.modal-actions { display: flex; gap: 9px; justify-content: flex-end; margin-top: 4px; }

/* ---------- แจ้งเตือนมุมจอ ---------- */
.toast-stack {
    position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
    display: flex; flex-direction: column; gap: 8px; z-index: 200;
    width: min(420px, calc(100vw - 32px));
}
.toast {
    background: var(--surface); border: 1px solid var(--line);
    border-left: 4px solid var(--brand);
    border-radius: var(--radius-sm); box-shadow: var(--shadow);
    padding: 12px 15px; font-size: .89rem;
    display: flex; align-items: center; gap: 10px;
}
.toast.ok { border-left-color: var(--ok); }
.toast.err { border-left-color: var(--danger); }
.toast .undo {
    margin-left: auto; background: transparent; border: none;
    color: var(--brand); font-family: inherit; font-weight: 600; font-size: .85rem;
    cursor: pointer; white-space: nowrap;
}

/* ---------- ท้ายหน้า ---------- */
footer.site {
    border-top: 1px solid var(--line);
    padding: 22px 20px 40px;
    text-align: center;
    color: var(--text-faint);
    font-size: .82rem;
}
footer.site a { color: var(--brand); text-decoration: none; }
footer.site a:hover { text-decoration: underline; }

/* ---------- หน้าจอเล็ก ---------- */
@media (max-width: 640px) {
    .page { padding: 20px 14px 60px; }
    .navbar { padding: 0 14px; }
    .modes { grid-template-columns: 1fr; }
    .link-row { grid-template-columns: 1fr auto; }
    .link-actions { grid-column: 1 / -1; justify-content: flex-end; }
    .user-chip .name { display: none; }
    .logo-badge { display: none; }
}

@media (prefers-reduced-motion: reduce) {
    * { transition: none !important; animation: none !important; }
}
