/* === CSS Custom Properties (Variables) === */
.dynamic-grid-table {
    /* Default (Light) Theme */
    --bg-primary: #f9f9f9;
    --bg-secondary: #f7f7f9;
    --bg-body: #ffffff;
    --bg-hover: #f1f1f1;
    --bg-button-hover: #e7e7e8;
    --text-primary: #222222;
    --text-secondary: #333333;
    --text-muted: #999999;
    --border-color: #e7e7e9;
    --border-accent: #e7e7e8;
    --outline-color: #000000;
    --resize-handle-color: gray;
    --resize-handle-active: green;
    --scrollbar-thumb: #aaa;
    --scrollbar-thumb-focus: #888;
    --border-radius: 10px;
    --cell-border-radius: 4px;
    --scrollbar-width: 8px;
    --scrollbar-height: 8px;
    --font-size: 14px;
    --font-size-header: 14px;
    --row-height: 30px;
}

/* === Theme Variations === */
.dynamic-grid-table[data-theme="dark"] {
    --bg-primary: #2d2d2d;
    --bg-secondary: #353535;
    --bg-body: #1a1a1a;
    --bg-hover: #404040;
    --bg-button-hover: #4a4a4a;
    --text-primary: #c7c7c7;
    --text-secondary: #c7c7c7;
    --text-muted: #888888;
    --border-color: #555555;
    --border-accent: #666666;
    --outline-color: #ffffff;
    --resize-handle-color: #cccccc;
    --resize-handle-active: #4ade80;
    --scrollbar-thumb: #555;
    --scrollbar-thumb-focus: #777;
}

.dynamic-grid-table[data-theme="square"] {
    --border-radius: 0px;
    --cell-border-radius: 0px;
    --scrollbar-width: 6px;
    --scrollbar-height: 6px;
}

.dynamic-grid-table[data-theme="blue"] {
    --bg-primary: #eff6ff;
    --bg-secondary: #dbeafe;
    --bg-body: #ffffff;
    --bg-hover: #e0f2fe;
    --bg-button-hover: #bae6fd;
    --text-primary: #1e40af;
    --text-secondary: #1f2937;
    --text-muted: #6b7280;
    --border-color: #93c5fd;
    --border-accent: #60a5fa;
    --outline-color: #2563eb;
    --resize-handle-color: #3b82f6;
    --resize-handle-active: #1d4ed8;
}

.dynamic-grid-table[data-theme="minimal"] {
    --bg-primary: #ffffff;
    --bg-secondary: #ffffff;
    --bg-body: #ffffff;
    --bg-hover: #f8fafc;
    --bg-button-hover: #f1f5f9;
    --text-primary: #374151;
    --text-secondary: #374151;
    --text-muted: #9ca3af;
    --border-color: #e5e7eb;
    --border-accent: #d1d5db;
    --outline-color: #6b7280;
    --resize-handle-color: #9ca3af;
    --resize-handle-active: #374151;
    --border-radius: 6px;
    --cell-border-radius: 2px;
    --scrollbar-width: 6px;
    --scrollbar-height: 6px;
}

/* === Scrollbar Styling === */
.dynamic-grid-table::-webkit-scrollbar {
    width: var(--scrollbar-width, 8px);
    height: var(--scrollbar-height, 8px);
}

.dynamic-grid-table::-webkit-scrollbar-track {
    background: var(--bg-secondary);
    border-radius: var(--border-radius);
}

.dynamic-grid-table::-webkit-scrollbar-thumb {
    background: var(--scrollbar-thumb);
    border-radius: 3px;
}

.dynamic-grid-table:focus::-webkit-scrollbar-thumb {
    background: var(--scrollbar-thumb-focus);
}

/* === Main Grid Container === */
.dynamic-grid-table {
    overflow: scroll;
    height: 100%;
    width: 100%;
    scroll-behavior: smooth;
    border-radius: var(--border-radius);
    border: 1px solid var(--border-color);
}

/* === Grouped Rows Styles === */
.grouped-rows {
    --y-offset: 0px;
    position: absolute;
    top: 0;
    width: 30px;
    height: calc(100% - 41px);
    left: -30px;
    /*background: blue;*/
    margin-top: 41px;
    overflow-y: hidden;
}

