/* ============================================================
   BASE / RESET
   ============================================================ */
html {
    box-sizing: border-box;
}

*, *::before, *::after {
    box-sizing: inherit;
}

:root {
    --bg-body: #F9F9F9;
    --bg-card: #ffffff;
    --bg-surface: #F5F5F5;
    --bg-hover: #EEE;
    --bg-even-row: #F1F1F1;
    --bg-warning: #FFF8E1;
    --bg-warning-border: #FFE0B2;
    --bg-note: #FFF8E1;
    --bg-detail-row: #e8f0fe;
    --bg-delete-row: #fee;
    --border-main: #BBB;
    --border-light: #DDD;
    --border-row: #DfDfDf;
    --border-neutral: #CCC;
    --border-hover: #CCC;
    --border-active: #E0E0E0;
    --text-primary: #333;
    --text-secondary: #666;
    --text-muted: #888;
    --text-link: #555;
    --color-green: #4CAF50;
    --color-green-border: #45a049;
    --color-red: #f44336;
    --color-red-border: #da190b;
    --color-blue: #2196F3;
    --color-blue-border: #1976D2;
    --color-neutral-bg: #f1f1f1;
    --color-neutral-border: #CCC;
    --color-delete-bg: #fee;
    --color-delete-border: #c99;
    --scrollbar-thumb: #dddddd;
    --scrollbar-track: #F9F9F9;
}

body {
    margin: 10px;
    line-height: 1.25em;
    font-size: 13px;
    background: var(--bg-body);
    overflow-y: overlay;
}

body, select, option, optgroup, button {
    font-family: "Lucida Grande", Verdana, Arial, "Bitstream Vera Sans", sans-serif;
}

input:focus,
select:focus,
textarea:focus {
    outline: 2px solid var(--color-blue);
    outline-offset: 1px;
}

::selection {
    background: var(--color-blue);
    color: white;
}


/* ============================================================
   TYPOGRAPHY / GENERIC ELEMENTS
   ============================================================ */
dl {
    margin-top: 0;
}

dd {
    margin-left: 10px;
}

a:hover {
    cursor: pointer;
    text-decoration: underline;
}

a {
    color: var(--color-blue);
}


/* ============================================================
   FORM ELEMENTS
   ============================================================ */
input,
select,
textarea {
    width: 100%;
    display: inline-block;
    box-sizing: border-box;
    border-radius: 3px;
    border: 1px solid var(--border-main);
    padding: 4px 6px;
    font-family: inherit;
    font-size: inherit;
    background: var(--bg-card);
    color: var(--text-primary);
}

input[type="checkbox"] {
    width: auto;
    margin: 0 4px 0 0;
    vertical-align: middle;
}

textarea {
    height: 100px;
    resize: vertical;
}

/* Character source textareas */
#editSource_foreignLetters,
#editSource_objects,
#editSource_emojis {
    height: 60px;
    white-space: normal !important;
    overflow-x: hidden;
    background: var(--bg-surface);
    grid-column: 1 / -1;
}


/* ============================================================
   PANELS — shared card style for all three forms
   ============================================================ */
#newOrChangeForm,
#changePasswordForm,
#editConfigForm {
    display: none;
    max-width: 520px;
    margin: 20px auto;
    padding: 16px;
    background: var(--bg-card);
    border: 1px solid var(--border-light);
    border-radius: 6px;
}


/* ============================================================
   FORM GROUPS
   ============================================================ */
.formGroup {
    display: grid;
    grid-template-columns: 140px 1fr;
    align-items: center;
    gap: 8px;
    margin-bottom: 12px;
}

.formGroup > label {
    text-align: right;
    font-weight: 600;
    color: var(--text-primary);
    user-select: none;
}

.formGroupTop {
    align-items: start;
}

.formGroupTop > label {
    padding-top: 6px;
}

