/* Reset & Base Styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary-color: #0051c3;
    --primary-hover: #0043a3;
    --sidebar-width: 280px;
    --nav-height: 56px;
    --text-primary: #1c1e21;
    --text-secondary: #626976;
    --border-color: #e2e8f0;
    --bg-primary: #ffffff;
    --bg-secondary: #f8fafc;
    --bg-sidebar: #fbfbfc;
    --success-color: #10b981;
    --warning-color: #f59e0b;
    --error-color: #ef4444;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Noto Sans Thai', 'Helvetica Neue', Arial, sans-serif;
    background: var(--bg-secondary);
    color: var(--text-primary);
    font-size: 14px;
    line-height: 1.6;
}

/* Top Navigation */
.top-nav {
    background: var(--bg-primary);
    border-bottom: 1px solid var(--border-color);
    height: var(--nav-height);
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 100;
}

.nav-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 100%;
    padding: 0 20px;
}

.nav-left {
    display: flex;
    align-items: center;
    gap: 16px;
}

.menu-toggle {
    display: none;
    background: none;
    border: none;
    cursor: pointer;
    padding: 8px;
    color: var(--text-primary);
}

.nav-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 600;
    font-size: 16px;
}

.nav-brand svg {
    color: var(--primary-color);
}

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

.nav-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    background: none;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    cursor: pointer;
    color: var(--text-primary);
    font-size: 13px;
    transition: all 0.2s;
}

.nav-btn:hover {
    background: var(--bg-secondary);
    border-color: var(--primary-color);
    color: var(--primary-color);
}

.last-update {
    font-size: 12px;
    color: var(--text-secondary);
}

/* Layout Container */
.layout-container {
    display: flex;
    margin-top: var(--nav-height);
    min-height: calc(100vh - var(--nav-height));
}

/* Sidebar */
.sidebar {
    width: var(--sidebar-width);
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border-color);
    position: fixed;
    left: 0;
    top: var(--nav-height);
    bottom: 0;
    overflow-y: auto;
    z-index: 50;
}

.sidebar-content {
    padding: 20px;
}

.sidebar-search {
    position: relative;
    margin-bottom: 24px;
}

.search-input {
    width: 100%;
    padding: 8px 12px 8px 36px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-size: 13px;
    background: var(--bg-primary);
    transition: border-color 0.2s;
}

.search-input:focus {
    outline: none;
    border-color: var(--primary-color);
}

.search-icon {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-secondary);
    width: 16px;
    height: 16px;
}

.sidebar-section {
    margin-bottom: 28px;
}

.sidebar-title {
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    color: var(--text-secondary);
    margin-bottom: 12px;
    letter-spacing: 0.5px;
}

