* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Verdana, Arial, sans-serif;
    background: #f7f7f7;
    color: #333;
    min-height: 100vh;
}

.container {
    width: 100%;
    margin: 0;
    padding: 20px 30px;
}

/* Header Styles */
header {
    background: #1a1a2e;
    color: white;
    padding: 40px 30px;
    margin: -20px -30px 40px -30px;
    border-bottom: 4px solid #667eea;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    width: calc(100% + 60px);
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
}

h1 {
    font-size: 2.2em;
    margin-bottom: 10px;
    font-weight: 700;
    letter-spacing: -0.5px;
}

.subtitle {
    font-size: 1em;
    color: #d0d0d0;
    margin-bottom: 20px;
}

.data-status {
    color: #9fe3b1;
    margin-bottom: 8px;
}

.header-right {
    display: flex;
    align-items: center;
    margin-top: 20px; /* to align with stats */
}

.theme-toggle-btn {
    background: rgba(255, 255, 255, 0.1);
    color: white;
    border: 1px solid rgba(255, 255, 255, 0.2);
    padding: 8px 16px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.9em;
    transition: all 0.2s ease;
}

.theme-toggle-btn:hover {
    background: rgba(255, 255, 255, 0.2);
}

.stats {
    display: flex;
    gap: 30px;
    flex-wrap: wrap;
    margin-top: 25px;
}

.stat-box {
    display: flex;
    align-items: baseline;
    gap: 10px;
}

.stat-number {
    font-size: 1.8em;
    font-weight: bold;
    color: #667eea;
}

.stat-label {
    font-size: 0.95em;
    color: #b0b0b0;
}

/* Table Controls */
.dt-length select,
.dt-search input {
    padding: 8px 12px;
    background: white;
    border: 1px solid #ccc;
    border-radius: 4px;
    color: #333;
    font-size: 0.95em;
    font-family: inherit;
    transition: all 0.2s ease;
}

.dt-length select:hover,
.dt-search input:hover {
    border-color: #667eea;
    box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.1);
}

.dt-length select:focus,
.dt-search input:focus {
    outline: none;
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
}

.dt-search input::placeholder {
    color: #999;
}

/* Table Wrapper */
.table-wrapper {
    background: white;
    border-radius: 4px;
    overflow: visible;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
    border: 2px solid #ccc;
    margin-bottom: 20px;
    position: relative;
}

.dt-scroll {
    overflow: auto;
    max-height: 70vh;
    -webkit-overflow-scrolling: touch;
}

table {
    width: 100%;
    table-layout: fixed;
    border-collapse: collapse;
    min-width: 1200px;
    border: 1px solid #ccc;
}

thead {
    background: #f8f9fa;
    border-bottom: 2px solid #ddd;
}

th {
    padding: 10px 12px;
    text-align: left;
    font-weight: 600;
    color: #333;
    font-size: 0.9em;
    white-space: nowrap;
    user-select: none;
    border-right: 1px solid #eee;
    cursor: pointer;
    position: sticky;
    top: 0;
    z-index: 10;
    background: #f8f9fa;
    transition: background-color 0.2s ease;
    height: 40px;
    vertical-align: middle;
}

.column-resize-handle {
    position: absolute;
    top: 0;
    right: 0;
    width: 8px;
    height: 100%;
    cursor: col-resize;
    z-index: 11;
}

.column-resize-handle:hover {
    background: rgba(102, 126, 234, 0.35);
}

th:hover {
    background: #f0f0f0;
}

th.sortable::after {
    content: ' ⇅';
    opacity: 0.4;
    font-size: 0.85em;
}

th.sort-asc::after {
    content: ' ▲';
    opacity: 1;
}

th.sort-desc::after {
    content: ' ▼';
    opacity: 1;
}

th:last-child {
    border-right: none;
}

tbody tr {
    border-bottom: 1px solid #eee;
    transition: background-color 0.15s ease;
}

tbody tr:hover {
    background: #f8f9fa;
}

tbody tr:last-child {
    border-bottom: none;
}