/* Config form: labels left-aligned, full-width inputs */
#editConfigForm .formGroup {
    grid-template-columns: auto 1fr;
    align-items: start;
}

#editConfigForm .formGroup > label {
    text-align: left;
    padding-top: 2px;
}

#editCategoriesContainer,
#editSubCategoriesContainer {
    grid-column: 1 / -1;
}


/* ============================================================
   INPUT WITH ACTION (copy icon)
   ============================================================ */
.inputWithAction {
    display: flex;
    align-items: center;
    gap: 6px;
}

.inputWithAction input {
    flex: 1;
    min-width: 0;
}

.inputWithAction span {
    cursor: pointer;
    user-select: none;
    font-size: 18px;
    line-height: 1;
    padding: 4px;
    border-radius: 3px;
}

.inputWithAction span:hover {
    background: var(--bg-hover);
}


/* ============================================================
   COMPLEXITY CHECKBOXES
   ============================================================ */
.complexityGrid {
    display: flex;
    flex-wrap: wrap;
    gap: 8px 14px;
}

.complexityItem {
    display: flex;
    align-items: center;
    font-size: 13px;
    font-weight: normal;
    color: var(--text-primary);
    cursor: pointer;
    user-select: none;
    white-space: nowrap;
}


/* ============================================================
   FORM ACTIONS (button row)
   ============================================================ */
.formActions {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-top: 24px;
    padding-top: 16px;
    border-top: 1px solid var(--bg-hover);
}

.formActions input {
    width: 100%;
    padding: 8px 12px;
    font-size: 14px;
    cursor: pointer;
    background: var(--color-neutral-bg);
    border: 1px solid var(--color-neutral-border);
    color: var(--text-primary);
}

/* Save buttons — green */
#saveButton,
#saveConfigButton {
    background: #4CAF50;
    color: white;
    border: 1px solid #45a049;
}

/* Delete button — red */
#deleteButton {
    background: #f44336;
    color: white;
    border: 1px solid #da190b;
}

/* Cancel buttons — neutral */
#cancelButton,
#cancelChangePasswordButton,
#cancelConfigButton {
    background: var(--color-neutral-bg);
    color: var(--text-primary);
    border: 1px solid var(--color-neutral-border);
}

/* Re-encrypt / change password confirm — green */
#changePasswordButton {
    background: #4CAF50;
    color: white;
    border: 1px solid #45a049;
}

#changePasswordButton:disabled {
    background: var(--color-neutral-bg);
    border-color: var(--border-main);
    cursor: not-allowed;
}

/* Change master password (in config form) — blue */
#changePasswordFromConfigButton {
    background: #2196F3;
    color: white;
    border: 1px solid #1976D2;
}


/* ============================================================
   CHANGE PASSWORD FORM — specific elements
   ============================================================ */
.changePasswordTitle {
    margin: 0 0 8px 0;
    font-size: 18px;
    font-weight: 600;
    color: var(--text-primary);
}

.changePasswordHint {
    margin: 0 0 20px 0;
    font-size: 14px;
    color: var(--text-secondary);
    line-height: 1.4;
}

#newMasterPasswordHash {
    margin: 12px 0;
    padding: 12px;
    background: var(--bg-surface);
    border: 1px solid var(--border-light);
    border-radius: 4px;
    font-size: 13px;
    color: var(--text-primary);
    word-break: break-all;
    min-height: 44px;
    display: flex;
    align-items: center;
}

#newMasterPasswordHash:empty {
    display: none;
}

.changePasswordNote {
    margin: 20px 0 0 0;
    padding: 12px;
    background: var(--bg-note);
    border: 1px solid var(--bg-warning-border);
    border-radius: 4px;
    font-size: 13px;
    color: var(--text-secondary);
    line-height: 1.5;
}

.changePasswordNote strong {
    color: #E65100;
}


/* ============================================================
   CATEGORY / SUBCATEGORY EDITOR ROWS
   ============================================================ */