/* Filter Group */
.filter-group {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.filter-item {
    display: flex;
    align-items: center;
    padding: 8px 12px;
    border-radius: 6px;
    cursor: pointer;
    transition: background 0.2s;
    position: relative;
}

.filter-item:hover {
    background: var(--bg-secondary);
}

.filter-item input[type="radio"] {
    position: absolute;
    opacity: 0;
    cursor: pointer;
}

.filter-item input[type="radio"]:checked + span {
    color: var(--primary-color);
    font-weight: 500;
}

.filter-item input[type="radio"]:checked ~ .badge {
    background: var(--primary-color);
    color: white;
}

.filter-item span:first-of-type {
    flex: 1;
    font-size: 13px;
}

.badge {
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 11px;
    font-weight: 600;
    background: var(--bg-secondary);
    color: var(--text-secondary);
}

.badge-success {
    background: rgba(16, 185, 129, 0.1);
    color: var(--success-color);
}

.badge-warning {
    background: rgba(245, 158, 11, 0.1);
    color: var(--warning-color);
}

/* Gateway List */
.gateway-list {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.gateway-list-item {
    padding: 8px 12px;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.2s;
    font-size: 13px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.gateway-list-item:hover {
    background: var(--bg-secondary);
    padding-left: 16px;
}

.gateway-list-item.active {
    background: rgba(0, 81, 195, 0.1);
    color: var(--primary-color);
    font-weight: 500;
}

/* Stats Mini */
.stats-mini {
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 12px;
}

.stat-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 0;
}

.stat-item:not(:last-child) {
    border-bottom: 1px solid var(--border-color);
}

.stat-label {
    font-size: 12px;
    color: var(--text-secondary);
}

.stat-value {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-primary);
}

/* Main Content */
.main-content {
    margin-left: var(--sidebar-width);
    flex: 1;
    padding: 24px 32px;
    width: 100%;
}

/* Breadcrumb */
.breadcrumb {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 24px;
    font-size: 13px;
}

.breadcrumb-item {
    color: var(--text-secondary);
    text-decoration: none;
    transition: color 0.2s;
}

.breadcrumb-item:hover:not(.current) {
    color: var(--primary-color);
}

.breadcrumb-item.current {
    color: var(--text-primary);
}

.breadcrumb svg {
    width: 14px;
    height: 14px;
    color: var(--text-secondary);
}

/* Page Header */
.page-header {
    margin-bottom: 32px;
}

.page-header h1 {
    font-size: 32px;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 8px;
}

.page-header p {
    font-size: 16px;
    color: var(--text-secondary);
}

.page-header .page-updated {
    font-size: 13px;
    color: var(--text-secondary);
    margin-top: 4px;
}

/* Content Area */
.content-area {
    background: var(--bg-primary);
    border-radius: 8px;
    padding: 24px;
    min-height: 400px;
}

/* Loading State */
.loading-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 60px 20px;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 3px solid var(--border-color);
    border-top-color: var(--primary-color);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin-bottom: 16px;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.loading-state p {
    color: var(--text-secondary);
    font-size: 14px;
}

/* Error State */
.error-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 60px 20px;
    text-align: center;
}

.error-state svg {
    color: var(--error-color);
    margin-bottom: 16px;
}

.error-state h3 {
    font-size: 18px;
    margin-bottom: 8px;
}

.error-state p {
    color: var(--text-secondary);
    margin-bottom: 20px;
}

/* Empty State */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 60px 20px;
    text-align: center;
}

.empty-state svg {
    color: var(--text-secondary);
    margin-bottom: 16px;
}

.empty-state h3 {
    font-size: 18px;
    margin-bottom: 8px;
}

.empty-state p {
    color: var(--text-secondary);
}

