:root {
    --bg: #0f1115;
    --panel: #171a21;
    --panel2: #1d212b;
    --border: #2a2f3a;
    --text: #e6e8ec;
    --muted: #8a90a0;
    --accent: #4fa8ff;
    --accent2: #2f6fd6;
    --good: #3ecf8e;
    --warn: #e6b34f;
    --bad: #e05c5c;
    --mono: 'SF Mono', Consolas, 'Courier New', monospace;
}

@media (prefers-color-scheme: light) {
    :root:not([data-theme="dark"]) {
        --bg: #f4f5f7;
        --panel: #ffffff;
        --panel2: #eef0f4;
        --border: #dde1e8;
        --text: #1a1d24;
        --muted: #666d7e;
        --accent: #2f6fd6;
        --accent2: #1f56b0;
    }
}

:root[data-theme="dark"] {
    --bg: #0f1115;
    --panel: #171a21;
    --panel2: #1d212b;
    --border: #2a2f3a;
    --text: #e6e8ec;
    --muted: #8a90a0;
}

* {
    box-sizing: border-box;
}

html,
body {
    height: 100%;
}

html {
    scroll-padding-top: env(safe-area-inset-top, 0px);
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    padding-top: env(safe-area-inset-top, 0px);
    padding-bottom: env(safe-area-inset-bottom, 0px);
    display: flex;
    flex-direction: column;
    min-height: 100%;
}

header {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    background: var(--panel);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
}

header h1 {
    font-size: 16px;
    margin: 0;
    font-weight: 600;
}

header .sub {
    font-size: 12px;
    color: var(--muted);
    margin-top: 2px;
}

.toolbar {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    align-items: center;
}

button,
.filebtn {
    background: var(--panel2);
    color: var(--text);
    border: 1px solid var(--border);
    padding: 8px 14px;
    border-radius: 8px;
    font-size: 13px;
    cursor: pointer;
    font-weight: 500;
}

button.primary {
    background: var(--accent2);
    border-color: var(--accent2);
    color: #fff;
}

button.primary:hover {
    background: var(--accent);
}

button:hover {
    border-color: var(--accent);
}

button:disabled {
    opacity: .4;
    cursor: not-allowed;
}

input[type=file] {
    display: none;
}

main {
    flex: 1;
    display: flex;
    overflow: hidden;
    min-height: 0;
}

.empty {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    gap: 12px;
    color: var(--muted);
    padding: 40px;
}

.empty .icon {
    font-size: 40px;
}

.workspace {
    flex: 1;
    display: flex;
    overflow: hidden;
    min-height: 0;
    flex-wrap: wrap;
}

.hexpane {
    flex: 2;
    min-width: 0;
    max-width: 100%;
    width: 100%;
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--border);
    overflow: hidden;
}

.searchbar {
    display: flex;
    gap: 8px;
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
    background: var(--panel);
    flex-wrap: wrap;
    align-items: center;
}

.searchbar input[type=text] {
    background: var(--panel2);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 6px 10px;
    border-radius: 6px;
    font-size: 12px;
    font-family: var(--mono);
    width: 140px;
}

.searchbar label {
    font-size: 12px;
    color: var(--muted);
}

.gridwrap {
    flex: 1;
    min-width: 0;
    max-width: 100%;
    width: 100%;
    min-height: 0;
    overflow-x: auto;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

table.hexgrid {
    border-collapse: collapse;
    font-family: var(--mono);
    font-size: 12px;
    min-width: 760px;
}

table.hexgrid th {
    color: var(--muted);
    font-weight: 500;
    text-align: center;
    padding: 4px 3px;
    position: sticky;
    top: 0;
    background: var(--bg);
    z-index: 1;
}

table.hexgrid td.offset {
    color: var(--muted);
    padding: 2px 8px 2px 2px;
    text-align: right;
    user-select: none;
    position: sticky;
    left: 0;
    background: var(--bg);
}

table.hexgrid td.byte {
    text-align: center;
    padding: 2px 1px;
    cursor: pointer;
    border-radius: 3px;
    min-width: 20px;
}

table.hexgrid td.byte:hover {
    background: var(--panel2);
}

table.hexgrid td.byte.sel {
    background: var(--accent2);
    color: #fff;
}

table.hexgrid td.byte.dirty {
    color: var(--warn);
    font-weight: 700;
}

table.hexgrid td.byte.match {
    outline: 1px solid var(--good);
}

table.hexgrid td.ascii {
    padding: 2px 6px;
    color: var(--muted);
    white-space: pre;
    font-size: 11px;
}

table.hexgrid td.ascii span.sel {
    background: var(--accent2);
    color: #fff;
    border-radius: 2px;
}

table.hexgrid td.ascii span.dirty {
    color: var(--warn);
    font-weight: 700;
}

.sidepane {
    flex: 1;
    min-width: 260px;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--panel);
}