#editCategoriesContainer div,
#editSubCategoriesContainer div {
    display: grid;
    grid-template-columns: 1fr 50px 30px;
    gap: 4px;
    margin-bottom: 4px;
}

/* Delete button inside each row */
#editCategoriesContainer button,
#editSubCategoriesContainer button {
    padding: 2px 6px;
    font-size: 10px;
    cursor: pointer;
    background: var(--bg-delete-row);
    border: 1px solid var(--color-delete-border);
    border-radius: 3px;
    color: var(--color-red);
}

/* Add row buttons */
#addCategoryButton,
#addSubCategoryButton {
    background: var(--color-neutral-bg);
    border: 1px solid var(--color-neutral-border);
    border-radius: 4px;
    padding: 6px 12px;
    cursor: pointer;
    font-size: 12px;
    width: 100%;
    text-align: left;
    grid-column: 1 / -1 !important;
    color: var(--text-primary);
}


/* ============================================================
   TABLE
   ============================================================ */
table {
    margin: 10px 12px 12px 0;
    border: 1px solid var(--border-main);
    font-size: 90%;
    border-radius: 5px;
}

th {
    position: sticky;
    top: 0;
}

td, th {
    background-color: var(--bg-card);
    padding: 4px 6px;
    border: 1px solid var(--border-row);
    border-width: 1px 0 0 1px;
    white-space: pre-line;
    max-width: 300px;
    overflow: hidden;
    text-overflow: ellipsis;
    vertical-align: top;
    color: var(--text-primary);
}

td.actionColumn,
th.actionColumn {
    min-width: 100px;
}

tr.evenRow td,
tr.evenRow th {
    background-color: var(--bg-even-row);
}

.actionColumn {
    font-size: 11pt;
    white-space: nowrap;
}

.actionColumn a {
    display: inline-block;
    margin-right: 4px;
    text-decoration: none;
}

.actionColumn a:last-child {
    margin-right: 0;
}

/* Mobile sub-info line (hidden on desktop) */
.mobileSubInfo {
    display: none;
}


/* ============================================================
   LAYOUT — GRID CONTAINER
   ============================================================ */
#passwordTableContainer {
    display: none;
    grid-template-columns: 150px 1fr;
    grid-template-rows: 40px auto;
    align-items: start;
}

#headerRow {
    grid-column: 1 / -1;
    grid-row: 1 / 2;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 4px 0;
    width: 100%;
    box-sizing: border-box;
}

#searchField {
    flex: 0 1 320px;
    min-width: 0;
    height: 32px;
    font-size: 14px;
}

#newAccount {
    height: 32px;
    white-space: nowrap;
    flex-shrink: 0;
    width: auto;
    padding: 0 12px;
}

#categoryTree {
    cursor: default;
    position: sticky;
    top: 0;
    max-height: 100vh;
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track);
    white-space: nowrap;
    padding-bottom: 10px;
    grid-column: 1 / 1;
    grid-row: 2 / 2;
    align-self: start;
}

.selectCategory:hover,
.selectSubCategory:hover {
    text-decoration: underline;
}

#passwordTable {
    grid-column: 2 / 2;
    grid-row: 2 / 2;
    align-self: start;
}


/* ============================================================
   SPECIFIC UI ELEMENTS
   ============================================================ */
#messageBox {
    display: none;
    z-index: 1;
    position: fixed;
    top: 0;
    margin: auto;
    left: 0;
    right: 0;
    height: 50px;
    line-height: 50px;
    width: 50%;
    text-align: center;
    font-size: 120%;
    font-weight: bold;
    color: white;
    border-radius: 5px;
}

#messageBox.red   { background: red; }
#messageBox.green { background: green; }

#loginForm {
    margin: 20px;
    display: flex;
    gap: 8px;
    align-items: stretch;
}

#loginForm input[type="password"] {
    width: 220px;
    height: 36px;
    flex-shrink: 0;
}