/* Gateway Grid */
.gateway-grid {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

/* Detail Cards */
.detail-card {
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    overflow: hidden;
    transition: box-shadow 0.2s;
    width: 100%;
}

.detail-card:hover {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.card-header {
    padding: 16px 20px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.card-header h3 {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
}

.card-body {
    padding: 20px;
}

.detail-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 0;
    border-bottom: 1px solid var(--border-color);
}

.detail-row:last-child {
    border-bottom: none;
}

.detail-label {
    font-size: 14px;
    color: var(--text-secondary);
    font-weight: 500;
}

.detail-value {
    font-size: 14px;
    color: var(--text-primary);
    font-weight: 600;
    text-align: right;
}

/* Status Badge */
.status-badge {
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 600;
}

.status-badge.status-active {
    background: rgba(16, 185, 129, 0.1);
    color: var(--success-color);
}

.status-badge.status-inactive {
    background: rgba(239, 68, 68, 0.1);
    color: var(--error-color);
}

/* Detail List */
.detail-list {
    list-style: none;
    padding: 0;
    margin: 0;
}

.detail-list li {
    position: relative;
    padding: 12px 0 12px 24px;
    border-bottom: 1px solid var(--border-color);
    font-size: 14px;
    line-height: 1.6;
    color: var(--text-primary);
}

.detail-list li:last-child {
    border-bottom: none;
}

.detail-list li::before {
    content: "✓";
    position: absolute;
    left: 0;
    color: var(--primary-color);
    font-weight: bold;
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 20px;
    border: none;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
}

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

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

/* Utility Classes */
.hidden {
    display: none !important;
}

/* Responsive */
@media (max-width: 768px) {
    .menu-toggle {
        display: block;
    }

    .sidebar {
        transform: translateX(-100%);
        transition: transform 0.3s;
    }

    .sidebar.active {
        transform: translateX(0);
    }

    .main-content {
        margin-left: 0;
        padding: 20px;
    }

    .nav-right span {
        display: none;
    }

    .page-header h1 {
        font-size: 24px;
    }
}

/* ============================================================
   Form page (setting.html)
   ============================================================ */
/* Sheet selector bar above the form */
.sheet-selector-bar {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 16px 0;
    margin-bottom: 24px;
    border-bottom: 1px solid var(--border-color);
}

.sheet-selector-label {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
}

.sheet-select {
    flex: 1;
    max-width: 320px;
    padding: 8px 12px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    background: var(--bg-primary);
    font-family: inherit;
    font-size: 14px;
    color: var(--text-primary);
}

.sheet-select:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(0, 81, 195, 0.12);
}

.sheet-select:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.form-page {
    max-width: 880px;
    margin: 0 auto 80px;
    padding: 0;
}

.form-header {
    margin-bottom: 32px;
}

.form-header strong {
    color: var(--primary-color);
    font-weight: 600;
}

.form-hint {
    margin-top: 4px;
    font-size: 13px;
}

.sidebar-create-btn {
    width: 100%;
    margin-top: 12px;
    padding: 10px 12px;
    text-align: center;
    font-size: 13px;
}

.sidebar-loading,
.sidebar-empty,
.sidebar-error {
    padding: 12px;
    font-size: 13px;
    color: var(--text-secondary);
}

.sidebar-error {
    color: var(--error-color);
}


.form-header h1 {
    font-size: 28px;
    font-weight: 600;
    margin-bottom: 8px;
}

.form-header p {
    color: var(--text-secondary);
    font-size: 14px;
}

.gateway-form {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.form-section {
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 20px 24px;
}

.form-section legend {
    font-size: 14px;
    font-weight: 600;
    color: var(--primary-color);
    padding: 0 8px;
}

.form-row {
    margin-top: 16px;
}

.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 16px;
    margin-top: 16px;
}

.gateway-form label {
    display: flex;
    flex-direction: column;
    gap: 6px;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-primary);
}

.gateway-form label > span {
    color: var(--text-secondary);
    font-weight: 500;
}

.gateway-form input[type="text"],
.gateway-form textarea,
.gateway-form select {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-family: inherit;
    font-size: 14px;
    color: var(--text-primary);
    background: var(--bg-primary);
    transition: border-color 0.15s, box-shadow 0.15s;
}

.gateway-form input[type="text"]:focus,
.gateway-form textarea:focus,
.gateway-form select:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(0, 81, 195, 0.12);
}

/* Status & Display selects: tint the box red/green based on the chosen value */
.gateway-form select[data-state="positive"] {
    border-color: rgba(16, 185, 129, 0.55);
    background-color: rgba(16, 185, 129, 0.08);
    color: var(--success-color);
    font-weight: 600;
}

.gateway-form select[data-state="negative"] {
    border-color: rgba(239, 68, 68, 0.55);
    background-color: rgba(239, 68, 68, 0.08);
    color: var(--error-color);
    font-weight: 600;
}

/* Colored entries inside the open dropdown list */
.gateway-form option[value="Active"],
.gateway-form option[value="Display"] {
    color: var(--success-color);
    font-weight: 600;
}

.gateway-form option[value="Inactive"],
.gateway-form option[value="Hidden"] {
    color: var(--error-color);
    font-weight: 600;
}

.gateway-form textarea {
    resize: vertical;
    min-height: 80px;
    font-family: inherit;
}

.gateway-form small {
    color: var(--text-secondary);
    font-size: 12px;
    font-weight: 400;
}

.form-actions {
    display: flex;
    gap: 12px;
    padding-top: 8px;
}

.btn {
    padding: 10px 20px;
    font-size: 14px;
    font-weight: 500;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    background: var(--bg-primary);
    color: var(--text-primary);
    cursor: pointer;
    transition: all 0.15s;
}