.grouped-rows .grouped-row {
    /* start adn end row are set by
    <div class="grouped-row" start="0" length="5"></div>
     */
    --start-row: calc(attr(s type(<integer>), 1));
    --amount-of-rows: calc(attr(l type(<integer>), 1));
    position: absolute;
    top: calc(var(--start-row) * var(--row-height) + var(--y-offset) * -1 + 3px);
    left: 10px;
    width: 12px;
    height: calc(var(--amount-of-rows) * var(--row-height) - 9px);
    background: transparent;
    z-index: 100;
    border: 2px solid var(--text-primary);
    border-right: unset;
}

.grouped-row::before {
    --margin: 4px;
    content: '➖';
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 12px;
    position: sticky;
    top: calc(var(--margin) * 2);
    width: 21px;
    height: 21px;
    background: var(--text-primary);
    margin: var(--margin) 0;
    translate: -12px 0;
    outline: 3px solid #ffffff;
}

.grouped-rows .grouped-row.hidden {
    height: calc(var(--row-height) - 9px);
    top: calc(var(--start-row)* var(--row-height) + var(--y-offset)* -1 + 3px);););
}

.grouped-rows .grouped-row.hidden::before {
    content: '＋';
    margin: 0 0;
    outline: none;
    translate: -12px;
}

/* === Common Table Styles === */
.dynamic-grid-table-header,
.dynamic-grid-table-body {
    background: var(--bg-primary);
    font-family: Arial, sans-serif;
    overflow-x: auto;
    border-radius: var(--cell-border-radius);
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
    width: fit-content;
    table-layout: fixed;

    counter-reset: row;
}

.dynamic-grid-table-header *,
.dynamic-grid-table-body * {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
}

.dynamic-grid-table-header table,
.dynamic-grid-table-body table {
    border: none;
}

/* === Header Styles === */
table.dynamic-grid-table-header {
    position: sticky;
    top: 0;
    z-index: 1000;
    border-bottom: 1px solid var(--border-color);
}

.dynamic-grid-table-header .header-row {
    background: var(--bg-secondary);
    color: var(--text-primary);
    font-size: var(--font-size-header, 14px);
    text-transform: capitalize;
    height: var(--row-height);
}

.dynamic-grid-table-header .header-cell {
    max-height: var(--row-height);
    vertical-align: middle;
    border-right: 1px solid var(--border-color);
}

.dynamic-grid-table-header .header-cell:first-child {
    position: sticky;
    left: 0;
    z-index: 100;
    background: var(--bg-secondary);
    width: 100%;
}

.dynamic-grid-table-header .header-cell:first-child::after {
    width: 1px;
    position: absolute;
    height: 100%;
    background: var(--border-accent);
    top: 0;
    left: -1px;
}

/* === Header Cell Content === */
.header-cell-content {
    display: flex;
    flex-direction: row-reverse;
    margin: 4px;
    align-items: center;
}

.header-cell-content .header-cell-text {
    text-overflow: ellipsis;
    overflow: hidden;
    text-wrap: nowrap;
}

.dynamic-grid-table-header .header-cell-button {
    display: block;
    height: 32px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    line-height: 12px;
    padding: 0;
    font-size: var(--font-size, 12px);
    scale: 1 0.5;
    width: 16px;
    margin: 0 0 0 auto;
    color: var(--text-muted);
    background-color: transparent;
}

.dynamic-grid-table-header .header-cell-button:hover {
    background-color: var(--bg-button-hover);
    border-radius: var(--cell-border-radius);
}

/* === Header Resize Handle === */
.header-resize-handle {
    position: absolute;
    right: 0;
    top: 0;
    width: 10px;
    translate: 5px 0;
    height: 100%;
    cursor: col-resize;
    background-color: transparent;
}

.header-resize-handle.hover {
    background-color: transparent;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: row;
}

.header-resize-handle.hover::after,
.header-resize-handle.hover::before {
    content: '';
    height: 50%;
    background: var(--resize-handle-color);
    width: 3px;
    position: absolute;
    translate: -3px 0;
}

.header-resize-handle.hover::before {
    translate: 4px 0;
}

.header-resize-handle.hover:active::after {
    content: '';
    height: 100%;
    background: var(--resize-handle-active);
    width: 2px;
    position: absolute;
    translate: 2px 0;
    top: 0;
}

.header-resize-handle.hover:active::before {
    background: transparent;
}

/* === Data Row Styles === */
.dynamic-grid-table-header .data-row {
    height: var(--row-height);
    background: var(--bg-body);
}

.dynamic-grid-table-header .data-row:hover {
    background-color: var(--bg-hover);
}