td {
    padding: 10px 12px;
    color: #333;
    font-size: 0.9em;
    border-right: 1px solid #eee;
    min-height: 40px;
    height: auto;
    vertical-align: middle;
    overflow: hidden;
    overflow-wrap: break-word;
    white-space: normal;
    line-height: 1.25;
}

td:last-child {
    border-right: none;
}

td a {
    color: #0645ad;
    text-decoration: none;
    transition: color 0.2s;
    cursor: pointer;
}

td a:hover {
    color: #3366cc;
    text-decoration: underline;
}

img {
    max-width: 100%;
    height: auto;
    border-radius: 4px;
}

#ships-table td[data-column="image"] img {
    max-width: 120px;
    cursor: pointer;
}

/* Data Table Layout */
.dt-container {
    margin: 0;
    padding: 0 20px;
}

.dt-layout-row {
    display: flex;
    flex-wrap: wrap;
    gap: 15px;
    padding: 0;
    background: transparent;
    border: none;
    margin: 0;
    justify-content: space-between;
    align-items: center;
    overflow-x: visible;
}

.dt-layout-row:first-of-type {
    margin-bottom: 20px;
    padding: 15px 5px;
}

.dt-layout-row:last-of-type {
    margin-top: 20px;
    padding: 15px 5px;
}

.dt-layout-cell {
    flex: 1;
    min-width: 150px;
}

.dt-layout-cell.dt-layout-full {
    flex-basis: 100%;
}

.dt-layout-cell.dt-layout-start {
    flex-grow: 0;
}

.dt-layout-cell.dt-layout-end {
    flex-grow: 0;
    text-align: right;
    /* default: no large padding here, specific rows get their own spacing */
    padding-right: 0;
}

/* Top row (controls) - give space for the search box at the right */
.dt-layout-row:first-of-type .dt-layout-cell.dt-layout-end {
    padding-right: 340px;
}

/* Bottom row (info + paging) - ensure pagination sits to the right in a single line */
.dt-layout-row:last-of-type .dt-layout-cell.dt-layout-end {
    padding-right: 0;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 8px;
}