.sidepane h2 {
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: .04em;
    color: var(--muted);
    margin: 14px 14px 6px;
}

.inspector {
    padding: 0 14px 14px;
    overflow: auto;
}

.field {
    margin-bottom: 10px;
}

.field label {
    display: block;
    font-size: 11px;
    color: var(--muted);
    margin-bottom: 4px;
}

.field input,
.field select {
    width: 100%;
    background: var(--panel2);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 7px 9px;
    border-radius: 6px;
    font-size: 13px;
    font-family: var(--mono);
}

.field input:focus,
.field select:focus {
    outline: none;
    border-color: var(--accent);
}

.row2 {
    display: flex;
    gap: 8px;
}

.row2 .field {
    flex: 1;
}

.presetlist {
    padding: 0 14px 14px;
    overflow: auto;
    flex: 1;
}

.preset {
    background: var(--panel2);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px 12px;
    margin-bottom: 8px;
    cursor: pointer;
}

.preset:hover {
    border-color: var(--accent);
}

.preset .name {
    font-size: 13px;
    font-weight: 600;
    margin-bottom: 2px;
}

.preset .desc {
    font-size: 11px;
    color: var(--muted);
}

.statusbar {
    border-top: 1px solid var(--border);
    background: var(--panel);
    padding: 8px 16px;
    font-size: 11px;
    color: var(--muted);
    display: flex;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
}

.badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 100px;
    font-size: 10px;
    font-weight: 600;
    margin-left: 6px;
}

.badge.dirty {
    background: rgba(230, 179, 79, .15);
    color: var(--warn);
}

.badge.clean {
    background: rgba(62, 207, 142, .15);
    color: var(--good);
}

::-webkit-scrollbar {
    width: 10px;
    height: 10px;
}

::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 6px;
}

@media (max-width:760px) {
    .workspace {
        flex-direction: column;
    }

    .hexpane {
        border-right: none;
        border-bottom: 1px solid var(--border);
        max-height: 50vh;
    }
}

.diffOverlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .55);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 20px;
}

.diffModal {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 20px;
    width: 520px;
    max-width: 100%;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
}

.diffModal h3 {
    margin: 0 0 12px;
    font-size: 15px;
}

.diffPickers {
    display: flex;
    gap: 10px;
    margin-bottom: 14px;
    flex-wrap: wrap;
}

.diffPickers>div {
    flex: 1;
    min-width: 180px;
}

.diffPickers label {
    display: block;
    font-size: 11px;
    color: var(--muted);
    margin-bottom: 6px;
}

.diffResults {
    overflow: auto;
    flex: 1;
    border-top: 1px solid var(--border);
    margin-top: 6px;
}

table.diffTable {
    width: 100%;
    border-collapse: collapse;
    font-family: var(--mono);
    font-size: 12px;
}

table.diffTable th {
    text-align: left;
    color: var(--muted);
    font-weight: 500;
    padding: 6px 4px;
    position: sticky;
    top: 0;
    background: var(--panel);
}

table.diffTable td {
    padding: 5px 4px;
    border-bottom: 1px solid var(--border);
}

table.diffTable tr.diffRow {
    cursor: pointer;
}

table.diffTable tr.diffRow:hover {
    background: var(--panel2);
}

td.diffOld {
    color: var(--bad);
}

td.diffNew {
    color: var(--good);
}

.diffFooter {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 12px;
    gap: 8px;
    flex-wrap: wrap;
}

.hex-table-container {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    width: 100%;
}

.hex-scroll {
    width: 100%;
    min-width: 0;
}

#hexTable {
    min-width: 760px;
    /* offset + 16 colunas + ascii, ajuste se precisar */
    border-collapse: collapse;
    white-space: nowrap;
}

#statusLeft::before {
    content: '';
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--good);
    margin-right: 6px;
}

@media (max-width: 768px) {
    .hex-table {
        font-size: 11px;
    }

    .hex-table td,
    .hex-table th {
        padding: 2px 4px;
    }
}