/* === Body Cell Styles === */
.dynamic-grid-table-body tbody tr td[bold] {
    font-weight: bold;
}

.dynamic-grid-table-body tbody tr td[data-invalid="true"] {
    background: #ffcccc;
}

.dynamic-grid-table-body tbody tr td.body-cell > span {
    display: block;
    width: 100%;
    height: 100%;
    align-content: center;
    overflow: hidden;
    text-overflow: ellipsis;
}

.dynamic-grid-table-body tbody tr td.editable {
    cursor: pointer;
    background: var(--bg-hover);
}

td.body-cell {
    color: var(--text-secondary);
    background: var(--bg-body);
    border-right: 1px solid var(--border-color);
    border-bottom: 1px solid var(--border-color);
    font-size: var(--font-size);
}

td.body-cell:not(:first-child) {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    outline: var(--bg-body);
    position: relative;
}

td.body-cell:first-child {
    position: sticky;
    left: 0;
    background: var(--bg-secondary);
}

td.body-cell:focus {
    outline: 1px solid var(--outline-color);
}

td.body-cell.edited {
    background: var(--bg-hover);
}

/* === Input Styles === */
.body-cell input[type] {
    width: 100%;
    height: 100%;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    font-size: var(--font-size, 12px);
    font-family: Arial, sans-serif;
}

.body-cell input[type="text"],
.body-cell input[type="number"][step],
.body-cell select {
    padding: 0 4px;
    box-sizing: border-box;
    outline: none;
}

.body-cell input[type="number"]:not([step]) {
    padding: 0 4px;
    box-sizing: border-box;
    outline: none;
}

.body-cell input[type="date"] {
    padding: 0 4px;
    box-sizing: border-box;
    outline: none;
    background: var(--bg-body);
    color: var(--text-secondary);
}

.body-cell input[type="checkbox"] {
    width: 100%;
    height: 16px;
    vertical-align: middle;
    margin: auto;
}

.body-cell input[type="checkbox"]:focus {
    outline: 1px solid var(--outline-color);
}

.body-cell input[type="tel"]:invalid {
    border: 1px solid red;
}

.body-cell:has(select[name="_"]) {
    position: relative;
}

.body-cell:has(select[name="_"])::after {
    content: '▼';
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%) scaleY(0.5);
    color: var(--text-muted);
    pointer-events: none;
}

.body-cell select {
    width: 100%;
    height: 100%;
    border: none;
    color: var(--text-secondary);
    background: transparent;
    appearance: none;
}

.body-cell button[name="_"] {
    width: calc(100% - 8px);
    height: calc(100% - 8px);
    border: none;
    background: var(--border-color);
    color: var(--bg-body);
    font-size: var(--font-size, 12px);
    margin: 0 4px;
    cursor: pointer;
}

/* === Data Cell Styles === */
.dynamic-grid-table-header .data-cell {
    padding: 8px;
    white-space: nowrap;
    font-size: var(--font-size, 12px);
    color: var(--text-secondary);
    text-align: left;
}

.body-cell.disabled {
    color: var(--text-muted);
    background: var(--bg-primary);
}

/*when not 'editing' an input field*/
.body-cell[data-value]:has(input[type="text"]:not(:focus)) > input ,
.body-cell[data-value]:has(input[type="number"]:not(:focus)) > input ,
.body-cell[data-value]:has(input[type="tel"]:not(:focus)) > input ,
.body-cell[data-value]:has(input[type="email"]:not(:focus)) > input {
    display: block;
    content-visibility: hidden;
    z-index: 1;
    position: relative;
}

.body-cell[data-value]:has(input[type="text"]:not(:focus))::after,
.body-cell[data-value]:has(input[type="number"]:not(:focus))::after,
.body-cell[data-value]:has(input[type="tel"]:not(:focus))::after,
.body-cell[data-value]:has(input[type="email"]:not(:focus))::after {
    content: attr(data-value, '');
    width: calc(100% - 8px);
    height: 100%;
    color: var(--text-secondary);
    text-overflow: ellipsis;
    overflow: hidden;
    position: absolute;
    top: 0;
    padding: 0 4px;
    display: flex;
    align-items: center;
}


/* === CUSTOM CELL STYLES === */
[ctrl] td.email-cell span {display: none;}
:not([ctrl]) td.email-cell a {display: none;}
[ctrl] td.email-cell a {display: block}