#loginForm input[type="button"] {
    width: auto;
    height: 36px;
    padding: 0 16px;
    flex-shrink: 0;
    box-sizing: border-box;
}

#timeoutMessage {
    display: none;
    position: fixed;
    bottom: 0;
    margin-bottom: 5px;
    left: 5px;
    z-index: 1;
    background-color: var(--bg-surface);
    color: var(--text-primary);
}

#openChangePasswordForm {
    position: fixed;
    bottom: 20px;
    right: 20px;
    font-size: 24px;
    background: none;
    border: none;
    cursor: pointer;
    z-index: 10;
    user-select: none;
    padding: 8px;
}


/* ============================================================
   MOBILE  (max-width: 600px)
   ============================================================ */
@media only screen and (max-width: 600px) {

    html, body {
        margin: 0;
        padding: 0;
        height: 100%;
        overflow: hidden;
    }

    body {
        font-size: 16px;
    }

    /* --- Table --- */
    table {
        width: 100%;
        margin: 0;
        table-layout: fixed;
        border-radius: 0;
        border-left: none;
        border-right: none;
    }

    td, th {
        max-width: none;
        padding: 2px 4px;
        font-size: 16px;
    }

    /* Hide columns: category, subcategory, url, comment */
    .url,
    td:nth-child(3), th:nth-child(3),
    td:nth-child(4), th:nth-child(4),
    td:nth-child(6), th:nth-child(6),
    td:nth-child(7), th:nth-child(7) {
        display: none;
        width: 0;
        padding: 0;
    }

    /* Action column */
    td:nth-child(1), th:nth-child(1) {
        width: 108px;
        text-align: left;
        vertical-align: top;
    }

    /* Description column */
    td:nth-child(2), th:nth-child(2) {
        width: calc(100vw - 108px);
        max-width: none;
        cursor: pointer;
        overflow: hidden;
        text-overflow: ellipsis;
        font-size: 16px;
        word-wrap: break-word;
    }

    .actionColumn {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 6px;
        padding: 6px !important;
    }

    .actionColumn a {
        font-size: 22px;
        line-height: 1;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        min-width: 44px;
        min-height: 44px;
        padding: 8px;
        border: 1px solid #CCC;
        border-radius: 6px;
        background: #F5F5F5;
        text-decoration: none;
        margin-right: 0;
    }

    .actionColumn a:active {
        background: #E0E0E0;
    }

    .mobileSubInfo {
        display: block;
        font-size: 14px;
        color: #888;
        margin-top: 1px;
    }

    /* Detail row */
    .detailRow td {
        max-width: 100% !important;
        width: 100% !important;
        padding: 6px 10px;
        font-size: 16px;
        color: #555;
        border-top: none;
        background: #e8f0fe !important;
        white-space: normal;
        overflow: visible;
    }

    /* --- Grid layout --- */
    #passwordTableContainer {
        grid-template-columns: 1fr;
        grid-template-rows: 52px 1fr;
        height: 100vh;
        width: 100vw;
        overflow: hidden;
    }

    #headerRow {
        grid-column: 1;
        grid-row: 1;
        width: 100%;
        height: 52px;
        padding: 6px 8px;
        display: flex;
        flex-direction: row;
        align-items: center;
        gap: 6px;
        box-sizing: border-box;
    }

    #headerRow a {
        font-size: 20px;
        line-height: 1;
        flex: 0 0 32px;
        height: 40px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        text-decoration: none;
    }

    #headerRow #searchField {
        height: 40px;
        font-size: 16px;
        flex: 1 1 0;
        min-width: 0;
    }

    #headerRow #newAccount {
        height: 40px;
        font-size: 16px;
        padding: 0 14px;
        flex: 0 0 auto;
        white-space: nowrap;
    }

    #passwordTable {
        grid-column: 1;
        grid-row: 2;
        max-height: calc(100vh - 52px);
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        width: 100%;
        box-sizing: border-box;
    }

    #categoryTree {
        display: none;
    }

    #openChangePasswordForm {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        position: fixed;
        bottom: 16px;
        right: 16px;
        width: 56px;
        height: 56px;
        font-size: 28px;
        background: #fff;
        border: 1px solid #CCC;
        border-radius: 50%;
        box-shadow: 0 2px 8px rgba(0,0,0,0.15);
        cursor: pointer;
        z-index: 100;
        user-select: none;
    }

    #openChangePasswordForm:active {
        background: #F0F0F0;
        transform: scale(0.95);
    }

    #loginForm {
        flex-direction: column;
        align-items: stretch;
        gap: 10px;
        margin: 20px 10px;
    }

    #loginForm input[type="password"],
    #loginForm input[type="button"] {
        width: 100%;
        height: 44px;
        font-size: 16px;
    }

    #timeoutMessage {
        bottom: 4px;
        left: 4px;
        font-size: 13px;
        padding: 2px 4px;
    }

    /* --- All three forms: fullscreen on mobile --- */
    #newOrChangeForm,
    #changePasswordForm,
    #editConfigForm {
        max-width: none;
        width: 100%;
        margin: 0;
        padding: 12px;
        border: none;
        border-radius: 0;
        height: 100vh;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        box-sizing: border-box;
        position: relative;
        z-index: 101;
    }

    /* --- Form groups --- */
    .formGroup {
        grid-template-columns: 1fr;
        gap: 4px;
        margin-bottom: 14px;
    }

    .formGroup > label {
        text-align: left;
        font-size: 16px;
    }

    .formGroupTop > label {
        padding-top: 0;
    }

    /* --- Inputs --- */
    input,
    select,
    textarea {
        font-size: 16px; /* prevents iOS zoom */
        padding: 10px 12px;
        border-radius: 6px;
    }

    input,
    select {
        height: 44px;
    }

    textarea {
        height: 120px;
    }

    /* --- Copy icon --- */
    .inputWithAction span {
        font-size: 28px;
        padding: 8px 10px;
        min-width: 44px;
        min-height: 44px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        border: 1px solid #CCC;
        border-radius: 6px;
        background: #F5F5F5;
    }

    .inputWithAction span:active {
        background: #E0E0E0;
    }

    /* --- Complexity checkboxes --- */
    .complexityGrid {
        gap: 10px 12px;
    }

    .complexityItem {
        font-size: 16px;
        padding: 10px 12px;
        background: #F5F5F5;
        border-radius: 6px;
        border: 1px solid #DDD;
        min-height: 44px;
        display: flex;
        align-items: center;
    }

    .complexityItem input[type="checkbox"] {
        width: 22px;
        height: 22px;
        margin-right: 8px;
    }

    /* --- Form actions --- */
    .formActions {
        flex-direction: column;
        gap: 10px;
        margin-top: 24px;
        padding-top: 20px;
    }

    .formActions input {
        width: 100%;
        padding: 12px;
        font-size: 16px;
        border-radius: 6px;
        min-height: 44px;
    }

    /* --- Typography in forms --- */
    .changePasswordTitle {
        font-size: 20px;
        margin-bottom: 12px;
    }

    .changePasswordHint {
        font-size: 16px;
        margin-bottom: 24px;
    }

    #newMasterPasswordHash {
        font-size: 16px;
        padding: 14px;
        margin: 16px 0;
    }

    .changePasswordNote {
        font-size: 16px;
        padding: 14px;
        margin-top: 24px;
        line-height: 1.6;
    }

    /* --- Category/Subcategory row inputs --- */
    #editCategoriesContainer input,
    #editSubCategoriesContainer input {
        font-size: 14px;
        padding: 6px 8px;
        height: auto;
    }

    #editCategoriesContainer button,
    #editSubCategoriesContainer button {
        padding: 2px 8px;
        font-size: 12px;
        height: auto;
    }

    #addCategoryButton,
    #addSubCategoryButton {
        padding: 6px 12px;
    }
}

