/**
 * IWCSECBOND — Shared responsive layout & mobile UI
 */

:root {
    --primary: #1a4b8c;
    --primary-light: #2563eb;
    --primary-dark: #0f3460;
    --accent: #0ea5e9;
    --success: #059669;
    --danger: #dc2626;
    --warning: #d97706;
    --surface: #ffffff;
    --bg: #eef2f7;
    --bg-soft: #f8fafc;
    --border: #e2e8f0;
    --text: #1e293b;
    --text-muted: #64748b;
    --radius: 12px;
    --radius-sm: 8px;
    --shadow: 0 4px 24px rgba(15, 52, 96, 0.08);
    --shadow-lg: 0 12px 40px rgba(15, 52, 96, 0.15);
    --nav-height: 52px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

*, *::before, *::after { box-sizing: border-box; }

html {
    -webkit-text-size-adjust: 100%;
    scroll-behavior: smooth;
}

body {
    font-family: 'Sarabun', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    margin: 0;
    padding: 16px;
    padding-bottom: calc(16px + var(--safe-bottom));
    background: linear-gradient(160deg, #eef2f7 0%, #e8eef5 50%, #dfe8f2 100%);
    background-attachment: fixed;
    color: var(--text);
    font-size: 16px;
    line-height: 1.5;
    min-height: 100vh;
}

@media (min-width: 769px) {
    body { padding: 24px; }
}

/* ── Container ── */
.container {
    max-width: 1200px;
    margin: 0 auto;
    background: var(--surface);
    padding: 20px;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

@media (max-width: 768px) {
    .container {
        padding: 16px;
        border-radius: var(--radius-sm);
    }
}

.container .container {
    box-shadow: none;
    padding: 0;
    max-width: none;
}

h1, h2, h3 {
    color: var(--primary-dark);
    line-height: 1.3;
}

h2 {
    text-align: center;
    font-size: clamp(1.15rem, 4vw, 1.6rem);
    margin: 0 0 20px;
    font-weight: 700;
}

/* ── User bar ── */
.user-nav {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    margin-bottom: 16px;
    padding: 12px 16px;
    background: var(--bg-soft);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 14px;
}

.user-nav a {
    color: var(--danger);
    text-decoration: none;
    font-weight: 600;
    padding: 6px 12px;
    border-radius: 6px;
    border: 1px solid rgba(220, 38, 38, 0.3);
    transition: background 0.2s, color 0.2s;
}

.user-nav a:hover {
    background: var(--danger);
    color: #fff;
}

/* ── Global navigation ── */
.global-nav {
    margin-bottom: 20px;
    border-radius: var(--radius-sm);
    overflow: hidden;
    border: 1px solid var(--border);
    background: var(--surface);
}

.nav-toggle {
    display: none;
    width: 100%;
    padding: 14px 18px;
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
    color: #fff;
    border: none;
    font-family: inherit;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    text-align: left;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

.nav-toggle::after {
    content: '▼';
    font-size: 12px;
    transition: transform 0.25s ease;
}

.global-nav.is-open .nav-toggle::after {
    transform: rotate(180deg);
}

.global-nav-links {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 12px;
    justify-content: center;
}

.global-nav-links a {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 10px 14px;
    background: var(--bg-soft);
    color: var(--primary-dark);
    text-decoration: none;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 500;
    border: 1px solid var(--border);
    transition: all 0.2s ease;
    flex: 1 1 auto;
    justify-content: center;
    min-width: 140px;
}

.global-nav-links a:hover,
.global-nav-links a.active {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(26, 75, 140, 0.25);
}

@media (max-width: 768px) {
    .nav-toggle { display: flex; }

    .global-nav-links {
        display: none;
        flex-direction: column;
        padding: 8px;
        gap: 6px;
        border-top: 1px solid var(--border);
    }

    .global-nav.is-open .global-nav-links { display: flex; }

    .global-nav-links a {
        width: 100%;
        min-width: unset;
        padding: 14px 16px;
        font-size: 15px;
        justify-content: flex-start;
    }
}

/* ── Forms & buttons ── */
.search-box,
.filter-box,
.upload-box {
    background: var(--bg-soft);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 20px;
    margin-bottom: 20px;
}

.upload-box {
    border-style: dashed;
    border-color: var(--accent);
    text-align: center;
}

.search-box { text-align: center; }

.input-group {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    justify-content: center;
}

input[type="text"],
input[type="number"],
input[type="password"],
input[type="file"],
select,
textarea {
    font-family: inherit;
    font-size: 16px;
    max-width: 100%;
}

input[type="text"],
input[type="number"],
input[type="password"] {
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: #fff;
    transition: border-color 0.2s, box-shadow 0.2s;
}

input[type="text"]:focus,
input[type="number"]:focus,
input[type="password"]:focus {
    outline: none;
    border-color: var(--primary-light);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

button,
.btn-primary,
.ai-btn {
    font-family: inherit;
    font-size: 15px;
    font-weight: 600;
    padding: 12px 20px;
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background 0.2s, transform 0.15s;
    touch-action: manipulation;
}

button:active,
.ai-btn:active { transform: scale(0.98); }

.upload-box form {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
}

.upload-box input[type="file"] {
    width: 100%;
    max-width: 360px;
    padding: 10px;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
}

.upload-box button[type="submit"] {
    background: var(--primary-light);
    color: #fff;
    width: 100%;
    max-width: 360px;
}

.upload-box button[type="submit"]:hover {
    background: var(--primary);
}

/* ── Alerts ── */
.alert,
.alert-info,
.alert-warning,
.alert-danger {
    padding: 16px;
    margin-bottom: 16px;
    border-radius: var(--radius-sm);
    font-size: 15px;
    line-height: 1.6;
}

.alert-info { color: #0c4a6e; background: #e0f2fe; border: 1px solid #7dd3fc; }
.alert-warning { color: #92400e; background: #fef3c7; border: 1px solid #fcd34d; }
.alert-danger { color: #991b1b; background: #fee2e2; border: 1px solid #fca5a5; }

/* ── Tables: horizontal scroll fallback ── */
.table-responsive {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 12px 0;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
}

.table-responsive table {
    margin-top: 0;
    min-width: 480px;
}

/* Card-style table on mobile (requires data-label on td) */
@media (max-width: 768px) {
    table.responsive-cards {
        min-width: unset;
        border: none;
    }

    table.responsive-cards thead { display: none; }

    table.responsive-cards tbody tr {
        display: block;
        margin-bottom: 12px;
        background: var(--surface);
        border: 1px solid var(--border);
        border-radius: var(--radius-sm);
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
        overflow: hidden;
    }

    table.responsive-cards tbody tr.clickable-row {
        cursor: pointer;
        transition: box-shadow 0.2s;
    }

    table.responsive-cards tbody tr.clickable-row:active {
        box-shadow: 0 0 0 2px var(--accent);
    }

    table.responsive-cards tbody td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 12px;
        padding: 10px 14px;
        border: none;
        border-bottom: 1px solid var(--border);
        text-align: right !important;
    }

    table.responsive-cards tbody td:last-child { border-bottom: none; }

    table.responsive-cards tbody td::before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--text-muted);
        text-align: left;
        flex-shrink: 0;
        font-size: 13px;
    }

    table.responsive-cards tbody td:first-child {
        background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
        color: #fff;
        font-weight: 700;
        justify-content: center;
        padding: 14px;
    }

    table.responsive-cards tbody td:first-child::before { display: none; }
}

/* Key-value table (bond info) */
@media (max-width: 768px) {
    table.kv-table thead { display: none; }

    table.kv-table tbody tr {
        display: block;
        margin-bottom: 8px;
        border: 1px solid var(--border);
        border-radius: var(--radius-sm);
        overflow: hidden;
        background: #fff;
    }

    table.kv-table tbody td {
        display: block;
        width: 100% !important;
        padding: 10px 14px;
        border: none;
    }

    table.kv-table .field-label {
        background: var(--bg-soft);
        font-size: 12px;
        font-weight: 700;
        color: var(--text-muted);
        text-transform: uppercase;
        letter-spacing: 0.03em;
        padding-bottom: 4px;
    }

    table.kv-table tbody td:not(.field-label) {
        font-size: 15px;
        word-break: break-word;
    }
}

/* Compare pivot — bond cards on mobile */
.compare-mobile-cards { display: none; }

@media (max-width: 768px) {
    .compare-pc-table { display: none !important; }

    .compare-mobile-cards {
        display: flex;
        flex-direction: column;
        gap: 16px;
    }

    .compare-bond-card {
        border: 1px solid var(--border);
        border-radius: var(--radius-sm);
        overflow: hidden;
        box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
    }

    .compare-bond-card-header {
        background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
        color: #fff;
        padding: 14px 16px;
        font-weight: 700;
        font-size: 1.05rem;
    }

    .compare-bond-card-body { padding: 0; }

    .compare-field-row {
        display: flex;
        justify-content: space-between;
        gap: 10px;
        padding: 10px 14px;
        border-bottom: 1px solid var(--border);
        font-size: 14px;
    }

    .compare-field-row:last-child { border-bottom: none; }

    .compare-field-label {
        color: var(--text-muted);
        font-weight: 600;
        flex-shrink: 0;
        max-width: 45%;
    }

    .compare-field-value {
        text-align: right;
        word-break: break-word;
    }
}

/* ── Chart ── */
.chart-container {
    position: relative;
    height: clamp(240px, 50vw, 400px);
    width: 100%;
    margin: 16px 0;
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: #fff;
}

/* ── AI sections ── */
.ai-selection-box,
.ai-container {
    border-radius: var(--radius-sm);
    margin-top: 20px;
}

.ai-selection-header {
    flex-wrap: wrap;
}

.ai-result-content {
    font-size: 15px;
    overflow-wrap: anywhere;
}

/* ── Export / action row ── */
.export-section {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    justify-content: center;
    margin-top: 20px;
}

.export-section form,
.export-section button,
.export-section .ai-btn {
    flex: 1 1 140px;
    max-width: 220px;
}

@media (max-width: 768px) {
    .export-section form,
    .export-section button,
    .export-section .ai-btn {
        flex: 1 1 100%;
        max-width: none;
        width: 100%;
        margin-left: 0 !important;
    }

    .input-group input[type="text"] { width: 100% !important; }

    .search-box button,
    .search-box input[type="text"],
    .filter-box button { width: 100%; margin-top: 8px; }

    .filter-box .form-group,
    .filter-box .checkbox-group {
        display: block;
        width: 100%;
        margin-right: 0;
        margin-top: 10px;
        height: auto;
    }

    .filter-box input[type="number"] { width: 100%; }

    #addBondBtn { width: 100%; padding: 10px !important; margin-bottom: 10px !important; }

    .summary-box {
        flex-direction: column !important;
        gap: 12px !important;
    }

    .summary-box > div {
        border-left: none !important;
        padding-left: 0 !important;
        text-align: left !important;
    }
}

/* ── Board market cards ── */
.board-mobile-cards { display: none; }

@media (max-width: 768px) {
    .board-pc-table { display: none !important; }

    .board-mobile-cards {
        display: flex;
        flex-direction: column;
        gap: 12px;
    }

    .board-card {
        background: #fff;
        border: 1px solid var(--border);
        border-radius: var(--radius-sm);
        overflow: hidden;
        box-shadow: 0 2px 10px rgba(0, 0, 0, 0.06);
        cursor: pointer;
        transition: transform 0.15s, box-shadow 0.15s;
    }

    .board-card:active {
        transform: scale(0.99);
        box-shadow: 0 0 0 2px var(--accent);
    }

    .board-card-name {
        background: #2c3e50;
        color: #fff;
        padding: 14px 16px;
        font-weight: 700;
        font-size: 1.05rem;
    }

    .board-card-grid {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 0;
    }

    .board-card-item {
        padding: 12px 14px;
        border-top: 1px solid var(--border);
        text-align: center;
    }

    .board-card-item:nth-child(odd) { border-right: 1px solid var(--border); }

    .board-card-item.full { grid-column: 1 / -1; }

    .board-card-label {
        font-size: 11px;
        color: var(--text-muted);
        font-weight: 600;
        margin-bottom: 4px;
        text-transform: uppercase;
    }

    .board-card-value {
        font-size: 1.1rem;
        font-weight: 700;
    }

    .board-card-value.bid { color: #059669; }
    .board-card-value.offer { color: #dc2626; }
    .board-card-value.yield { color: #475569; }
}

/* ── Selection mobile cards ── */
@media (max-width: 768px) {
    .selection-pc-table { display: none !important; }

    .selection-mobile-cards {
        display: flex;
        flex-direction: column;
        gap: 10px;
    }

    .selection-bond-card {
        border: 1px solid var(--border);
        border-radius: var(--radius-sm);
        padding: 14px;
        background: #fff;
    }

    .selection-bond-name {
        font-weight: 700;
        color: var(--primary);
        margin-bottom: 10px;
        font-size: 1rem;
    }

    .selection-bond-grid {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 8px;
        font-size: 13px;
    }

    .selection-bond-grid span.label {
        color: var(--text-muted);
        display: block;
        font-size: 11px;
        font-weight: 600;
    }
}

@media (min-width: 769px) {
    .selection-mobile-cards { display: none !important; }
}

/* ── Modal (shared) ── */
@media (max-width: 768px) {
    .modal-content {
        width: 95% !important;
        max-height: 90vh !important;
        padding: 20px 16px !important;
        border-radius: 12px !important;
    }

    .modal-close {
        top: 8px !important;
        right: 12px !important;
        font-size: 28px !important;
    }

    #chartContainer > div { height: 200px !important; }
}

/* ── Board edit (Bootstrap page) ── */
@media (max-width: 768px) {
    .board-edit-actions {
        flex-direction: column !important;
    }

    .board-edit-actions .btn {
        width: 100%;
    }

    .board-edit-card .card-header h5 {
        font-size: 1rem;
    }

    .board-edit-card .table-input {
        font-size: 14px;
        padding: 10px 6px;
    }
}

@media print {
    .global-nav,
    .nav-toggle,
    .user-nav,
    .no-print { display: none !important; }
}