.btn:hover {
    background: var(--bg-secondary);
    border-color: var(--text-secondary);
}

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

.btn-primary:hover {
    background: var(--primary-hover);
    border-color: var(--primary-hover);
    color: #fff;
}

.btn-primary:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.form-message {
    margin-top: 8px;
    padding: 12px 16px;
    border-radius: 6px;
    font-size: 14px;
}

.form-message-success {
    background: rgba(16, 185, 129, 0.1);
    color: var(--success-color);
    border: 1px solid rgba(16, 185, 129, 0.3);
}

.form-message-error {
    background: rgba(239, 68, 68, 0.1);
    color: var(--error-color);
    border: 1px solid rgba(239, 68, 68, 0.3);
}

/* Modal (used for create-master prompt) */
.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.55);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 20px;
    animation: modalFadeIn 0.15s ease-out;
}

@keyframes modalFadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.modal {
    background: var(--bg-primary);
    border-radius: 10px;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.25);
    padding: 24px;
    width: 100%;
    max-width: 440px;
    animation: modalSlideIn 0.18s ease-out;
}

@keyframes modalSlideIn {
    from { transform: translateY(-12px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

.modal-title {
    font-size: 18px;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 6px;
}

.modal-description {
    color: var(--text-secondary);
    font-size: 13px;
    line-height: 1.5;
    margin-bottom: 16px;
}

.modal-input {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-family: inherit;
    font-size: 14px;
    color: var(--text-primary);
    background: var(--bg-primary);
    transition: border-color 0.15s, box-shadow 0.15s;
}

.modal-input:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(0, 81, 195, 0.12);
}

.modal-error {
    margin-top: 10px;
    padding: 8px 12px;
    background: rgba(239, 68, 68, 0.08);
    color: var(--error-color);
    border-radius: 6px;
    font-size: 13px;
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 20px;
}

.modal-actions-stack {
    flex-direction: column;
    align-items: stretch;
}

.modal-actions-stack .btn {
    width: 100%;
}

.modal-wide {
    max-width: 560px;
}

.btn-ghost {
    background: transparent;
    border-color: transparent;
    color: var(--text-secondary);
}

.btn-ghost:hover {
    background: var(--bg-secondary);
    border-color: var(--border-color);
}

/* Failure report list */
.failure-list {
    list-style: none;
    padding: 0;
    margin: 12px 0 0;
    max-height: 320px;
    overflow-y: auto;
    border: 1px solid var(--border-color);
    border-radius: 6px;
}

.failure-list li {
    padding: 10px 12px;
    border-bottom: 1px solid var(--border-color);
    font-size: 13px;
}

.failure-list li:last-child {
    border-bottom: none;
}

.failure-list .failure-sheet {
    font-weight: 600;
    color: var(--text-primary);
}

.failure-list .failure-error {
    display: block;
    margin-top: 2px;
    color: var(--error-color);
    font-size: 12px;
}

/* Multi-input fields (one input per item, joined with `|` on submit) */
.multi-input-field {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.multi-input-label {
    color: var(--text-secondary);
    font-weight: 500;
    font-size: 13px;
}

.multi-input-list {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.multi-input-row {
    display: flex;
    gap: 8px;
    align-items: stretch;
}

.multi-input-row input[type="text"] {
    flex: 1;
}

.multi-input-remove {
    flex: 0 0 auto;
    width: 36px;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    color: var(--text-secondary);
    font-size: 18px;
    line-height: 1;
    cursor: pointer;
    transition: all 0.15s;
}

.multi-input-remove:hover {
    background: rgba(239, 68, 68, 0.08);
    border-color: var(--error-color);
    color: var(--error-color);
}

.btn-add-item {
    align-self: flex-start;
    margin-top: 4px;
    padding: 6px 14px;
    font-size: 12px;
    font-weight: 500;
    background: transparent;
    border: 1px dashed var(--border-color);
    border-radius: 6px;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.15s;
}

.btn-add-item:hover {
    border-color: var(--primary-color);
    border-style: solid;
    color: var(--primary-color);
}