/* ============================================================
    DARK MODE  (prefers-color-scheme: dark)
    ============================================================ */
@media (prefers-color-scheme: dark) {
    :root {
        --bg-body: #0f0f1a;
        --bg-card: #1a1a2e;
        --bg-surface: #16162a;
        --bg-hover: #252540;
        --bg-even-row: #1a1a2e;
        --bg-warning: #2a2510;
        --bg-warning-border: #5a4a20;
        --bg-note: #2a2510;
        --bg-detail-row: #1a2040;
        --bg-delete-row: #2a1a1a;
        --border-main: #3a3a50;
        --border-light: #2a2a40;
        --border-row: #2a2a3a;
        --border-neutral: #3a3a50;
        --border-hover: #3a3a50;
        --border-active: #3a3a50;
        --text-primary: #d4d4de;
        --text-secondary: #a0a0b0;
        --text-muted: #888898;
        --text-link: #b0b0c0;
        --scrollbar-thumb: #3a3a50;
        --scrollbar-track: #1a1a2e;
        --color-green: #66bb6a;
        --color-green-border: #4caf50;
        --color-red: #ef5350;
        --color-red-border: #c62828;
        --color-blue: #8ab4f8;
        --color-blue-border: #90caf9;
        --color-neutral-bg: #2a2a3a;
        --color-neutral-border: #3a3a50;
        --color-delete-bg: #2a1a1a;
        --color-delete-border: #885555;
    }

    /* Category tree */
    #categoryTree {
        color: var(--text-primary);
    }

    .selectCategory,
    .selectSubCategory {
        color: var(--text-primary);
    }

    a {
        color: #8ab4f8;
    }

    a:hover {
        color: #b4d7fc;
    }

    /* Mobile: override remaining hardcoded colors */
    .actionColumn a {
        border-color: var(--border-neutral);
        background: var(--bg-surface);
        color: #8ab4f8;
    }

    .actionColumn a:active {
        background: var(--bg-hover);
    }

    .inputWithAction span {
        border-color: var(--border-neutral);
        background: var(--bg-surface);
    }

    .inputWithAction span:active {
        background: var(--bg-hover);
    }

    .complexityItem {
        background: var(--bg-surface);
        border-color: var(--border-light);
    }

    .mobileSubInfo {
        color: var(--text-muted);
    }

    .detailRow td {
        color: var(--text-secondary);
        background: var(--bg-detail-row) !important;
    }

    #openChangePasswordForm {
        background: var(--bg-card);
        border-color: var(--border-neutral);
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
    }

    #openChangePasswordForm:active {
        background: var(--bg-hover);
    }

    #loginForm input[type="password"],
    #loginForm input[type="button"] {
        background: var(--bg-surface);
        border-color: var(--border-main);
        color: var(--text-primary);
    }

    #newOrChangeForm,
    #changePasswordForm,
    #editConfigForm {
        background: var(--bg-card);
    }

    input,
    select {
        background: var(--bg-surface);
        border-color: var(--border-main);
        color: var(--text-primary);
    }

    textarea {
        background: var(--bg-surface);
        border-color: var(--border-main);
        color: var(--text-primary);
    }

    #newMasterPasswordHash {
        background: var(--bg-surface);
        border-color: var(--border-light);
        color: var(--text-primary);
    }

    .changePasswordNote {
        background: var(--bg-note);
        border-color: var(--bg-warning-border);
        color: var(--text-secondary);
    }

    /* Scrollbar webkit */
    ::-webkit-scrollbar {
        width: 8px;
    }

    ::-webkit-scrollbar-track {
        background: var(--scrollbar-track);
    }

    ::-webkit-scrollbar-thumb {
        background: var(--scrollbar-thumb);
        border-radius: 4px;
    }

    ::-webkit-scrollbar-thumb:hover {
        background: #555570;
    }
}