/* Modals */
.modal { display: none; position: fixed; z-index: 1200; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgba(0,0,0,0.6); }
.modal[aria-hidden="false"] { display: block; }
.modal .modal-content { background: #fff; margin: 6% auto; padding: 18px; border-radius: 8px; width: 90%; max-width: 760px; box-shadow: 0 6px 30px rgba(11,22,44,0.5); }
.modal .modal-content.small { max-width: 420px; }
.modal .modal-content.image-modal { position: relative; padding: 44px 12px 12px; text-align: center; width: 70vw; max-width: 70vw; max-height: 80vh; overflow: auto; }
.modal .image-close { background: transparent; border: 0; font-size: 28px; line-height: 1; position: absolute; right: 14px; top: 10px; cursor: pointer; }
.modal img { max-width: 100%; max-height: 70vh; height: auto; border-radius: 6px; }
.image-caption { margin-top: 8px; font-size: 0.95rem; color: #333; }

.loading-state { position:absolute; inset:0; display:flex; align-items:center; justify-content:center; background:rgba(255,255,255,0.9); font-weight:600; color:#444; z-index:500; }
.load-error { padding: 40px 20px; text-align: center; }
.load-error h2 { color: #d32f2f; margin-bottom: 10px; }
.hidden { display: none !important; }
.is-loading { opacity: 0.5; pointer-events: none; }

/* Column settings list */
#columnCheckboxes { display:flex; flex-wrap:wrap; gap:8px; }
#columnCheckboxes label { display:flex; align-items:center; gap:8px; padding:6px 8px; border-radius:6px; background:#f7f8fa; }
.column-actions { display:flex; gap:8px; margin:12px 0; flex-wrap:wrap; }
.column-modal-footer { margin-top:12px; text-align:right; }
#columnCheckboxes label { cursor: grab; }
#columnCheckboxes label.dragging { opacity: 0.5; }

/* small control button */
.dt-button { background:#667eea; color:#fff; border:0; padding:6px 10px; border-radius:6px; cursor:pointer; margin-left:0; }
.dt-button:hover { background:#5566d6; }

/* Ensure the control button sits nicely left of the search input */
.dt-search .dt-button { margin-right: 8px; }
.dt-length {
    display: flex;
    align-items: center;
    gap: 8px;
}

.dt-search {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-right: 10px;
}

.dt-search input {
    width: 200px;
}

.dt-info {
    color: #555;
    font-size: 0.9em;
}

.dt-paging {
    justify-content: flex-end;
    padding-right: 5px;
}

/* Prevent pagination buttons from stacking vertically */
.dt-paging,
.dt-paging nav {
    display: flex;
    gap: 6px;
    align-items: center;
    flex-wrap: nowrap;
}

.dt-paging-button {
    padding: 6px 10px;
    background: #f8f9fa;
    border: 1px solid #ccc;
    color: #333;
    border-radius: 3px;
    cursor: pointer;
    font-size: 0.9em;
    font-weight: 500;
    transition: all 0.2s ease;
    min-width: 32px;
    text-align: center;
    white-space: nowrap;
}

.dt-paging-button:hover:not(.disabled) {
    background: white;
    border-color: #667eea;
    color: #667eea;
}

.dt-paging-button.disabled {
    opacity: 0.5;
    cursor: not-allowed;
    background: #f5f5f5;
}

.dt-paging-button.current {
    background: #667eea;
    border-color: #667eea;
    color: white;
}

/* Mobile-only sort control and card list; shown by the max-width: 700px media query */
.dt-sort-cell,
.ship-cards {
    display: none;
}

/* Footer */
footer {
    text-align: center;
    margin-top: 40px;
    padding: 30px 20px;
    color: #666;
    font-size: 0.9em;
    border-top: 1px solid #eee;
}

/* Responsive Design */
@media (max-width: 768px) {
    h1 {
        font-size: 1.6em;
    }

    .subtitle {
        font-size: 0.95em;
    }

    .stats {
        gap: 20px;
    }

    .dt-search {
        margin-right: 0;
    }

    .dt-search input {
        width: 100%;
        flex: 1;
        min-width: 0;
    }

    th, td {
        padding: 10px 12px;
        font-size: 0.9em;
    }

    .dt-layout-row {
        flex-direction: column;
        gap: 10px;
    }

    .dt-layout-cell {
        width: 100%;
    }

    .dt-layout-cell.dt-layout-end {
        text-align: left;
    }

    /* Drop the desktop search offset so the search box gets the full width */
    .dt-layout-row:first-of-type .dt-layout-cell.dt-layout-end {
        padding-right: 0;
    }

    .dt-paging {
        justify-content: flex-start;
        flex-wrap: wrap;
    }

    .dt-info {
        margin-bottom: 10px;
    }
}

@media (max-width: 480px) {
    header {
        padding: 20px 15px;
    }

    h1 {
        font-size: 1.3em;
    }

    .subtitle {
        font-size: 0.9em;
    }

    .stats {
        flex-direction: column;
        gap: 15px;
    }

    th, td {
        padding: 8px 10px;
        font-size: 0.85em;
    }

    .dt-paging-button {
        padding: 5px 8px;
        font-size: 0.85em;
        min-width: 28px;
    }
}

/* Mobile card view (must match MOBILE_QUERY in script.js) */
@media (max-width: 700px) {
    .container {
        padding: 12px;
    }

    /* Compact header so ships start near the top of the screen */
    header {
        padding: 14px;
        margin: -12px -12px 12px;
        width: calc(100% + 24px);
        gap: 10px;
    }

    h1 {
        font-size: 1.15em;
        margin-bottom: 6px;
    }

    .data-status {
        font-size: 0.8em;
        margin-bottom: 4px;
    }

    .subtitle {
        display: none;
    }

    .stats {
        flex-direction: row;
        gap: 4px 12px;
        margin-top: 8px;
    }

    .stat-box {
        gap: 4px;
    }

    .stat-number {
        font-size: 1em;
    }

    .stat-label {
        font-size: 0.8em;
    }

    .header-right {
        margin-top: 0;
    }

    .theme-toggle-btn {
        padding: 6px 10px;
        font-size: 0.8em;
        white-space: nowrap;
    }

    /* Drop the boxed table frame; cards sit straight on the page */
    .table-wrapper {
        background: transparent;
        border: none;
        box-shadow: none;
    }

    .dt-container {
        padding: 0;
    }

    /* Search on top, then sort + rows per page; stays pinned while scrolling */
    .dt-layout-row:first-of-type {
        display: grid;
        grid-template-columns: 1fr auto;
        gap: 8px;
        position: sticky;
        top: 0;
        z-index: 20;
        margin: 0 -12px 12px;
        padding: 10px 12px;
        background: #f7f7f7;
        border-bottom: 1px solid #ddd;
    }

    .dt-layout-row:first-of-type .dt-layout-cell {
        min-width: 0;
        width: auto;
    }

    .dt-layout-row:first-of-type .dt-layout-end {
        grid-column: 1 / -1;
        order: -1;
    }

    .dt-sort-cell {
        display: block;
    }

    .dt-sort {
        display: flex;
        align-items: center;
        gap: 6px;
    }

    .dt-sort select {
        flex: 1;
        min-width: 0;
        padding: 8px;
        border: 1px solid #ccc;
        border-radius: 4px;
        background: white;
        color: #333;
        font-family: inherit;
    }

    .dt-sort .dt-button {
        padding: 8px 10px;
        white-space: nowrap;
    }

    .dt-length label,
    .dt-search label,
    #column-toggle-btn {
        display: none;
    }

    .dt-length select {
        padding: 8px;
    }

    .dt-scroll {
        display: none;
    }

    .ship-cards {
        display: flex;
        flex-direction: column;
        gap: 14px;
    }

    .ship-card {
        background: white;
        border: 1px solid #e2e2e2;
        border-radius: 10px;
        box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
        overflow: hidden;
        /* Lets the browser skip drawing cards that are off-screen */
        content-visibility: auto;
        contain-intrinsic-size: auto 520px;
    }

    .ship-card-media img {
        display: block;
        width: 100%;
        max-width: none;
        aspect-ratio: 16 / 9;
        object-fit: cover;
        border-radius: 0;
    }

    .ship-card-noimage {
        display: flex;
        align-items: center;
        justify-content: center;
        aspect-ratio: 16 / 9;
        background: #eceef3;
        color: #888;
        font-size: 0.85em;
    }

    .ship-card-body {
        padding: 12px 14px 10px;
    }

    .ship-card-name {
        font-size: 1.1em;
        line-height: 1.25;
    }

    .ship-card-name a,
    .ship-card a {
        color: #0645ad;
        text-decoration: none;
    }

    .ship-card-maker,
    .ship-card-role {
        font-size: 0.85em;
        color: #666;
        margin-top: 2px;
    }

    .ship-card-tags {
        display: flex;
        flex-wrap: wrap;
        gap: 6px;
        margin-top: 8px;
    }

    .ship-card-tag {
        font-size: 0.75em;
        padding: 2px 8px;
        border-radius: 999px;
        background: #eef0fb;
        color: #4a55a2;
    }

    .ship-card-stats {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 8px 12px;
        margin-top: 12px;
    }

    .ship-card-label {
        font-size: 0.7em;
        text-transform: uppercase;
        letter-spacing: 0.04em;
        color: #888;
    }

    .ship-card-value {
        font-size: 0.85em;
        font-weight: 600;
        overflow-wrap: anywhere;
    }

    .ship-card-details {
        margin-top: 12px;
        border-top: 1px solid #eee;
        padding-top: 8px;
    }

    .ship-card-details summary {
        cursor: pointer;
        font-size: 0.85em;
        font-weight: 600;
        color: #667eea;
        padding: 4px 0;
    }

    .ship-card-group {
        font-size: 0.7em;
        text-transform: uppercase;
        letter-spacing: 0.04em;
        color: #888;
        margin: 10px 0 4px;
    }

    .ship-card-list .ship-card-stat {
        display: flex;
        justify-content: space-between;
        gap: 12px;
        padding: 3px 0;
    }

    .ship-card-list .ship-card-label {
        font-size: 0.8em;
        text-transform: none;
        letter-spacing: 0;
    }

    .ship-card-list .ship-card-value {
        font-weight: 500;
        text-align: right;
    }

    .modal .modal-content.image-modal {
        width: 95vw;
        max-width: 95vw;
        margin-top: 20vh;
    }
}

/* Dark Mode Styles */
body.dark-mode {
    background: #2d2d2d;
    color: #e0e0e0;
}

body.dark-mode .container {
    background: #2d2d2d;
}

body.dark-mode .table-wrapper {
    background: #1e1e1e;
    border-color: #444;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

body.dark-mode .dt-length select,
body.dark-mode .dt-search input {
    background: #2d2d2d;
    border-color: #555;
    color: #e0e0e0;
}

body.dark-mode .dt-length select:hover,
body.dark-mode .dt-search input:hover {
    border-color: #667eea;
}

body.dark-mode .dt-length select:focus,
body.dark-mode .dt-search input:focus {
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
}

body.dark-mode .dt-search input::placeholder {
    color: #aaa;
}

body.dark-mode table {
    border-color: #444;
}

body.dark-mode thead {
    background: #333;
    border-bottom-color: #555;
}

body.dark-mode th {
    color: #e0e0e0;
    border-right-color: #555;
    background: #333;
}

body.dark-mode th:hover {
    background: #444;
}

body.dark-mode tbody tr {
    border-bottom-color: #444;
}

body.dark-mode tbody tr:hover {
    background: #333;
}

body.dark-mode td {
    color: #e0e0e0;
    border-right-color: #444;
}

body.dark-mode td a {
    color: #8ab4f8;
}

body.dark-mode td a:hover {
    color: #aecbfa;
}

body.dark-mode .dt-info {
    color: #e0e0e0;
}

@media (max-width: 700px) {
    /* The cards sit straight on the page, so drop the table box's dark background and shadow */
    body.dark-mode .table-wrapper {
        background: transparent;
        box-shadow: none;
    }

    body.dark-mode .dt-layout-row:first-of-type {
        background: #2d2d2d;
        border-bottom-color: #444;
    }

    body.dark-mode .dt-sort select {
        background: #2d2d2d;
        border-color: #555;
        color: #e0e0e0;
    }

    body.dark-mode .ship-card {
        background: #1e1e1e;
        border-color: #444;
        box-shadow: none;
    }

    body.dark-mode .ship-card a {
        color: #8ab4f8;
    }

    body.dark-mode .ship-card-maker,
    body.dark-mode .ship-card-role,
    body.dark-mode .ship-card-label,
    body.dark-mode .ship-card-group {
        color: #aaa;
    }

    body.dark-mode .ship-card-tag {
        background: #2c3150;
        color: #b9c1f5;
    }

    body.dark-mode .ship-card-noimage {
        background: #2a2a2a;
        color: #999;
    }

    body.dark-mode .ship-card-details {
        border-top-color: #333;
    }

    body.dark-mode .ship-card-details summary {
        color: #8c9bf0;
    }
}

body.dark-mode .loading-state {
    background: #1e1e1e;
    color: #e0e0e0;
}

/* Dark mode modal styles */
body.dark-mode .modal {
    background: rgba(0, 0, 0, 0.8);
}

body.dark-mode .modal-content {
    background: #1e1e1e;
    color: #e0e0e0;
    border: 1px solid #444;
}

body.dark-mode .image-close {
    color: #e0e0e0;
    background: #333;
    border: 1px solid #555;
}

body.dark-mode .image-close:hover {
    background: #444;
}

body.dark-mode .image-caption {
    color: #e0e0e0;
}

body.dark-mode #columnCheckboxes label {
    background: #333;
    color: #e0e0e0;
}

body.dark-mode footer {
    color: #ccc;
    border-top: 1px solid #444;
}

body.dark-mode footer a {
    color: #4fc3f7;
}

body.dark-mode footer a:hover {
    color: #81d4fa;
}
